]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
(postscript->png): call lilypond-ps2png
[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* ((cmd (format #f "ps2pdf -sPAPERSIZE=~a '~a'"
36                       (sanitize-command-option papersizename) name))
37          (pdf-name (string-append (basename name ".ps") ".pdf" )))
38
39     (if (access? pdf-name W_OK)
40         (delete-file pdf-name))
41
42     (ly:message (_ "Converting to `~a'...") pdf-name)
43     (ly:progress "\n")
44     (ly:system cmd)))
45
46 (define-public (postscript->png resolution papersizename name)
47   (let* ((prefix (ly:effective-prefix))
48
49          ;; run the source, if  we are in the build-directory
50          (ps2png-source (if prefix
51                            (format "~a/scripts/lilypond-ps2png.py" prefix)
52                            "lilypond-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                           "lilypond-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     ;;(ly:message (_ "Converting to `~a'...")
65     ;;      (string-append (basename name ".ps") "-page1.png" )))
66     (ly:message (_ "Converting to ~a...") "PNG")
67     (ly:system cmd)
68     (ly:progress "\n")))
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)) module)
74       paper-book filename))
75    formats))
76
77 (define-public (completize-formats formats)
78   (define new-fmts '())
79
80   (if (member "png" formats)
81       (set! formats (cons "ps" formats)))
82   (if (member "pdf" formats)
83       (set! formats (cons "ps" formats)))
84
85   (for-each
86    (lambda (x)
87      (if (member x formats) (set! new-fmts (cons x new-fmts))))
88    '("tex" "dvi" "ps" "pdf" "png"))
89
90   (uniq-list (reverse new-fmts)))
91
92 (define (header-to-file file-name key value)
93   (set! key (symbol->string key))
94   (if (not (equal? "-" file-name))
95       (set! file-name (string-append file-name "." key)))
96   (ly:message (_ "Writing header field `~a' to `~a'...")
97               key
98               (if (equal? "-" file-name) "<stdout>" file-name))
99   (if (equal? file-name "-")
100       (display value)
101       (display value (open-file file-name "w")))
102   (ly:progress "\n")
103   "")
104
105 (define-public (output-scopes scopes fields basename)
106   (define (output-scope scope)
107     (apply
108      string-append
109      (module-map
110       (lambda (sym var)
111         (let ((val (if (variable-bound? var) (variable-ref var) "")))
112           (if (and (memq sym fields) (string? val))
113               (header-to-file basename sym val))
114           ""))
115       scope)))
116   (apply string-append (map output-scope scopes)))
117