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