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