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 (ly:debug (_ "Invoking `~a'...") (string-join command))
28 (let ((status (apply ly:spawn command)))
31 (ly:message (_ "`~a' failed (~a)") command status)
33 ;; hmmm. what's the best failure option?
34 (throw 'ly-file-failed)))))
36 (define-public (sanitize-command-option str)
37 "Kill dubious shell quoting."
41 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
44 (define-public (search-executable names)
45 (define (helper path lst)
48 (if (search-path path (car lst)) (car lst)
49 (helper path (cdr lst)))))
51 (let ((path (parse-path (getenv "PATH"))))
54 (define-public (search-gs)
56 ;; must be sure that we don't catch stuff from old GUBs.
57 (search-executable '("gs")))
59 (define-public (postscript->pdf paper-width paper-height name)
60 (let* ((pdf-name (string-append
61 (dir-basename name ".ps" ".eps")
63 (is-eps (string-match "\\.eps$" name))
64 (*unspecified* (if #f #f))
66 (remove (lambda (x) (eq? x *unspecified*))
69 (if (ly:get-option 'verbose) *unspecified* "-q")
70 (if (or (ly:get-option 'gs-load-fonts)
71 (ly:get-option 'gs-load-lily-fonts)
72 (eq? PLATFORM 'windows))
78 (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
81 (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
82 "-dCompatibilityLevel=1.4"
87 (string-append "-sOutputFile=" pdf-name)
89 (string-append "-f" name)))))
91 (ly:message (_ "Converting to `~a'...") pdf-name)
95 (define-public (postscript->png resolution paper-width paper-height name)
96 (let* ((verbose (ly:get-option 'verbose))
99 ;; Do not try to guess the name of the png file,
100 ;; GS produces PNG files like BASE-page%d.png.
101 (ly:message (_ "Converting to ~a...") "PNG")
103 #:resolution resolution
104 #:page-width paper-width
105 #:page-height paper-height
106 #:rename-page-1 rename-page-1
108 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
109 #:pixmap-format (ly:get-option 'pixmap-format))
112 (define-public (postprocess-output paper-book module filename formats)
113 (let* ((completed (completize-formats formats))
114 (base (dir-basename filename ".ps" ".eps"))
115 (intermediate (remove (lambda (x) (member x formats)) completed)))
116 (for-each (lambda (f)
117 ((eval (string->symbol (format #f "convert-to-~a" f))
118 module) paper-book filename)) completed)
119 (if (ly:get-option 'delete-intermediate-files)
120 (for-each (lambda (f)
121 (if (file-exists? f) (delete-file f)))
122 (map (lambda (x) (string-append base "." x)) intermediate)))))
124 (define-public (completize-formats formats)
125 (define new-fmts '())
126 (if (member "png" formats)
127 (set! formats (cons "ps" formats)))
128 (if (member "pdf" formats)
129 (set! formats (cons "ps" formats)))
130 (for-each (lambda (x)
131 (if (member x formats) (set! new-fmts (cons x new-fmts))))
133 (uniq-list (reverse new-fmts)))
135 (define (header-to-file file-name key value)
136 (set! key (symbol->string key))
137 (if (not (equal? "-" file-name))
138 (set! file-name (string-append file-name "." key)))
139 (ly:message (_ "Writing header field `~a' to `~a'...")
141 (if (equal? "-" file-name) "<stdout>" file-name))
142 (if (equal? file-name "-")
144 (let ((port (open-file file-name "w")))
151 (define-public (output-scopes scopes fields basename)
152 (define (output-scope scope)
157 (let ((val (if (variable-bound? var) (variable-ref var) "")))
158 (if (and (memq sym fields) (string? val))
159 (header-to-file basename sym val))
162 (apply string-append (map output-scope scopes)))
164 (define-public (relevant-book-systems book)
165 (let ((systems (ly:paper-book-systems book)))
167 (if (and (not (ly:get-option 'include-book-title-preview))
169 (ly:prob-property (car systems) 'is-book-title #f))
173 (define-public (relevant-dump-systems systems)
174 (let ((to-dump-systems '()))
177 (if (or (paper-system-title? sys)
178 (not (pair? to-dump-systems))
179 (paper-system-title? (car to-dump-systems)))
180 (set! to-dump-systems (cons sys to-dump-systems))))
184 (define missing-stencil-list '())
186 (define-public (backend-testing output-module)
187 (define (missing-stencil-expression name)
189 (ly:warning (_ "missing stencil expression `~S'") name)
193 (if (not (module-defined? output-module x))
195 (module-define! output-module x
196 (lambda* (#:optional y . z)
197 (missing-stencil-expression x)))
198 (set! missing-stencil-list (append (list x)
199 missing-stencil-list)))))
200 (ly:all-stencil-commands)))
202 (define-public (remove-stencil-warnings output-module)
205 (module-remove! output-module x))
206 missing-stencil-list))
208 (define (filter-out pred? lst)
209 (filter (lambda (x) (not (pred? x))) lst))
211 (define-public (font-name-split font-name)
212 "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
214 (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
215 (if (regexp-match? match)
216 (cons (match:substring match 1) (match:substring match 2))
217 (cons font-name-designsize #f))))
219 ;; Example of a pango-physical-font
220 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
221 (define-public (pango-pf-font-name pango-pf)
222 "Return the font-name of the pango physical font @var{pango-pf}."
223 (list-ref pango-pf 0))
224 (define-public (pango-pf-file-name pango-pf)
225 "Return the file-name of the pango physical font @var{pango-pf}."
226 (list-ref pango-pf 1))
227 (define-public (pango-pf-fontindex pango-pf)
228 "Return the fontindex of the pango physical font @var{pango-pf}."
229 (list-ref pango-pf 2))
231 (define (pango-font-name pango-font)
232 (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
234 (pango-pf-font-name (car pf-fonts))
237 (define-public (define-fonts paper define-font define-pango-pf)
238 "Return a string of all fonts used in @var{paper}, invoking the functions
239 @var{define-font} and @var{define-pango-pf} for producing the actual font
242 (let* ((font-list (ly:paper-fonts paper))
243 (pango-fonts (filter ly:pango-font? font-list))
244 (other-fonts (filter-out ly:pango-font? font-list))
245 (other-font-names (map ly:font-name other-fonts))
247 (filter-out (lambda (x)
248 (member (pango-font-name x) other-font-names))
251 (define (font-load-command font)
252 (let* ((font-name (ly:font-name font))
253 (designsize (ly:font-design-size font))
254 (magnification (* (ly:font-magnification font)))
255 (ops (ly:output-def-lookup paper 'output-scale))
256 (scaling (* ops magnification designsize)))
257 (if (equal? font-name "unknown")
258 (display (list font font-name)))
259 (define-font font font-name scaling)))
261 (define (pango-font-load-command pango-font)
262 (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
263 (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
264 (font-name (pango-pf-font-name pango-pf))
265 (scaling (ly:output-def-lookup paper 'output-scale)))
266 (if (equal? font-name "unknown")
267 (display (list pango-font font-name)))
268 (define-pango-pf pango-pf font-name scaling)))
271 (apply string-append (map font-load-command other-fonts))
272 (apply string-append (map pango-font-load-command pango-only-fonts)))))