]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
fa2a37bfb364aa1b87983be59be8694cc0234c0b
[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* ((prefix (ly:effective-prefix))
49          ;; FIXME: should scripts/ps2png.py be installed in PREFIX?
50          (ps2png-source (if prefix
51                            (format "~a/scripts/ps2png.py" prefix)
52                            "ps2png"))
53          (cmd (format #f
54                       "~a --resolution=~S --papersize=~a~a '~a'"
55                       (if (file-exists? ps2png-source)
56                           (format "python ~a" ps2png-source)
57                           "ps2png")
58                       resolution
59                       (sanitize-command-option papersizename)
60                       (if (ly:get-option 'verbose) " --verbose " "")
61                       name)))
62     ;; Do not try to guess the name of the png file,
63     ;; GS produces PNG files like BASE-page%d.png.
64     ;;(format (current-error-port) (_ "Converting to `~a'...")
65     ;;      (string-append (basename name ".ps") "-page1.png" )))
66     (format (current-error-port) (_ "Converting to ~a...") "PNG")
67     (newline (current-error-port))
68     (ly:system cmd)))
69
70 (define-public (postprocess-output paper-book module filename formats)
71   (for-each
72    (lambda (f)
73      ((eval (string->symbol (string-append "convert-to-" f))
74             module)
75       paper-book filename))
76    
77    formats))
78
79 (define-public (completize-formats formats)
80   (define new-fmts '())
81
82   (if (member "png" formats)
83       (set! formats (cons "ps" formats)))
84   (if (member "pdf" formats)
85       (set! formats (cons "ps" formats)))
86
87   (for-each
88    (lambda (x)
89      (if (member x formats) (set! new-fmts (cons x new-fmts))))
90    '("tex" "dvi" "ps" "pdf" "png"))
91
92   (reverse new-fmts))
93
94 (define (header-to-file file-name key value)
95   (set! key (symbol->string key))
96   (if (not (equal? "-" file-name))
97       (set! file-name (string-append file-name "." key)))
98   (format (current-error-port)
99           (_ "Writing header field `~a' to `~a'...")
100           key
101           (if (equal? "-" file-name) "<stdout>" file-name))
102   (if (equal? file-name "-")
103       (display value)
104       (display value (open-file file-name "w")))
105   (newline (current-error-port))
106   "")
107
108 (define-public (output-scopes scopes fields basename)
109   (define (output-scope scope)
110     (apply
111      string-append
112      (module-map
113       (lambda (sym var)
114         (let ((val (if (variable-bound? var) (variable-ref var) "")))
115           (if (and (memq sym fields) (string? val))
116               (header-to-file basename sym val))
117           ""))
118       scope)))
119   (apply string-append (map output-scope scopes)))
120