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