]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Fix #1186.
[lilypond.git] / scm / ps-to-png.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2010 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 (define-module (scm ps-to-png))
19
20 (use-modules
21  (ice-9 optargs)
22  (ice-9 regex)
23  (ice-9 rw)
24  (srfi srfi-1)
25  (srfi srfi-13)
26  (srfi srfi-14)
27  (lily)
28  )
29
30 (define-public _ gettext)
31
32 (define PLATFORM
33   (string->symbol
34    (string-downcase
35     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
36
37 (define (re-sub re sub string)
38   (regexp-substitute/global #f re string 'pre sub 'post))
39
40 (define (search-executable names)
41   (define (helper path lst)
42     (if (null? (cdr lst))
43         (car lst)
44         (if (search-path path (car lst)) (car lst)
45             (helper path (cdr lst)))))
46
47   (let ((path (parse-path (getenv "PATH"))))
48     (helper path names)))
49
50 (define (search-gs)
51   (search-executable '("gs-nox" "gs-8.15" "gs")))
52
53 (define (gulp-port port max-length)
54   (let ((str (make-string max-length)))
55     (read-string!/partial str port 0 max-length)
56     str))
57
58 (define-public (gulp-file file-name . max-size)
59   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
60
61 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
62 (define (my-system be-verbose exit-on-error cmd)
63   (define status 0)
64   (if be-verbose
65       (begin
66         (format (current-error-port) (_ "Invoking `~a'...") cmd)
67         (newline (current-error-port))))
68   (set! status (system cmd))
69   (if (not (= status 0))
70       (begin
71         (format (current-error-port)
72                 (format #f (_ "~a exited with status: ~S") "GS" status))
73         (if exit-on-error (exit 1))))
74   status)
75
76 (define (scale-down-image be-verbose factor file)
77   (define (with-pbm)
78     (let* ((status 0)
79            (old (string-append file ".old")))
80       
81       (rename-file file old)
82       (my-system
83        be-verbose #t
84        (format #f
85                "pngtopnm ~a | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > ~a"
86                old factor file))
87       (delete-file old)))
88
89   (with-pbm))
90
91 (define-public (ps-page-count ps-name)
92   (let* ((byte-count 10240)
93          (header (gulp-file ps-name byte-count))
94          (first-null (string-index header #\nul))
95          (match (string-match "%%Pages: ([0-9]+)"
96                               (if (number? first-null)
97                                   (substring header 0 first-null)
98                                   header))))
99     (if match (string->number (match:substring match 1)) 0)))
100
101 (define-public (make-ps-images ps-name . rest)
102   (let-keywords*
103    rest #f
104    ((resolution 90)
105     (page-width  100)
106     (page-height 100)
107     (rename-page-1 #f)
108     (be-verbose (ly:get-option 'verbose))
109     (pixmap-format 'png16m)
110     (anti-alias-factor 1))
111
112    (let* ((format-str (format "~a" pixmap-format))
113           (extension (cond
114                       ((string-contains format-str "png") "png")
115                       ((string-contains format-str "jpg") "jpeg")
116                       ((string-contains format-str "jpeg") "jpeg")
117                       (else
118                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
119           (base (dir-basename ps-name ".ps" ".eps"))
120           (png1 (format "~a.~a" base extension))
121           (pngn (format  "~a-page%d.~a" base extension))
122           (page-count (ps-page-count ps-name))
123           (multi-page? (> page-count 1))
124           (output-file (if multi-page? pngn png1))
125
126           (gs-variable-options
127             (if (string-suffix-ci? ".eps" ps-name)
128                 "-dEPSCrop"
129                 (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
130                         page-width page-height)))
131           (cmd (ly:format "~a\
132  ~a\
133  ~a\
134  -dGraphicsAlphaBits=4\
135  -dTextAlphaBits=4\
136  -dNOPAUSE\
137  -sDEVICE=~a\
138  -sOutputFile=~S\
139  -r~a\
140  ~S\
141  -c quit"
142                        (search-gs)
143                        (if be-verbose "" "-q")
144                        gs-variable-options
145                        pixmap-format
146                        output-file 
147                        (* anti-alias-factor resolution) ps-name))
148           (status 0)
149           (files '()))
150
151      ;; The wrapper on windows cannot handle `=' signs,
152      ;; gs has a workaround with #.
153      (if (eq? PLATFORM 'windows)
154          (begin
155            (set! cmd (re-sub "=" "#" cmd))
156            (set! cmd (re-sub "-dSAFER " "" cmd))))
157
158      (set! status (my-system be-verbose #f cmd))
159
160      (set! files
161            (if multi-page?
162                (map
163                 (lambda (n)
164                   (format "~a-page~a.png" base (1+ n)))
165                 (iota page-count))
166                (list (format "~a.png" base))))
167      
168      (if (not (= 0 status))
169          (begin
170            (map delete-file files)
171            (exit 1)))
172
173      (if (and rename-page-1 multi-page?)
174          (begin
175            (rename-file (re-sub "%d" "1" pngn) png1)
176            (set! files
177                  (cons png1
178                        (cdr files)))
179            ))
180
181      (if (not (= 1 anti-alias-factor))
182          (for-each
183           (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))
184      files)))