1 ;;;; backend-library.scm -- helpers for the backends.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.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)
32 (regexp-substitute/global #f "[^- 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
35 (define-public (search-executable names)
36 (define (helper path lst)
39 (if (search-path path (car lst)) (car lst)
40 (helper path (cdr lst)))))
42 (let ((path (parse-path (getenv "PATH"))))
45 (define-public (search-gs)
46 (search-executable '("gs-nox" "gs-8.15" "gs")))
48 (define-public (postscript->pdf papersizename name)
49 (let* ((pdf-name (string-append (basename name ".ps") ".pdf"))
54 -dCompatibilityLevel=1.4 \
65 (if (ly:get-option 'verbose) "" "-q")
66 (if (ly:get-option 'gs-font-load)
69 (sanitize-command-option papersizename)
72 ;; The wrapper on windows cannot handle `=' signs,
73 ;; gs has a workaround with #.
74 (if (eq? PLATFORM 'windows)
76 (set! cmd (string-regexp-substitute "=" "#" cmd))
77 (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
79 (if (access? pdf-name W_OK)
80 (delete-file pdf-name))
82 (ly:message (_ "Converting to `~a'...") pdf-name)
87 (use-modules (scm ps-to-png))
89 (define-public (postscript->png resolution paper-size-name name)
90 ;; Do not try to guess the name of the png file,
91 ;; GS produces PNG files like BASE-page%d.png.
92 ;;(ly:message (_ "Converting to `~a'...")
93 ;; (string-append (basename name ".ps") "-page1.png" )))
94 (let ((paper-size (sanitize-command-option paper-size-name))
95 (verbose (ly:get-option 'verbose))
97 (ly:message (_ "Converting to ~a...") "PNG")
98 (make-ps-images name resolution paper-size rename-page-1 verbose
99 (ly:get-option 'anti-alias-factor))
102 (define-public (postprocess-output paper-book module filename formats)
104 ((completed (completize-formats formats))
105 (base (string-regexp-substitute "\\.[a-z]+$" "" filename))
106 (intermediate (remove
112 ((eval (string->symbol (string-append "convert-to-" f)) module)
113 paper-book filename))
116 (if (ly:get-option 'delete-intermediate-files)
119 (delete-file (string-append base "." f)))
123 (define-public (completize-formats formats)
124 (define new-fmts '())
126 (if (member "png" formats)
127 (set! formats (cons "ps" formats)))
128 (if (member "pdf" formats)
129 (set! formats (cons "ps" formats)))
133 (if (member x formats) (set! new-fmts (cons x new-fmts))))
134 '("tex" "dvi" "ps" "pdf" "png"))
136 (uniq-list (reverse new-fmts)))
138 (define (header-to-file file-name key value)
139 (set! key (symbol->string key))
140 (if (not (equal? "-" file-name))
141 (set! file-name (string-append file-name "." key)))
142 (ly:message (_ "Writing header field `~a' to `~a'...")
144 (if (equal? "-" file-name) "<stdout>" file-name))
145 (if (equal? file-name "-")
147 (display value (open-file file-name "w")))
151 (define-public (output-scopes scopes fields basename)
152 (define (output-scope scope)
157 (let ((val (if (variable-bound? var) (variable-ref var) "")))
158 (if (and (memq sym fields) (string? val))
159 (header-to-file basename sym val))
162 (apply string-append (map output-scope scopes)))