1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2005--2015 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
18 (define-module (scm ps-to-png))
30 (define (re-sub re sub string)
31 (regexp-substitute/global #f re string 'pre sub 'post))
33 (define-public (gulp-file file-name . max-size)
34 (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
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.
49 "pngtopnm \"~a\" | pnmscale -reduce ~a | pnmtopng -compression 9 > \"~a\""
52 (rename-file file old)
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)
64 (if match (string->number (match:substring match 1)) 0)))
66 (define-public (make-ps-images base-name tmp-name is-eps . rest)
73 (be-verbose (ly:get-option 'verbose))
74 (pixmap-format 'png16m)
75 (anti-alias-factor 1))
77 (let* ((format-str (format #f "~a" pixmap-format))
79 ((string-contains format-str "png") "png")
80 ((string-contains format-str "jpg") "jpeg")
81 ((string-contains format-str "jpeg") "jpeg")
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))
89 ;; Escape `%' (except `page%d') for ghostscript
90 (base-name-gs (string-join
91 (string-split base-name #\%)
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))
97 (*unspecified* (if #f #f))
99 (remove (lambda (x) (eq? x *unspecified*))
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))
111 (ly:format "-dDEVICEWIDTHPOINTS=~$" page-width))
114 (ly:format "-dDEVICEHEIGHTPOINTS=~$" page-height))
115 "-dGraphicsAlphaBits=4"
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))))
131 (format #f "~a-page~a.png" base-name (1+ n)))
133 (list (format #f "~a.png" base-name))))
135 (if (and rename-page-1 multi-page?)
137 (rename-file (re-sub "%d" "1" pngn) png1)
143 (if (not (= 1 anti-alias-factor))
145 (lambda (f) (scale-down-image anti-alias-factor f)) files))