]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Issue 4374 / 4: Use mkstemp for intermediate ps files
[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 PLATFORM
35   (string->symbol
36    (string-downcase
37     (car (string-tokenize (utsname:sysname (uname)) char-set:letter)))))
38
39 (define (re-sub re sub string)
40   (regexp-substitute/global #f re string 'pre sub 'post))
41
42 (define (search-executable names)
43   (define (helper path lst)
44     (if (null? (cdr lst))
45         (car lst)
46         (if (search-path path (car lst)) (car lst)
47             (helper path (cdr lst)))))
48
49   (let ((path (parse-path (getenv "PATH"))))
50     (helper path names)))
51
52 (define (search-gs)
53   (search-executable '("gs-nox" "gs-8.15" "gs")))
54
55 (define (gulp-port port max-length)
56   (let ((str (make-string max-length)))
57     (read-string!/partial str port 0 max-length)
58     str))
59
60 (define-public (gulp-file file-name . max-size)
61   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
62
63 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
64 (define (my-system be-verbose exit-on-error cmd)
65   (define status 0)
66   (ly:debug (_ "Invoking `~a'...\n") cmd)
67   (set! status (system cmd))
68   (if (not (= status 0))
69       (begin
70         (ly:error (_ "~a exited with status: ~S") "GS" status)
71         (if exit-on-error (exit 1))))
72   status)
73
74 (define (scale-down-image be-verbose factor file)
75   (define (with-pbm)
76     (let* ((status 0)
77            (old (string-append file ".old")))
78
79       (rename-file file old)
80       (my-system
81        be-verbose #t
82        (format #f
83                "pngtopnm \"~a\" | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > \"~a\""
84                old factor file))
85       (delete-file old)))
86
87   (with-pbm))
88
89 (define-public (ps-page-count ps-name)
90   (let* ((byte-count 10240)
91          (header (gulp-file ps-name byte-count))
92          (first-null (string-index header #\nul))
93          (match (string-match "%%Pages: ([0-9]+)"
94                               (if (number? first-null)
95                                   (substring header 0 first-null)
96                                   header))))
97     (if match (string->number (match:substring match 1)) 0)))
98
99 (define-public (make-ps-images base-name tmp-name is-eps . rest)
100   (let-keywords*
101    rest #f
102    ((resolution 90)
103     (page-width  100)
104     (page-height 100)
105     (rename-page-1 #f)
106     (be-verbose (ly:get-option 'verbose))
107     (pixmap-format 'png16m)
108     (anti-alias-factor 1))
109
110    (let* ((format-str (format #f "~a" pixmap-format))
111           (extension (cond
112                       ((string-contains format-str "png") "png")
113                       ((string-contains format-str "jpg") "jpeg")
114                       ((string-contains format-str "jpeg") "jpeg")
115                       (else
116                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
117           (png1 (format #f "~a.~a" base-name extension))
118           (pngn (format #f "~a-page%d.~a" base-name extension))
119           (page-count (ps-page-count tmp-name))
120           (multi-page? (> page-count 1))
121           (output-file (if multi-page? pngn png1))
122
123           (gs-variable-options
124            (if is-eps
125                "-dEPSCrop"
126                (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
127                        page-width page-height)))
128           (cmd (ly:format "~a\
129  ~a\
130  ~a\
131  -dGraphicsAlphaBits=4\
132  -dTextAlphaBits=4\
133  -dNOPAUSE\
134  -sDEVICE=~a\
135  -sOutputFile=~S\
136  -r~a\
137  ~S\
138  -c quit"
139                           (search-gs)
140                           (if be-verbose "" "-q")
141                           gs-variable-options
142                           pixmap-format
143                           output-file
144                           (* anti-alias-factor resolution) tmp-name))
145           (status 0)
146           (files '()))
147
148      ;; The wrapper on windows cannot handle `=' signs,
149      ;; gs has a workaround with #.
150      (if (eq? PLATFORM 'windows)
151          (begin
152            (set! cmd (re-sub "=" "#" cmd))
153            (set! cmd (re-sub "-dSAFER " "" cmd))))
154
155      (set! status (my-system be-verbose #f cmd))
156
157      (set! files
158            (if multi-page?
159                (map
160                 (lambda (n)
161                   (format #f "~a-page~a.png" base-name (1+ n)))
162                 (iota page-count))
163                (list (format #f "~a.png" base-name))))
164
165      (if (not (= 0 status))
166          (begin
167            (for-each delete-file files)
168            (exit 1)))
169
170      (if (and rename-page-1 multi-page?)
171          (begin
172            (rename-file (re-sub "%d" "1" pngn) png1)
173            (set! files
174                  (cons png1
175                        (cdr files)))
176            ))
177
178      (if (not (= 1 anti-alias-factor))
179          (for-each
180           (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))
181      files)))