3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 (define-module (scm ps-to-png))
18 ;; gettext wrapper for guile < 1.7.2
19 (if (defined? 'gettext)
20 (define-public _ gettext)
21 (define-public (_ x) x))
26 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
28 (define (re-sub re sub string)
29 (regexp-substitute/global #f re string 'pre sub 'post))
31 (define (search-executable names)
32 (define (helper path lst)
35 (if (search-path path (car lst)) (car lst)
36 (helper path (cdr lst)))))
38 (let ((path (parse-path (getenv "PATH"))))
42 (search-executable '("gs-nox" "gs-8.15" "gs")))
44 (define (gulp-port port max-length)
45 (let ((str (make-string max-length)))
46 (read-string!/partial str port 0 max-length)
49 (define (gulp-file nm len)
50 (gulp-port (open-file nm "r") len))
52 ;;; ARGH - cuases memory usage to explode with GUILE cvs.
54 (define BOUNDING-BOX-RE
55 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
57 (define (get-bbox file-name)
58 (let* ((bbox (string-append file-name ".bbox"))
59 ;; -sOutputFile does not work with bbox?
69 (s (gulp-file d bbox 10240))
70 (m (string-match BOUNDING_BOX_RE s)))
75 (map (lambda (x) (string->number (car x))) (vector->list m)))
79 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
80 (define (my-system be-verbose exit-on-error cmd)
84 (format (current-error-port) (_ "Invoking `~a'...") cmd)
85 (newline (current-error-port))))
88 (set! status (system cmd))
90 (if (not (= status 0))
92 (format (current-error-port)
93 (format #f (_ "~a exited with status: ~S") "GS" status))
99 (define (scale-down-image be-verbose factor file)
101 (percentage (* 100 (/ 1.0 factor)))
102 (old (string-append file ".old")))
104 (rename-file file old)
105 (my-system be-verbose
107 (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
112 (define-public (ps-page-count ps-name)
114 ((header (gulp-file ps-name 10240))
115 (match (string-match "%%Pages: ([0-9]+)" header))
117 (string->number (match:substring match 1))
121 (define-public (make-ps-images ps-name . rest)
123 rest ((resolution 90)
130 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
131 (header (gulp-file ps-name 10240))
132 (png1 (string-append base ".png"))
133 (pngn (string-append base "-page%d.png"))
134 (page-count (ps-page-count ps-name))
136 (multi-page? (> page-count 1))
137 (output-file (if multi-page? pngn png1))
139 ;;png16m is because Lily produces color nowadays.
142 (format #f "-sPAPERSIZE=~a" paper-size)
147 -dGraphicsAlphaBits=4\
156 (if verbose? "" "-q")
159 (* aa-factor resolution) ps-name))
163 ;; The wrapper on windows cannot handle `=' signs,
164 ;; gs has a workaround with #.
165 (if (eq? PLATFORM 'windows)
167 (set! cmd (re-sub "=" "#" cmd))
168 (set! cmd (re-sub "-dSAFER " "" cmd))))
170 (set! status (my-system verbose? #f cmd))
176 (format "~a-page~a.png" base n))
178 (list (format "~a.png" base)))
180 (if (not (= 0 status))
182 (map delete-file files)
185 (if (and rename-page-1? multi-page?)
187 (rename-file (re-sub "%d" "1" pngn) png1)
193 (if (not (= 1 aa-factor))
194 (for-each (lambda (f) (scale-down-image verbose? aa-factor f))