]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
a9367de7c2896b39efe7448f296fc7fb46414065
[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     (if (ly:get-option 'verbose)
18         (ly:message (_ "Invoking `~a'...") command))
19     
20     (set! status (system silenced))
21     (if (> status 0)
22         (begin
23           (ly:message (_ "`~a' failed (~a)") command status)
24           (ly:progress "\n")
25           ;; hmmm.  what's the best failure option? 
26           (throw 'ly-file-failed)))))
27
28 (define-public (sanitize-command-option str)
29   (string-append
30    "\""
31    (regexp-substitute/global #f "[^- 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
32    "\""))
33
34 (define-public (postscript->pdf papersizename name)
35   (let* ((pdf-name (string-append (basename name ".ps") ".pdf" ))
36          (cmd (format #f
37                       "gs\
38  -dCompatibilityLevel=1.4 \
39  -dSAFER\
40  -sPAPERSIZE=~a\
41  -q\
42  -dNOPAUSE\
43  -dBATCH\
44  -sDEVICE=pdfwrite\
45  -sOutputFile=~S\
46  -c .setpdfwrite\
47  -f ~S\
48 "
49                       (sanitize-command-option papersizename)
50                       pdf-name
51                       name)))
52
53     (if (access? pdf-name W_OK)
54         (delete-file pdf-name))
55
56     (ly:message (_ "Converting to `~a'...") pdf-name)
57     (ly:progress "\n")
58     (ly:system cmd)))
59
60 (define-public (postscript->png resolution papersizename name)
61   (let* ((prefix (ly:effective-prefix))
62
63          ;; run the source, if  we are in the build-directory
64          (ps2png-source (if prefix
65                            (format "~a/scripts/lilypond-ps2png.py" prefix)
66                            "lilypond-ps2png"))
67          (cmd (format #f
68                       "~a --resolution=~S --papersize=~a~a ~S"
69                       (if (file-exists? ps2png-source)
70                           (format "python ~a" ps2png-source)
71                           "lilypond-ps2png")
72                       resolution
73                       (sanitize-command-option papersizename)
74                       (if (ly:get-option 'verbose) " --verbose " "")
75                       name)))
76     ;; Do not try to guess the name of the png file,
77     ;; GS produces PNG files like BASE-page%d.png.
78     ;;(ly:message (_ "Converting to `~a'...")
79     ;;      (string-append (basename name ".ps") "-page1.png" )))
80     (ly:message (_ "Converting to ~a...") "PNG")
81     (ly:system cmd)
82     (ly:progress "\n")))
83
84 (define-public (postprocess-output paper-book module filename formats)
85   (for-each
86    (lambda (f)
87      ((eval (string->symbol (string-append "convert-to-" f)) module)
88       paper-book filename))
89    formats))
90
91 (define-public (completize-formats formats)
92   (define new-fmts '())
93
94   (if (member "png" formats)
95       (set! formats (cons "ps" formats)))
96   (if (member "pdf" formats)
97       (set! formats (cons "ps" formats)))
98
99   (for-each
100    (lambda (x)
101      (if (member x formats) (set! new-fmts (cons x new-fmts))))
102    '("tex" "dvi" "ps" "pdf" "png"))
103
104   (uniq-list (reverse new-fmts)))
105
106 (define (header-to-file file-name key value)
107   (set! key (symbol->string key))
108   (if (not (equal? "-" file-name))
109       (set! file-name (string-append file-name "." key)))
110   (ly:message (_ "Writing header field `~a' to `~a'...")
111               key
112               (if (equal? "-" file-name) "<stdout>" file-name))
113   (if (equal? file-name "-")
114       (display value)
115       (display value (open-file file-name "w")))
116   (ly:progress "\n")
117   "")
118
119 (define-public (output-scopes scopes fields basename)
120   (define (output-scope scope)
121     (apply
122      string-append
123      (module-map
124       (lambda (sym var)
125         (let ((val (if (variable-bound? var) (variable-ref var) "")))
126           (if (and (memq sym fields) (string? val))
127               (header-to-file basename sym val))
128           ""))
129       scope)))
130   (apply string-append (map output-scope scopes)))
131