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