1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2005--2010 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 (use-modules (scm ps-to-png)
26 (define-public (ly:system command . rest)
28 (dev-null "/dev/null")
29 (silenced (if (or (ly:get-option 'verbose)
30 (not (access? dev-null W_OK)))
32 (format #f "~a > ~a 2>&1 " command dev-null))))
33 (if (ly:get-option 'verbose)
35 (ly:message (_ "Invoking `~a'...") command))
40 (system-with-env silenced (car rest))
45 (ly:message (_ "`~a' failed (~a)") command status)
47 ;; hmmm. what's the best failure option?
48 (throw 'ly-file-failed)))))
50 (define-public (system-with-env cmd env)
52 "Execute CMD in fork, with ENV (a list of strings) as the environment"
54 ;; laziness: should use execle?
56 ((pid (primitive-fork)))
64 (cdr (waitpid pid)))))
66 (define-public (sanitize-command-option str)
67 "Kill dubious shell quoting"
71 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
74 (define-public (search-executable names)
75 (define (helper path lst)
78 (if (search-path path (car lst)) (car lst)
79 (helper path (cdr lst)))))
81 (let ((path (parse-path (getenv "PATH"))))
84 (define-public (search-gs)
86 ;; must be sure that we don't catch stuff from old GUBs.
87 (search-executable '("gs")))
89 (define-public (postscript->pdf paper-width paper-height name)
90 (let* ((pdf-name (string-append
91 (dir-basename name ".ps" ".eps")
93 (is-eps (string-match "\\.eps$" name))
94 (paper-size-string (if is-eps
96 (ly:format "-dDEVICEWIDTHPOINTS=~$\
97 -dDEVICEHEIGHTPOINTS=~$"
98 paper-width paper-height)))
105 -dCompatibilityLevel=1.4\
115 (if (ly:get-option 'verbose) "" "-q")
116 (if (or (ly:get-option 'gs-load-fonts)
117 (ly:get-option 'gs-load-lily-fonts))
123 ;; The wrapper on windows cannot handle `=' signs,
124 ;; gs has a workaround with #.
125 (if (eq? PLATFORM 'windows)
127 (set! cmd (string-regexp-substitute "=" "#" cmd))
128 (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
130 (ly:message (_ "Converting to `~a'...") pdf-name)
134 (define-public (postscript->png resolution paper-width paper-height name)
135 (let* ((verbose (ly:get-option 'verbose))
138 ;; Do not try to guess the name of the png file,
139 ;; GS produces PNG files like BASE-page%d.png.
140 (ly:message (_ "Converting to ~a...") "PNG")
142 #:resolution resolution
143 #:page-width paper-width
144 #:page-height paper-height
145 #:rename-page-1 rename-page-1
147 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
148 #:pixmap-format (ly:get-option 'pixmap-format))
151 (define-public (postprocess-output paper-book module filename formats)
152 (let* ((completed (completize-formats formats))
153 (base (dir-basename filename ".ps" ".eps"))
154 (intermediate (remove (lambda (x) (member x formats)) completed)))
155 (for-each (lambda (f)
156 ((eval (string->symbol (format "convert-to-~a" f))
157 module) paper-book filename)) completed)
158 (if (ly:get-option 'delete-intermediate-files)
159 (for-each (lambda (f)
160 (if (file-exists? f) (delete-file f)))
161 (map (lambda (x) (string-append base "." x)) intermediate)))))
163 (define-public (completize-formats formats)
164 (define new-fmts '())
165 (if (member "png" formats)
166 (set! formats (cons "ps" formats)))
167 (if (member "pdf" formats)
168 (set! formats (cons "ps" formats)))
169 (for-each (lambda (x)
170 (if (member x formats) (set! new-fmts (cons x new-fmts))))
172 (uniq-list (reverse new-fmts)))
174 (define (header-to-file file-name key value)
175 (set! key (symbol->string key))
176 (if (not (equal? "-" file-name))
177 (set! file-name (string-append file-name "." key)))
178 (ly:message (_ "Writing header field `~a' to `~a'...")
180 (if (equal? "-" file-name) "<stdout>" file-name))
181 (if (equal? file-name "-")
183 (let ((port (open-file file-name "w")))
190 (define-public (output-scopes scopes fields basename)
191 (define (output-scope scope)
196 (let ((val (if (variable-bound? var) (variable-ref var) "")))
197 (if (and (memq sym fields) (string? val))
198 (header-to-file basename sym val))
201 (apply string-append (map output-scope scopes)))
203 (define-public (relevant-book-systems book)
204 (let ((systems (ly:paper-book-systems book)))
206 (if (and (not (ly:get-option 'include-book-title-preview))
208 (ly:prob-property (car systems) 'is-book-title #f))
212 (define-public (relevant-dump-systems systems)
213 (let ((to-dump-systems '()))
216 (if (or (paper-system-title? sys)
217 (not (pair? to-dump-systems))
218 (paper-system-title? (car to-dump-systems)))
219 (set! to-dump-systems (cons sys to-dump-systems))))
223 (define missing-stencil-list '())
225 (define-public (backend-testing output-module)
226 (define (missing-stencil-expression name)
228 (ly:warning (_ "missing stencil expression `~S'") name)
232 (if (not (module-defined? output-module x))
234 (module-define! output-module x
235 (lambda* (#:optional y . z)
236 (missing-stencil-expression x)))
237 (set! missing-stencil-list (append (list x)
238 missing-stencil-list)))))
239 (ly:all-stencil-commands)))
241 (define-public (remove-stencil-warnings output-module)
244 (module-remove! output-module x))
245 missing-stencil-list))
247 (define (filter-out pred? lst)
248 (filter (lambda (x) (not (pred? x))) lst))
250 (define-public (font-name-split font-name)
251 "Return (FONT-NAME . DESIGN-SIZE) from FONT-NAME string or #f."
252 (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
253 (if (regexp-match? match)
254 (cons (match:substring match 1) (match:substring match 2))
255 (cons font-name-designsize #f))))
257 ;; Example of a pango-physical-font
258 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
259 (define-public (pango-pf-font-name pango-pf)
260 "Return the font-name of the pango physical font PANGO-PF."
261 (list-ref pango-pf 0))
262 (define-public (pango-pf-file-name pango-pf)
263 "Return the file-name of the pango physical font PANGO-PF."
264 (list-ref pango-pf 1))
265 (define-public (pango-pf-fontindex pango-pf)
266 "Return the fontindex of the pango physical font PANGO-PF."
267 (list-ref pango-pf 2))
269 (define (pango-font-name pango-font)
270 (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
272 (pango-pf-font-name (car pf-fonts))
275 (define-public (define-fonts paper define-font define-pango-pf)
276 "Return a string of all fonts used in PAPER, invoking the functions
277 DEFINE-FONT DEFINE-PANGO-PF for producing the actual font definition."
279 (let* ((font-list (ly:paper-fonts paper))
280 (pango-fonts (filter ly:pango-font? font-list))
281 (other-fonts (filter-out ly:pango-font? font-list))
282 (other-font-names (map ly:font-name other-fonts))
284 (filter-out (lambda (x)
285 (member (pango-font-name x) other-font-names))
288 (define (font-load-command font)
289 (let* ((font-name (ly:font-name font))
290 (designsize (ly:font-design-size font))
291 (magnification (* (ly:font-magnification font)))
292 (ops (ly:output-def-lookup paper 'output-scale))
293 (scaling (* ops magnification designsize)))
294 (if (equal? font-name "unknown")
295 (display (list font font-name)))
296 (define-font font font-name scaling)))
298 (define (pango-font-load-command pango-font)
299 (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
300 (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
301 (font-name (pango-pf-font-name pango-pf))
302 (scaling (ly:output-def-lookup paper 'output-scale)))
303 (if (equal? font-name "unknown")
304 (display (list pango-font font-name)))
305 (define-pango-pf pango-pf font-name scaling)))
308 (apply string-append (map font-load-command other-fonts))
309 (apply string-append (map pango-font-load-command pango-only-fonts)))))