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