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 (define-public (sanitize-command-option str)
36 "Kill dubious shell quoting."
40 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
43 (define-public (search-executable names)
44 (define (helper path lst)
47 (if (search-path path (car lst)) (car lst)
48 (helper path (cdr lst)))))
50 (let ((path (parse-path (getenv "PATH"))))
53 (define-public (search-gs)
55 ;; must be sure that we don't catch stuff from old GUBs.
56 (search-executable '("gs")))
58 (define-public (postscript->pdf paper-width paper-height name)
59 (let* ((pdf-name (string-append
60 (dir-basename name ".ps" ".eps")
62 (is-eps (string-match "\\.eps$" name))
63 (*unspecified* (if #f #f))
65 (remove (lambda (x) (eq? x *unspecified*))
68 (if (ly:get-option 'verbose) *unspecified* "-q")
69 (if (or (ly:get-option 'gs-load-fonts)
70 (ly:get-option 'gs-load-lily-fonts)
71 (eq? PLATFORM 'windows))
77 (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
80 (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
81 "-dCompatibilityLevel=1.4"
85 (if (ly:bigpdfs) "-dSubsetFonts=false")
87 (string-append "-sOutputFile="
89 (string-split pdf-name #\%)
92 (string-append "-f" name)))))
94 (ly:message (_ "Converting to `~a'...\n") pdf-name)
97 (define-public (postscript->png resolution paper-width paper-height name)
98 (let* ((verbose (ly:get-option 'verbose))
101 ;; Do not try to guess the name of the png file,
102 ;; GS produces PNG files like BASE-page%d.png.
103 (ly:message (_ "Converting to ~a...") "PNG")
105 #:resolution resolution
106 #:page-width paper-width
107 #:page-height paper-height
108 #:rename-page-1 rename-page-1
110 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
111 #:pixmap-format (ly:get-option 'pixmap-format))
114 (define-public (copy-binary-file from-name to-name)
115 (if (eq? PLATFORM 'windows)
116 ;; MINGW hack: MinGW Guile's copy-file is broken.
117 ;; It opens files by the text mode instead of the binary mode.
118 ;; (It is fixed from Guile 2.0.9.)
119 ;; By the text mode, copied binary files are broken.
120 ;; So, we open files by the binary mode and copy by ourselves.
121 (let ((port-from (open-file from-name "rb"))
122 (port-to (open-file to-name "wb")))
123 (let loop((c (read-char port-from)))
125 (begin (close port-from)
127 (begin (write-char c port-to)
128 (loop (read-char port-from))))))
129 ;; Cygwin and other platforms:
130 ;; Pass through to copy-file
131 (copy-file from-name to-name)))
133 (define-public (make-tmpfile)
136 ;; MINGW hack: TMP / TEMP may include
137 ;; unusable characters (Unicode etc.).
138 ((eq? PLATFORM 'windows) "./tmp-")
139 ;; Cygwin can handle any characters
140 ;; including Unicode.
141 ((eq? PLATFORM 'cygwin) (string-append
145 ;; Other platforms (POSIX platforms)
146 ;; use TMPDIR or /tmp.
148 (or (getenv "TMPDIR")
152 (port-tmp (mkstemp! tmpl)))
153 (if (eq? PLATFORM 'windows)
154 ;; MINGW hack: MinGW Guile's mkstemp! is broken.
155 ;; It creates a file by the text mode instead of the binary mode.
156 ;; (It is fixed from Guile 2.0.9.)
157 ;; We need the binary mode for embeddings CFFs.
158 ;; So, we re-open the same file by the binary mode.
159 (let* ((filename (port-filename port-tmp))
160 (port (open-file filename "r+b")))
163 ;; Cygwin and other platforms:
164 ;; Pass through the return value of mkstemp!
167 (define-public (postprocess-output paper-book module filename formats)
168 (let* ((completed (completize-formats formats))
169 (base (dir-basename filename ".ps" ".eps"))
170 (intermediate (remove (lambda (x) (member x formats)) completed)))
171 (for-each (lambda (f)
172 ((eval (string->symbol (format #f "convert-to-~a" f))
173 module) paper-book filename)) completed)
174 (if (ly:get-option 'delete-intermediate-files)
175 (for-each (lambda (f)
176 (if (file-exists? f) (delete-file f)))
177 (map (lambda (x) (string-append base "." x)) intermediate)))))
179 (define-public (completize-formats formats)
180 (define new-fmts '())
181 (if (member "png" formats)
182 (set! formats (cons "ps" formats)))
183 (if (member "pdf" formats)
184 (set! formats (cons "ps" formats)))
185 (for-each (lambda (x)
186 (if (member x formats) (set! new-fmts (cons x new-fmts))))
188 (uniq-list (reverse new-fmts)))
190 (define (header-to-file file-name key value)
191 (set! key (symbol->string key))
192 (if (not (equal? "-" file-name))
193 (set! file-name (string-append file-name "." key)))
194 (ly:message (_ "Writing header field `~a' to `~a'...")
196 (if (equal? "-" file-name) "<stdout>" file-name))
197 (if (equal? file-name "-")
199 (let ((port (open-file file-name "w")))
206 (define-public (output-scopes scopes fields basename)
207 (define (output-scope scope)
211 (let ((val (if (variable-bound? var) (variable-ref var) "")))
212 (if (and (memq sym fields) (string? val))
213 (header-to-file basename sym val))
216 (string-concatenate (map output-scope scopes)))
218 (define-public (relevant-book-systems book)
219 (let ((systems (ly:paper-book-systems book)))
221 (if (and (not (ly:get-option 'include-book-title-preview))
223 (ly:prob-property (car systems) 'is-book-title #f))
227 (define-public (relevant-dump-systems systems)
228 (let ((to-dump-systems '()))
231 (if (or (paper-system-title? sys)
232 (not (pair? to-dump-systems))
233 (paper-system-title? (car to-dump-systems)))
234 (set! to-dump-systems (cons sys to-dump-systems))))
238 (define missing-stencil-list '())
240 (define-public (backend-testing output-module)
241 (define (missing-stencil-expression name)
243 (ly:warning (_ "missing stencil expression `~S'") name)
246 (for-each (lambda (x)
247 (if (not (module-defined? output-module x))
249 (module-define! output-module x
250 (lambda* (#:optional y . z)
251 (missing-stencil-expression x)))
252 (set! missing-stencil-list (cons x missing-stencil-list)))))
253 (ly:all-stencil-commands)))
255 (define-public (remove-stencil-warnings output-module)
258 (module-remove! output-module x))
259 missing-stencil-list))
261 (define (filter-out pred? lst)
262 (filter (lambda (x) (not (pred? x))) lst))
264 (define-public (font-name-split font-name)
265 "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
267 (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
268 (if (regexp-match? match)
269 (cons (match:substring match 1) (match:substring match 2))
270 (cons font-name-designsize #f))))
272 ;; Example of a pango-physical-font
273 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
274 (define-public (pango-pf-font-name pango-pf)
275 "Return the font-name of the pango physical font @var{pango-pf}."
276 (list-ref pango-pf 0))
277 (define-public (pango-pf-file-name pango-pf)
278 "Return the file-name of the pango physical font @var{pango-pf}."
279 (list-ref pango-pf 1))
280 (define-public (pango-pf-fontindex pango-pf)
281 "Return the fontindex of the pango physical font @var{pango-pf}."
282 (list-ref pango-pf 2))
284 (define (pango-font-name pango-font)
285 (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
287 (pango-pf-font-name (car pf-fonts))
290 (define-public (define-fonts paper define-font define-pango-pf)
291 "Return a string of all fonts used in @var{paper}, invoking the functions
292 @var{define-font} and @var{define-pango-pf} for producing the actual font
295 (let* ((font-list (ly:paper-fonts paper))
296 (pango-fonts (filter ly:pango-font? font-list))
297 (other-fonts (filter-out ly:pango-font? font-list))
298 (other-font-names (map ly:font-name other-fonts))
300 (filter-out (lambda (x)
301 (member (pango-font-name x) other-font-names))
304 (define (font-load-command font)
305 (let* ((font-name (ly:font-name font))
306 (designsize (ly:font-design-size font))
307 (magnification (* (ly:font-magnification font)))
308 (ops (ly:output-def-lookup paper 'output-scale))
309 (scaling (* ops magnification designsize)))
310 (if (equal? font-name "unknown")
311 (display (list font font-name)))
312 (define-font font font-name scaling)))
314 (define (pango-font-load-command pango-font)
315 (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
316 (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
317 (font-name (pango-pf-font-name pango-pf))
318 (scaling (ly:output-def-lookup paper 'output-scale)))
319 (if (equal? font-name "unknown")
320 (display (list pango-font font-name)))
321 (define-pango-pf pango-pf font-name scaling)))
324 (string-concatenate (map font-load-command other-fonts))
325 (string-concatenate (map pango-font-load-command pango-only-fonts)))))