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 (search-pngtopam)
38 (if (eq? PLATFORM 'windows)
39 '("pngtopam.exe" "pngtopnm.exe")
40 '("pngtopam" "pngtopnm"))))
42 (define (search-pamscale)
44 (if (eq? PLATFORM 'windows)
45 '("pamscale.exe" "pnmscale.exe")
46 '("pamscale" "pnmscale"))))
48 (define (search-pnmtopng)
50 (if (eq? PLATFORM 'windows)
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 ly:system-with-shell.
63 "~a \"~a\" | ~a -reduce ~a | ~a -compression 9 > \"~a\""
65 (search-pamscale) factor
69 (rename-file file old)
70 (ly:system-with-shell cmd)
73 (define-public (ps-page-count ps-name)
74 (let* ((byte-count 10240)
75 (header (gulp-file ps-name byte-count))
76 (first-null (string-index header #\nul))
77 (match (string-match "%%Pages: ([0-9]+)"
78 (if (number? first-null)
79 (substring header 0 first-null)
81 (if match (string->number (match:substring match 1)) 0)))
83 (define-public (make-ps-images base-name tmp-name is-eps . rest)
90 (be-verbose (ly:get-option 'verbose))
91 (pixmap-format 'png16m)
92 (anti-alias-factor 1))
94 (let* ((format-str (format #f "~a" pixmap-format))
96 ((string-contains format-str "png") "png")
97 ((string-contains format-str "jpg") "jpeg")
98 ((string-contains format-str "jpeg") "jpeg")
100 (ly:error "Unknown pixmap format ~a" pixmap-format))))
101 (png1 (format #f "~a.~a" base-name extension))
102 (pngn (format #f "~a-page%d.~a" base-name extension))
103 (page-count (ps-page-count tmp-name))
104 (multi-page? (> page-count 1))
106 ;; Escape `%' (except `page%d') for ghostscript
107 (base-name-gs (string-join
108 (string-split base-name #\%)
110 (png1-gs (format #f "~a.~a" base-name-gs extension))
111 (pngn-gs (format #f "~a-page%d.~a" base-name-gs extension))
112 (output-file (if multi-page? pngn-gs png1-gs))
114 (*unspecified* (if #f #f))
116 (remove (lambda (x) (eq? x *unspecified*))
119 (if (ly:get-option 'verbose) *unspecified* "-q")
120 (if (or (ly:get-option 'gs-load-fonts)
121 (ly:get-option 'gs-load-lily-fonts)
122 (eq? PLATFORM 'windows))
128 (ly:format "-dDEVICEWIDTHPOINTS=~$" page-width))
131 (ly:format "-dDEVICEHEIGHTPOINTS=~$" page-height))
132 "-dGraphicsAlphaBits=4"
136 (ly:format "-sDEVICE=~a" pixmap-format)
137 (string-append "-sOutputFile=" output-file)
138 (ly:format "-r~a" (* anti-alias-factor resolution))
139 (string-append "-f" tmp-name))))
148 (format #f "~a-page~a.png" base-name (1+ n)))
150 (list (format #f "~a.png" base-name))))
152 (if (and rename-page-1 multi-page?)
154 (rename-file (re-sub "%d" "1" pngn) png1)
160 (if (not (= 1 anti-alias-factor))
162 (lambda (f) (scale-down-image anti-alias-factor f)) files))