]> git.donarmstrong.com Git - lilypond.git/blob - scm/ps-to-png.scm
Merge branch 'master' of git+ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scm / ps-to-png.scm
1 ;;;; ps-to-png.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2005--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 (define-module (scm ps-to-png))
8
9 (use-modules
10  (ice-9 optargs)
11  (ice-9 regex)
12  (ice-9 rw)
13  (srfi srfi-1)
14  (srfi srfi-13)
15  (srfi srfi-14)
16  (lily)
17  )
18
19 ;; gettext wrapper for guile < 1.7.2
20 (if (defined? 'gettext)
21     (define-public _ gettext)
22     (define-public (_ x) x))
23
24 (define PLATFORM
25   (string->symbol
26    (string-downcase
27     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
28
29 (define (re-sub re sub string)
30   (regexp-substitute/global #f re string 'pre sub 'post))
31
32 (define (search-executable names)
33   (define (helper path lst)
34     (if (null? (cdr lst))
35         (car lst)
36         (if (search-path path (car lst)) (car lst)
37             (helper path (cdr lst)))))
38
39   (let ((path (parse-path (getenv "PATH"))))
40     (helper path names)))
41
42 (define (search-gs)
43   (search-executable '("gs-nox" "gs-8.15" "gs")))
44
45 (define (gulp-port port max-length)
46   (let ((str (make-string max-length)))
47     (read-string!/partial str port 0 max-length)
48     str))
49
50 (define-public (gulp-file file-name . max-size)
51   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
52
53 (define BOUNDING-BOX-RE
54   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
55
56 (define (unused-found-broken-get-bbox file-name)
57   (let* ((bbox (string-append file-name ".bbox"))
58          ;; -sOutputFile does not work with bbox?
59          (cmd (format #t "gs\
60  -sDEVICE=bbox\
61  -q\
62  -dNOPAUSE\
63  ~S\
64  -c showpage\
65  -c quit 2>~S"
66                       file-name bbox))
67          (status (system cmd))
68          (s (gulp-file bbox 10240))
69          (m (string-match BOUNDING_BOX_RE s)))
70
71     (if m
72         (list->vector
73          (map (lambda (x) (string->number (car x))) (vector->list m)))
74         #f)))
75
76
77 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
78 (define (my-system be-verbose exit-on-error cmd)
79   (define status 0)
80   (if be-verbose
81       (begin
82         (format (current-error-port) (_ "Invoking `~a'...") cmd)
83         (newline (current-error-port))))
84   (set! status (system cmd))
85   (if (not (= status 0))
86       (begin
87         (format (current-error-port)
88                 (format #f (_ "~a exited with status: ~S") "GS" status))
89         (if exit-on-error (exit 1))))
90   status)
91
92 (define (scale-down-image be-verbose factor file)
93   (let* ((status 0)
94          (old (string-append file ".old")))
95
96     (rename-file file old)
97     (my-system
98      be-verbose #t
99      (format #f
100              "pngtopnm ~a | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > ~a"
101              old factor file))
102     (delete-file old)))
103
104 (define-public (ps-page-count ps-name)
105   (let* ((byte-count 10240)
106          (header (gulp-file ps-name byte-count))
107          (first-null (string-index header #\nul))
108          (match (string-match "%%Pages: ([0-9]+)"
109                               (if (number? first-null)
110                                   (substring header 0 first-null)
111                                   header))))
112     (if match (string->number (match:substring match 1)) 0)))
113
114 (define-public (make-ps-images ps-name . rest)
115   (let-keywords*
116    rest #f
117    ((resolution 90)
118     (page-width  100)
119     (page-height 100)
120     (rename-page-1 #f)
121     (be-verbose #f)
122     (pixmap-format 'png16m)
123     (anti-alias-factor 1))
124
125    (let* ((format-str (format "~a" pixmap-format))
126           (extension (cond
127                       ((string-contains format-str "png") "png")
128                       ((string-contains format-str "jpg") "jpeg")
129                       ((string-contains format-str "jpeg") "jpeg")
130                       (else
131                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
132           (base (basename (re-sub "[.]e?ps" "" ps-name)))
133           (png1 (format "~a.~a" base extension))
134           (pngn (format  "~a-page%d.~a" base extension))
135           (page-count (ps-page-count ps-name))
136           (multi-page? (> page-count 1))
137           (output-file (if multi-page? pngn png1))
138
139           (gs-variable-options
140            (if multi-page?
141                (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
142                        page-width page-height)
143                "-dEPSCrop"))
144           (cmd (format #f "~a\
145  ~a\
146  ~a\
147  -dGraphicsAlphaBits=4\
148  -dTextAlphaBits=4\
149  -dNOPAUSE\
150  -sDEVICE=~a\
151  -sOutputFile=~S\
152  -r~S\
153  ~S\
154  -c quit"
155                        (search-gs)
156                        (if be-verbose "" "-q")
157                        gs-variable-options
158                        pixmap-format
159                        output-file 
160                        (* anti-alias-factor resolution) ps-name))
161           (status 0)
162           (files '()))
163
164      ;; The wrapper on windows cannot handle `=' signs,
165      ;; gs has a workaround with #.
166      (if (eq? PLATFORM 'windows)
167          (begin
168            (set! cmd (re-sub "=" "#" cmd))
169            (set! cmd (re-sub "-dSAFER " "" cmd))))
170
171      (set! status (my-system be-verbose #f cmd))
172
173      (set! files
174            (if multi-page?
175                (map
176                 (lambda (n)
177                   (format "~a-page~a.png" base (1+ n)))
178                 (iota page-count))
179                (list (format "~a.png" base))))
180      
181      (if (not (= 0 status))
182          (begin
183            (map delete-file files)
184            (exit 1)))
185
186      (if (and rename-page-1 multi-page?)
187          (begin
188            (rename-file (re-sub "%d" "1" pngn) png1)
189            (set! files
190                  (cons png1
191                        (cdr files)))
192            ))
193
194      (if (not (= 1 anti-alias-factor))
195          (for-each
196           (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))
197      files)))