]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / scm / ps-to-png.scm
1 ;;;; ps-to-png.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2005--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 (define-module (scm ps-to-png))
8
9 (use-modules
10  (ice-9 optargs)
11  (ice-9 regex)
12  (ice-9 rw)
13  (srfi srfi-1)
14  (srfi srfi-13)
15  (srfi srfi-14)
16  (lily)
17  )
18
19 ;; gettext wrapper for guile < 1.7.2
20 (if (defined? 'gettext)
21     (define-public _ gettext)
22     (define-public (_ x) x))
23
24 (define PLATFORM
25   (string->symbol
26    (string-downcase
27     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
28
29 (define (re-sub re sub string)
30   (regexp-substitute/global #f re string 'pre sub 'post))
31
32 (define (search-executable names)
33   (define (helper path lst)
34     (if (null? (cdr lst))
35         (car lst)
36         (if (search-path path (car lst)) (car lst)
37             (helper path (cdr lst)))))
38
39   (let ((path (parse-path (getenv "PATH"))))
40     (helper path names)))
41
42 (define (search-gs)
43   (search-executable '("gs-nox" "gs-8.15" "gs")))
44
45 (define (gulp-port port max-length)
46   (let ((str (make-string max-length)))
47     (read-string!/partial str port 0 max-length)
48     str))
49
50 (define (gulp-file nm len)
51
52   ;; string routines barf when strlen() != string-length,.
53   ;; which may happen as side effect of read-string!/partial.
54   
55                                         ;  (gulp-port (open-file nm "r") len))
56   (ly:gulp-file nm len))
57
58
59 (define BOUNDING-BOX-RE
60   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
61
62 (define (get-bbox file-name)
63   (let* ((bbox (string-append file-name ".bbox"))
64          ;; -sOutputFile does not work with bbox?
65          (cmd (format #t "gs\
66  -sDEVICE=bbox\
67  -q\
68  -dNOPAUSE\
69  ~S\
70  -c showpage\
71  -c quit 2>~S"
72                       file-name bbox))
73          (status (system cmd))
74          (s (gulp-file d bbox 10240))
75          (m (string-match BOUNDING_BOX_RE s)))
76
77     (if m
78         (list->vector
79          (map (lambda (x) (string->number (car x))) (vector->list m)))
80         #f)))
81
82
83 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
84 (define (my-system be-verbose exit-on-error cmd)
85   (define status 0)
86   (if be-verbose
87       (begin
88         (format (current-error-port) (_ "Invoking `~a'...") cmd)
89         (newline (current-error-port))))
90
91   
92   (set! status (system cmd))
93   
94   (if (not (= status 0))
95       (begin
96         (format (current-error-port)
97                 (format #f (_ "~a exited with status: ~S") "GS" status))
98         (if exit-on-error
99             (exit 1))))
100
101   status)
102
103 (define (scale-down-image be-verbose factor file)
104   (let* ((status 0)
105          (percentage (* 100 (/ 1.0 factor)))
106          (old (string-append file ".old")))
107
108     (rename-file file old)
109     (my-system be-verbose
110                #t
111                (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
112     (delete-file old)
113     ))
114
115 (define-public (ps-page-count ps-name)
116   (let*
117       ((byte-count 10240)
118        (header (gulp-file ps-name byte-count))
119        (first-null (string-index header #\nul))
120        (match (string-match "%%Pages: ([0-9]+)"
121                             (if (number? first-null)
122                                 (substring header 0 first-null)
123                                 header))))
124
125     (if match
126         (string->number (match:substring match 1))
127         0)))
128
129 (define-public (make-ps-images ps-name . rest)
130   (let-optional
131    rest ((resolution 90)
132          (paper-size "a4")
133          (rename-page-1? #f)
134          (verbose? #f)
135          (aa-factor 1) 
136          )
137    
138    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
139           (png1 (string-append base ".png"))
140           (pngn (string-append base "-page%d.png"))
141           (page-count (ps-page-count ps-name))
142           
143           (multi-page? (> page-count 1))
144           (output-file (if multi-page? pngn png1))
145
146           ;; png16m is because Lily produces color nowadays.
147           ;; can't use pngalpha device, since IE is broken.
148           ;;
149           (gs-variable-options
150            (if multi-page?
151                (format #f "-sPAPERSIZE=~a" paper-size)
152                "-dEPSCrop"))
153
154           (cmd (format #f "~a\
155  ~a\
156  ~a\
157  -dGraphicsAlphaBits=4\
158  -dTextAlphaBits=4\
159  -dNOPAUSE\
160  -sDEVICE=png16m\
161  -sOutputFile=~S\
162  -r~S\
163  ~S\
164  -c quit"
165                        (search-gs)
166                        (if verbose? "" "-q")
167                        gs-variable-options
168                        output-file 
169                        (* aa-factor resolution) ps-name))
170           (status 0)
171           (files '()))
172
173      ;; The wrapper on windows cannot handle `=' signs,
174      ;; gs has a workaround with #.
175      (if (eq? PLATFORM 'windows)
176          (begin
177            (set! cmd (re-sub "=" "#" cmd))
178            (set! cmd (re-sub "-dSAFER " "" cmd))))
179
180      (set! status (my-system verbose? #f cmd))
181
182      (set! files
183            (if multi-page?
184                (map
185                 (lambda (n)
186                   (format "~a-page~a.png" base (1+ n)))
187                 (iota page-count))
188                (list (format "~a.png" base))))
189      
190      (if (not (= 0 status))
191          (begin
192            (map delete-file files)
193            (exit 1)))
194
195      (if (and rename-page-1? multi-page?)
196          (begin
197            (rename-file (re-sub "%d" "1" pngn) png1)
198            (set! files
199                  (cons png1
200                        (cdr files)))
201            ))
202
203      (if (not (= 1 aa-factor))
204          (for-each  (lambda (f) (scale-down-image verbose? aa-factor f))
205                     files))
206
207      files)))