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-public (gulp-file file-name . max-size)
51 ;; string routines barf when strlen() != string-length,.
52 ;; which may happen as side effect of read-string!/partial.
53 ;; (gulp-port (open-file nm "r") len))
54 (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
56 (define BOUNDING-BOX-RE
57 "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
59 (define (unused-found-broken-get-bbox file-name)
60 (let* ((bbox (string-append file-name ".bbox"))
61 ;; -sOutputFile does not work with bbox?
71 (s (gulp-file bbox 10240))
72 (m (string-match BOUNDING_BOX_RE s)))
76 (map (lambda (x) (string->number (car x))) (vector->list m)))
80 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
81 (define (my-system be-verbose exit-on-error cmd)
85 (format (current-error-port) (_ "Invoking `~a'...") cmd)
86 (newline (current-error-port))))
87 (set! status (system cmd))
88 (if (not (= status 0))
90 (format (current-error-port)
91 (format #f (_ "~a exited with status: ~S") "GS" status))
92 (if exit-on-error (exit 1))))
95 (define (scale-down-image be-verbose factor file)
97 ;;(percentage (* 100 (/ 1.0 factor)))
98 (old (string-append file ".old")))
100 (rename-file file old)
103 ;; convert -scale creates (a large rgb) png from a grayscale
104 ;; (format #f "convert -scale \"~a%\" -depth 8 ~a ~a" percentage old file))
106 (format #f "pngtopnm ~a | pnmscale -reduce ~a | pnmtopng > ~a" old factor file))
109 (define-public (ps-page-count ps-name)
110 (let* ((byte-count 10240)
111 (header (gulp-file ps-name byte-count))
112 (first-null (string-index header #\nul))
113 (match (string-match "%%Pages: ([0-9]+)"
114 (if (number? first-null)
115 (substring header 0 first-null)
117 (if match (string->number (match:substring match 1)) 0)))
119 (define-public (ps-has-color ps-name)
120 (string-contains (gulp-file ps-name) " setrgbcolor"))
122 (define-public (make-ps-images ps-name . rest)
130 (pixmap-format 'png16m)
131 (anti-alias-factor 1))
133 (let* ((format-str (format "~a" pixmap-format))
135 ((string-contains format-str "png") "png")
136 ((string-contains format-str "jpg") "jpeg")
137 ((string-contains format-str "jpeg") "jpeg")
139 (ly:error "Unknown pixmap format ~a" pixmap-format))))
140 (base (basename (re-sub "[.]e?ps" "" ps-name)))
141 (png1 (format "~a.~a" base extension))
142 (pngn (format "~a-page%d.~a" base extension))
143 (page-count (ps-page-count ps-name))
144 (multi-page? (> page-count 1))
145 (output-file (if multi-page? pngn png1))
149 (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
150 page-width page-height)
155 -dGraphicsAlphaBits=4\
164 (if be-verbose "" "-q")
168 (* anti-alias-factor resolution) ps-name))
172 ;; The wrapper on windows cannot handle `=' signs,
173 ;; gs has a workaround with #.
174 (if (eq? PLATFORM 'windows)
176 (set! cmd (re-sub "=" "#" cmd))
177 (set! cmd (re-sub "-dSAFER " "" cmd))))
179 (set! status (my-system be-verbose #f cmd))
185 (format "~a-page~a.png" base (1+ n)))
187 (list (format "~a.png" base))))
189 (if (not (= 0 status))
191 (map delete-file files)
194 (if (and rename-page-1 multi-page?)
196 (rename-file (re-sub "%d" "1" pngn) png1)
202 (if (not (= 1 anti-alias-factor))
204 (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))