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