]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / backend-library.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2015 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;;
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.
10 ;;;;
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.
15 ;;;;
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/>.
18
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20 ;; backend helpers.
21
22 (use-modules (scm ps-to-png)
23              (scm paper-system)
24              (ice-9 optargs))
25
26 (define-public (ly:system command)
27   (ly:debug (_ "Invoking `~a'...") (string-join command))
28   (let ((status (apply ly:spawn command)))
29     (if (> status 0)
30         (begin
31           (ly:warning (_ "`~a' failed (~a)\n") command status)
32           ;; hmmm.  what's the best failure option?
33           (throw 'ly-file-failed)))))
34
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")
42                          "cmd.exe")
43                      "/c")
44                ;; POSIX (also Cygwin): Use /bin/sh
45                (list "/bin/sh"
46                      "-c")))
47         (c (list (if (eq? PLATFORM 'windows)
48                      ;; MinGW hack: Double quotes can not be used here.
49                      ;; So we remove them.
50                      ;; FIXME: The filename that contains space
51                      ;; can't be handled.
52                      (string-join (string-split command #\") "")
53                      ;; Other environments (also Cygwin):
54                      ;; Double quotes can be used. Pass through.
55                      command
56                      ))))
57     (ly:system (append s c))))
58
59 (define-public (search-executable names)
60   (define (helper path lst)
61     (if (null? (cdr lst))
62         (car lst)
63         (if (search-path path (car lst)) (car lst)
64             (helper path (cdr lst)))))
65
66   (let ((path (parse-path (getenv "PATH"))))
67     (helper path names)))
68
69 (define-public (search-gs)
70
71   ;; must be sure that we don't catch stuff from old GUBs.
72   (search-executable '("gs")))
73
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))
78          (cmd
79           (remove (lambda (x) (eq? x *unspecified*))
80                   (list
81                    (search-gs)
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))
86                        "-dNOSAFER"
87                        "-dSAFER")
88
89                    (if is-eps
90                        "-dEPSCrop"
91                        (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
92                    (if is-eps
93                        *unspecified*
94                        (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
95                    "-dCompatibilityLevel=1.4"
96                    "-dNOPAUSE"
97                    "-dBATCH"
98                    "-r1200"
99                    (if (ly:bigpdfs) "-dSubsetFonts=false")
100                    "-sDEVICE=pdfwrite"
101                    "-dAutoRotatePages=/None"
102                    (string-append "-sOutputFile="
103                                   (string-join
104                                    (string-split pdf-name #\%)
105                                    "%%"))
106                    "-c.setpdfwrite"
107                    (string-append "-f" tmp-name)))))
108
109     (ly:message (_ "Converting to `~a'...\n") pdf-name)
110     (ly:system cmd)))
111
112 (define-public (postscript->png resolution paper-width paper-height
113                                 base-name tmp-name is-eps)
114   (let* ((verbose (ly:get-option 'verbose))
115          (rename-page-1 #f))
116
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
125                     #:be-verbose verbose
126                     #:anti-alias-factor (ly:get-option 'anti-alias-factor)
127                     #:pixmap-format (ly:get-option 'pixmap-format))
128     (ly:progress "\n")))
129
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))
134         (begin
135           (ly:message (_ "Copying to `~a'...\n") ps-name)
136           (copy-binary-file tmp-name ps-name)))))
137
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)))
148           (if (eof-object? c)
149               (begin (close port-from)
150                      (close port-to))
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)))
156
157 (define-public (make-tmpfile)
158   (let* ((tmpl
159           (string-append (cond
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
166                                                    (or (getenv "TMP")
167                                                        (getenv "TEMP"))
168                                                    "/"))
169                           ;; Other platforms (POSIX platforms)
170                           ;; use TMPDIR or /tmp.
171                           (else (string-append
172                                  (or (getenv "TMPDIR")
173                                      "/tmp")
174                                  "/")))
175                           "lilypond-XXXXXX"))
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")))
185           (close port-tmp)
186           port)
187         ;; Cygwin and other platforms:
188         ;; Pass through the return value of mkstemp!
189         port-tmp)))
190
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)
198              (or (not is-eps)
199                  (not (member "ps" completed)))
200              (file-exists? tmp-name))
201         (begin (ly:message (_ "Deleting `~a'...\n") tmp-name)
202                (delete-file tmp-name)))))
203
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))))
213             '("ps" "pdf" "png"))
214   (uniq-list (reverse new-fmts)))
215
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'...")
221               key
222               (if (equal? "-" file-name) "<stdout>" file-name))
223   (if (equal? file-name "-")
224       (display value)
225       (let ((port (open-file file-name "w")))
226         (display value port)
227         (close-port port)))
228
229   (ly:progress "\n")
230   "")
231
232 (define-public (output-scopes scopes fields basename)
233   (define (output-scope scope)
234     (string-concatenate
235      (module-map
236       (lambda (sym var)
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))
240           ""))
241       scope)))
242   (string-concatenate (map output-scope scopes)))
243
244 (define-public (relevant-book-systems book)
245   (let ((systems (ly:paper-book-systems book)))
246     ;; skip booktitles.
247     (if (and (not (ly:get-option 'include-book-title-preview))
248              (pair? systems)
249              (ly:prob-property (car systems) 'is-book-title #f))
250         (cdr systems)
251         systems)))
252
253 (define-public (relevant-dump-systems systems)
254   (let ((to-dump-systems '()))
255     (for-each
256      (lambda (sys)
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))))
261      systems)
262     to-dump-systems))
263
264 (define missing-stencil-list '())
265
266 (define-public (backend-testing output-module)
267   (define (missing-stencil-expression name)
268     (begin
269       (ly:warning (_ "missing stencil expression `~S'") name)
270       ""))
271
272   (for-each (lambda (x)
273               (if (not (module-defined? output-module x))
274                   (begin
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)))
280
281 (define-public (remove-stencil-warnings output-module)
282   (for-each
283    (lambda (x)
284      (module-remove! output-module x))
285    missing-stencil-list))
286
287 (define-public (font-name-split font-name)
288   "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
289 or @code{#f}."
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))))
294
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))
306
307 (define (pango-font-name pango-font)
308   (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
309     (if (pair? pf-fonts)
310         (pango-pf-font-name (car pf-fonts))
311         "")))
312
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
316 definition."
317
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))
322          (pango-only-fonts
323           (remove (lambda (x)
324                     (member (pango-font-name x) other-font-names))
325                   pango-fonts)))
326
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)))
336
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)))
345
346     (string-append
347      (string-concatenate (map font-load-command other-fonts))
348      (string-concatenate (map pango-font-load-command pango-only-fonts)))))