Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / scm / ps-to-png.scm
blobcb20163c01e2062f495a7b95fa076e8975c74f5b
1 ;;;; ps-to-png.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2005--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 (define-module (scm ps-to-png))
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  )
19 ;; gettext wrapper for guile < 1.7.2
20 (if (defined? 'gettext)
21     (define-public _ gettext)
22     (define-public (_ x) x))
24 (define PLATFORM
25   (string->symbol
26    (string-downcase
27     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
29 (define (re-sub re sub string)
30   (regexp-substitute/global #f re string 'pre sub 'post))
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)))))
39   (let ((path (parse-path (getenv "PATH"))))
40     (helper path names)))
42 (define (search-gs)
43   (search-executable '("gs-nox" "gs-8.15" "gs")))
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))
50 (define-public (gulp-file file-name . max-size)
51   (ly:gulp-file file-name (if (pair? max-size) (car max-size))))
53 ;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
54 (define (my-system be-verbose exit-on-error cmd)
55   (define status 0)
56   (if be-verbose
57       (begin
58         (format (current-error-port) (_ "Invoking `~a'...") cmd)
59         (newline (current-error-port))))
60   (set! status (system cmd))
61   (if (not (= status 0))
62       (begin
63         (format (current-error-port)
64                 (format #f (_ "~a exited with status: ~S") "GS" status))
65         (if exit-on-error (exit 1))))
66   status)
68 (define (scale-down-image be-verbose factor file)
69   (define (with-pbm)
70     (let* ((status 0)
71            (old (string-append file ".old")))
72       
73       (rename-file file old)
74       (my-system
75        be-verbose #t
76        (format #f
77                "pngtopnm ~a | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > ~a"
78                old factor file))
79       (delete-file old)))
81   (with-pbm))
83 (define-public (ps-page-count ps-name)
84   (let* ((byte-count 10240)
85          (header (gulp-file ps-name byte-count))
86          (first-null (string-index header #\nul))
87          (match (string-match "%%Pages: ([0-9]+)"
88                               (if (number? first-null)
89                                   (substring header 0 first-null)
90                                   header))))
91     (if match (string->number (match:substring match 1)) 0)))
93 (define-public (make-ps-images ps-name . rest)
94   (let-keywords*
95    rest #f
96    ((resolution 90)
97     (page-width  100)
98     (page-height 100)
99     (rename-page-1 #f)
100     (be-verbose (ly:get-option 'verbose))
101     (pixmap-format 'png16m)
102     (anti-alias-factor 1))
104    (let* ((format-str (format "~a" pixmap-format))
105           (extension (cond
106                       ((string-contains format-str "png") "png")
107                       ((string-contains format-str "jpg") "jpeg")
108                       ((string-contains format-str "jpeg") "jpeg")
109                       (else
110                        (ly:error "Unknown pixmap format ~a" pixmap-format))))
111           (base (dir-basename ps-name ".ps" ".eps"))
112           (png1 (format "~a.~a" base extension))
113           (pngn (format  "~a-page%d.~a" base extension))
114           (page-count (ps-page-count ps-name))
115           (multi-page? (> page-count 1))
116           (output-file (if multi-page? pngn png1))
118           (gs-variable-options
119            (if multi-page?
120                (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
121                        page-width page-height)
122                "-dEPSCrop"))
123           (cmd (format #f "~a\
124  ~a\
125  ~a\
126  -dGraphicsAlphaBits=4\
127  -dTextAlphaBits=4\
128  -dNOPAUSE\
129  -sDEVICE=~a\
130  -sOutputFile=~S\
131  -r~S\
132  ~S\
133  -c quit"
134                        (search-gs)
135                        (if be-verbose "" "-q")
136                        gs-variable-options
137                        pixmap-format
138                        output-file 
139                        (* anti-alias-factor resolution) ps-name))
140           (status 0)
141           (files '()))
143      ;; The wrapper on windows cannot handle `=' signs,
144      ;; gs has a workaround with #.
145      (if (eq? PLATFORM 'windows)
146          (begin
147            (set! cmd (re-sub "=" "#" cmd))
148            (set! cmd (re-sub "-dSAFER " "" cmd))))
150      (set! status (my-system be-verbose #f cmd))
152      (set! files
153            (if multi-page?
154                (map
155                 (lambda (n)
156                   (format "~a-page~a.png" base (1+ n)))
157                 (iota page-count))
158                (list (format "~a.png" base))))
159      
160      (if (not (= 0 status))
161          (begin
162            (map delete-file files)
163            (exit 1)))
165      (if (and rename-page-1 multi-page?)
166          (begin
167            (rename-file (re-sub "%d" "1" pngn) png1)
168            (set! files
169                  (cons png1
170                        (cdr files)))
171            ))
173      (if (not (= 1 anti-alias-factor))
174          (for-each
175           (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))
176      files)))