1 ;;;; font.scm -- construct font trees
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 ;; lookup-font should be written in C.
12 (define-class <Font-tree-element>
15 (define-class <Font-tree-leaf> (<Font-tree-element>)
16 (default-size #:init-keyword #:default-size)
17 (size-vector #:init-keyword #:size-vector))
19 (define-class <Font-tree-node> (<Font-tree-element>)
20 (qualifier #:init-keyword #:qualifier #:accessor font-qualifier)
21 (default #:init-keyword #:default #:accessor font-default)
22 (children #:init-keyword #:children #:accessor font-children))
24 (define (make-font-tree-leaf size size-font-vector)
25 (make <Font-tree-leaf> #:default-size size #:size-vector size-font-vector))
27 (define (make-font-tree-node
29 (make <Font-tree-node>
32 #:children (make-hash-table 11)))
34 (define-method (display (leaf <Font-tree-leaf>) port)
35 (map (lambda (x) (display x port))
37 "#<Font-size-family:\n"
38 (slot-ref leaf 'default-size)
39 (slot-ref leaf 'size-vector)
43 (define-method (display (node <Font-tree-node>) port)
56 (display (car x) port)
58 (display (cdr x) port))
59 (hash-table->alist (font-children node)))
62 (define default-qualifier-order
63 '(font-encoding font-family font-shape font-series))
65 (define-method (add-font (node <Font-tree-node>) fprops size-family)
66 (define (assoc-delete key alist)
67 (assoc-remove! (list-copy alist) key))
69 (define (make-node fprops size-family)
71 (make-font-tree-leaf (car size-family) (cdr size-family))
72 (let* ((qual (next-qualifier default-qualifier-order fprops)))
73 (make-font-tree-node qual
74 (assoc-get qual fprops)))))
76 (define (next-qualifier order props)
78 ((and (null? props) (null? order))
80 ((null? props) (car order))
81 ((null? order) (caar props))
83 (if (assoc-get (car order) props)
85 (next-qualifier (cdr order) props)))))
87 (let* ((q (font-qualifier node))
88 (d (font-default node))
89 (v (assoc-get q fprops d))
90 (new-fprops (assoc-delete q fprops))
91 (child (hashq-ref (slot-ref node 'children)
95 (set! child (make-node new-fprops size-family))
96 (hashq-set! (slot-ref node 'children) v child)))
97 (if (pair? new-fprops)
98 (add-font child new-fprops size-family))))
100 (define-method (add-font (node <Font-tree-leaf>) fprops size-family)
101 (throw "must add to node, not leaf"))
103 (define-method (g-lookup-font (node <Font-tree-node>) alist-chain)
104 (let* ((qual (font-qualifier node))
105 (def (font-default node))
106 (val (chain-assoc-get qual alist-chain def))
107 (desired-child (hashq-ref (font-children node) val)))
110 (g-lookup-font desired-child alist-chain)
111 (g-lookup-font (hashq-ref (font-children node) def) alist-chain))))
113 (define-method (g-lookup-font (node <Font-tree-leaf>) alist-chain)
116 ;; two step call is handy for debugging.
117 (define (lookup-font node alist-chain)
118 (g-lookup-font node alist-chain))
121 ;; Ugh. Currently, we load the PFB Feta fonts for `fetaDynamic' with
122 ;; Pango. This should be changed to load the Emmentaler fonts instead
123 ;; (with Pango too), but then we need support for a `font-style'
124 ;; property which isn't implemented yet.
125 (define feta-alphabet-size-vector
128 (cons (ly:pt (cdr tup))
129 (format "feta-alphabet~a ~a"
141 ;; Each size family is a vector of fonts, loaded with a delay. The
142 ;; vector should be sorted according to ascending design size.
143 (define-public (add-music-fonts node factor)
147 (list (cons 'font-encoding (car x)))
148 (cons (* factor (cadr x))
150 `((fetaDynamic ,(ly:pt 20.0) ,feta-alphabet-size-vector)
151 (fetaNumber ,(ly:pt 20.0) ,feta-alphabet-size-vector)
152 (fetaMusic ,(ly:pt 20.0)
153 #(,(delay (ly:system-font-load "emmentaler-11"))
154 ,(delay (ly:system-font-load "emmentaler-13"))
155 ,(delay (ly:system-font-load "emmentaler-14"))
156 ,(delay (ly:system-font-load "emmentaler-16"))
157 ,(delay (ly:system-font-load "emmentaler-18"))
158 ,(delay (ly:system-font-load "emmentaler-20"))
159 ,(delay (ly:system-font-load "emmentaler-23"))
160 ,(delay (ly:system-font-load "emmentaler-26"))))
161 (fetaBraces ,(ly:pt 20.0)
162 #(,(delay (ly:system-font-load "aybabtu")))))))
164 (define-public (add-pango-fonts node lily-family family factor)
165 (define (add-node shape series)
167 `((font-family . ,lily-family)
168 (font-shape . ,shape)
169 (font-series . ,series)
170 (font-encoding . latin1) ;; ugh.
172 `(,(ly:pt (* factor 11.0))
175 (ly:make-pango-description-string
176 `(((font-family . ,family)
177 (font-series . ,series)
178 (font-shape . ,shape)))
181 (add-node 'upright 'normal)
182 (add-node 'caps 'normal)
183 (add-node 'upright 'bold)
184 (add-node 'italic 'normal)
185 (add-node 'italic 'bold))
187 (define-public (make-pango-font-tree roman-str sans-str typewrite-str factor)
188 (let ((n (make-font-tree-node 'font-encoding 'fetaMusic)))
189 (add-music-fonts n factor)
190 (add-pango-fonts n 'roman roman-str factor)
191 (add-pango-fonts n 'sans sans-str factor)
192 (add-pango-fonts n 'typewriter typewrite-str factor)
195 (define-public (make-century-schoolbook-tree factor)
196 (make-pango-font-tree
197 "Century Schoolbook L"
198 "Sans" "Mono" factor))
200 (define-public (magstep s)
201 (exp (* (/ s 6) (log 2))))
203 (define-public (magnification->font-size m)
204 (* 6 (/ (log m) (log 2))))