(module mrtex2im mzscheme
(require (lib "kw.ss")
(lib "class.ss")
(lib "process.ss")
(lib "mred.ss" "mred")
(lib "file.ss"))
(current-directory (find-system-path 'temp-dir))
(define setup-latex-path (make-parameter (build-path "/usr/bin/latex")))
(define setup-dvips-path (make-parameter (build-path "/usr/bin/dvips")))
(define setup-convert-path (make-parameter (build-path "/usr/bin/convert")))
(define (get-string-from-port fileport)
(let loop ((str "") (line (read-line fileport)))
(if (eof-object? line)
str
(loop (string-append str "\n" line)
(read-line fileport)))))
(define (run-latex/dvips file)
(let ([dvi-file (string-append (path->string file) ".dvi")]
[eps-file (string-append (path->string file) ".eps")])
(if (and (zero? (system*/exit-code (setup-latex-path) "-interaction=batchmode" (path->string file)))
(zero? (system*/exit-code (setup-dvips-path) "-o" eps-file "-E" dvi-file)))
eps-file
(begin0
eps-file
(printf "WARNING: Error generating latex or postscript file.")))))
99
(define (run-convert trans? aa? bgcolor res file)
(let ([png-file (string-append file ".png")])
(if trans?
(system*/exit-code (setup-convert-path)
"+adjoin"
(if aa? "-antialias" "+antialias")
"-transparent" bgcolor
"-density"
res
file
png-file)
(system*/exit-code (setup-convert-path)
"+adjoin"
(if aa? "-antialias" "+antialias")
"-density"
res
file
png-file))
png-file))
(define/kw (tex2im str-or-path #:key
[resolution "150x150"]
[bgcolor "white"]
[fgcolor "black"]
[transparency #f]
[noformula #f]
[anti-aliasing #t]
[extra-header (build-path (find-system-path 'home-dir) ".tex2im-header")])
(let ([tmp-file (make-temporary-file)])
(with-output-to-file tmp-file
(lambda ()
(printf "\\documentclass[12pt]{article} \\usepackage{color} \\usepackage[dvips]{graphicx} \\pagestyle{empty}")
(if (file-exists? extra-header)
(printf (call-with-input-file #:extra-header
(lambda (fileport)
(get-string-from-port fileport)))))
(printf "\\pagecolor{~a} \\begin{document} {\\color{~a}" bgcolor fgcolor)
(if (not noformula) (printf "\\begin{eqnarray*}"))
(if (string? str-or-path)
(printf str-or-path)
(printf (call-with-input-file str-or-path
(lambda (fileport)
(get-string-from-port fileport)))))
(if (not noformula) (printf "\\end{eqnarray*}"))
(printf "}\\end{document}"))
'truncate)
(make-object bitmap%
(run-convert transparency anti-aliasing bgcolor resolution
(run-latex/dvips tmp-file))
'png
#f)))
(provide tex2im))