]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
Run grand-replace for 2010.
[lilypond.git] / scm / backend-library.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2010 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              (ice-9 optargs))
24
25 (define-public (ly:system command . rest)
26   (let* ((status 0)
27          (dev-null "/dev/null")
28          (silenced (if (or (ly:get-option 'verbose)
29                            (not (access? dev-null W_OK)))
30                        command
31                        (format #f "~a > ~a 2>&1 " command dev-null))))
32     (if (ly:get-option 'verbose)
33         (begin
34           (ly:message (_ "Invoking `~a'...") command))
35           (ly:progress "\n"))
36
37     (set! status
38           (if (pair? rest)
39               (system-with-env silenced (car rest))
40               (system silenced)))
41         
42     (if (> status 0)
43         (begin
44           (ly:message (_ "`~a' failed (~a)") command status)
45           (ly:progress "\n")
46           ;; hmmm.  what's the best failure option? 
47           (throw 'ly-file-failed)))))
48
49 (define-public (system-with-env cmd env)
50
51   "Execute CMD in fork, with ENV (a list of strings) as the environment"
52   (let*
53       ;; laziness: should use execle?
54       
55       ((pid (primitive-fork)))
56     (if (= 0 pid)
57         ;; child
58         (begin
59           (environ env)
60           (system cmd))
61         
62         ;; parent
63         (cdr (waitpid pid)))))
64
65 (define-public (sanitize-command-option str)
66   "Kill dubious shell quoting"
67   
68   (string-append
69    "\""
70    (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
71    "\""))
72
73 (define-public (search-executable names)
74   (define (helper path lst)
75     (if (null? (cdr lst))
76         (car lst)
77         (if (search-path path (car lst)) (car lst)
78             (helper path (cdr lst)))))
79
80   (let ((path (parse-path (getenv "PATH"))))
81     (helper path names)))
82
83 (define-public (search-gs)
84   
85   ;; must be sure that we don't catch stuff from old GUBs.
86   (search-executable '("gs")))
87   
88 (define-public (postscript->pdf paper-width paper-height name)
89   (let* ((pdf-name (string-append
90                     (dir-basename name ".ps" ".eps")
91                     ".pdf"))
92          (is-eps (string-match "\\.eps$" name))
93          (paper-size-string (if is-eps
94                                 "-dEPSCrop"
95                                 (ly:format "-dDEVICEWIDTHPOINTS=~$\
96  -dDEVICEHEIGHTPOINTS=~$"
97                                         paper-width paper-height)))
98
99          (cmd (ly:format
100                       "~a\
101  ~a\
102  ~a\
103  ~a\
104  -dCompatibilityLevel=1.4\
105  -dNOPAUSE\
106  -dBATCH\
107  -r1200\
108  -sDEVICE=pdfwrite\
109  -sOutputFile=~S\
110  -c .setpdfwrite\
111  -f ~S\
112 "
113                       (search-gs)
114                       (if (ly:get-option 'verbose) "" "-q")
115                       (if (or (ly:get-option 'gs-load-fonts)
116                               (ly:get-option 'gs-load-lily-fonts))
117                           "-dNOSAFER"
118                           "-dSAFER")
119                       paper-size-string
120                       pdf-name
121                       name)))
122     ;; The wrapper on windows cannot handle `=' signs,
123     ;; gs has a workaround with #.
124     (if (eq? PLATFORM 'windows)
125         (begin
126           (set! cmd (string-regexp-substitute "=" "#" cmd))
127           (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))
128           (if (access? pdf-name W_OK)
129               (delete-file pdf-name))))
130
131     (ly:message (_ "Converting to `~a'...") pdf-name)
132     (ly:progress "\n")
133     (ly:system cmd)))
134
135 (define-public (postscript->png resolution paper-width paper-height name)
136   (let* ((verbose (ly:get-option 'verbose))
137          (rename-page-1 #f))
138
139     ;; Do not try to guess the name of the png file,
140     ;; GS produces PNG files like BASE-page%d.png.
141     (ly:message (_ "Converting to ~a...") "PNG")
142     (make-ps-images name
143                     #:resolution resolution
144                     #:page-width paper-width
145                     #:page-height paper-height
146                     #:rename-page-1 rename-page-1
147                     #:be-verbose verbose
148                     #:anti-alias-factor (ly:get-option 'anti-alias-factor)
149                     #:pixmap-format (ly:get-option 'pixmap-format))
150     (ly:progress "\n")))
151
152 (define-public (postprocess-output paper-book module filename formats)
153   (let* ((completed (completize-formats formats))
154          (base (dir-basename filename ".ps" ".eps"))
155          (intermediate (remove (lambda (x) (member x formats)) completed)))
156     (for-each (lambda (f)
157                 ((eval (string->symbol (format "convert-to-~a" f))
158                        module) paper-book filename)) completed)
159     (if (ly:get-option 'delete-intermediate-files)
160         (for-each (lambda (f)
161                     (if (file-exists? f) (delete-file f)))
162                   (map (lambda (x) (string-append base "." x)) intermediate)))))
163
164 (define-public (completize-formats formats)
165   (define new-fmts '())
166   (if (member "png" formats)
167       (set! formats (cons "ps" formats)))
168   (if (member "pdf" formats)
169       (set! formats (cons "ps" formats)))
170   (for-each (lambda (x)
171               (if (member x formats) (set! new-fmts (cons x new-fmts))))
172             '("ps" "pdf" "png"))
173   (uniq-list (reverse new-fmts)))
174
175 (define (header-to-file file-name key value)
176   (set! key (symbol->string key))
177   (if (not (equal? "-" file-name))
178       (set! file-name (string-append file-name "." key)))
179   (ly:message (_ "Writing header field `~a' to `~a'...")
180               key
181               (if (equal? "-" file-name) "<stdout>" file-name))
182   (if (equal? file-name "-")
183       (display value)
184       (let ((port (open-file file-name "w")))
185         (display value port)
186         (close-port port)))
187
188   (ly:progress "\n")
189   "")
190
191 (define-public (output-scopes scopes fields basename)
192   (define (output-scope scope)
193     (apply
194      string-append
195      (module-map
196       (lambda (sym var)
197         (let ((val (if (variable-bound? var) (variable-ref var) "")))
198           (if (and (memq sym fields) (string? val))
199               (header-to-file basename sym val))
200           ""))
201       scope)))
202   (apply string-append (map output-scope scopes)))
203
204 (define missing-stencil-list '())
205
206 (define-public (backend-testing output-module)
207   (define (missing-stencil-expression name)
208     (begin
209       (ly:warning (_ "missing stencil expression `~S'") name)
210       ""))
211
212   (map (lambda (x)
213          (if (not (module-defined? output-module x))
214              (begin
215                (module-define! output-module x
216                                (lambda* (#:optional y . z)
217                                  (missing-stencil-expression x)))
218                (set! missing-stencil-list (append (list x)
219                                                   missing-stencil-list)))))
220        (ly:all-stencil-commands)))
221
222 (define-public (remove-stencil-warnings output-module)
223   (for-each
224     (lambda (x)
225       (module-remove! output-module x))
226     missing-stencil-list))