]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
Issue 4431 / 1: Delete duplicate and unused procedures in ps-to-png.scm
[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 (define-public (search-executable names)
36   (define (helper path lst)
37     (if (null? (cdr lst))
38         (car lst)
39         (if (search-path path (car lst)) (car lst)
40             (helper path (cdr lst)))))
41
42   (let ((path (parse-path (getenv "PATH"))))
43     (helper path names)))
44
45 (define-public (search-gs)
46
47   ;; must be sure that we don't catch stuff from old GUBs.
48   (search-executable '("gs")))
49
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))
54          (cmd
55           (remove (lambda (x) (eq? x *unspecified*))
56                   (list
57                    (search-gs)
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))
62                        "-dNOSAFER"
63                        "-dSAFER")
64
65                    (if is-eps
66                        "-dEPSCrop"
67                        (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
68                    (if is-eps
69                        *unspecified*
70                        (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
71                    "-dCompatibilityLevel=1.4"
72                    "-dNOPAUSE"
73                    "-dBATCH"
74                    "-r1200"
75                    (if (ly:bigpdfs) "-dSubsetFonts=false")
76                    "-sDEVICE=pdfwrite"
77                    (string-append "-sOutputFile="
78                                   (string-join
79                                    (string-split pdf-name #\%)
80                                    "%%"))
81                    "-c.setpdfwrite"
82                    (string-append "-f" tmp-name)))))
83
84     (ly:message (_ "Converting to `~a'...\n") pdf-name)
85     (ly:system cmd)))
86
87 (define-public (postscript->png resolution paper-width paper-height
88                                 base-name tmp-name is-eps)
89   (let* ((verbose (ly:get-option 'verbose))
90          (rename-page-1 #f))
91
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
100                     #:be-verbose verbose
101                     #:anti-alias-factor (ly:get-option 'anti-alias-factor)
102                     #:pixmap-format (ly:get-option 'pixmap-format))
103     (ly:progress "\n")))
104
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))
109         (begin
110           (ly:message (_ "Copying to `~a'...\n") ps-name)
111           (copy-binary-file tmp-name ps-name)))))
112
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)))
123           (if (eof-object? c)
124               (begin (close port-from)
125                      (close port-to))
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)))
131
132 (define-public (make-tmpfile)
133   (let* ((tmpl
134           (string-append (cond
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
141                                                    (or (getenv "TMP")
142                                                        (getenv "TEMP"))
143                                                    "/"))
144                           ;; Other platforms (POSIX platforms)
145                           ;; use TMPDIR or /tmp.
146                           (else (string-append
147                                  (or (getenv "TMPDIR")
148                                      "/tmp")
149                                  "/")))
150                           "lilypond-XXXXXX"))
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")))
160           (close port-tmp)
161           port)
162         ;; Cygwin and other platforms:
163         ;; Pass through the return value of mkstemp!
164         port-tmp)))
165
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)
173              (or (not is-eps)
174                  (not (member "ps" completed)))
175              (file-exists? tmp-name))
176         (begin (ly:message (_ "Deleting `~a'...\n") tmp-name)
177                (delete-file tmp-name)))))
178
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))))
188             '("ps" "pdf" "png"))
189   (uniq-list (reverse new-fmts)))
190
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'...")
196               key
197               (if (equal? "-" file-name) "<stdout>" file-name))
198   (if (equal? file-name "-")
199       (display value)
200       (let ((port (open-file file-name "w")))
201         (display value port)
202         (close-port port)))
203
204   (ly:progress "\n")
205   "")
206
207 (define-public (output-scopes scopes fields basename)
208   (define (output-scope scope)
209     (string-concatenate
210      (module-map
211       (lambda (sym var)
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))
215           ""))
216       scope)))
217   (string-concatenate (map output-scope scopes)))
218
219 (define-public (relevant-book-systems book)
220   (let ((systems (ly:paper-book-systems book)))
221     ;; skip booktitles.
222     (if (and (not (ly:get-option 'include-book-title-preview))
223              (pair? systems)
224              (ly:prob-property (car systems) 'is-book-title #f))
225         (cdr systems)
226         systems)))
227
228 (define-public (relevant-dump-systems systems)
229   (let ((to-dump-systems '()))
230     (for-each
231      (lambda (sys)
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))))
236      systems)
237     to-dump-systems))
238
239 (define missing-stencil-list '())
240
241 (define-public (backend-testing output-module)
242   (define (missing-stencil-expression name)
243     (begin
244       (ly:warning (_ "missing stencil expression `~S'") name)
245       ""))
246
247   (for-each (lambda (x)
248               (if (not (module-defined? output-module x))
249                   (begin
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)))
255
256 (define-public (remove-stencil-warnings output-module)
257   (for-each
258    (lambda (x)
259      (module-remove! output-module x))
260    missing-stencil-list))
261
262 (define (filter-out pred? lst)
263   (filter (lambda (x) (not (pred? x))) lst))
264
265 (define-public (font-name-split font-name)
266   "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
267 or @code{#f}."
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))))
272
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))
284
285 (define (pango-font-name pango-font)
286   (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
287     (if (pair? pf-fonts)
288         (pango-pf-font-name (car pf-fonts))
289         "")))
290
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
294 definition."
295
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))
300          (pango-only-fonts
301           (filter-out (lambda (x)
302                         (member (pango-font-name x) other-font-names))
303                       pango-fonts)))
304
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)))
314
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)))
323
324     (string-append
325      (string-concatenate (map font-load-command other-fonts))
326      (string-concatenate (map pango-font-load-command pango-only-fonts)))))