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