1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2005--2011 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)
27 (if (ly:get-option 'verbose)
29 (ly:message (_ "Invoking `~a'...") (string-join command)))
31 (let ((status (apply ly:spawn command)))
34 (ly:message (_ "`~a' failed (~a)") command status)
36 ;; hmmm. what's the best failure option?
37 (throw 'ly-file-failed)))))
39 (define-public (sanitize-command-option str)
40 "Kill dubious shell quoting."
44 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
47 (define-public (search-executable names)
48 (define (helper path lst)
51 (if (search-path path (car lst)) (car lst)
52 (helper path (cdr lst)))))
54 (let ((path (parse-path (getenv "PATH"))))
57 (define-public (search-gs)
59 ;; must be sure that we don't catch stuff from old GUBs.
60 (search-executable '("gs")))
62 (define-public (postscript->pdf paper-width paper-height name)
63 (let* ((pdf-name (string-append
64 (dir-basename name ".ps" ".eps")
66 (is-eps (string-match "\\.eps$" name))
67 (*unspecified* (if #f #f))
69 (remove (lambda (x) (eq? x *unspecified*))
72 (if (ly:get-option 'verbose) *unspecified* "-q")
73 (if (or (ly:get-option 'gs-load-fonts)
74 (ly:get-option 'gs-load-lily-fonts)
75 (eq? PLATFORM 'windows))
81 (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
84 (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
85 "-dCompatibilityLevel=1.4"
90 (string-append "-sOutputFile=" pdf-name)
92 (string-append "-f" name)))))
94 (ly:message (_ "Converting to `~a'...") pdf-name)
98 (define-public (postscript->png resolution paper-width paper-height name)
99 (let* ((verbose (ly:get-option 'verbose))
102 ;; Do not try to guess the name of the png file,
103 ;; GS produces PNG files like BASE-page%d.png.
104 (ly:message (_ "Converting to ~a...") "PNG")
106 #:resolution resolution
107 #:page-width paper-width
108 #:page-height paper-height
109 #:rename-page-1 rename-page-1
111 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
112 #:pixmap-format (ly:get-option 'pixmap-format))
115 (define-public (postprocess-output paper-book module filename formats)
116 (let* ((completed (completize-formats formats))
117 (base (dir-basename filename ".ps" ".eps"))
118 (intermediate (remove (lambda (x) (member x formats)) completed)))
119 (for-each (lambda (f)
120 ((eval (string->symbol (format "convert-to-~a" f))
121 module) paper-book filename)) completed)
122 (if (ly:get-option 'delete-intermediate-files)
123 (for-each (lambda (f)
124 (if (file-exists? f) (delete-file f)))
125 (map (lambda (x) (string-append base "." x)) intermediate)))))
127 (define-public (completize-formats formats)
128 (define new-fmts '())
129 (if (member "png" formats)
130 (set! formats (cons "ps" formats)))
131 (if (member "pdf" formats)
132 (set! formats (cons "ps" formats)))
133 (for-each (lambda (x)
134 (if (member x formats) (set! new-fmts (cons x new-fmts))))
136 (uniq-list (reverse new-fmts)))
138 (define (header-to-file file-name key value)
139 (set! key (symbol->string key))
140 (if (not (equal? "-" file-name))
141 (set! file-name (string-append file-name "." key)))
142 (ly:message (_ "Writing header field `~a' to `~a'...")
144 (if (equal? "-" file-name) "<stdout>" file-name))
145 (if (equal? file-name "-")
147 (let ((port (open-file file-name "w")))
154 (define-public (output-scopes scopes fields basename)
155 (define (output-scope scope)
160 (let ((val (if (variable-bound? var) (variable-ref var) "")))
161 (if (and (memq sym fields) (string? val))
162 (header-to-file basename sym val))
165 (apply string-append (map output-scope scopes)))
167 (define-public (relevant-book-systems book)
168 (let ((systems (ly:paper-book-systems book)))
170 (if (and (not (ly:get-option 'include-book-title-preview))
172 (ly:prob-property (car systems) 'is-book-title #f))
176 (define-public (relevant-dump-systems systems)
177 (let ((to-dump-systems '()))
180 (if (or (paper-system-title? sys)
181 (not (pair? to-dump-systems))
182 (paper-system-title? (car to-dump-systems)))
183 (set! to-dump-systems (cons sys to-dump-systems))))
187 (define missing-stencil-list '())
189 (define-public (backend-testing output-module)
190 (define (missing-stencil-expression name)
192 (ly:warning (_ "missing stencil expression `~S'") name)
196 (if (not (module-defined? output-module x))
198 (module-define! output-module x
199 (lambda* (#:optional y . z)
200 (missing-stencil-expression x)))
201 (set! missing-stencil-list (append (list x)
202 missing-stencil-list)))))
203 (ly:all-stencil-commands)))
205 (define-public (remove-stencil-warnings output-module)
208 (module-remove! output-module x))
209 missing-stencil-list))
211 (define (filter-out pred? lst)
212 (filter (lambda (x) (not (pred? x))) lst))
214 (define-public (font-name-split font-name)
215 "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
217 (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
218 (if (regexp-match? match)
219 (cons (match:substring match 1) (match:substring match 2))
220 (cons font-name-designsize #f))))
222 ;; Example of a pango-physical-font
223 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
224 (define-public (pango-pf-font-name pango-pf)
225 "Return the font-name of the pango physical font @var{pango-pf}."
226 (list-ref pango-pf 0))
227 (define-public (pango-pf-file-name pango-pf)
228 "Return the file-name of the pango physical font @var{pango-pf}."
229 (list-ref pango-pf 1))
230 (define-public (pango-pf-fontindex pango-pf)
231 "Return the fontindex of the pango physical font @var{pango-pf}."
232 (list-ref pango-pf 2))
234 (define (pango-font-name pango-font)
235 (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
237 (pango-pf-font-name (car pf-fonts))
240 (define-public (define-fonts paper define-font define-pango-pf)
241 "Return a string of all fonts used in @var{paper}, invoking the functions
242 @var{define-font} and @var{define-pango-pf} for producing the actual font
245 (let* ((font-list (ly:paper-fonts paper))
246 (pango-fonts (filter ly:pango-font? font-list))
247 (other-fonts (filter-out ly:pango-font? font-list))
248 (other-font-names (map ly:font-name other-fonts))
250 (filter-out (lambda (x)
251 (member (pango-font-name x) other-font-names))
254 (define (font-load-command font)
255 (let* ((font-name (ly:font-name font))
256 (designsize (ly:font-design-size font))
257 (magnification (* (ly:font-magnification font)))
258 (ops (ly:output-def-lookup paper 'output-scale))
259 (scaling (* ops magnification designsize)))
260 (if (equal? font-name "unknown")
261 (display (list font font-name)))
262 (define-font font font-name scaling)))
264 (define (pango-font-load-command pango-font)
265 (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
266 (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
267 (font-name (pango-pf-font-name pango-pf))
268 (scaling (ly:output-def-lookup paper 'output-scale)))
269 (if (equal? font-name "unknown")
270 (display (list pango-font font-name)))
271 (define-pango-pf pango-pf font-name scaling)))
274 (apply string-append (map font-load-command other-fonts))
275 (apply string-append (map pango-font-load-command pango-only-fonts)))))