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