3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 (define-module (scm ps-to-png))
17 ;; gettext wrapper for guile < 1.7.2
18 (if (defined? 'gettext)
19 (define-public _ gettext)
20 (define-public (_ x) x))
25 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
27 (define (re-sub re sub string)
28 (regexp-substitute/global #f re string 'pre sub 'post))
30 (define (search-executable names)
31 (define (helper path lst)
34 (if (search-path path (car lst)) (car lst)
35 (helper path (cdr lst)))))
37 (let ((path (parse-path (getenv "PATH"))))
41 (search-executable '("gs-nox" "gs-8.15" "gs")))
43 (define (gulp-port port max-length)
44 (let ((str (make-string max-length)))
45 (read-string!/partial str port)
48 (define (dir-listing dir-name)
49 (define (dir-helper dir lst)
50 (let ((e (readdir dir)))
51 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
52 (reverse (dir-helper (opendir dir-name) '())))
54 (define (dir-re dir re)
55 (filter (lambda (x) (string-match re x)) (dir-listing dir)))
57 (define BOUNDING-BOX-RE
58 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
60 (define (get-bbox file-name)
61 (let* ((bbox (string-append file-name ".bbox"))
62 ;; -sOutputFile does not work with bbox?
72 (s (gulp-port (open-file bbox "r") 10240))
73 (m (string-match BOUNDING_BOX_RE s)))
78 (map (lambda (x) (string->number (car x))) (vector->list m)))
81 (define-public (make-ps-images ps-name . rest)
87 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
88 (header (gulp-port (open-file ps-name "r") 10240))
89 (png1 (string-append base ".png"))
90 (pngn (string-append base "-page%d.png"))
91 (pngn-re (re-sub "%d" "[0-9]*" pngn))
92 (multi-page? (and (string-match "\n%%Pages: " header)
93 (not (string-match "\n%%Pages: 1\n" header))))
94 (output-file (if multi-page? pngn png1))
96 ;;png16m is because Lily produces color nowadays.
100 -dGraphicsAlphaBits=4\
111 (if verbose? "" "-q")
112 output-file paper-size resolution ps-name)
116 -dGraphicsAlphaBits=4\
126 (if verbose? "" "-q")
127 output-file resolution ps-name)))
128 (foo (for-each delete-file (append (dir-re "." png1)
129 (dir-re "." pngn-re))))
132 (format (current-error-port) (_ "Invoking `~a'...") cmd)
133 (newline (current-error-port)))))
135 ;; The wrapper on windows cannot handle `=' signs,
136 ;; gs has a workaround with #.
137 (if (eq? PLATFORM 'windows)
139 (set! cmd (re-sub "=" "#" cmd))
140 (set! cmd (re-sub "-dSAFER " "" cmd)))))
141 (status (system cmd)))
142 (if (not (= status 0))
145 (append (dir-re "." png1) (dir-re "." pngn-re)))
146 (format (current-error-port)
147 (format #f (_ "~a exited with status: ~S") "GS" status))
149 (if (and rename-page-1? multi-page?)
150 (rename-file (re-sub "%d" "1" pngn) png1))
151 (append (dir-re "." png1) (dir-re "." pngn-re)))))