3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 (define-module (scm ps-to-png))
19 ;; gettext wrapper for guile < 1.7.2
20 (if (defined? 'gettext)
21 (define-public _ gettext)
22 (define-public (_ x) x))
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)
36 (if (search-path path (car lst)) (car lst)
37 (helper path (cdr lst)))))
39 (let ((path (parse-path (getenv "PATH"))))
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)
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)
58 (format (current-error-port) (_ "Invoking `~a'...") cmd)
59 (newline (current-error-port))))
60 (set! status (system cmd))
61 (if (not (= status 0))
63 (format (current-error-port)
64 (format #f (_ "~a exited with status: ~S") "GS" status))
65 (if exit-on-error (exit 1))))
68 (define (scale-down-image be-verbose factor file)
71 (old (string-append file ".old")))
73 (rename-file file old)
77 "pngtopnm ~a | pnmscale -reduce ~a 2>/dev/null | pnmtopng -compression 9 2>/dev/null > ~a"
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)
91 (if match (string->number (match:substring match 1)) 0)))
93 (define-public (make-ps-images ps-name . rest)
101 (pixmap-format 'png16m)
102 (anti-alias-factor 1))
104 (let* ((format-str (format "~a" pixmap-format))
106 ((string-contains format-str "png") "png")
107 ((string-contains format-str "jpg") "jpeg")
108 ((string-contains format-str "jpeg") "jpeg")
110 (ly:error "Unknown pixmap format ~a" pixmap-format))))
111 (base (basename (re-sub "[.]e?ps" "" ps-name)))
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))
120 (format #f "-dDEVICEWIDTHPOINTS=~,2f -dDEVICEHEIGHTPOINTS=~,2f"
121 page-width page-height)
126 -dGraphicsAlphaBits=4\
135 (if be-verbose "" "-q")
139 (* anti-alias-factor resolution) ps-name))
143 ;; The wrapper on windows cannot handle `=' signs,
144 ;; gs has a workaround with #.
145 (if (eq? PLATFORM 'windows)
147 (set! cmd (re-sub "=" "#" cmd))
148 (set! cmd (re-sub "-dSAFER " "" cmd))))
150 (set! status (my-system be-verbose #f cmd))
156 (format "~a-page~a.png" base (1+ n)))
158 (list (format "~a.png" base))))
160 (if (not (= 0 status))
162 (map delete-file files)
165 (if (and rename-page-1 multi-page?)
167 (rename-file (re-sub "%d" "1" pngn) png1)
173 (if (not (= 1 anti-alias-factor))
175 (lambda (f) (scale-down-image be-verbose anti-alias-factor f)) files))