]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Issue 4417 / 3: Fix png filename handling
[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 be-verbose factor file)
52   (define (with-pbm)
53     (let* ((status 0)
54            (old (string-append file ".old")))
55
56       (rename-file file old)
57       (my-system
58        be-verbose #t
59        (format #f
60                "pngtopnm \"~a\" | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > \"~a\""
61                old factor file))
62       (delete-file old)))
63
64   (with-pbm))
65
66 (define-public (ps-page-count ps-name)
67   (let* ((byte-count 10240)
68          (header (gulp-file ps-name byte-count))
69          (first-null (string-index header #\nul))
70          (match (string-match "%%Pages: ([0-9]+)"
71                               (if (number? first-null)
72                                   (substring header 0 first-null)
73                                   header))))
74     (if match (string->number (match:substring match 1)) 0)))
75
76 (define-public (make-ps-images base-name tmp-name is-eps . rest)
77   (let-keywords*
78    rest #f
79    ((resolution 90)
80     (page-width  100)
81     (page-height 100)
82     (rename-page-1 #f)
83     (be-verbose (ly:get-option 'verbose))
84     (pixmap-format 'png16m)
85     (anti-alias-factor 1))
86
87    (let* ((format-str (format #f "~a" pixmap-format))
88           (extension (cond
89                       ((string-contains format-str "png") "png")
90                       ((string-contains format-str "jpg") "jpeg")
91                       ((string-contains format-str "jpeg") "jpeg")
92                       (else
93                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
94           (png1 (format #f "~a.~a" base-name extension))
95           (pngn (format #f "~a-page%d.~a" base-name extension))
96           (page-count (ps-page-count tmp-name))
97           (multi-page? (> page-count 1))
98
99           ;; Escape `%' (except `page%d') for ghostscript
100           (base-name-gs (string-join
101                          (string-split base-name #\%)
102                          "%%"))
103           (png1-gs (format #f "~a.~a" base-name-gs extension))
104           (pngn-gs (format #f "~a-page%d.~a" base-name-gs extension))
105           (output-file (if multi-page? pngn-gs png1-gs))
106
107           (*unspecified* (if #f #f))
108           (cmd
109            (remove (lambda (x) (eq? x *unspecified*))
110                    (list
111                     (search-gs)
112                     (if (ly:get-option 'verbose) *unspecified* "-q")
113                     (if (or (ly:get-option 'gs-load-fonts)
114                             (ly:get-option 'gs-load-lily-fonts)
115                             (eq? PLATFORM 'windows))
116                         "-dNOSAFER"
117                         "-dSAFER")
118
119                     (if is-eps
120                         "-dEPSCrop"
121                         (ly:format "-dDEVICEWIDTHPOINTS=~$" page-width))
122                     (if is-eps
123                         *unspecified*
124                         (ly:format "-dDEVICEHEIGHTPOINTS=~$" page-height))
125                     "-dGraphicsAlphaBits=4"
126                     "-dTextAlphaBits=4"
127                     "-dNOPAUSE"
128                     "-dBATCH"
129                     (ly:format "-sDEVICE=~a" pixmap-format)
130                     (string-append "-sOutputFile=" output-file)
131                     (ly:format "-r~a" (* anti-alias-factor resolution))
132                     (string-append "-f" tmp-name))))
133           (files '()))
134
135      (ly:system cmd)
136
137      (set! files
138            (if multi-page?
139                (map
140                 (lambda (n)
141                   (format #f "~a-page~a.png" base-name (1+ n)))
142                 (iota page-count))
143                (list (format #f "~a.png" base-name))))
144
145      (if (and rename-page-1 multi-page?)
146          (begin
147            (rename-file (re-sub "%d" "1" pngn) png1)
148            (set! files
149                  (cons png1
150                        (cdr files)))
151            ))
152
153      (if (not (= 1 anti-alias-factor))
154          (for-each
155           (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))
156      files)))