3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 (define-module (scm ps-to-png))
19 ;; gettext wrapper for guile < 1.7.2
20 (if (defined? 'gettext)
21 (define-public _ gettext)
22 (define-public (_ x) x))
27 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
29 (define (re-sub re sub string)
30 (regexp-substitute/global #f re string 'pre sub 'post))
32 (define (search-executable names)
33 (define (helper path lst)
36 (if (search-path path (car lst)) (car lst)
37 (helper path (cdr lst)))))
39 (let ((path (parse-path (getenv "PATH"))))
43 (search-executable '("gs-nox" "gs-8.15" "gs")))
45 (define (gulp-port port max-length)
46 (let ((str (make-string max-length)))
47 (read-string!/partial str port 0 max-length)
50 (define (gulp-file nm len)
52 ;; string routines barf when strlen() != string-length,.
53 ;; which may happen as side effect of read-string!/partial.
55 ; (gulp-port (open-file nm "r") len))
56 (ly:gulp-file nm len))
59 (define BOUNDING-BOX-RE
60 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
62 (define (get-bbox file-name)
63 (let* ((bbox (string-append file-name ".bbox"))
64 ;; -sOutputFile does not work with bbox?
74 (s (gulp-file d bbox 10240))
75 (m (string-match BOUNDING_BOX_RE s)))
79 (map (lambda (x) (string->number (car x))) (vector->list m)))
83 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
84 (define (my-system be-verbose exit-on-error cmd)
88 (format (current-error-port) (_ "Invoking `~a'...") cmd)
89 (newline (current-error-port))))
92 (set! status (system cmd))
94 (if (not (= status 0))
96 (format (current-error-port)
97 (format #f (_ "~a exited with status: ~S") "GS" status))
103 (define (scale-down-image be-verbose factor file)
105 (percentage (* 100 (/ 1.0 factor)))
106 (old (string-append file ".old")))
108 (rename-file file old)
109 (my-system be-verbose
111 (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
115 (define-public (ps-page-count ps-name)
118 (header (gulp-file ps-name byte-count))
119 (first-null (string-index header #\nul))
120 (match (string-match "%%Pages: ([0-9]+)"
121 (if (number? first-null)
122 (substring header 0 first-null)
126 (string->number (match:substring match 1))
129 (define-public (make-ps-images ps-name . rest)
131 rest ((resolution 90)
139 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
140 (png1 (string-append base ".png"))
141 (pngn (string-append base "-page%d.png"))
142 (page-count (ps-page-count ps-name))
144 (multi-page? (> page-count 1))
145 (output-file (if multi-page? pngn png1))
147 ;; png16m is because Lily produces color nowadays.
148 ;; can't use pngalpha device, since IE is broken.
152 (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f" page-width page-height)
158 -dGraphicsAlphaBits=4\
167 (if verbose? "" "-q")
170 (* aa-factor resolution) ps-name))
174 ;; The wrapper on windows cannot handle `=' signs,
175 ;; gs has a workaround with #.
176 (if (eq? PLATFORM 'windows)
178 (set! cmd (re-sub "=" "#" cmd))
179 (set! cmd (re-sub "-dSAFER " "" cmd))))
181 (set! status (my-system verbose? #f cmd))
187 (format "~a-page~a.png" base (1+ n)))
189 (list (format "~a.png" base))))
191 (if (not (= 0 status))
193 (map delete-file files)
196 (if (and rename-page-1? multi-page?)
198 (rename-file (re-sub "%d" "1" pngn) png1)
204 (if (not (= 1 aa-factor))
205 (for-each (lambda (f) (scale-down-image verbose? aa-factor f))