1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2005--2015 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:warning (_ "`~a' failed (~a)\n") command status)
32 ;; hmmm. what's the best failure option?
33 (throw 'ly-file-failed)))))
35 ;; ly:system can't handle pipe and redirection.
36 ;; This procedure can handle them by using shell.
37 (define-public (ly:system-with-shell command)
38 (let ((s (if (eq? PLATFORM 'windows)
39 ;; MinGW (except Cygwin): Use COMSPEC (cmd.exe)
40 ;; FIXME: Command window is displayed briefly
41 (list (or (getenv "COMSPEC")
44 ;; POSIX (also Cygwin): Use /bin/sh
47 (c (list (if (eq? PLATFORM 'windows)
48 ;; MinGW hack: Double quotes can not be used here.
50 ;; FIXME: The filename that contains space
52 (string-join (string-split command #\") "")
53 ;; Other environments (also Cygwin):
54 ;; Double quotes can be used. Pass through.
57 (ly:system (append s c))))
59 (define-public (search-executable names)
60 (define (helper path lst)
63 (if (search-path path (car lst)) (car lst)
64 (helper path (cdr lst)))))
66 (let ((path (parse-path (getenv "PATH"))))
69 (define-public (search-gs)
71 ;; must be sure that we don't catch stuff from old GUBs.
72 (search-executable '("gs")))
74 (define-public (postscript->pdf paper-width paper-height
75 base-name tmp-name is-eps)
76 (let* ((pdf-name (string-append base-name ".pdf"))
77 (*unspecified* (if #f #f))
79 (remove (lambda (x) (eq? x *unspecified*))
82 (if (ly:get-option 'verbose) *unspecified* "-q")
83 (if (or (ly:get-option 'gs-load-fonts)
84 (ly:get-option 'gs-load-lily-fonts)
85 (eq? PLATFORM 'windows))
91 (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
94 (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
95 "-dCompatibilityLevel=1.4"
99 (if (ly:bigpdfs) "-dSubsetFonts=false")
101 "-dAutoRotatePages=/None"
102 (string-append "-sOutputFile="
104 (string-split pdf-name #\%)
107 (string-append "-f" tmp-name)))))
109 (ly:message (_ "Converting to `~a'...\n") pdf-name)
112 (define-public (postscript->png resolution paper-width paper-height
113 base-name tmp-name is-eps)
114 (let* ((verbose (ly:get-option 'verbose))
117 ;; Do not try to guess the name of the png file,
118 ;; GS produces PNG files like BASE-page%d.png.
119 (ly:message (_ "Converting to ~a...") "PNG")
120 (make-ps-images base-name tmp-name is-eps
121 #:resolution resolution
122 #:page-width paper-width
123 #:page-height paper-height
124 #:rename-page-1 rename-page-1
126 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
127 #:pixmap-format (ly:get-option 'pixmap-format))
130 (define-public (postscript->ps base-name tmp-name is-eps)
131 (let* ((ps-name (string-append base-name
132 (if is-eps ".eps" ".ps"))))
133 (if (not (equal? ps-name tmp-name))
135 (ly:message (_ "Copying to `~a'...\n") ps-name)
136 (copy-binary-file tmp-name ps-name)))))
138 (define-public (copy-binary-file from-name to-name)
139 (if (eq? PLATFORM 'windows)
140 ;; MINGW hack: MinGW Guile's copy-file is broken.
141 ;; It opens files by the text mode instead of the binary mode.
142 ;; (It is fixed from Guile 2.0.9.)
143 ;; By the text mode, copied binary files are broken.
144 ;; So, we open files by the binary mode and copy by ourselves.
145 (let ((port-from (open-file from-name "rb"))
146 (port-to (open-file to-name "wb")))
147 (let loop((c (read-char port-from)))
149 (begin (close port-from)
151 (begin (write-char c port-to)
152 (loop (read-char port-from))))))
153 ;; Cygwin and other platforms:
154 ;; Pass through to copy-file
155 (copy-file from-name to-name)))
157 (define-public (make-tmpfile)
160 ;; MINGW hack: TMP / TEMP may include
161 ;; unusable characters (Unicode etc.).
162 ((eq? PLATFORM 'windows) "./tmp-")
163 ;; Cygwin can handle any characters
164 ;; including Unicode.
165 ((eq? PLATFORM 'cygwin) (string-append
169 ;; Other platforms (POSIX platforms)
170 ;; use TMPDIR or /tmp.
172 (or (getenv "TMPDIR")
176 (port-tmp (mkstemp! tmpl)))
177 (if (eq? PLATFORM 'windows)
178 ;; MINGW hack: MinGW Guile's mkstemp! is broken.
179 ;; It creates a file by the text mode instead of the binary mode.
180 ;; (It is fixed from Guile 2.0.9.)
181 ;; We need the binary mode for embeddings CFFs.
182 ;; So, we re-open the same file by the binary mode.
183 (let* ((filename (port-filename port-tmp))
184 (port (open-file filename "r+b")))
187 ;; Cygwin and other platforms:
188 ;; Pass through the return value of mkstemp!
191 (define-public (postprocess-output paper-book module formats
192 base-name tmp-name is-eps)
193 (let* ((completed (completize-formats formats is-eps)))
194 (for-each (lambda (f)
195 ((eval (string->symbol (format #f "convert-to-~a" f)) module)
196 paper-book base-name tmp-name is-eps)) completed)
197 (if (and (ly:get-option 'delete-intermediate-files)
199 (not (member "ps" completed)))
200 (file-exists? tmp-name))
201 (begin (ly:message (_ "Deleting `~a'...\n") tmp-name)
202 (delete-file tmp-name)))))
204 (define-public (completize-formats formats is-eps)
205 (define new-fmts '())
206 (if (and is-eps (member "eps" formats))
207 (set! formats (cons "ps" formats)))
208 (if (not (or (member "pdf" formats)
209 (member "png" formats)))
210 (set! formats (cons "ps" formats)))
211 (for-each (lambda (x)
212 (if (member x formats) (set! new-fmts (cons x new-fmts))))
214 (uniq-list (reverse new-fmts)))
216 (define (header-to-file file-name key value)
217 (set! key (symbol->string key))
218 (if (not (equal? "-" file-name))
219 (set! file-name (string-append file-name "." key)))
220 (ly:message (_ "Writing header field `~a' to `~a'...")
222 (if (equal? "-" file-name) "<stdout>" file-name))
223 (if (equal? file-name "-")
225 (let ((port (open-file file-name "w")))
232 (define-public (output-scopes scopes fields basename)
233 (define (output-scope scope)
237 (let ((val (if (variable-bound? var) (variable-ref var) "")))
238 (if (and (memq sym fields) (string? val))
239 (header-to-file basename sym val))
242 (string-concatenate (map output-scope scopes)))
244 (define-public (relevant-book-systems book)
245 (let ((systems (ly:paper-book-systems book)))
247 (if (and (not (ly:get-option 'include-book-title-preview))
249 (ly:prob-property (car systems) 'is-book-title #f))
253 (define-public (relevant-dump-systems systems)
254 (let ((to-dump-systems '()))
257 (if (or (paper-system-title? sys)
258 (not (pair? to-dump-systems))
259 (paper-system-title? (car to-dump-systems)))
260 (set! to-dump-systems (cons sys to-dump-systems))))
264 (define missing-stencil-list '())
266 (define-public (backend-testing output-module)
267 (define (missing-stencil-expression name)
269 (ly:warning (_ "missing stencil expression `~S'") name)
272 (for-each (lambda (x)
273 (if (not (module-defined? output-module x))
275 (module-define! output-module x
276 (lambda* (#:optional y . z)
277 (missing-stencil-expression x)))
278 (set! missing-stencil-list (cons x missing-stencil-list)))))
279 (ly:all-stencil-commands)))
281 (define-public (remove-stencil-warnings output-module)
284 (module-remove! output-module x))
285 missing-stencil-list))
287 (define-public (font-name-split font-name)
288 "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
290 (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
291 (if (regexp-match? match)
292 (cons (match:substring match 1) (match:substring match 2))
293 (cons font-name-designsize #f))))
295 ;; Example of a pango-physical-font
296 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
297 (define-public (pango-pf-font-name pango-pf)
298 "Return the font-name of the pango physical font @var{pango-pf}."
299 (list-ref pango-pf 0))
300 (define-public (pango-pf-file-name pango-pf)
301 "Return the file-name of the pango physical font @var{pango-pf}."
302 (list-ref pango-pf 1))
303 (define-public (pango-pf-fontindex pango-pf)
304 "Return the fontindex of the pango physical font @var{pango-pf}."
305 (list-ref pango-pf 2))
307 (define (pango-font-name pango-font)
308 (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
310 (pango-pf-font-name (car pf-fonts))
313 (define-public (define-fonts paper define-font define-pango-pf)
314 "Return a string of all fonts used in @var{paper}, invoking the functions
315 @var{define-font} and @var{define-pango-pf} for producing the actual font
318 (let* ((font-list (ly:paper-fonts paper))
319 (pango-fonts (filter ly:pango-font? font-list))
320 (other-fonts (remove ly:pango-font? font-list))
321 (other-font-names (map ly:font-name other-fonts))
324 (member (pango-font-name x) other-font-names))
327 (define (font-load-command font)
328 (let* ((font-name (ly:font-name font))
329 (designsize (ly:font-design-size font))
330 (magnification (* (ly:font-magnification font)))
331 (ops (ly:output-def-lookup paper 'output-scale))
332 (scaling (* ops magnification designsize)))
333 (if (equal? font-name "unknown")
334 (display (list font font-name)))
335 (define-font font font-name scaling)))
337 (define (pango-font-load-command pango-font)
338 (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
339 (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
340 (font-name (pango-pf-font-name pango-pf))
341 (scaling (ly:output-def-lookup paper 'output-scale)))
342 (if (equal? font-name "unknown")
343 (display (list pango-font font-name)))
344 (define-pango-pf pango-pf font-name scaling)))
347 (string-concatenate (map font-load-command other-fonts))
348 (string-concatenate (map pango-font-load-command pango-only-fonts)))))