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))
129 (if (access? pdf-name W_OK)
130 (delete-file pdf-name))))
132 (ly:message (_ "Converting to `~a'...") pdf-name)
136 (define-public (postscript->png resolution paper-width paper-height name)
137 (let* ((verbose (ly:get-option 'verbose))
140 ;; Do not try to guess the name of the png file,
141 ;; GS produces PNG files like BASE-page%d.png.
142 (ly:message (_ "Converting to ~a...") "PNG")
144 #:resolution resolution
145 #:page-width paper-width
146 #:page-height paper-height
147 #:rename-page-1 rename-page-1
149 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
150 #:pixmap-format (ly:get-option 'pixmap-format))
153 (define-public (postprocess-output paper-book module filename formats)
154 (let* ((completed (completize-formats formats))
155 (base (dir-basename filename ".ps" ".eps"))
156 (intermediate (remove (lambda (x) (member x formats)) completed)))
157 (for-each (lambda (f)
158 ((eval (string->symbol (format "convert-to-~a" f))
159 module) paper-book filename)) completed)
160 (if (ly:get-option 'delete-intermediate-files)
161 (for-each (lambda (f)
162 (if (file-exists? f) (delete-file f)))
163 (map (lambda (x) (string-append base "." x)) intermediate)))))
165 (define-public (completize-formats formats)
166 (define new-fmts '())
167 (if (member "png" formats)
168 (set! formats (cons "ps" formats)))
169 (if (member "pdf" formats)
170 (set! formats (cons "ps" formats)))
171 (for-each (lambda (x)
172 (if (member x formats) (set! new-fmts (cons x new-fmts))))
174 (uniq-list (reverse new-fmts)))
176 (define (header-to-file file-name key value)
177 (set! key (symbol->string key))
178 (if (not (equal? "-" file-name))
179 (set! file-name (string-append file-name "." key)))
180 (ly:message (_ "Writing header field `~a' to `~a'...")
182 (if (equal? "-" file-name) "<stdout>" file-name))
183 (if (equal? file-name "-")
185 (let ((port (open-file file-name "w")))
192 (define-public (output-scopes scopes fields basename)
193 (define (output-scope scope)
198 (let ((val (if (variable-bound? var) (variable-ref var) "")))
199 (if (and (memq sym fields) (string? val))
200 (header-to-file basename sym val))
203 (apply string-append (map output-scope scopes)))
205 (define-public (relevant-book-systems book)
206 (let ((systems (ly:paper-book-systems book)))
208 (if (and (not (ly:get-option 'include-book-title-preview))
210 (ly:prob-property (car systems) 'is-book-title #f))
214 (define-public (relevant-dump-systems systems)
215 (let ((to-dump-systems '()))
218 (if (or (paper-system-title? sys)
219 (not (pair? to-dump-systems))
220 (paper-system-title? (car to-dump-systems)))
221 (set! to-dump-systems (cons sys to-dump-systems))))
225 (define missing-stencil-list '())
227 (define-public (backend-testing output-module)
228 (define (missing-stencil-expression name)
230 (ly:warning (_ "missing stencil expression `~S'") name)
234 (if (not (module-defined? output-module x))
236 (module-define! output-module x
237 (lambda* (#:optional y . z)
238 (missing-stencil-expression x)))
239 (set! missing-stencil-list (append (list x)
240 missing-stencil-list)))))
241 (ly:all-stencil-commands)))
243 (define-public (remove-stencil-warnings output-module)
246 (module-remove! output-module x))
247 missing-stencil-list))