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