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