]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Only use color in png if necessary.
[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   ;; string routines barf when strlen() != string-length,.
52   ;; which may happen as side effect of read-string!/partial.
53   ;;  (gulp-port (open-file nm "r") len))
54   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
55
56 (define BOUNDING-BOX-RE
57   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
58
59 (define (unused-found-broken-get-bbox file-name)
60   (let* ((bbox (string-append file-name ".bbox"))
61          ;; -sOutputFile does not work with bbox?
62          (cmd (format #t "gs\
63  -sDEVICE=bbox\
64  -q\
65  -dNOPAUSE\
66  ~S\
67  -c showpage\
68  -c quit 2>~S"
69                       file-name bbox))
70          (status (system cmd))
71          (s (gulp-file bbox 10240))
72          (m (string-match BOUNDING_BOX_RE s)))
73
74     (if m
75         (list->vector
76          (map (lambda (x) (string->number (car x))) (vector->list m)))
77         #f)))
78
79
80 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
81 (define (my-system be-verbose exit-on-error cmd)
82   (define status 0)
83   (if be-verbose
84       (begin
85         (format (current-error-port) (_ "Invoking `~a'...") cmd)
86         (newline (current-error-port))))
87   (set! status (system cmd))
88   (if (not (= status 0))
89       (begin
90         (format (current-error-port)
91                 (format #f (_ "~a exited with status: ~S") "GS" status))
92         (if exit-on-error (exit 1))))
93   status)
94
95 (define (scale-down-image be-verbose factor file)
96   (let* ((status 0)
97          (percentage (* 100 (/ 1.0 factor)))
98          (old (string-append file ".old")))
99
100     (rename-file file old)
101     (my-system
102      be-verbose #t
103      (format #f "convert -scale \"~a%\" -depth 8 ~a ~a" percentage old file))
104     (delete-file old)))
105
106 (define-public (ps-page-count ps-name)
107   (let* ((byte-count 10240)
108          (header (gulp-file ps-name byte-count))
109          (first-null (string-index header #\nul))
110          (match (string-match "%%Pages: ([0-9]+)"
111                               (if (number? first-null)
112                                   (substring header 0 first-null)
113                                   header))))
114     (if match (string->number (match:substring match 1)) 0)))
115
116 (define-public (ps-has-color ps-name)
117   (string-contains (gulp-file ps-name) " setrgbcolor"))
118
119 (define-public (make-ps-images ps-name . rest)
120   (let-keywords*
121    rest #f
122    ((resolution 90)
123     (page-width  100)
124     (page-height 100)
125     (rename-page-1 #f)
126     (be-verbose #f)
127     (pixmap-format 'png16m)
128     (anti-alias-factor 1))
129
130    (let* ((format-str (format "~a" pixmap-format))
131           (extension (cond
132                       ((string-contains format-str "png") "png")
133                       ((string-contains format-str "jpg") "jpeg")
134                       ((string-contains format-str "jpeg") "jpeg")
135                       (else
136                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
137           (base (basename (re-sub "[.]e?ps" "" ps-name)))
138           (png1 (format "~a.~a" base extension))
139           (pngn (format  "~a-page%d.~a" base extension))
140           (page-count (ps-page-count ps-name))
141           (multi-page? (> page-count 1))
142           (output-file (if multi-page? pngn png1))
143
144           (gs-variable-options
145            (if multi-page?
146                (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
147                        page-width page-height)
148                "-dEPSCrop"))
149           (cmd (format #f "~a\
150  ~a\
151  ~a\
152  -dGraphicsAlphaBits=4\
153  -dTextAlphaBits=4\
154  -dNOPAUSE\
155  -sDEVICE=~a\
156  -sOutputFile=~S\
157  -r~S\
158  ~S\
159  -c quit"
160                        (search-gs)
161                        (if be-verbose "" "-q")
162                        gs-variable-options
163                        pixmap-format
164                        output-file 
165                        (* anti-alias-factor resolution) ps-name))
166           (status 0)
167           (files '()))
168
169      ;; The wrapper on windows cannot handle `=' signs,
170      ;; gs has a workaround with #.
171      (if (eq? PLATFORM 'windows)
172          (begin
173            (set! cmd (re-sub "=" "#" cmd))
174            (set! cmd (re-sub "-dSAFER " "" cmd))))
175
176      (set! status (my-system be-verbose #f cmd))
177
178      (set! files
179            (if multi-page?
180                (map
181                 (lambda (n)
182                   (format "~a-page~a.png" base (1+ n)))
183                 (iota page-count))
184                (list (format "~a.png" base))))
185      
186      (if (not (= 0 status))
187          (begin
188            (map delete-file files)
189            (exit 1)))
190
191      (if (and rename-page-1 multi-page?)
192          (begin
193            (rename-file (re-sub "%d" "1" pngn) png1)
194            (set! files
195                  (cons png1
196                        (cdr files)))
197            ))
198
199      (if (not (= 1 anti-alias-factor))
200          (for-each  (lambda (f) (scale-down-image be-verbose anti-alias-factor f))
201                     files))
202
203      files)))