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