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