]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
(make-ps-images): Bugfix: `Pages: 1\n' is not
[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
15 ;; gettext wrapper for guile < 1.7.2
16 (if (defined? 'gettext)
17     (define-public _ gettext)
18     (define-public (_ x) x))
19
20 (define (re-sub re sub string)
21   (regexp-substitute/global #f re string 'pre sub 'post))
22
23 (define (gulp-port port max-length)
24   (let ((str (make-string max-length)))
25     (read-string!/partial str port)
26     str))
27
28 (define (dir-listing dir-name)
29   (define (dir-helper dir lst)
30     (let ((e (readdir dir)))
31       (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
32   (reverse (dir-helper (opendir dir-name) '())))
33
34 (define (dir-re dir re)
35   (filter (lambda (x) (string-match re x)) (dir-listing dir)))
36
37 (define BOUNDING-BOX-RE
38   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
39
40 (define (get-bbox file-name)
41   (let* ((bbox (string-append file-name ".bbox"))
42          ;; -sOutputFile does not work with bbox?
43          (cmd (format #t "gs\
44  -sDEVICE=bbox\
45  -q\
46  -dNOPAUSE\
47  ~S\
48  -c showpage\
49  -c quit 2>~S"
50                           file-name bbox))
51          (status (system cmd))
52          (s (gulp-port (open-file bbox "r") 10240))
53          (m (string-match BOUNDING_BOX_RE s)))
54     (display m)
55     (newline)
56     (if m
57         (list->vector
58          (map (lambda (x) (string->number (car x))) (vector->list m)))
59         #f)))
60
61 (define-public (make-ps-images ps-name . rest)
62   (let-optional
63    rest ((resolution 90)
64          (paper-size "a4")
65          (rename-page-1? #f)
66          (verbose? #f))
67    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
68           (header (gulp-port (open-file ps-name "r") 10240))
69           (png1 (string-append base ".png"))
70           (pngn (string-append base "-page%d.png"))
71           (pngn-re (re-sub "%d" "[0-9]*" pngn))
72           (multi-page? (and (string-match "\n%%Pages: " header)
73                             (not (string-match "\n%%Pages: 1\n" header))))
74           (output-file (if multi-page? pngn png1))
75           ;;png16m is because Lily produces color nowadays.
76           (cmd (format #f (if multi-page?
77                               "gs\
78  -dEPSCrop\
79  -dGraphicsAlphaBits=4\
80  -dNOPAUSE\
81  -dTextAlphaBits=4\
82  -sDEVICE=png16m\
83  -sOutputFile='~a'\
84  -sPAPERSIZE=~a\
85  -q\
86  -r~S\
87  '~a'\
88  -c showpage\
89  -c quit"
90                               "gs\
91  -s\
92  -dGraphicsAlphaBits=4\
93  -dNOPAUSE\
94  -dTextAlphaBits=4\
95  -sDEVICE=png16m\
96  -sOutputFile='~a'\
97  -sPAPERSIZE=~a\
98  -q\
99  -r~S\
100  '~a'\
101  -c quit")
102                        output-file paper-size resolution ps-name))
103           (foo (for-each delete-file (append (dir-re "." png1)
104                                              (dir-re "." pngn-re))))
105           (bar (if verbose?
106                    (begin
107                      (format (current-error-port) (_ "Invoking `~a'...") cmd)
108                      (newline (current-error-port)))))
109           (status (system cmd)))
110      (if (not (= status 0))
111          (begin
112            (map delete-file
113                 (append (dir-re "." png1) (dir-re "." pngn-re)))
114            (format (current-error-port)
115                    (format #f (_ "~a exited with status: ~S") "GS" status))
116            (exit 1)))
117      (if (and rename-page-1? multi-page?)
118          (rename (re-sub "%d" "1" pngn) png1))
119      (append (dir-re "." png1) (dir-re "." pngn-re)))))