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