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))
24 (let ((s (read-delimited "" port)))
25 (if (eof-object? s) "" s)))
27 (define (dir-listing dir-name)
28 (define (dir-helper dir lst)
29 (let ((e (readdir dir)))
30 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
31 (reverse (dir-helper (opendir dir-name) '())))
33 (define (dir-re dir re)
34 (filter (lambda (x) (string-match re x)) (dir-listing dir)))
36 (define BOUNDING-BOX-RE
37 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
39 (define (get-bbox file-name)
40 (let* ((bbox (string-append file-name ".bbox"))
41 ;; -sOutputFile does not work with bbox?
51 (s (read (open-file bbox "r")))
52 (m (string-match BOUNDING_BOX_RE s)))
57 (map (lambda (x) (string->number (car x))) (vector->list m)))
60 (define-public (make-ps-images ps-name . rest)
66 (let* ((base (basename (re-sub "\.e?ps" "" ps-name)))
67 (header (read (open-file ps-name "r")))
68 (png1 (string-append base ".png"))
69 (pngn (string-append base "-page%d.png"))
70 (pngn-re (re-sub "%d" "[0-9]*" pngn))
71 (multi-page? (string-match "\n%%Pages: " header))
72 (output-file (if multi-page? pngn png1))
73 ;;png16m is because Lily produces color nowadays.
74 (cmd (format #f (if multi-page?
77 -dGraphicsAlphaBits=4\
90 -dGraphicsAlphaBits=4\
100 output-file paper-size resolution ps-name))
101 (foo (for-each delete-file (append (dir-re "." png1)
102 (dir-re "." pngn-re))))
105 (format (current-error-port) (_ "Invoking `~a'...") cmd)
106 (newline (current-error-port)))))
107 (status (system cmd)))
108 (if (not (= status 0))
111 (append (dir-re "." png1) (dir-re "." pngn-re)))
112 (format (current-error-port)
113 (format #f (_ "~a exited with status: ~S") "GS" status))
115 (if (and rename-page-1? multi-page?)
116 (rename (re-sub "%d" "1" pngn) png1))
117 (append (dir-re "." png1) (dir-re "." pngn-re)))))