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