]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
* scm/framework-tex.scm (convert-to-dvi): Barf if NAME contains
[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 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
9 ;; backend helpers.
10
11 (define-public (ly:system command)
12   (let* ((status 0)
13          (silenced
14           (string-append command (if (ly:get-option 'verbose)
15                                      ""
16                                      " > /dev/null 2>&1 "))))
17
18     (if (ly:get-option 'verbose)
19         (format (current-error-port) (_ "Invoking `~a'...") command))
20     
21     (set! status (system silenced))
22     (if (> status 0)
23         (begin
24           (format (current-error-port) (_ "`~a' failed (~a)") command status)
25           (newline (current-error-port))
26           
27           ;; hmmm.  what's the best failure option? 
28           (throw 'ly-file-failed)))))
29
30 (define-public (sanitize-command-option str)
31   (string-append
32    "\""
33    (regexp-substitute/global #f "[^- 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
34    "\""))
35
36 (define-public (postscript->pdf papersizename name)
37   (let* ((cmd (format #f "ps2pdf -sPAPERSIZE=~a '~a'"
38                       (sanitize-command-option papersizename) name))
39          (pdf-name (string-append (basename name ".ps") ".pdf" )))
40
41     (if (access? pdf-name W_OK)
42         (delete-file pdf-name))
43
44     (format (current-error-port) (_ "Converting to `~a'...") pdf-name)
45     (ly:system cmd)))
46
47 (define-public (postscript->png resolution papersizename name)
48   (let ((cmd (format #f
49               "ps2png --resolution=~S --papersize=~a~a '~a'"
50               resolution
51               (sanitize-command-option papersizename)
52               (if (ly:get-option 'verbose) " --verbose " "")
53               name)))
54     ;; Do not try to guess the name of the png file
55     (format (current-error-port) (_ "Converting to `~a'...") "png")
56     (ly:system cmd)))
57
58 (define-public (postprocess-output paper-book module filename formats)
59   (for-each
60    (lambda (f)
61      ((eval (string->symbol (string-append "convert-to-" f))
62             module)
63       paper-book filename))
64    
65    formats))
66
67 (define-public (completize-formats formats)
68   (define new-fmts '())
69
70   (if (member "png" formats)
71       (set! formats (cons "ps" formats)))
72   (if (member "pdf" formats)
73       (set! formats (cons "ps" formats)))
74
75   (for-each
76    (lambda (x)
77      (if (member x formats) (set! new-fmts (cons x new-fmts))))
78    '("tex" "dvi" "ps" "pdf" "png"))
79
80   new-fmts)
81
82 (define (header-to-file file-name key value)
83   (set! key (symbol->string key))
84   (if (not (equal? "-" file-name))
85       (set! file-name (string-append file-name "." key)))
86   (format (current-error-port)
87           (_ "Writing header field `~a' to `~a'...")
88           key
89           (if (equal? "-" file-name) "<stdout>" file-name))
90   (if (equal? file-name "-")
91       (display value)
92       (display value (open-file file-name "w")))
93   (newline (current-error-port))
94   "")
95
96 (define-public (output-scopes scopes fields basename)
97   (define (output-scope scope)
98     (apply
99      string-append
100      (module-map
101       (lambda (sym var)
102         (let ((val (if (variable-bound? var) (variable-ref var) "")))
103           (if (and (memq sym fields) (string? val))
104               (header-to-file basename sym val))
105           ""))
106       scope)))
107   (apply string-append (map output-scope scopes)))
108