]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
* lily/tie-column.cc (new_directions): put Tie down on center
[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 ;;; ARGH - cuases memory usage to explode with GUILE cvs.  
60
61 (define BOUNDING-BOX-RE
62   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
63
64 (define (get-bbox file-name)
65   (let* ((bbox (string-append file-name ".bbox"))
66          ;; -sOutputFile does not work with bbox?
67          (cmd (format #t "gs\
68  -sDEVICE=bbox\
69  -q\
70  -dNOPAUSE\
71  ~S\
72  -c showpage\
73  -c quit 2>~S"
74                           file-name bbox))
75          (status (system cmd))
76          (s (gulp-file d bbox 10240))
77          (m (string-match BOUNDING_BOX_RE s)))
78
79     (if m
80         (list->vector
81          (map (lambda (x) (string->number (car x))) (vector->list m)))
82         #f)))
83
84
85 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
86 (define (my-system be-verbose exit-on-error cmd)
87   (define status 0)
88   (if be-verbose
89       (begin
90         (format (current-error-port) (_ "Invoking `~a'...") cmd)
91         (newline (current-error-port))))
92
93   
94   (set! status (system cmd))
95   
96   (if (not (= status 0))
97       (begin
98         (format (current-error-port)
99                 (format #f (_ "~a exited with status: ~S") "GS" status))
100         (if exit-on-error
101             (exit 1))))
102
103   status)
104
105 (define (scale-down-image be-verbose factor file)
106   (let* ((status 0)
107          (percentage (* 100 (/ 1.0 factor)))
108          (old (string-append file ".old")))
109
110   (rename-file file old)
111   (my-system be-verbose
112              #t
113              (format #f "convert -scale \"~a%\" ~a ~a" percentage old file))
114   (delete-file old)
115   ))
116
117
118 (define-public (ps-page-count ps-name)
119   (let*
120       ((header (gulp-file ps-name 10240))
121        (match (string-match "%%Pages: ([0-9]+)" header))
122        (count (if match
123                   (string->number (match:substring match 1))
124                   0)))
125     count))
126
127 (define-public (make-ps-images ps-name . rest)
128   (let-optional
129    rest ((resolution 90)
130          (paper-size "a4")
131          (rename-page-1? #f)
132          (verbose? #f)
133          (aa-factor 1) 
134          )
135    
136    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
137           (header (gulp-file ps-name 10240))
138           (png1 (string-append base ".png"))
139           (pngn (string-append base "-page%d.png"))
140           (page-count (ps-page-count ps-name))
141           
142           (multi-page? (> page-count 1))
143           (output-file (if multi-page? pngn png1))
144
145           ;;png16m is because Lily produces color nowadays.
146           (gs-variable-options
147             (if multi-page?
148                 (format #f "-sPAPERSIZE=~a" paper-size)
149                 "-dEPSCrop"))
150           (cmd (format #f "~a\
151  ~a\
152  ~a\
153  -dGraphicsAlphaBits=4\
154  -dTextAlphaBits=4\
155  -dNOPAUSE\
156  -sDEVICE=pngalpha\
157  -sOutputFile=~S\
158  -r~S\
159  ~S\
160  -c quit"
161                            (search-gs)
162                            (if verbose? "" "-q")
163                            gs-variable-options
164                            output-file 
165                            (* aa-factor resolution) ps-name))
166           (status 0)
167           (files '()))
168
169      ;; The wrapper on windows cannot handle `=' signs,
170      ;; gs has a workaround with #.
171      (if (eq? PLATFORM 'windows)
172          (begin
173            (set! cmd (re-sub "=" "#" cmd))
174            (set! cmd (re-sub "-dSAFER " "" cmd))))
175
176      (set! status (my-system verbose? #f cmd))
177
178      (set! files
179            (if multi-page?
180                (map
181                 (lambda (n)
182                   (format "~a-page~a.png" base (1+ n)))
183                 (iota page-count))
184                (list (format "~a.png" base))))
185      
186      (if (not (= 0 status))
187          (begin
188            (map delete-file files)
189            (exit 1)))
190
191      (if (and rename-page-1? multi-page?)
192          (begin
193            (rename-file (re-sub "%d" "1" pngn) png1)
194            (set! files
195                  (cons png1
196                        (cdr files)))
197            ))
198
199      (if (not (= 1 aa-factor))
200          (for-each  (lambda (f) (scale-down-image verbose? aa-factor f))
201                     files))
202                             
203      files)))