]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
Remove tex and texstr backends (part 1).
[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--2008 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
9 ;; backend helpers.
10
11 (define-public (ly:system command . rest)
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
22           (if (pair? rest)
23               (system-with-env silenced (car rest))
24               (system silenced)))
25         
26     (if (> status 0)
27         (begin
28           (ly:message (_ "`~a' failed (~a)") command status)
29           (ly:progress "\n")
30           ;; hmmm.  what's the best failure option? 
31           (throw 'ly-file-failed)))))
32
33 (define-public (system-with-env cmd env)
34
35   "Execute CMD in fork, with ENV (a list of strings) as the environment"
36   (let*
37       ;; laziness: should use execle?
38       
39       ((pid (primitive-fork)))
40     (if (= 0 pid)
41         ;; child
42         (begin
43           (environ env)
44           (system cmd))
45         
46         ;; parent
47         (cdr (waitpid pid)))))
48
49 (define-public (sanitize-command-option str)
50   "Kill dubious shell quoting"
51   
52   (string-append
53    "\""
54    (regexp-substitute/global #f "[^-_ 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
55    "\""))
56
57 (define-public (search-executable names)
58   (define (helper path lst)
59     (if (null? (cdr lst))
60         (car lst)
61         (if (search-path path (car lst)) (car lst)
62             (helper path (cdr lst)))))
63
64   (let ((path (parse-path (getenv "PATH"))))
65     (helper path names)))
66
67 (define-public (search-gs)
68   
69   ;; must be sure that we don't catch stuff from old GUBs.
70   (search-executable '("gs")))
71   
72 (define-public (postscript->pdf paper-width paper-height name)
73   (let* ((pdf-name (string-append
74                     (dir-basename name ".ps" ".eps")
75                     ".pdf"))
76          (is-eps (string-match "\\.eps$" name))
77          (paper-size-string (if is-eps
78                                 " -dEPSCrop "
79                                 (ly:format "-dDEVICEWIDTHPOINTS=~$ \
80 -dDEVICEHEIGHTPOINTS=~$ "
81                                         paper-width paper-height )))
82
83          (cmd (simple-format #f
84                       "~a\
85  ~a\
86  ~a\
87  ~a\
88  -dCompatibilityLevel=1.4 \
89  -dNOPAUSE\
90  -dBATCH\
91  -r1200 \
92  -sDEVICE=pdfwrite\
93  -sOutputFile=~S\
94  -c .setpdfwrite\
95  -f ~S\
96 "
97                       (search-gs)
98                       (if (ly:get-option 'verbose) "" "-q")
99                       (if (or (ly:get-option 'gs-load-fonts)
100                               (ly:get-option 'gs-load-lily-fonts))
101                               
102                           " -dNOSAFER "
103                           " -dSAFER ")
104                       paper-size-string
105                       pdf-name
106                       name)))
107     ;; The wrapper on windows cannot handle `=' signs,
108     ;; gs has a workaround with #.
109     (if (eq? PLATFORM 'windows)
110         (begin
111           (set! cmd (string-regexp-substitute "=" "#" cmd))
112           (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))
113           (if (access? pdf-name W_OK)
114               (delete-file pdf-name))))
115
116     (ly:message (_ "Converting to `~a'...") pdf-name)
117     (ly:progress "\n")
118     (ly:system cmd)))
119
120 (use-modules (scm ps-to-png))
121
122 (define-public (postscript->png resolution paper-width paper-height name)
123   (let* ((verbose (ly:get-option 'verbose))
124          (rename-page-1 #f))
125
126     ;; Do not try to guess the name of the png file,
127     ;; GS produces PNG files like BASE-page%d.png.
128     (ly:message (_ "Converting to ~a...") "PNG")
129     (make-ps-images name
130                     #:resolution resolution
131                     #:page-width paper-width
132                     #:page-height paper-height
133                     #:rename-page-1 rename-page-1
134                     #:be-verbose verbose
135                     #:anti-alias-factor (ly:get-option 'anti-alias-factor)
136                     #:pixmap-format (ly:get-option 'pixmap-format))
137     (ly:progress "\n")))
138
139 (define-public (postprocess-output paper-book module filename formats)
140   (let* ((completed (completize-formats formats))
141          (base (string-regexp-substitute "\\.[a-z]+$" "" filename))
142          (intermediate (remove (lambda (x) (member x formats)) completed)))
143     
144     (for-each (lambda (f)
145                 ((eval (string->symbol (format "convert-to-~a" f))
146                        module) paper-book filename)) completed)
147     (if (ly:get-option 'delete-intermediate-files)
148         (for-each (lambda (f)
149                     (delete-file (string-append base "." f))) intermediate))))
150
151 (define-public (completize-formats formats)
152   (define new-fmts '())
153   (if (member "png" formats)
154       (set! formats (cons "ps" formats)))
155   (if (member "pdf" formats)
156       (set! formats (cons "ps" formats)))
157   (for-each (lambda (x)
158               (if (member x formats) (set! new-fmts (cons x new-fmts))))
159             '("ps" "pdf" "png"))
160   (uniq-list (reverse new-fmts)))
161
162 (define (header-to-file file-name key value)
163   (set! key (symbol->string key))
164   (if (not (equal? "-" file-name))
165       (set! file-name (string-append file-name "." key)))
166   (ly:message (_ "Writing header field `~a' to `~a'...")
167               key
168               (if (equal? "-" file-name) "<stdout>" file-name))
169   (if (equal? file-name "-")
170       (display value)
171       (let ((port (open-file file-name "w")))
172         (display value port)
173         (close-port port)))
174
175   (ly:progress "\n")
176   "")
177
178 (define-public (output-scopes scopes fields basename)
179   (define (output-scope scope)
180     (apply
181      string-append
182      (module-map
183       (lambda (sym var)
184         (let ((val (if (variable-bound? var) (variable-ref var) "")))
185           (if (and (memq sym fields) (string? val))
186               (header-to-file basename sym val))
187           ""))
188       scope)))
189   (apply string-append (map output-scope scopes)))
190