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)))
82 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
83 (define (my-system be-verbose exit-on-error cmd)
87 (format (current-error-port) (_ "Invoking `~a'...") cmd)
88 (newline (current-error-port))))
91 (set! status (system cmd))
93 (if (not (= status 0))
95 (format (current-error-port)
96 (format #f (_ "~a exited with status: ~S") "GS" status))
102 (define (scale-down-image be-verbose factor file)
104 (percentage (* 100 (/ 1.0 factor)))
105 (old (string-append file ".old")))
107 (rename-file file old)
108 (my-system be-verbose
110 (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
114 (define-public (make-ps-images ps-name . rest)
116 rest ((resolution 90)
123 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
124 (header (gulp-port (open-file ps-name "r") 10240))
125 (png1 (string-append base ".png"))
126 (pngn (string-append base "-page%d.png"))
127 (pngn-re (re-sub "%d" "[0-9]*" pngn))
128 (multi-page? (and (string-match "\n%%Pages: " header)
129 (not (string-match "\n%%Pages: 1\n" header))))
130 (output-file (if multi-page? pngn png1))
132 ;;png16m is because Lily produces color nowadays.
135 (format #f "-sPAPERSIZE=~a" paper-size)
140 -dGraphicsAlphaBits=4\
149 (if verbose? "" "-q")
152 (* aa-factor resolution) ps-name))
156 (for-each delete-file (append (dir-re "." png1)
157 (dir-re "." pngn-re)))
159 ;; The wrapper on windows cannot handle `=' signs,
160 ;; gs has a workaround with #.
161 (if (eq? PLATFORM 'windows)
163 (set! cmd (re-sub "=" "#" cmd))
164 (set! cmd (re-sub "-dSAFER " "" cmd))))
166 (set! status (my-system verbose? #f cmd))
169 (append (dir-re "." png1) (dir-re "." pngn-re)))
171 (if (not (= 0 status))
173 (map delete-file files)
176 (if (and rename-page-1? multi-page?)
177 (rename-file (re-sub "%d" "1" pngn) png1))
180 (append (dir-re "." png1) (dir-re "." pngn-re)))
183 (if (not (= 1 aa-factor))
184 (for-each (lambda (f) (scale-down-image verbose? aa-factor f))