]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Merge with master
[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--2006 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  (lily)
17  )
18
19 ;; gettext wrapper for guile < 1.7.2
20 (if (defined? 'gettext)
21     (define-public _ gettext)
22     (define-public (_ x) x))
23
24 (define PLATFORM
25   (string->symbol
26    (string-downcase
27     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
28
29 (define (re-sub re sub string)
30   (regexp-substitute/global #f re string 'pre sub 'post))
31
32 (define (search-executable names)
33   (define (helper path lst)
34     (if (null? (cdr lst))
35         (car lst)
36         (if (search-path path (car lst)) (car lst)
37             (helper path (cdr lst)))))
38
39   (let ((path (parse-path (getenv "PATH"))))
40     (helper path names)))
41
42 (define (search-gs)
43   (search-executable '("gs-nox" "gs-8.15" "gs")))
44
45 (define (gulp-port port max-length)
46   (let ((str (make-string max-length)))
47     (read-string!/partial str port 0 max-length)
48     str))
49
50 (define-public (gulp-file file-name . max-size)
51   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
52
53 (define BOUNDING-BOX-RE
54   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
55
56 (define (unused-found-broken-get-bbox file-name)
57   (let* ((bbox (string-append file-name ".bbox"))
58          ;; -sOutputFile does not work with bbox?
59          (cmd (format #t "gs\
60  -sDEVICE=bbox\
61  -q\
62  -dNOPAUSE\
63  ~S\
64  -c showpage\
65  -c quit 2>~S"
66                       file-name bbox))
67          (status (system cmd))
68          (s (gulp-file bbox 10240))
69          (m (string-match BOUNDING_BOX_RE s)))
70
71     (if m
72         (list->vector
73          (map (lambda (x) (string->number (car x))) (vector->list m)))
74         #f)))
75
76
77 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
78 (define (my-system be-verbose exit-on-error cmd)
79   (define status 0)
80   (if be-verbose
81       (begin
82         (format (current-error-port) (_ "Invoking `~a'...") cmd)
83         (newline (current-error-port))))
84   (set! status (system cmd))
85   (if (not (= status 0))
86       (begin
87         (format (current-error-port)
88                 (format #f (_ "~a exited with status: ~S") "GS" status))
89         (if exit-on-error (exit 1))))
90   status)
91
92 (define (scale-down-image be-verbose factor file)
93   (define (with-pbm)
94     (let* ((status 0)
95            (old (string-append file ".old")))
96       
97       (rename-file file old)
98       (my-system
99        be-verbose #t
100        (format #f
101                "pngtopnm ~a | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > ~a"
102                old factor file))
103       (delete-file old)))
104
105   (with-pbm))
106
107 (define-public (ps-page-count ps-name)
108   (let* ((byte-count 10240)
109          (header (gulp-file ps-name byte-count))
110          (first-null (string-index header #\nul))
111          (match (string-match "%%Pages: ([0-9]+)"
112                               (if (number? first-null)
113                                   (substring header 0 first-null)
114                                   header))))
115     (if match (string->number (match:substring match 1)) 0)))
116
117 (define-public (make-ps-images ps-name . rest)
118   (let-keywords*
119    rest #f
120    ((resolution 90)
121     (page-width  100)
122     (page-height 100)
123     (rename-page-1 #f)
124     (be-verbose #f)
125     (pixmap-format 'png16m)
126     (anti-alias-factor 1))
127
128    (let* ((format-str (format "~a" pixmap-format))
129           (extension (cond
130                       ((string-contains format-str "png") "png")
131                       ((string-contains format-str "jpg") "jpeg")
132                       ((string-contains format-str "jpeg") "jpeg")
133                       (else
134                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
135           (base (basename (re-sub "[.]e?ps" "" ps-name)))
136           (png1 (format "~a.~a" base extension))
137           (pngn (format  "~a-page%d.~a" base extension))
138           (page-count (ps-page-count ps-name))
139           (multi-page? (> page-count 1))
140           (output-file (if multi-page? pngn png1))
141
142           (gs-variable-options
143            (if multi-page?
144                (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
145                        page-width page-height)
146                "-dEPSCrop"))
147           (cmd (format #f "~a\
148  ~a\
149  ~a\
150  -dGraphicsAlphaBits=4\
151  -dTextAlphaBits=4\
152  -dNOPAUSE\
153  -sDEVICE=~a\
154  -sOutputFile=~S\
155  -r~S\
156  ~S\
157  -c quit"
158                        (search-gs)
159                        (if be-verbose "" "-q")
160                        gs-variable-options
161                        pixmap-format
162                        output-file 
163                        (* anti-alias-factor resolution) ps-name))
164           (status 0)
165           (files '()))
166
167      ;; The wrapper on windows cannot handle `=' signs,
168      ;; gs has a workaround with #.
169      (if (eq? PLATFORM 'windows)
170          (begin
171            (set! cmd (re-sub "=" "#" cmd))
172            (set! cmd (re-sub "-dSAFER " "" cmd))))
173
174      (set! status (my-system be-verbose #f cmd))
175
176      (set! files
177            (if multi-page?
178                (map
179                 (lambda (n)
180                   (format "~a-page~a.png" base (1+ n)))
181                 (iota page-count))
182                (list (format "~a.png" base))))
183      
184      (if (not (= 0 status))
185          (begin
186            (map delete-file files)
187            (exit 1)))
188
189      (if (and rename-page-1 multi-page?)
190          (begin
191            (rename-file (re-sub "%d" "1" pngn) png1)
192            (set! files
193                  (cons png1
194                        (cdr files)))
195            ))
196
197      (if (not (= 1 anti-alias-factor))
198          (for-each
199           (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))
200      files)))