]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
d76f926a90203b02d006a082d99d25f8f2190c9d
[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          page-width
133          page-height
134          (rename-page-1? #f)
135          (verbose? #f)
136          (aa-factor 1) 
137          )
138
139    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
140           (png1 (string-append base ".png"))
141           (pngn (string-append base "-page%d.png"))
142           (page-count (ps-page-count ps-name))
143           
144           (multi-page? (> page-count 1))
145           (output-file (if multi-page? pngn png1))
146
147           ;; png16m is because Lily produces color nowadays.
148           ;; can't use pngalpha device, since IE is broken.
149           ;;
150           (gs-variable-options
151            (if multi-page?
152                (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f" page-width page-height)
153                "-dEPSCrop"))
154
155           (cmd (format #f "~a\
156  ~a\
157  ~a\
158  -dGraphicsAlphaBits=4\
159  -dTextAlphaBits=4\
160  -dNOPAUSE\
161  -sDEVICE=png16m\
162  -sOutputFile=~S\
163  -r~S\
164  ~S\
165  -c quit"
166                        (search-gs)
167                        (if verbose? "" "-q")
168                        gs-variable-options
169                        output-file 
170                        (* aa-factor resolution) ps-name))
171           (status 0)
172           (files '()))
173
174      ;; The wrapper on windows cannot handle `=' signs,
175      ;; gs has a workaround with #.
176      (if (eq? PLATFORM 'windows)
177          (begin
178            (set! cmd (re-sub "=" "#" cmd))
179            (set! cmd (re-sub "-dSAFER " "" cmd))))
180
181      (set! status (my-system verbose? #f cmd))
182
183      (set! files
184            (if multi-page?
185                (map
186                 (lambda (n)
187                   (format "~a-page~a.png" base (1+ n)))
188                 (iota page-count))
189                (list (format "~a.png" base))))
190      
191      (if (not (= 0 status))
192          (begin
193            (map delete-file files)
194            (exit 1)))
195
196      (if (and rename-page-1? multi-page?)
197          (begin
198            (rename-file (re-sub "%d" "1" pngn) png1)
199            (set! files
200                  (cons png1
201                        (cdr files)))
202            ))
203
204      (if (not (= 1 aa-factor))
205          (for-each  (lambda (f) (scale-down-image verbose? aa-factor f))
206                     files))
207
208      files)))