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