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