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