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 (search-executable names)
36 (define (helper path lst)
39 (if (search-path path (car lst)) (car lst)
40 (helper path (cdr lst)))))
42 (let ((path (parse-path (getenv "PATH"))))
45 (define-public (search-gs)
47 ;; must be sure that we don't catch stuff from old GUBs.
48 (search-executable '("gs")))
50 (define-public (postscript->pdf paper-width paper-height
51 base-name tmp-name is-eps)
52 (let* ((pdf-name (string-append base-name ".pdf"))
53 (*unspecified* (if #f #f))
55 (remove (lambda (x) (eq? x *unspecified*))
58 (if (ly:get-option 'verbose) *unspecified* "-q")
59 (if (or (ly:get-option 'gs-load-fonts)
60 (ly:get-option 'gs-load-lily-fonts)
61 (eq? PLATFORM 'windows))
67 (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
70 (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
71 "-dCompatibilityLevel=1.4"
75 (if (ly:bigpdfs) "-dSubsetFonts=false")
77 (string-append "-sOutputFile="
79 (string-split pdf-name #\%)
82 (string-append "-f" tmp-name)))))
84 (ly:message (_ "Converting to `~a'...\n") pdf-name)
87 (define-public (postscript->png resolution paper-width paper-height
88 base-name tmp-name is-eps)
89 (let* ((verbose (ly:get-option 'verbose))
92 ;; Do not try to guess the name of the png file,
93 ;; GS produces PNG files like BASE-page%d.png.
94 (ly:message (_ "Converting to ~a...") "PNG")
95 (make-ps-images base-name tmp-name is-eps
96 #:resolution resolution
97 #:page-width paper-width
98 #:page-height paper-height
99 #:rename-page-1 rename-page-1
101 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
102 #:pixmap-format (ly:get-option 'pixmap-format))
105 (define-public (postscript->ps base-name tmp-name is-eps)
106 (let* ((ps-name (string-append base-name
107 (if is-eps ".eps" ".ps"))))
108 (if (not (equal? ps-name tmp-name))
110 (ly:message (_ "Copying to `~a'...\n") ps-name)
111 (copy-binary-file tmp-name ps-name)))))
113 (define-public (copy-binary-file from-name to-name)
114 (if (eq? PLATFORM 'windows)
115 ;; MINGW hack: MinGW Guile's copy-file is broken.
116 ;; It opens files by the text mode instead of the binary mode.
117 ;; (It is fixed from Guile 2.0.9.)
118 ;; By the text mode, copied binary files are broken.
119 ;; So, we open files by the binary mode and copy by ourselves.
120 (let ((port-from (open-file from-name "rb"))
121 (port-to (open-file to-name "wb")))
122 (let loop((c (read-char port-from)))
124 (begin (close port-from)
126 (begin (write-char c port-to)
127 (loop (read-char port-from))))))
128 ;; Cygwin and other platforms:
129 ;; Pass through to copy-file
130 (copy-file from-name to-name)))
132 (define-public (make-tmpfile)
135 ;; MINGW hack: TMP / TEMP may include
136 ;; unusable characters (Unicode etc.).
137 ((eq? PLATFORM 'windows) "./tmp-")
138 ;; Cygwin can handle any characters
139 ;; including Unicode.
140 ((eq? PLATFORM 'cygwin) (string-append
144 ;; Other platforms (POSIX platforms)
145 ;; use TMPDIR or /tmp.
147 (or (getenv "TMPDIR")
151 (port-tmp (mkstemp! tmpl)))
152 (if (eq? PLATFORM 'windows)
153 ;; MINGW hack: MinGW Guile's mkstemp! is broken.
154 ;; It creates a file by the text mode instead of the binary mode.
155 ;; (It is fixed from Guile 2.0.9.)
156 ;; We need the binary mode for embeddings CFFs.
157 ;; So, we re-open the same file by the binary mode.
158 (let* ((filename (port-filename port-tmp))
159 (port (open-file filename "r+b")))
162 ;; Cygwin and other platforms:
163 ;; Pass through the return value of mkstemp!
166 (define-public (postprocess-output paper-book module formats
167 base-name tmp-name is-eps)
168 (let* ((completed (completize-formats formats is-eps)))
169 (for-each (lambda (f)
170 ((eval (string->symbol (format #f "convert-to-~a" f)) module)
171 paper-book base-name tmp-name is-eps)) completed)
172 (if (and (ly:get-option 'delete-intermediate-files)
174 (not (member "ps" completed)))
175 (file-exists? tmp-name))
176 (begin (ly:message (_ "Deleting `~a'...\n") tmp-name)
177 (delete-file tmp-name)))))
179 (define-public (completize-formats formats is-eps)
180 (define new-fmts '())
181 (if (and is-eps (member "eps" formats))
182 (set! formats (cons "ps" formats)))
183 (if (not (or (member "pdf" formats)
184 (member "png" formats)))
185 (set! formats (cons "ps" formats)))
186 (for-each (lambda (x)
187 (if (member x formats) (set! new-fmts (cons x new-fmts))))
189 (uniq-list (reverse new-fmts)))
191 (define (header-to-file file-name key value)
192 (set! key (symbol->string key))
193 (if (not (equal? "-" file-name))
194 (set! file-name (string-append file-name "." key)))
195 (ly:message (_ "Writing header field `~a' to `~a'...")
197 (if (equal? "-" file-name) "<stdout>" file-name))
198 (if (equal? file-name "-")
200 (let ((port (open-file file-name "w")))
207 (define-public (output-scopes scopes fields basename)
208 (define (output-scope scope)
212 (let ((val (if (variable-bound? var) (variable-ref var) "")))
213 (if (and (memq sym fields) (string? val))
214 (header-to-file basename sym val))
217 (string-concatenate (map output-scope scopes)))
219 (define-public (relevant-book-systems book)
220 (let ((systems (ly:paper-book-systems book)))
222 (if (and (not (ly:get-option 'include-book-title-preview))
224 (ly:prob-property (car systems) 'is-book-title #f))
228 (define-public (relevant-dump-systems systems)
229 (let ((to-dump-systems '()))
232 (if (or (paper-system-title? sys)
233 (not (pair? to-dump-systems))
234 (paper-system-title? (car to-dump-systems)))
235 (set! to-dump-systems (cons sys to-dump-systems))))
239 (define missing-stencil-list '())
241 (define-public (backend-testing output-module)
242 (define (missing-stencil-expression name)
244 (ly:warning (_ "missing stencil expression `~S'") name)
247 (for-each (lambda (x)
248 (if (not (module-defined? output-module x))
250 (module-define! output-module x
251 (lambda* (#:optional y . z)
252 (missing-stencil-expression x)))
253 (set! missing-stencil-list (cons x missing-stencil-list)))))
254 (ly:all-stencil-commands)))
256 (define-public (remove-stencil-warnings output-module)
259 (module-remove! output-module x))
260 missing-stencil-list))
262 (define (filter-out pred? lst)
263 (filter (lambda (x) (not (pred? x))) lst))
265 (define-public (font-name-split font-name)
266 "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
268 (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
269 (if (regexp-match? match)
270 (cons (match:substring match 1) (match:substring match 2))
271 (cons font-name-designsize #f))))
273 ;; Example of a pango-physical-font
274 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
275 (define-public (pango-pf-font-name pango-pf)
276 "Return the font-name of the pango physical font @var{pango-pf}."
277 (list-ref pango-pf 0))
278 (define-public (pango-pf-file-name pango-pf)
279 "Return the file-name of the pango physical font @var{pango-pf}."
280 (list-ref pango-pf 1))
281 (define-public (pango-pf-fontindex pango-pf)
282 "Return the fontindex of the pango physical font @var{pango-pf}."
283 (list-ref pango-pf 2))
285 (define (pango-font-name pango-font)
286 (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
288 (pango-pf-font-name (car pf-fonts))
291 (define-public (define-fonts paper define-font define-pango-pf)
292 "Return a string of all fonts used in @var{paper}, invoking the functions
293 @var{define-font} and @var{define-pango-pf} for producing the actual font
296 (let* ((font-list (ly:paper-fonts paper))
297 (pango-fonts (filter ly:pango-font? font-list))
298 (other-fonts (filter-out ly:pango-font? font-list))
299 (other-font-names (map ly:font-name other-fonts))
301 (filter-out (lambda (x)
302 (member (pango-font-name x) other-font-names))
305 (define (font-load-command font)
306 (let* ((font-name (ly:font-name font))
307 (designsize (ly:font-design-size font))
308 (magnification (* (ly:font-magnification font)))
309 (ops (ly:output-def-lookup paper 'output-scale))
310 (scaling (* ops magnification designsize)))
311 (if (equal? font-name "unknown")
312 (display (list font font-name)))
313 (define-font font font-name scaling)))
315 (define (pango-font-load-command pango-font)
316 (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
317 (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
318 (font-name (pango-pf-font-name pango-pf))
319 (scaling (ly:output-def-lookup paper 'output-scale)))
320 (if (equal? font-name "unknown")
321 (display (list pango-font font-name)))
322 (define-pango-pf pango-pf font-name scaling)))
325 (string-concatenate (map font-load-command other-fonts))
326 (string-concatenate (map pango-font-load-command pango-only-fonts)))))