]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
Revert "Apply scripts/auxiliar/fixscm.sh"
[lilypond.git] / scm / backend-library.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2012 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 (sanitize-command-option str)
36   "Kill dubious shell quoting."
37   
38   (string-append
39    "\""
40    (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
41    "\""))
42
43 (define-public (search-executable names)
44   (define (helper path lst)
45     (if (null? (cdr lst))
46         (car lst)
47         (if (search-path path (car lst)) (car lst)
48             (helper path (cdr lst)))))
49
50   (let ((path (parse-path (getenv "PATH"))))
51     (helper path names)))
52
53 (define-public (search-gs)
54   
55   ;; must be sure that we don't catch stuff from old GUBs.
56   (search-executable '("gs")))
57   
58 (define-public (postscript->pdf paper-width paper-height name)
59   (let* ((pdf-name (string-append
60                     (dir-basename name ".ps" ".eps")
61                     ".pdf"))
62          (is-eps (string-match "\\.eps$" name))
63          (*unspecified* (if #f #f))
64          (cmd
65           (remove (lambda (x) (eq? x *unspecified*))
66           (list
67                (search-gs)
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))
72                    "-dNOSAFER"
73                    "-dSAFER")
74
75                (if is-eps
76                    "-dEPSCrop"
77                    (ly:format "-dDEVICEWIDTHPOINTS=~$" paper-width))
78                (if is-eps
79                    *unspecified*
80                    (ly:format "-dDEVICEHEIGHTPOINTS=~$" paper-height))
81                "-dCompatibilityLevel=1.4"
82                "-dNOPAUSE"
83                "-dBATCH"
84                "-r1200"
85                "-sDEVICE=pdfwrite"
86                (string-append "-sOutputFile=" pdf-name)
87                "-c.setpdfwrite"
88                (string-append "-f" name)))))
89
90     (ly:message (_ "Converting to `~a'...\n") pdf-name)
91     (ly:system cmd)))
92
93 (define-public (postscript->png resolution paper-width paper-height name)
94   (let* ((verbose (ly:get-option 'verbose))
95          (rename-page-1 #f))
96
97     ;; Do not try to guess the name of the png file,
98     ;; GS produces PNG files like BASE-page%d.png.
99     (ly:message (_ "Converting to ~a...") "PNG")
100     (make-ps-images name
101                     #:resolution resolution
102                     #:page-width paper-width
103                     #:page-height paper-height
104                     #:rename-page-1 rename-page-1
105                     #:be-verbose verbose
106                     #:anti-alias-factor (ly:get-option 'anti-alias-factor)
107                     #:pixmap-format (ly:get-option 'pixmap-format))
108     (ly:progress "\n")))
109
110 (define-public (postprocess-output paper-book module filename formats)
111   (let* ((completed (completize-formats formats))
112          (base (dir-basename filename ".ps" ".eps"))
113          (intermediate (remove (lambda (x) (member x formats)) completed)))
114     (for-each (lambda (f)
115                 ((eval (string->symbol (format #f "convert-to-~a" f))
116                        module) paper-book filename)) completed)
117     (if (ly:get-option 'delete-intermediate-files)
118         (for-each (lambda (f)
119                     (if (file-exists? f) (delete-file f)))
120                   (map (lambda (x) (string-append base "." x)) intermediate)))))
121
122 (define-public (completize-formats formats)
123   (define new-fmts '())
124   (if (member "png" formats)
125       (set! formats (cons "ps" formats)))
126   (if (member "pdf" formats)
127       (set! formats (cons "ps" formats)))
128   (for-each (lambda (x)
129               (if (member x formats) (set! new-fmts (cons x new-fmts))))
130             '("ps" "pdf" "png"))
131   (uniq-list (reverse new-fmts)))
132
133 (define (header-to-file file-name key value)
134   (set! key (symbol->string key))
135   (if (not (equal? "-" file-name))
136       (set! file-name (string-append file-name "." key)))
137   (ly:message (_ "Writing header field `~a' to `~a'...")
138               key
139               (if (equal? "-" file-name) "<stdout>" file-name))
140   (if (equal? file-name "-")
141       (display value)
142       (let ((port (open-file file-name "w")))
143         (display value port)
144         (close-port port)))
145
146   (ly:progress "\n")
147   "")
148
149 (define-public (output-scopes scopes fields basename)
150   (define (output-scope scope)
151     (apply
152      string-append
153      (module-map
154       (lambda (sym var)
155         (let ((val (if (variable-bound? var) (variable-ref var) "")))
156           (if (and (memq sym fields) (string? val))
157               (header-to-file basename sym val))
158           ""))
159       scope)))
160   (apply string-append (map output-scope scopes)))
161
162 (define-public (relevant-book-systems book)
163   (let ((systems (ly:paper-book-systems book)))
164     ;; skip booktitles.
165     (if (and (not (ly:get-option 'include-book-title-preview))
166              (pair? systems)
167              (ly:prob-property (car systems) 'is-book-title #f))
168         (cdr systems)
169         systems)))
170
171 (define-public (relevant-dump-systems systems)
172   (let ((to-dump-systems '()))
173     (for-each
174       (lambda (sys)
175         (if (or (paper-system-title? sys)
176                 (not (pair? to-dump-systems))
177                 (paper-system-title? (car to-dump-systems)))
178             (set! to-dump-systems (cons sys to-dump-systems))))
179       systems)
180     to-dump-systems))
181
182 (define missing-stencil-list '())
183
184 (define-public (backend-testing output-module)
185   (define (missing-stencil-expression name)
186     (begin
187       (ly:warning (_ "missing stencil expression `~S'") name)
188       ""))
189
190   (map (lambda (x)
191          (if (not (module-defined? output-module x))
192              (begin
193                (module-define! output-module x
194                                (lambda* (#:optional y . z)
195                                  (missing-stencil-expression x)))
196                (set! missing-stencil-list (append (list x)
197                                                   missing-stencil-list)))))
198        (ly:all-stencil-commands)))
199
200 (define-public (remove-stencil-warnings output-module)
201   (for-each
202     (lambda (x)
203       (module-remove! output-module x))
204     missing-stencil-list))
205
206 (define (filter-out pred? lst)
207   (filter (lambda (x) (not (pred? x))) lst))
208
209 (define-public (font-name-split font-name)
210   "Return @code{(FONT-NAME . DESIGN-SIZE)} from @var{font-name} string
211 or @code{#f}."
212   (let ((match (regexp-exec (make-regexp "(.*)-([0-9]*)") font-name)))
213     (if (regexp-match? match)
214         (cons (match:substring match 1) (match:substring match 2))
215         (cons font-name-designsize #f))))
216
217 ;; Example of a pango-physical-font
218 ;; ("Emmentaler-11" "/home/janneke/vc/lilypond/out/share/lilypond/current/fonts/otf/emmentaler-11.otf" 0)
219 (define-public (pango-pf-font-name pango-pf)
220   "Return the font-name of the pango physical font @var{pango-pf}."
221   (list-ref pango-pf 0))
222 (define-public (pango-pf-file-name pango-pf)
223   "Return the file-name of the pango physical font @var{pango-pf}."
224   (list-ref pango-pf 1))
225 (define-public (pango-pf-fontindex pango-pf)
226   "Return the fontindex of the pango physical font @var{pango-pf}."
227   (list-ref pango-pf 2))
228
229 (define (pango-font-name pango-font)
230   (let ((pf-fonts (ly:pango-font-physical-fonts pango-font)))
231     (if (pair? pf-fonts)
232         (pango-pf-font-name (car pf-fonts))
233         "")))
234
235 (define-public (define-fonts paper define-font define-pango-pf)
236   "Return a string of all fonts used in @var{paper}, invoking the functions
237 @var{define-font} and @var{define-pango-pf} for producing the actual font
238 definition."
239
240   (let* ((font-list (ly:paper-fonts paper))
241          (pango-fonts (filter ly:pango-font? font-list))
242          (other-fonts (filter-out ly:pango-font? font-list))
243          (other-font-names (map ly:font-name other-fonts))
244          (pango-only-fonts
245           (filter-out (lambda (x)
246                         (member (pango-font-name x) other-font-names))
247                       pango-fonts)))
248
249   (define (font-load-command font)
250     (let* ((font-name (ly:font-name font))
251            (designsize (ly:font-design-size font))
252            (magnification (* (ly:font-magnification font)))
253            (ops (ly:output-def-lookup paper 'output-scale))
254            (scaling (* ops magnification designsize)))
255       (if (equal? font-name "unknown")
256           (display (list font font-name)))
257       (define-font font font-name scaling)))
258
259   (define (pango-font-load-command pango-font)
260     (let* ((pf-fonts (ly:pango-font-physical-fonts pango-font))
261            (pango-pf (if (pair? pf-fonts) (car pf-fonts) '("" "" 0)))
262            (font-name (pango-pf-font-name pango-pf))
263            (scaling (ly:output-def-lookup paper 'output-scale)))
264       (if (equal? font-name "unknown")
265           (display (list pango-font font-name)))
266       (define-pango-pf pango-pf font-name scaling)))
267
268   (string-append
269    (apply string-append (map font-load-command other-fonts))
270    (apply string-append (map pango-font-load-command pango-only-fonts)))))