(module mrtex2im mzscheme
(require (lib "kw.ss")
(lib "class.ss")
(lib "process.ss")
(lib "mred.ss" "mred")
(lib "file.ss")
(lib "port.ss"))
(provide tex2im
tex2im:latex-exception
tex2im:dvips-exception
tex2im:convert-exception
setup-latex-path
setup-dvips-path
setup-convert-path)
(define tex2im:latex-exception 'tex2im:latex-exception)
(define tex2im:dvips-exception 'tex2im:dvips-exception)
(define tex2im:convert-exception 'tex2im:convert-exception)
(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 (display-latex-log-file file)
(parameterize ([current-directory (find-system-path 'temp-dir)])
(let* ([log-filename (string-append (path->string file) ".log")]
[filefp (open-input-file log-filename)])
(display (get-string-from-port filefp))
(close-input-port filefp))))
(define (run-latex/dvips file debug-latex-on?)
(parameterize ([current-directory (find-system-path 'temp-dir)])
(let ([dvi-file (string-append (path->string file) ".dvi")]
[eps-file (string-append (path->string file) ".eps")])
(cond [(not (zero? (parameterize ((current-output-port (open-output-nowhere))
(current-error-port (open-output-nowhere))) (system*/exit-code (setup-latex-path) "-interaction=batchmode" (path->string file)))))
(printf "WARNING: Error generating latex file.~n")
(if debug-latex-on? (begin
(printf "DEBUG: Here's latex log file:~n")
(display-latex-log-file file)))
(raise tex2im:latex-exception)]
[(not (zero? (parameterize ((current-output-port (open-output-nowhere))
(current-error-port (open-output-nowhere))) (system*/exit-code (setup-dvips-path) "-o" eps-file "-E" dvi-file))))
(printf "WARNING: Error generating eps file.~n")
(raise tex2im:dvips-exception)]
[else eps-file]))))
(define (run-convert trans? aa? bgcolor res file)
(parameterize ([current-directory (find-system-path 'temp-dir)])
(let ([png-file (string-append file ".png")])
(if trans?
(parameterize ((current-output-port (open-output-nowhere))
(current-error-port (open-output-nowhere)))
(system*/exit-code (setup-convert-path)
"+adjoin"
(if aa? "-antialias" "+antialias")
"-transparent" bgcolor
"-density"
res
file
png-file))
(parameterize ((current-output-port (open-output-nowhere))
(current-error-port (open-output-nowhere)))
(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")]
[debug-latex-on? #f])
(parameterize ([current-directory (find-system-path 'temp-dir)])
(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 debug-latex-on?))
'png
#f))))
)