]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
* scm/backend-library.scm (postscript->pdf):
[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 (define-public (make-ps-images ps-name . rest)
82   (let-optional
83    rest ((resolution 90)
84          (paper-size "a4")
85          (rename-page-1? #f)
86          (verbose? #f))
87    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
88           (header (gulp-port (open-file ps-name "r") 10240))
89           (png1 (string-append base ".png"))
90           (pngn (string-append base "-page%d.png"))
91           (pngn-re (re-sub "%d" "[0-9]*" pngn))
92           (multi-page? (and (string-match "\n%%Pages: " header)
93                             (not (string-match "\n%%Pages: 1\n" header))))
94           (output-file (if multi-page? pngn png1))
95
96           ;;png16m is because Lily produces color nowadays.
97           (cmd (if multi-page?
98                    (format #f "~a\
99  ~a\
100  -dGraphicsAlphaBits=4\
101  -dNOPAUSE\
102  -dTextAlphaBits=4\
103  -sDEVICE=png16m\
104  -sOutputFile='~a'\
105  -sPAPERSIZE=~a\
106  -r~S\
107  '~a'\
108  -c showpage\
109  -c quit" output-file paper-size resolution ps-name)
110                    (format #f "~a\
111  ~a\
112  -s\
113  -dGraphicsAlphaBits=4\
114  -dEPSCrop\
115  -dNOPAUSE\
116  -dTextAlphaBits=4\
117  -sDEVICE=png16m\
118  -sOutputFile='~a'\
119  -r~S\
120  '~a'\
121  -c quit"
122                            (search-gs)
123                            (if verbose? "" "-q")
124                            output-file resolution ps-name)))
125           (foo (for-each delete-file (append (dir-re "." png1)
126                                              (dir-re "." pngn-re))))
127           (bar (if verbose?
128                    (begin
129                      (format (current-error-port) (_ "Invoking `~a'...") cmd)
130                      (newline (current-error-port)))))
131           (baz 
132            ;; The wrapper on windows cannot handle `=' signs,
133            ;; gs has a workaround with #.
134            (if (eq? PLATFORM 'windows)
135                (begin
136                  (set! cmd (re-sub "=" "#" cmd))
137                  (set! cmd (re-sub "-dSAFER " "" cmd)))))
138           (status (system cmd)))
139      (if (not (= status 0))
140          (begin
141            (map delete-file
142                 (append (dir-re "." png1) (dir-re "." pngn-re)))
143            (format (current-error-port)
144                    (format #f (_ "~a exited with status: ~S") "GS" status))
145            (exit 1)))
146      (if (and rename-page-1? multi-page?)
147          (rename (re-sub "%d" "1" pngn) png1))
148      (append (dir-re "." png1) (dir-re "." pngn-re)))))