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