1 ;;;; framework-ps.scm --
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 (define-module (scm framework-ps))
9 (use-modules (ice-9 regex)
17 (define mm-to-bigpoint
20 (define-public (ps-font-command font . override-coding)
21 (let* ((name (ly:font-filename font))
22 (magnify (ly:font-magnification font))
23 (coding-alist (ly:font-encoding-alist font))
24 (input-encoding (assoc-get 'input-name coding-alist))
25 (font-encoding (assoc-get 'output-name coding-alist))
26 (coding-command (if (null? override-coding)
27 (if (equal? input-encoding font-encoding)
29 (car override-coding))))
32 "magfont" (string-encode-integer (hashq name 1000000))
33 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
34 (if (not coding-command) "" (string-append "e" coding-command)))))
36 (define (tex-font? fontname)
38 (equal? (substring fontname 0 2) "cm")
39 (equal? (substring fontname 0 2) "ec")))
41 (define (load-fonts bookpaper)
42 (let* ((fonts (ly:bookpaper-fonts bookpaper))
43 (font-names (uniq-list (sort (map ly:font-filename fonts) string<?)))
48 ((aname (string-append x ".pfa"))
49 (apath (ly:kpathsea-expand-path aname))
51 (bpath (if (not apath)
52 (ly:kpathsea-expand-path (string-append x ".pfb"))
57 (apath (ly:gulp-file apath))
58 (bpath (ly:pfb->pfa bpath))
60 (ly:warn "Can't find PFA font ~S" x)
62 (filter string? font-names))))
64 (string-join pfas "\n")))
66 (define (define-fonts bookpaper)
68 (define font-list (ly:bookpaper-fonts bookpaper))
69 (define (define-font command fontname scaling)
71 "/" command " { /" fontname " findfont "
72 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
74 (define (reencode-font plain encoding command)
75 (let ((coding-vector (get-coding-command encoding)))
77 plain " " coding-vector " /" command " reencode-font\n"
78 "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
81 (define (font-load-command font)
82 (let* ((specced-font-name (ly:font-name font))
83 (fontname (if specced-font-name
85 (ly:font-filename font)))
87 (coding-alist (ly:font-encoding-alist font))
88 (input-encoding (assoc-get 'input-name coding-alist))
89 (font-encoding (assoc-get 'output-name coding-alist))
90 (command (ps-font-command font))
91 ;; FIXME -- see (ps-font-command )
92 (plain (ps-font-command font #f))
93 (designsize (ly:font-design-size font))
94 (magnification (* (ly:font-magnification font)))
95 (ops (ly:output-def-lookup bookpaper 'outputscale))
96 (scaling (* ops magnification designsize)))
99 (define-font plain fontname scaling)
100 (if (or (equal? input-encoding font-encoding)
102 (equal? font-encoding "fetaBraces")
103 (equal? font-encoding "fetaNumber")
104 (equal? font-encoding "fetaMusic")
105 (equal? font-encoding "parmesanMusic"))
107 (reencode-font plain input-encoding command)))))
109 (define (font-load-encoding encoding)
110 (let ((filename (get-coding-filename encoding)))
111 (ly:gulp-file (ly:kpathsea-expand-path filename))))
113 (let* ((encoding-list (map (lambda (x)
114 (assoc-get 'input-name
115 (ly:font-encoding-alist x)))
117 (encodings (uniq-list (sort-list (filter string? encoding-list)
121 (apply string-append (map font-load-encoding encodings))
123 (map (lambda (x) (font-load-command x)) font-list)))))
125 ;; FIXME: duplicated in other output backends
126 ;; FIXME: silly interface name
127 (define (output-variables paper)
128 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
129 (define (value->string val)
131 ((string? val) (string-append "(" val ")"))
132 ((symbol? val) (symbol->string val))
133 ((number? val) (number->string val))
136 (define (output-entry ps-key ly-key)
139 (value->string (ly:output-def-lookup paper ly-key)) " def \n"))
142 "/lily-output-units " (number->string mm-to-bigpoint) " def %% milimeter \n"
143 (output-entry "staff-line-thickness" 'linethickness)
144 (output-entry "line-width" 'linewidth)
145 (output-entry "paper-size" 'papersize)
146 (output-entry "staff-height" 'staffheight) ;junkme.
148 (number->string (ly:output-def-lookup paper 'outputscale))
149 " lily-output-units mul def \n"))
151 (define (dump-page outputter page page-number page-count)
152 (ly:outputter-dump-string outputter
155 (number->string page-number) " " (number->string page-count) "\n"
156 "0 0 start-system { "
157 "set-ps-scale-to-lily-scale "
159 (ly:outputter-dump-stencil outputter page)
160 (ly:outputter-dump-string outputter "} stop-system \nshowpage\n"))
162 (define (eps-header bookpaper bbox)
163 (string-append "%!PS-Adobe-2.0 EPSF-2.0\n"
164 "%%Creator: creator time-stamp\n"
165 "%%BoundingBox: " (string-join (map number->string bbox) " ") "\n"
168 (define (page-header bookpaper page-count)
169 (string-append "%!PS-Adobe-3.0\n"
170 "%%Creator: creator time-stamp\n"
171 "%%Pages: " (number->string page-count) "\n"
172 "%%PageOrder: Ascend\n"
173 "%%DocumentPaperSizes: " (ly:output-def-lookup bookpaper 'papersize) "\n"))
175 (define (preamble bookpaper)
177 (output-variables bookpaper)
178 (ly:gulp-file "music-drawing-routines.ps")
179 (ly:gulp-file "lilyponddefs.ps")
180 (load-fonts bookpaper)
181 (define-fonts bookpaper)))
183 (define-public (output-framework outputter book scopes fields basename)
184 (let* ((bookpaper (ly:paper-book-book-paper book))
185 (pages (ly:paper-book-pages book))
187 (page-count (length pages)))
190 (ly:outputter-dump-string outputter x))
192 (page-header bookpaper page-count)
193 (preamble bookpaper)))
197 (set! page-number (1+ page-number))
198 (dump-page outputter page page-number page-count))
200 (ly:outputter-dump-string outputter "%%Trailer\n%%EOF\n")))
202 (define-public (output-preview-framework outputter book scopes fields basename)
203 (let* ((bookpaper (ly:paper-book-book-paper book))
204 (systems (ly:paper-book-lines book))
205 (scale (ly:output-def-lookup bookpaper 'outputscale ))
206 (titles (take-while ly:paper-system-title? systems))
207 (non-title (find (lambda (x) (not (ly:paper-system-title? x))) systems))
209 (stack-stencils Y DOWN 0.0
210 (map ly:paper-system-stencil
211 (append titles (list non-title)))))
212 (xext (ly:stencil-extent dump-me X))
213 (yext (ly:stencil-extent dump-me Y)))
217 (ly:outputter-dump-string outputter x))
219 (eps-header bookpaper
223 (round (* x scale mm-to-bigpoint))))
224 (list (car xext) (car yext)
225 (cdr xext) (cdr yext))))
226 (preamble bookpaper)))
229 (ly:outputter-dump-string outputter
230 (string-append "0 0 start-system { "
231 "set-ps-scale-to-lily-scale "
233 (ly:outputter-dump-stencil outputter dump-me)
234 (ly:outputter-dump-string outputter "} stop-system\n%%Trailer\n%%EOF\n")))
236 (define-public (convert-to-pdf book name)
238 ((defs (ly:paper-book-book-paper book))
239 (size (ly:output-def-lookup defs 'papersize)))
241 (postscript->pdf (if (string? size) size "a4")
244 (define-public (convert-to-png book name)
246 ((defs (ly:paper-book-book-paper book))
247 (resolution (ly:output-def-lookup defs 'pngresolution)))
249 (postscript->png (if (number? resolution) resolution 90)
252 (define-public (convert-to-dvi book name)
253 (ly:warn "Can not generate DVI via the postscript back-end"))
255 (define-public (convert-to-tex book name)
256 (ly:warn "Can not generate TeX via the postscript back-end"))
258 (define-public (convert-to-ps book name)