]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006 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   "Kill dubious shell quoting"
31   
32   (string-append
33    "\""
34    (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
35    "\""))
36
37 (define-public (search-executable names)
38   (define (helper path lst)
39     (if (null? (cdr lst))
40         (car lst)
41         (if (search-path path (car lst)) (car lst)
42             (helper path (cdr lst)))))
43
44   (let ((path (parse-path (getenv "PATH"))))
45     (helper path names)))
46
47 (define-public (search-gs)
48   
49   ;; must be sure that we don't catch stuff from old GUBs.
50   (search-executable '("gs")))
51
52 (define-public (postscript->pdf papersizename name)
53   (let* ((pdf-name (string-append (basename name ".ps") ".pdf"))
54          (cmd (format #f
55                       "~a\
56  ~a\
57  ~a\
58  -dCompatibilityLevel=1.4 \
59  -sPAPERSIZE=~a\
60  -dNOPAUSE\
61  -dBATCH\
62  -r1200 \
63  -sDEVICE=pdfwrite\
64  -sOutputFile=~S\
65  -c .setpdfwrite\
66  -f ~S\
67 "
68                       (search-gs)
69                       (if (ly:get-option 'verbose) "" "-q")
70                       (if (ly:get-option 'gs-font-load)
71                           " -dNOSAFER "
72                           " -dSAFER ")
73                       (sanitize-command-option papersizename)
74                       pdf-name
75                       name)))
76     ;; The wrapper on windows cannot handle `=' signs,
77     ;; gs has a workaround with #.
78     (if (eq? PLATFORM 'windows)
79         (begin
80           (set! cmd (string-regexp-substitute "=" "#" cmd))
81           (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
82
83     (if (access? pdf-name W_OK)
84         (delete-file pdf-name))
85
86     (ly:message (_ "Converting to `~a'...") pdf-name)
87     (ly:progress "\n")
88     (ly:system cmd)
89     ))
90
91 (use-modules (scm ps-to-png))
92
93 (define-public (postscript->png resolution paper-size-name name)
94     ;; Do not try to guess the name of the png file,
95     ;; GS produces PNG files like BASE-page%d.png.
96     ;;(ly:message (_ "Converting to `~a'...")
97     ;;      (string-append (basename name ".ps") "-page1.png" )))
98   (let ((paper-size (sanitize-command-option paper-size-name))
99         (verbose (ly:get-option 'verbose))
100         (rename-page-1 #f))
101
102     (ly:message (_ "Converting to ~a...") "PNG")
103     (make-ps-images name resolution paper-size rename-page-1 verbose
104                     (ly:get-option 'anti-alias-factor))
105     (ly:progress "\n")))
106
107 (define-public (postprocess-output paper-book module filename formats)
108   (let*
109       ((completed (completize-formats formats))
110        (base (string-regexp-substitute "\\.[a-z]+$" "" filename))
111        (intermediate (remove
112                       (lambda (x)
113                         (member x formats)) 
114                       completed)))
115     (for-each
116      (lambda (f)
117        ((eval (string->symbol (string-append "convert-to-" f)) module)
118         paper-book filename))
119      completed)
120
121     (if (ly:get-option 'delete-intermediate-files)
122         (for-each
123          (lambda (f)
124            (delete-file (string-append base "." f)))
125          intermediate))
126     ))
127
128 (define-public (completize-formats formats)
129   (define new-fmts '())
130
131   (if (member "png" formats)
132       (set! formats (cons "ps" formats)))
133   (if (member "pdf" formats)
134       (set! formats (cons "ps" formats)))
135
136   (for-each
137    (lambda (x)
138      (if (member x formats) (set! new-fmts (cons x new-fmts))))
139    '("tex" "dvi" "ps" "pdf" "png"))
140
141   (uniq-list (reverse new-fmts)))
142
143 (define (header-to-file file-name key value)
144   (set! key (symbol->string key))
145   (if (not (equal? "-" file-name))
146       (set! file-name (string-append file-name "." key)))
147   (ly:message (_ "Writing header field `~a' to `~a'...")
148               key
149               (if (equal? "-" file-name) "<stdout>" file-name))
150   (if (equal? file-name "-")
151       (display value)
152       (display value (open-file file-name "w")))
153   (ly:progress "\n")
154   "")
155
156 (define-public (output-scopes scopes fields basename)
157   (define (output-scope scope)
158     (apply
159      string-append
160      (module-map
161       (lambda (sym var)
162         (let ((val (if (variable-bound? var) (variable-ref var) "")))
163           (if (and (memq sym fields) (string? val))
164               (header-to-file basename sym val))
165           ""))
166       scope)))
167   (apply string-append (map output-scope scopes)))
168