]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[lilypond.git] / scm / ps-to-png.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2015 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 (re-sub re sub string)
35   (regexp-substitute/global #f re string 'pre sub 'post))
36
37 (define-public (gulp-file file-name . max-size)
38   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
39
40 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
41 (define (my-system be-verbose exit-on-error cmd)
42   (define status 0)
43   (ly:debug (_ "Invoking `~a'...\n") cmd)
44   (set! status (system cmd))
45   (if (not (= status 0))
46       (begin
47         (ly:error (_ "~a exited with status: ~S") "GS" status)
48         (if exit-on-error (exit 1))))
49   status)
50
51 (define (scale-down-image factor file)
52   (let* ((old (string-append file ".old"))
53          ;; Netpbm commands (pngtopnm, pnmscale, pnmtopng)
54          ;; outputs only standard output instead of a file.
55          ;; So we need pipe and redirection.
56          ;; However, ly:system can't handle them.
57          ;; Therefore, we use /bin/sh for handling them.
58          ;; FIXME: MinGW (except Cygwin) doesn't have /bin/sh.
59          (cmd
60           (list
61            "/bin/sh"
62            "-c"
63            (ly:format
64             "pngtopnm \"~a\" | pnmscale -reduce ~a | pnmtopng -compression 9 > \"~a\""
65             old factor file))))
66
67     (rename-file file old)
68     (ly:system cmd)
69     (delete-file old)))
70
71 (define-public (ps-page-count ps-name)
72   (let* ((byte-count 10240)
73          (header (gulp-file ps-name byte-count))
74          (first-null (string-index header #\nul))
75          (match (string-match "%%Pages: ([0-9]+)"
76                               (if (number? first-null)
77                                   (substring header 0 first-null)
78                                   header))))
79     (if match (string->number (match:substring match 1)) 0)))
80
81 (define-public (make-ps-images base-name tmp-name is-eps . rest)
82   (let-keywords*
83    rest #f
84    ((resolution 90)
85     (page-width  100)
86     (page-height 100)
87     (rename-page-1 #f)
88     (be-verbose (ly:get-option 'verbose))
89     (pixmap-format 'png16m)
90     (anti-alias-factor 1))
91
92    (let* ((format-str (format #f "~a" pixmap-format))
93           (extension (cond
94                       ((string-contains format-str "png") "png")
95                       ((string-contains format-str "jpg") "jpeg")
96                       ((string-contains format-str "jpeg") "jpeg")
97                       (else
98                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
99           (png1 (format #f "~a.~a" base-name extension))
100           (pngn (format #f "~a-page%d.~a" base-name extension))
101           (page-count (ps-page-count tmp-name))
102           (multi-page? (> page-count 1))
103
104           ;; Escape `%' (except `page%d') for ghostscript
105           (base-name-gs (string-join
106                          (string-split base-name #\%)
107                          "%%"))
108           (png1-gs (format #f "~a.~a" base-name-gs extension))
109           (pngn-gs (format #f "~a-page%d.~a" base-name-gs extension))
110           (output-file (if multi-page? pngn-gs png1-gs))
111
112           (*unspecified* (if #f #f))
113           (cmd
114            (remove (lambda (x) (eq? x *unspecified*))
115                    (list
116                     (search-gs)
117                     (if (ly:get-option 'verbose) *unspecified* "-q")
118                     (if (or (ly:get-option 'gs-load-fonts)
119                             (ly:get-option 'gs-load-lily-fonts)
120                             (eq? PLATFORM 'windows))
121                         "-dNOSAFER"
122                         "-dSAFER")
123
124                     (if is-eps
125                         "-dEPSCrop"
126                         (ly:format "-dDEVICEWIDTHPOINTS=~$" page-width))
127                     (if is-eps
128                         *unspecified*
129                         (ly:format "-dDEVICEHEIGHTPOINTS=~$" page-height))
130                     "-dGraphicsAlphaBits=4"
131                     "-dTextAlphaBits=4"
132                     "-dNOPAUSE"
133                     "-dBATCH"
134                     (ly:format "-sDEVICE=~a" pixmap-format)
135                     (string-append "-sOutputFile=" output-file)
136                     (ly:format "-r~a" (* anti-alias-factor resolution))
137                     (string-append "-f" tmp-name))))
138           (files '()))
139
140      (ly:system cmd)
141
142      (set! files
143            (if multi-page?
144                (map
145                 (lambda (n)
146                   (format #f "~a-page~a.png" base-name (1+ n)))
147                 (iota page-count))
148                (list (format #f "~a.png" base-name))))
149
150      (if (and rename-page-1 multi-page?)
151          (begin
152            (rename-file (re-sub "%d" "1" pngn) png1)
153            (set! files
154                  (cons png1
155                        (cdr files)))
156            ))
157
158      (if (not (= 1 anti-alias-factor))
159          (for-each
160           (lambda (f) (scale-down-image anti-alias-factor f)) files))
161      files)))