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