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 (postscript->pdf papersizename name)
36 (let* ((pdf-name (string-append (basename name ".ps") ".pdf" ))
40 -dCompatibilityLevel=1.4 \
50 (sanitize-command-option papersizename)
53 ;; The wrapper on windows cannot handle `=' signs,
54 ;; gs has a workaround with #.
55 (if (eq? PLATFORM 'windows)
56 (set! cmd (string-regexp-substitute "=" "#" cmd)))
58 (if (access? pdf-name W_OK)
59 (delete-file pdf-name))
61 (ly:message (_ "Converting to `~a'...") pdf-name)
65 (define-public (postscript->png resolution papersizename name)
66 (let* ((prefix (ly:effective-prefix))
68 ;; run the source, if we are in the build-directory
69 (ps2png-source (if prefix
70 (format "~a/scripts/lilypond-ps2png.py" prefix)
73 "~a --resolution=~S --papersize=~a~a ~S"
74 (if (file-exists? ps2png-source)
75 (format "python ~a" ps2png-source)
78 (sanitize-command-option papersizename)
79 (if (ly:get-option 'verbose) " --verbose " "")
81 ;; Do not try to guess the name of the png file,
82 ;; GS produces PNG files like BASE-page%d.png.
83 ;;(ly:message (_ "Converting to `~a'...")
84 ;; (string-append (basename name ".ps") "-page1.png" )))
85 (ly:message (_ "Converting to ~a...") "PNG")
89 (define-public (postprocess-output paper-book module filename formats)
92 ((eval (string->symbol (string-append "convert-to-" f)) module)
96 (define-public (completize-formats formats)
99 (if (member "png" formats)
100 (set! formats (cons "ps" formats)))
101 (if (member "pdf" formats)
102 (set! formats (cons "ps" formats)))
106 (if (member x formats) (set! new-fmts (cons x new-fmts))))
107 '("tex" "dvi" "ps" "pdf" "png"))
109 (uniq-list (reverse new-fmts)))
111 (define (header-to-file file-name key value)
112 (set! key (symbol->string key))
113 (if (not (equal? "-" file-name))
114 (set! file-name (string-append file-name "." key)))
115 (ly:message (_ "Writing header field `~a' to `~a'...")
117 (if (equal? "-" file-name) "<stdout>" file-name))
118 (if (equal? file-name "-")
120 (display value (open-file file-name "w")))
124 (define-public (output-scopes scopes fields basename)
125 (define (output-scope scope)
130 (let ((val (if (variable-bound? var) (variable-ref var) "")))
131 (if (and (memq sym fields) (string? val))
132 (header-to-file basename sym val))
135 (apply string-append (map output-scope scopes)))