1 ;;;; backend-library.scm -- helpers for the backends.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (define-public (ly:system command . rest)
13 (dev-null "/dev/null")
14 (silenced (if (or (ly:get-option 'verbose)
15 (not (access? dev-null W_OK)))
17 (format #f "~a > ~a 2>&1 " command dev-null))))
18 (if (ly:get-option 'verbose)
19 (ly:message (_ "Invoking `~a'...") command))
23 (system-with-env silenced (car rest))
28 (ly:message (_ "`~a' failed (~a)") command status)
30 ;; hmmm. what's the best failure option?
31 (throw 'ly-file-failed)))))
33 (define-public (system-with-env cmd env)
35 "Execute CMD in fork, with ENV (a list of strings) as the environment"
37 ;; laziness: should use execle?
39 ((pid (primitive-fork)))
47 (cdr (waitpid pid)))))
49 (define-public (sanitize-command-option str)
50 "Kill dubious shell quoting"
54 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
57 (define-public (search-executable names)
58 (define (helper path lst)
61 (if (search-path path (car lst)) (car lst)
62 (helper path (cdr lst)))))
64 (let ((path (parse-path (getenv "PATH"))))
67 (define-public (search-gs)
69 ;; must be sure that we don't catch stuff from old GUBs.
70 (search-executable '("gs")))
72 (define-public (postscript->pdf paper-width paper-height name)
73 (let* ((pdf-name (string-append
74 (basename (basename name ".ps") ".eps")
76 (is-eps (string-match "\\.eps$" name))
77 (paper-size-string (if is-eps
79 (ly:format "-dDEVICEWIDTHPOINTS=~$ \
80 -dDEVICEHEIGHTPOINTS=~$ "
81 paper-width paper-height )))
83 (cmd (simple-format #f
88 -dCompatibilityLevel=1.4 \
98 (if (ly:get-option 'verbose) "" "-q")
99 (if (or (ly:get-option 'gs-load-fonts)
100 (ly:get-option 'gs-load-lily-fonts))
107 ;; The wrapper on windows cannot handle `=' signs,
108 ;; gs has a workaround with #.
109 (if (eq? PLATFORM 'windows)
111 (set! cmd (string-regexp-substitute "=" "#" cmd))
112 (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
114 (if (access? pdf-name W_OK)
115 (delete-file pdf-name))
117 (ly:message (_ "Converting to `~a'...") pdf-name)
121 (use-modules (scm ps-to-png))
123 (define-public (postscript->png resolution paper-width paper-height name)
124 ;; Do not try to guess the name of the png file,
125 ;; GS produces PNG files like BASE-page%d.png.
126 ;;(ly:message (_ "Converting to `~a'...")
127 ;; (string-append (basename name ".ps") "-page1.png" )))
128 (let* ((verbose (ly:get-option 'verbose))
130 (ly:message (_ "Converting to ~a...") "PNG")
132 #:resolution resolution
133 #:page-width paper-width
134 #:page-height paper-height
135 #:rename-page-1 rename-page-1
137 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
138 #:pixmap-format (ly:get-option 'pixmap-format))
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)))
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))))
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 '("tex" "dvi" "ps" "pdf" "png"))
162 (uniq-list (reverse new-fmts)))
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'...")
170 (if (equal? "-" file-name) "<stdout>" file-name))
171 (if (equal? file-name "-")
173 (let ((port (open-file file-name "w")))
180 (define-public (output-scopes scopes fields basename)
181 (define (output-scope scope)
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))
191 (apply string-append (map output-scope scopes)))