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