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)))
74 (map (lambda (x) (string->number (car x))) (vector->list m)))
78 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
79 (define (my-system be-verbose exit-on-error cmd)
83 (format (current-error-port) (_ "Invoking `~a'...") cmd)
84 (newline (current-error-port))))
87 (set! status (system cmd))
89 (if (not (= status 0))
91 (format (current-error-port)
92 (format #f (_ "~a exited with status: ~S") "GS" status))
98 (define (scale-down-image be-verbose factor file)
100 (percentage (* 100 (/ 1.0 factor)))
101 (old (string-append file ".old")))
103 (rename-file file old)
104 (my-system be-verbose
106 (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
111 (define-public (ps-page-count ps-name)
113 ((header (gulp-file ps-name 10240))
114 (match (string-match "%%Pages: ([0-9]+)" header))
116 (string->number (match:substring match 1))
120 (define-public (make-ps-images ps-name . rest)
122 rest ((resolution 90)
129 (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
130 (header (gulp-file ps-name 10240))
131 (png1 (string-append base ".png"))
132 (pngn (string-append base "-page%d.png"))
133 (page-count (ps-page-count ps-name))
135 (multi-page? (> page-count 1))
136 (output-file (if multi-page? pngn png1))
138 ;;png16m is because Lily produces color nowadays.
141 (format #f "-sPAPERSIZE=~a" paper-size)
146 -dGraphicsAlphaBits=4\
155 (if verbose? "" "-q")
158 (* aa-factor resolution) ps-name))
162 ;; The wrapper on windows cannot handle `=' signs,
163 ;; gs has a workaround with #.
164 (if (eq? PLATFORM 'windows)
166 (set! cmd (re-sub "=" "#" cmd))
167 (set! cmd (re-sub "-dSAFER " "" cmd))))
169 (set! status (my-system verbose? #f cmd))
175 (format "~a-page~a.png" base (1+ n)))
177 (list (format "~a.png" base))))
179 (if (not (= 0 status))
181 (map delete-file files)
184 (if (and rename-page-1? multi-page?)
186 (rename-file (re-sub "%d" "1" pngn) png1)
192 (if (not (= 1 aa-factor))
193 (for-each (lambda (f) (scale-down-image verbose? aa-factor f))