3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 (define-module (scm ps-to-png))
15 ;; gettext wrapper for guile < 1.7.2
16 (if (defined? 'gettext)
17 (define-public _ gettext)
18 (define-public (_ x) x))
20 (define (re-sub re sub string)
21 (regexp-substitute/global #f re string 'pre sub 'post))
23 (define (gulp-port port how-much)
25 ((str (make-string how-much)))
27 (read-string!/partial str port)
30 (define (dir-listing dir-name)
31 (define (dir-helper dir lst)
32 (let ((e (readdir dir)))
33 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
34 (reverse (dir-helper (opendir dir-name) '())))
36 (define (dir-re dir re)
37 (filter (lambda (x) (string-match re x)) (dir-listing dir)))
39 (define BOUNDING-BOX-RE
40 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
42 (define (get-bbox file-name)
43 (let* ((bbox (string-append file-name ".bbox"))
44 ;; -sOutputFile does not work with bbox?
54 (s (gulp-port (open-file bbox "r") 10240))
55 (m (string-match BOUNDING_BOX_RE s)))
60 (map (lambda (x) (string->number (car x))) (vector->list m)))
63 (define-public (make-ps-images ps-name . rest)
69 (let* ((base (basename (re-sub "\.e?ps" "" ps-name)))
70 (header (gulp-port (open-file ps-name "r") 10240))
71 (png1 (string-append base ".png"))
72 (pngn (string-append base "-page%d.png"))
73 (pngn-re (re-sub "%d" "[0-9]*" pngn))
74 (multi-page? (string-match "\n%%Pages: " header))
75 (output-file (if multi-page? pngn png1))
76 ;;png16m is because Lily produces color nowadays.
77 (cmd (format #f (if multi-page?
80 -dGraphicsAlphaBits=4\
93 -dGraphicsAlphaBits=4\
103 output-file paper-size resolution ps-name))
104 (foo (for-each delete-file (append (dir-re "." png1)
105 (dir-re "." pngn-re))))
108 (format (current-error-port) (_ "Invoking `~a'...") cmd)
109 (newline (current-error-port)))))
110 (status (system cmd)))
111 (if (not (= status 0))
114 (append (dir-re "." png1) (dir-re "." pngn-re)))
115 (format (current-error-port)
116 (format #f (_ "~a exited with status: ~S") "GS" status))
118 (if (and rename-page-1? multi-page?)
119 (rename (re-sub "%d" "1" pngn) png1))
120 (append (dir-re "." png1) (dir-re "." pngn-re)))))