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 max-length)
24 (let ((str (make-string max-length)))
25 (read-string!/partial str port)
28 (define (dir-listing dir-name)
29 (define (dir-helper dir lst)
30 (let ((e (readdir dir)))
31 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
32 (reverse (dir-helper (opendir dir-name) '())))
34 (define (dir-re dir re)
35 (filter (lambda (x) (string-match re x)) (dir-listing dir)))
37 (define BOUNDING-BOX-RE
38 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
40 (define (get-bbox file-name)
41 (let* ((bbox (string-append file-name ".bbox"))
42 ;; -sOutputFile does not work with bbox?
52 (s (gulp-port (open-file bbox "r") 10240))
53 (m (string-match BOUNDING_BOX_RE s)))
58 (map (lambda (x) (string->number (car x))) (vector->list m)))
61 (define-public (make-ps-images ps-name . rest)
67 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
68 (header (gulp-port (open-file ps-name "r") 10240))
69 (png1 (string-append base ".png"))
70 (pngn (string-append base "-page%d.png"))
71 (pngn-re (re-sub "%d" "[0-9]*" pngn))
72 (multi-page? (and (string-match "\n%%Pages: " header)
73 (not (string-match "\n%%Pages: 1\n" header))))
74 (output-file (if multi-page? pngn png1))
76 ;;png16m is because Lily produces color nowadays.
79 -dGraphicsAlphaBits=4\
89 -c quit" output-file paper-size resolution ps-name)
92 -dGraphicsAlphaBits=4\
101 -c quit" output-file resolution ps-name)))
102 (foo (for-each delete-file (append (dir-re "." png1)
103 (dir-re "." pngn-re))))
106 (format (current-error-port) (_ "Invoking `~a'...") cmd)
107 (newline (current-error-port)))))
108 (status (system cmd)))
109 (if (not (= status 0))
112 (append (dir-re "." png1) (dir-re "." pngn-re)))
113 (format (current-error-port)
114 (format #f (_ "~a exited with status: ~S") "GS" status))
116 (if (and rename-page-1? multi-page?)
117 (rename (re-sub "%d" "1" pngn) png1))
118 (append (dir-re "." png1) (dir-re "." pngn-re)))))