]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
*** empty log message ***
[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  -dSAFER\
40  -dCompatibilityLevel=1.4 \
41  -sPAPERSIZE=~a\
42  -q\
43  -dNOPAUSE\
44  -dBATCH\
45  -r1200 \
46  -sDEVICE=pdfwrite\
47  -sOutputFile=~S\
48  -c .setpdfwrite\
49  -f ~S\
50 "
51                       (sanitize-command-option papersizename)
52                       pdf-name
53                       name)))
54     ;; The wrapper on windows cannot handle `=' signs,
55     ;; gs has a workaround with #.
56     (if (eq? PLATFORM 'windows)
57         (begin
58           (set! cmd (string-regexp-substitute "=" "#" cmd))
59           (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
60
61     (if (access? pdf-name W_OK)
62         (delete-file pdf-name))
63
64     (ly:message (_ "Converting to `~a'...") pdf-name)
65     (ly:progress "\n")
66     (ly:system cmd)
67     (if (running-from-gui?) (delete-file name))))
68
69 (define-public (postscript->png resolution papersizename name)
70   (let* ((prefix (ly:effective-prefix))
71
72          ;; run the source, if  we are in the build-directory
73          (ps2png-source (if prefix
74                            (format "~a/scripts/lilypond-ps2png.py" prefix)
75                            "lilypond-ps2png"))
76          (cmd (format #f
77                       "~a --resolution=~S --papersize=~a~a ~S"
78                       (if (file-exists? ps2png-source)
79                           (format "python ~a" ps2png-source)
80                           "lilypond-ps2png")
81                       resolution
82                       (sanitize-command-option papersizename)
83                       (if (ly:get-option 'verbose) " --verbose " "")
84                       name)))
85     ;; Do not try to guess the name of the png file,
86     ;; GS produces PNG files like BASE-page%d.png.
87     ;;(ly:message (_ "Converting to `~a'...")
88     ;;      (string-append (basename name ".ps") "-page1.png" )))
89     (ly:message (_ "Converting to ~a...") "PNG")
90     (ly:system cmd)
91     (ly:progress "\n")))
92
93 (define-public (postprocess-output paper-book module filename formats)
94   (for-each
95    (lambda (f)
96      ((eval (string->symbol (string-append "convert-to-" f)) module)
97       paper-book filename))
98    formats))
99
100 (define-public (completize-formats formats)
101   (define new-fmts '())
102
103   (if (member "png" formats)
104       (set! formats (cons "ps" formats)))
105   (if (member "pdf" formats)
106       (set! formats (cons "ps" formats)))
107
108   (for-each
109    (lambda (x)
110      (if (member x formats) (set! new-fmts (cons x new-fmts))))
111    '("tex" "dvi" "ps" "pdf" "png"))
112
113   (uniq-list (reverse new-fmts)))
114
115 (define (header-to-file file-name key value)
116   (set! key (symbol->string key))
117   (if (not (equal? "-" file-name))
118       (set! file-name (string-append file-name "." key)))
119   (ly:message (_ "Writing header field `~a' to `~a'...")
120               key
121               (if (equal? "-" file-name) "<stdout>" file-name))
122   (if (equal? file-name "-")
123       (display value)
124       (display value (open-file file-name "w")))
125   (ly:progress "\n")
126   "")
127
128 (define-public (output-scopes scopes fields basename)
129   (define (output-scope scope)
130     (apply
131      string-append
132      (module-map
133       (lambda (sym var)
134         (let ((val (if (variable-bound? var) (variable-ref var) "")))
135           (if (and (memq sym fields) (string? val))
136               (header-to-file basename sym val))
137           ""))
138       scope)))
139   (apply string-append (map output-scope scopes)))
140