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)
25 (define-public (ly:system command . rest)
27 (dev-null "/dev/null")
28 (silenced (if (or (ly:get-option 'verbose)
29 (not (access? dev-null W_OK)))
31 (format #f "~a > ~a 2>&1 " command dev-null))))
32 (if (ly:get-option 'verbose)
34 (ly:message (_ "Invoking `~a'...") command))
39 (system-with-env silenced (car rest))
44 (ly:message (_ "`~a' failed (~a)") command status)
46 ;; hmmm. what's the best failure option?
47 (throw 'ly-file-failed)))))
49 (define-public (system-with-env cmd env)
51 "Execute CMD in fork, with ENV (a list of strings) as the environment"
53 ;; laziness: should use execle?
55 ((pid (primitive-fork)))
63 (cdr (waitpid pid)))))
65 (define-public (sanitize-command-option str)
66 "Kill dubious shell quoting"
70 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
73 (define-public (search-executable names)
74 (define (helper path lst)
77 (if (search-path path (car lst)) (car lst)
78 (helper path (cdr lst)))))
80 (let ((path (parse-path (getenv "PATH"))))
83 (define-public (search-gs)
85 ;; must be sure that we don't catch stuff from old GUBs.
86 (search-executable '("gs")))
88 (define-public (postscript->pdf paper-width paper-height name)
89 (let* ((pdf-name (string-append
90 (dir-basename name ".ps" ".eps")
92 (is-eps (string-match "\\.eps$" name))
93 (paper-size-string (if is-eps
95 (ly:format "-dDEVICEWIDTHPOINTS=~$\
96 -dDEVICEHEIGHTPOINTS=~$"
97 paper-width paper-height)))
104 -dCompatibilityLevel=1.4\
114 (if (ly:get-option 'verbose) "" "-q")
115 (if (or (ly:get-option 'gs-load-fonts)
116 (ly:get-option 'gs-load-lily-fonts))
122 ;; The wrapper on windows cannot handle `=' signs,
123 ;; gs has a workaround with #.
124 (if (eq? PLATFORM 'windows)
126 (set! cmd (string-regexp-substitute "=" "#" cmd))
127 (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))
128 (if (access? pdf-name W_OK)
129 (delete-file pdf-name))))
131 (ly:message (_ "Converting to `~a'...") pdf-name)
135 (define-public (postscript->png resolution paper-width paper-height name)
136 (let* ((verbose (ly:get-option 'verbose))
139 ;; Do not try to guess the name of the png file,
140 ;; GS produces PNG files like BASE-page%d.png.
141 (ly:message (_ "Converting to ~a...") "PNG")
143 #:resolution resolution
144 #:page-width paper-width
145 #:page-height paper-height
146 #:rename-page-1 rename-page-1
148 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
149 #:pixmap-format (ly:get-option 'pixmap-format))
152 (define-public (postprocess-output paper-book module filename formats)
153 (let* ((completed (completize-formats formats))
154 (base (dir-basename filename ".ps" ".eps"))
155 (intermediate (remove (lambda (x) (member x formats)) completed)))
156 (for-each (lambda (f)
157 ((eval (string->symbol (format "convert-to-~a" f))
158 module) paper-book filename)) completed)
159 (if (ly:get-option 'delete-intermediate-files)
160 (for-each (lambda (f)
161 (if (file-exists? f) (delete-file f)))
162 (map (lambda (x) (string-append base "." x)) intermediate)))))
164 (define-public (completize-formats formats)
165 (define new-fmts '())
166 (if (member "png" formats)
167 (set! formats (cons "ps" formats)))
168 (if (member "pdf" formats)
169 (set! formats (cons "ps" formats)))
170 (for-each (lambda (x)
171 (if (member x formats) (set! new-fmts (cons x new-fmts))))
173 (uniq-list (reverse new-fmts)))
175 (define (header-to-file file-name key value)
176 (set! key (symbol->string key))
177 (if (not (equal? "-" file-name))
178 (set! file-name (string-append file-name "." key)))
179 (ly:message (_ "Writing header field `~a' to `~a'...")
181 (if (equal? "-" file-name) "<stdout>" file-name))
182 (if (equal? file-name "-")
184 (let ((port (open-file file-name "w")))
191 (define-public (output-scopes scopes fields basename)
192 (define (output-scope scope)
197 (let ((val (if (variable-bound? var) (variable-ref var) "")))
198 (if (and (memq sym fields) (string? val))
199 (header-to-file basename sym val))
202 (apply string-append (map output-scope scopes)))
204 (define missing-stencil-list '())
206 (define-public (backend-testing output-module)
207 (define (missing-stencil-expression name)
209 (ly:warning (_ "missing stencil expression `~S'") name)
213 (if (not (module-defined? output-module x))
215 (module-define! output-module x
216 (lambda* (#:optional y . z)
217 (missing-stencil-expression x)))
218 (set! missing-stencil-list (append (list x)
219 missing-stencil-list)))))
220 (ly:all-stencil-commands)))
222 (define-public (remove-stencil-warnings output-module)
225 (module-remove! output-module x))
226 missing-stencil-list))