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)
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))
21 (set! status (system silenced))
24 (ly:message (_ "`~a' failed (~a)") command status)
26 ;; hmmm. what's the best failure option?
27 (throw 'ly-file-failed)))))
29 (define-public (sanitize-command-option str)
30 "Kill dubious shell quoting"
34 (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
37 (define-public (search-executable names)
38 (define (helper path lst)
41 (if (search-path path (car lst)) (car lst)
42 (helper path (cdr lst)))))
44 (let ((path (parse-path (getenv "PATH"))))
47 (define-public (search-gs)
49 ;; must be sure that we don't catch stuff from old GUBs.
50 (search-executable '("gs")))
52 (define-public (postscript->pdf paper-width paper-height name)
53 (let* ((pdf-name (string-append
54 (basename (basename name ".ps") ".eps")
56 (is-eps (string-match "\\.eps$" name))
57 (paper-size-string (if is-eps
59 (format "-dDEVICEWIDTHPOINTS=~,2f \
60 -dDEVICEHEIGHTPOINTS=~,2f "
61 paper-width paper-height )))
68 -dCompatibilityLevel=1.4 \
78 (if (ly:get-option 'verbose) "" "-q")
79 (if (ly:get-option 'gs-load-fonts)
85 ;; The wrapper on windows cannot handle `=' signs,
86 ;; gs has a workaround with #.
87 (if (eq? PLATFORM 'windows)
89 (set! cmd (string-regexp-substitute "=" "#" cmd))
90 (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
92 (if (access? pdf-name W_OK)
93 (delete-file pdf-name))
95 (ly:message (_ "Converting to `~a'...") pdf-name)
99 (use-modules (scm ps-to-png))
101 (define-public (postscript->png resolution paper-width paper-height name)
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'...")
105 ;; (string-append (basename name ".ps") "-page1.png" )))
106 (let* ((verbose (ly:get-option 'verbose))
108 (ly:message (_ "Converting to ~a...") "PNG")
110 #:resolution resolution
111 #:page-width paper-width
112 #:page-height paper-height
113 #:rename-page-1 rename-page-1
115 #:anti-alias-factor (ly:get-option 'anti-alias-factor)
116 #:pixmap-format (ly:get-option 'pixmap-format))
119 (define-public (postprocess-output paper-book module filename formats)
120 (let* ((completed (completize-formats formats))
121 (base (string-regexp-substitute "\\.[a-z]+$" "" filename))
122 (intermediate (remove (lambda (x) (member x formats)) completed)))
124 (for-each (lambda (f)
125 ((eval (string->symbol (format "convert-to-~a" f))
126 module) paper-book filename)) completed)
127 (if (ly:get-option 'delete-intermediate-files)
128 (for-each (lambda (f)
129 (delete-file (string-append base "." f))) intermediate))))
131 (define-public (completize-formats formats)
132 (define new-fmts '())
133 (if (member "png" formats)
134 (set! formats (cons "ps" formats)))
135 (if (member "pdf" formats)
136 (set! formats (cons "ps" formats)))
137 (for-each (lambda (x)
138 (if (member x formats) (set! new-fmts (cons x new-fmts))))
139 '("tex" "dvi" "ps" "pdf" "png"))
140 (uniq-list (reverse new-fmts)))
142 (define (header-to-file file-name key value)
143 (set! key (symbol->string key))
144 (if (not (equal? "-" file-name))
145 (set! file-name (string-append file-name "." key)))
146 (ly:message (_ "Writing header field `~a' to `~a'...")
148 (if (equal? "-" file-name) "<stdout>" file-name))
149 (if (equal? file-name "-")
151 (let ((port (open-file file-name "w")))
158 (define-public (output-scopes scopes fields basename)
159 (define (output-scope scope)
164 (let ((val (if (variable-bound? var) (variable-ref var) "")))
165 (if (and (memq sym fields) (string? val))
166 (header-to-file basename sym val))
169 (apply string-append (map output-scope scopes)))