]> 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
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
76           ;;png16m is because Lily produces color nowadays.
77           (cmd (if multi-page?
78                    (format #f "gs\
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" output-file paper-size resolution ps-name)
90                    (format #f "gs\
91  -s\
92  -dGraphicsAlphaBits=4\
93  -dEPSCrop\
94  -dNOPAUSE\
95  -dTextAlphaBits=4\
96  -sDEVICE=png16m\
97  -sOutputFile='~a'\
98  -q\
99  -r~S\
100  '~a'\
101  -c quit" output-file resolution ps-name)))
102           (foo (for-each delete-file (append (dir-re "." png1)
103                                              (dir-re "." pngn-re))))
104           (bar (if verbose?
105                    (begin
106                      (format (current-error-port) (_ "Invoking `~a'...") cmd)
107                      (newline (current-error-port)))))
108           (status (system cmd)))
109      (if (not (= status 0))
110          (begin
111            (map delete-file
112                 (append (dir-re "." png1) (dir-re "." pngn-re)))
113            (format (current-error-port)
114                    (format #f (_ "~a exited with status: ~S") "GS" status))
115            (exit 1)))
116      (if (and rename-page-1? multi-page?)
117          (rename (re-sub "%d" "1" pngn) png1))
118      (append (dir-re "." png1) (dir-re "." pngn-re)))))