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 (gulp-port port max-length)
31 (let ((str (make-string max-length)))
32 (read-string!/partial str port)
35 (define (dir-listing dir-name)
36 (define (dir-helper dir lst)
37 (let ((e (readdir dir)))
38 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
39 (reverse (dir-helper (opendir dir-name) '())))
41 (define (dir-re dir re)
42 (filter (lambda (x) (string-match re x)) (dir-listing dir)))
44 (define BOUNDING-BOX-RE
45 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
47 (define (get-bbox file-name)
48 (let* ((bbox (string-append file-name ".bbox"))
49 ;; -sOutputFile does not work with bbox?
59 (s (gulp-port (open-file bbox "r") 10240))
60 (m (string-match BOUNDING_BOX_RE s)))
65 (map (lambda (x) (string->number (car x))) (vector->list m)))
68 (define-public (make-ps-images ps-name . rest)
74 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
75 (header (gulp-port (open-file ps-name "r") 10240))
76 (png1 (string-append base ".png"))
77 (pngn (string-append base "-page%d.png"))
78 (pngn-re (re-sub "%d" "[0-9]*" pngn))
79 (multi-page? (and (string-match "\n%%Pages: " header)
80 (not (string-match "\n%%Pages: 1\n" header))))
81 (output-file (if multi-page? pngn png1))
83 ;;png16m is because Lily produces color nowadays.
86 -dGraphicsAlphaBits=4\
96 -c quit" output-file paper-size resolution ps-name)
99 -dGraphicsAlphaBits=4\
108 -c quit" output-file resolution ps-name)))
109 (foo (for-each delete-file (append (dir-re "." png1)
110 (dir-re "." pngn-re))))
113 (format (current-error-port) (_ "Invoking `~a'...") cmd)
114 (newline (current-error-port)))))
116 ;; The wrapper on windows cannot handle `=' signs,
117 ;; gs has a workaround with #.
118 (if (eq? PLATFORM 'windows)
120 (set! cmd (re-sub "=" "#" cmd))
121 (set! cmd (re-sub "-dSAFER " "" cmd)))))
122 (status (system cmd)))
123 (if (not (= status 0))
126 (append (dir-re "." png1) (dir-re "." pngn-re)))
127 (format (current-error-port)
128 (format #f (_ "~a exited with status: ~S") "GS" status))
130 (if (and rename-page-1? multi-page?)
131 (rename (re-sub "%d" "1" pngn) png1))
132 (append (dir-re "." png1) (dir-re "." pngn-re)))))