1 ;;;; font.scm -- construct font trees
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 ;; As an excercise, do it with records.
8 ;; Should use GOOPS, really.
12 ;; lookup-font should be written in C.
14 ;; should dump tree to .texi as internal documentation
16 ;; * should extract design sizes from fonts: fonts should
17 ;; be read off the disk, on demand, something like:
19 ;; cmr -> ((font-load "cmr6") (font-load "cmr8") .. )
22 (define-class <Font-tree-element>
26 (define-class <Font-tree-leaf> (<Font-tree-element>)
27 (default-size #:init-keyword #:default-size)
28 (size-vector #:init-keyword #:size-vector))
30 (define-class <Font-tree-node> (<Font-tree-element>)
31 (qualifier #:init-keyword #:qualifier #:accessor font-qualifier)
32 (default #:init-keyword #:default #:accessor font-default)
33 (children #:init-keyword #:children #:accessor font-children))
35 (define (make-font-tree-leaf size size-font-vector)
36 (make <Font-tree-leaf> #:default-size size #:size-vector size-font-vector))
38 (define (make-font-tree-node
40 (make <Font-tree-node>
43 #:children (make-hash-table 11)))
45 (define-method (display (leaf <Font-tree-leaf>) port)
46 (map (lambda (x) (display x port))
48 "Font-size-family: \n"
49 (slot-ref leaf 'default-size)
50 (slot-ref leaf 'size-vector))))
52 (define-method (display (node <Font-tree-node>) port)
59 "Font_node { \nqual: "
67 (display (car x) port)
69 (display (cdr x) port))
70 (hash-table->alist (font-children node)))
74 (define default-qualifier-order
75 '(font-encoding font-family font-shape font-series))
77 (define-method (add-font (node <Font-tree-node>) fprops size-family)
78 (define (assoc-delete key alist)
79 (assoc-remove! (list-copy alist) key))
80 (define (make-node fprops size-family)
82 (make-font-tree-leaf (car size-family) (cdr size-family))
83 (let* ((qual (next-qualifier default-qualifier-order fprops)))
84 (make-font-tree-node qual
85 (assoc-get qual fprops)))))
87 (define (next-qualifier order props)
89 ((and (null? props) (null? order))
91 ((null? props) (car order))
92 ((null? order) (caar props))
94 (if (assoc-get (car order) props)
96 (next-qualifier (cdr order) props)))))
98 (let* ((q (font-qualifier node))
99 (d (font-default node))
100 (v (assoc-get q fprops d))
101 (new-fprops (assoc-delete q fprops))
102 (child (hashq-ref (slot-ref node 'children)
108 (set! child (make-node new-fprops size-family))
109 (hashq-set! (slot-ref node 'children) v child)))
110 (if (pair? new-fprops)
111 (add-font child new-fprops size-family))))
114 (define-method (add-font (node <Font-tree-leaf>) fprops size-family)
115 (throw "must add to node, not leaf"))
118 (define-method (g-lookup-font (node <Font-tree-node>) alist-chain)
119 (let* ((qual (font-qualifier node))
120 (def (font-default node))
121 (val (chain-assoc-get qual alist-chain def))
122 (desired-child (hashq-ref (font-children node) val)))
125 (g-lookup-font desired-child alist-chain)
126 (g-lookup-font (hashq-ref (font-children node) def) alist-chain))))
129 (define-method (g-lookup-font (node <Font-tree-leaf>) alist-chain)
132 ;; two step call is handy for debugging.
133 (define (lookup-font node alist-chain)
134 (g-lookup-font node alist-chain))
137 ; Each size family is a vector of fonts, loaded with a delay.
138 ; The vector should be sorted according to ascending design size.
140 (define feta-alphabet-size-vector
141 (if (defined? 'ly:kpathsea-find-file)
142 `#(,(delay (ly:font-load "feta-alphabet11"))
143 ,(delay (ly:font-load "feta-alphabet13"))
144 ,(delay (ly:font-load "feta-alphabet14"))
145 ,(delay (ly:font-load "feta-alphabet16"))
146 ,(delay (ly:font-load "feta-alphabet18"))
147 ,(delay (ly:font-load "feta-alphabet20"))
148 ,(delay (ly:font-load "feta-alphabet23"))
149 ,(delay (ly:font-load "feta-alphabet26")))
152 (cons (ly:pt (cdr tup))
153 (format "feta-alphabet~a ~a"
165 (define-public (add-music-fonts node factor)
169 (list (cons 'font-encoding (car x)))
170 (cons (* factor (cadr x))
172 `((fetaDynamic ,(ly:pt 20.0) ,feta-alphabet-size-vector)
173 (fetaNumber ,(ly:pt 20.0) ,feta-alphabet-size-vector)
174 (fetaMusic ,(ly:pt 20.0)
175 #(,(delay (ly:font-load "emmentaler-11"))
176 ,(delay (ly:font-load "emmentaler-13"))
177 ,(delay (ly:font-load "emmentaler-14"))
178 ,(delay (ly:font-load "emmentaler-16"))
179 ,(delay (ly:font-load "emmentaler-18"))
180 ,(delay (ly:font-load "emmentaler-20"))
181 ,(delay (ly:font-load "emmentaler-23"))
182 ,(delay (ly:font-load "emmentaler-26"))))
184 (fetaBraces ,(ly:pt 20.0) #(,(delay
185 (ly:font-load "aybabtu")))))))
187 (define-public (add-cmr-fonts node factor)
188 (add-font node '((font-encoding . TeX-math))
189 `(,(* factor 10) . #(,(delay (ly:font-load "msam10")))))
192 (add-font node `((font-encoding . TeX-text)
193 (font-series . ,(vector-ref (car x) 2))
194 (font-shape . ,(vector-ref (car x) 1))
195 (font-family . ,(vector-ref (car x) 0)))
196 (cons (* factor (cadr x))
198 `((#(roman upright medium)
199 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmr6"))
200 ,(delay (ly:font-load "cmr8"))
201 ,(delay (ly:font-load "cmr10"))
202 ,(delay (ly:font-load "cmr17")))))
203 (#(roman upright bold)
204 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmbx6"))
205 ,(delay (ly:font-load "cmbx8"))
206 ,(delay (ly:font-load "cmbx10"))
207 ,(delay (ly:font-load "cmbx12")))))
208 (#(roman italic medium)
209 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmti7"))
210 ,(delay (ly:font-load "cmti10"))
211 ,(delay (ly:font-load "cmti12")))))
212 (#(roman italic bold)
213 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmbxti8"))
214 ,(delay (ly:font-load "cmbxti10"))
215 ,(delay (ly:font-load "cmbxti14")))))
216 (#(roman caps medium)
217 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmcsc10")))))
218 (#(roman upright bold-narrow )
219 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmb10")))))
220 (#(sans upright medium)
221 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmss8"))
222 ,(delay (ly:font-load "cmss10"))
223 ,(delay (ly:font-load "cmss12"))
224 ,(delay (ly:font-load "cmss17")))))
225 (#(typewriter upright medium)
226 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "cmtt8"))
227 ,(delay (ly:font-load "cmtt10"))
228 ,(delay (ly:font-load "cmtt12"))))))))
230 ;; Debian lmodern font support.
231 (define-public (add-cork-lm-fonts node factor)
234 (add-font node `((font-encoding . cork-lm)
235 (font-series . ,(vector-ref (car x) 2))
236 (font-shape . ,(vector-ref (car x) 1))
237 (font-family . ,(vector-ref (car x) 0)))
238 (cons (* factor (cadr x)) (cddr x))))
239 `((#(roman upright medium)
240 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmr6"))
241 ,(delay (ly:font-load "lmr8"))
242 ,(delay (ly:font-load "lmr10"))
243 ,(delay (ly:font-load "lmr17")))))
244 (#(roman upright bold)
245 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmbx6"))
246 ,(delay (ly:font-load "lmbx8"))
247 ,(delay (ly:font-load "lmbx10"))
248 ,(delay (ly:font-load "lmbx12")))))
249 (#(roman italic medium)
250 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmri7"))
251 ,(delay (ly:font-load "lmri10"))
252 ,(delay (ly:font-load "lmri12")))))
253 (#(roman italic bold)
254 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmbxi10")))))
255 (#(roman caps medium)
256 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmcsc10")))))
257 (#(roman upright bold-narrow )
258 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmb10")))))
259 (#(sans upright medium)
260 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmss8"))
261 ,(delay (ly:font-load "lmss10"))
262 ,(delay (ly:font-load "lmss12"))
263 ,(delay (ly:font-load "lmss17")))))
264 (#(sans upright bold)
265 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmssbx10")))))
267 (#(typewriter upright medium)
268 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "lmtt8"))
269 ,(delay (ly:font-load "lmtt10"))
270 ,(delay (ly:font-load "lmtt12"))))))))
272 ;; ec-fonts-mftraced font support.
273 (define-public (add-ec-fonts node factor)
275 (lambda (x) (add-font node
276 `((font-encoding . Extended-TeX-Font-Encoding---Latin)
277 (font-series . ,(vector-ref (car x) 2))
278 (font-shape . ,(vector-ref (car x) 1))
279 (font-family . ,(vector-ref (car x) 0)))
280 (cons (* factor (cadr x)) (cddr x))))
282 `((#(roman upright medium)
283 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecrm6"))
284 ,(delay (ly:font-load "ecrm8"))
285 ,(delay (ly:font-load "ecrm10"))
286 ,(delay (ly:font-load "ecrm17")))))
287 (#(roman upright bold)
288 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecbx6"))
289 ,(delay (ly:font-load "ecbx8"))
290 ,(delay (ly:font-load "ecbx10"))
291 ,(delay (ly:font-load "ecbx12")))))
292 (#(roman italic medium)
293 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecti7"))
294 ,(delay (ly:font-load "ecti10"))
295 ,(delay (ly:font-load "ecti12")))))
296 (#(roman italic bold)
297 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecbi8"))
298 ,(delay (ly:font-load "ecbi10"))
299 ,(delay (ly:font-load "ecbi14")))))
300 (#(roman caps medium)
301 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "eccc10")))))
302 (#(roman slanted-caps medium)
303 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecsc10")))))
304 (#(roman upright bold-narrow )
305 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecrb10")))))
306 (#(sans upright medium)
307 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ecss8"))
308 ,(delay (ly:font-load "ecss10"))
309 ,(delay (ly:font-load "ecss12"))
310 ,(delay (ly:font-load "ecss17")))))
311 (#(typewriter upright medium)
312 . (,(ly:pt 10.0) . #(,(delay (ly:font-load "ectt8"))
313 ,(delay (ly:font-load "ectt10"))
314 ,(delay (ly:font-load "ectt12"))))))))
316 (define-public (add-pango-fonts node lily-family family factor)
317 (define (add-node shape series)
319 `((font-family . ,lily-family)
320 (font-shape . ,shape)
321 (font-series . ,series)
322 (font-encoding . latin1) ;; ugh.
325 `(,(ly:pt (* factor 11.0))
328 (ly:make-pango-description-string
329 `(((font-family . ,family)
330 (font-series . ,series)
331 (font-shape . ,shape)))
334 (add-node 'upright 'normal)
335 (add-node 'upright 'bold)
336 (add-node 'italic 'normal)
337 (add-node 'italic 'bold)
340 (define-public (make-cmr-tree factor)
341 (let ((n (make-font-tree-node 'font-encoding 'fetaMusic)))
342 (add-music-fonts n factor)
343 (add-cmr-fonts n factor)
344 (if (defined? 'ly:kpathsea-find-file)
346 (if (ly:kpathsea-find-file "lmr10.pfb")
347 (add-cork-lm-fonts n factor))
348 (if (ly:kpathsea-find-file "ecrm10.pfa")
349 (add-ec-fonts n factor))))
352 (define-public (make-century-schoolbook-tree factor)
353 (let ((n (make-font-tree-node 'font-encoding 'fetaMusic)))
354 (add-music-fonts n factor)
355 (add-pango-fonts n 'roman "Century Schoolbook L" factor)
356 (add-pango-fonts n 'sans "Luxi Sans" factor)
359 (define-public (magstep x)
360 (exp (* (/ x 6) (log 2))))