render: font: Print "?" instead of crashing when character is missing.
[chickadee.git] / chickadee / render / font.scm
CommitLineData
233d13ca
DT
1;;; Chickadee Game Toolkit
2;;; Copyright © 2017 David Thompson <davet@gnu.org>
3;;;
4;;; Chickadee is free software: you can redistribute it and/or modify
5;;; it under the terms of the GNU General Public License as published
6;;; by the Free Software Foundation, either version 3 of the License,
7;;; or (at your option) any later version.
8;;;
9;;; Chickadee is distributed in the hope that it will be useful, but
10;;; WITHOUT ANY WARRANTY; without even the implied warranty of
11;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;;; General Public License for more details.
13;;;
14;;; You should have received a copy of the GNU General Public License
15;;; along with this program. If not, see
16;;; <http://www.gnu.org/licenses/>.
17
18;;; Commentary:
19;;
20;; Bitmap font rendering.
21;;
22;;; Code:
23
24(define-module (chickadee render font)
25 #:use-module (ice-9 format)
a4670ba8
DT
26 #:use-module (ice-9 match)
27 #:use-module (srfi srfi-1)
233d13ca
DT
28 #:use-module (srfi srfi-9)
29 #:use-module (srfi srfi-9 gnu)
30 #:use-module (srfi srfi-11)
31 #:use-module (sxml xpath)
32 #:use-module (sxml simple)
33 #:use-module (chickadee math matrix)
34 #:use-module (chickadee math rect)
35 #:use-module (chickadee math vector)
36 #:use-module (chickadee render)
37 #:use-module (chickadee render shader)
38 #:use-module (chickadee render sprite)
39 #:use-module (chickadee render texture)
40 #:export (load-font
b61558f5 41 load-tile-font
233d13ca
DT
42 font?
43 font-face
44 font-line-height
45 font-bold?
46 font-italic?
fedd9bca 47 draw-text*
233d13ca
DT
48 draw-text))
49
50(define-record-type <font-char>
45c5d680 51 (make-font-char id texture-region offset dimensions advance)
233d13ca
DT
52 font-char?
53 (id font-char-id)
45c5d680 54 (texture-region font-char-texture-region)
233d13ca
DT
55 (offset font-char-offset)
56 (dimensions font-char-dimensions)
57 (advance font-char-advance))
58
59(define-record-type <font>
60 (make-font face bold? italic? line-height chars kerning)
61 font?
62 (face font-face)
63 (bold? font-bold?)
64 (italic? font-italic?)
65 (line-height font-line-height)
66 (chars font-chars)
67 (kerning font-kerning))
68
69(define (display-font font port)
70 (format port "#<font face: ~a line-height: ~d bold?: ~a italic?: ~a>"
71 (font-face font)
72 (font-line-height font)
73 (font-bold? font)
74 (font-italic? font)))
75
76(set-record-type-printer! <font> display-font)
77
b61558f5
DT
78(define* (load-tile-font file tile-width tile-height characters #:key
79 (face "untitled") (margin 0) (spacing 0))
80 "Load the font named FACE from FILE, a bitmap image containing the
81characters in the string CHARACTERS that are TILE-WIDTH by TILE-HEIGHT
82pixels in size. The characters in the image *must* appear in the
83order that they are specified in the character set or text will not
84render properly. Optionally, each tile may have SPACING pixels of
85horizontal and vertical space between surrounding tiles and the entire
86image may have MARGIN pixels of empty space around its border."
87 (let* ((atlas (split-texture (load-image file) tile-width tile-height
88 #:margin margin
89 #:spacing spacing))
90 (chars
91 (let ((table (make-hash-table)))
92 (string-for-each-index
93 (lambda (i)
94 (hash-set! table (string-ref characters i)
95 (make-font-char (string-ref characters i)
96 (texture-atlas-ref atlas i)
97 (vec2 0.0 0.0)
98 (vec2 tile-width tile-height)
99 (vec2 tile-width 0.0))))
100 characters)
101 table))
102 ;; These fonts are by definition monospace fonts, so no
103 ;; kerning.
104 (kernings (make-hash-table)))
105 (make-font face #f #f tile-height chars kernings)))
106
233d13ca 107(define (load-font file)
a4670ba8
DT
108 "Load the AngelCode formatted bitmap font within FILE. The file
109extension must be either .xml or .fnt."
110 (cond
111 ((string-suffix? ".xml" file)
112 (parse-bmfont-sxml file (call-with-input-file file xml->sxml)))
113 ((string-suffix? ".fnt" file)
114 (parse-bmfont-sxml file (parse-fnt file)))
115 (else
116 (error "unknown bmfont file type: " file))))
117
118(define (parse-fnt file)
119 (define (newline? char)
120 (eqv? char #\newline))
121 (define (whitespace? char)
122 (and (not (newline? char))
123 (char-set-contains? char-set:whitespace char)))
124 (define (letter? char)
125 (char-set-contains? char-set:letter char))
126 (define (consume-whitespace port)
127 (match (peek-char port)
128 ((? eof-object?) *unspecified*)
129 ((? whitespace?)
130 (read-char port)
131 (consume-whitespace port))
132 (_ *unspecified*)))
133 (define (read-tag port)
134 (list->symbol
135 (let loop ()
136 (match (peek-char port)
137 ((? letter? char)
138 (read-char port)
139 (cons char (loop)))
140 ((? whitespace? char)
141 '())))))
142 (define (read-key port)
143 (list->symbol
144 (let loop ()
145 (match (read-char port)
146 (#\= '())
147 ((? letter? char)
148 (cons char (loop)))))))
149 (define (read-quoted-string port)
150 (match (read-char port)
151 (#\" #t))
152 (list->string
153 (let loop ()
154 (match (read-char port)
155 (#\"
156 (if (or (whitespace? (peek-char port))
157 (newline? (peek-char port)))
158 '()
159 (cons #\" (loop))))
160 (char (cons char (loop)))))))
161 (define (read-unquoted-string port)
162 (list->string
163 (let loop ()
164 (match (peek-char port)
165 ((or (? whitespace?)
166 (? newline?))
167 '())
168 (char
169 (read-char port)
170 (cons char (loop)))))))
171 (define (read-value port)
172 (match (peek-char port)
173 (#\"
174 (read-quoted-string port))
175 (_ (read-unquoted-string port))))
176 (define (read-key/value-pair port)
177 (list (read-key port) (read-value port)))
178 (define (read-key/value-pairs port)
179 (cons '@
180 (let loop ()
181 (consume-whitespace port)
182 (match (peek-char port)
183 ((? newline?)
184 (read-char port)
185 '())
186 ((? letter?)
187 (cons (read-key/value-pair port)
188 (loop)))))))
189 (define (read-line port)
190 (list (read-tag port) (read-key/value-pairs port)))
191 `(*TOP*
192 (font
193 ,@(call-with-input-file file
194 (lambda (port)
195 (let loop ((pages '()))
196 (match (peek-char port)
197 ((? eof-object?)
198 `((pages (@ (count ,(number->string (length pages))))
199 ,@pages)))
200 ((? newline?)
201 (read-char port)
202 (loop pages))
203 ((? letter?)
204 (match (read-line port)
205 ((tag ('@ ('count count)))
206 (cons (cons* tag
207 `(@ (count ,count))
208 (list-tabulate (string->number count)
209 (lambda (i)
210 (read-line port))))
211 (loop pages)))
212 ((and ('page . _) page)
213 (loop (cons page pages)))
214 (exp (cons exp (loop pages))))))))))))
215
216(define (parse-bmfont-sxml file tree)
233d13ca
DT
217 (define directory (dirname file))
218 (define* (attr tree name #:optional (parse identity))
219 (let ((result ((sxpath `(@ ,name *text*)) tree)))
220 (if (null? result)
221 #f
222 (parse (car result)))))
223 (define (parse-pages nodes)
224 (let ((table (make-hash-table)))
225 (for-each (lambda (node)
226 (let* ((id (attr node 'id string->number))
227 (file (attr node 'file))
228 (texture (load-image
229 (string-append directory "/" file))))
230 (hash-set! table id texture)))
231 nodes)
232 table))
233 (define (string->character s)
234 (integer->char (string->number s)))
235 (define (parse-chars nodes pages image-width image-height line-height)
236 (define (x->s x)
237 (exact->inexact (/ x image-width)))
238 (define (y->t y)
239 (exact->inexact (/ y image-height)))
240 (let ((table (make-hash-table)))
241 (for-each (lambda (node)
242 (let* ((id (attr node 'id string->character))
243 (width (attr node 'width string->number))
244 (height (attr node 'height string->number))
245 (x (attr node 'x string->number))
246 ;; Invert the y axis. Our origin is the
247 ;; bottom-left corner, not top-left.
248 (y (- image-height height
249 (attr node 'y string->number)))
250 (x-offset (attr node 'xoffset string->number))
251 (y-offset (- line-height height
252 (attr node 'yoffset string->number)))
253 (x-advance (attr node 'xadvance string->number))
254 (page (or (attr node 'page string->number) 0))
45c5d680 255 (region (make-texture-region (hash-ref pages page)
fedd9bca 256 (make-rect x y width height)))
233d13ca 257 (char (make-font-char id
233d13ca
DT
258 region
259 (vec2 x-offset y-offset)
260 (vec2 width height)
261 (vec2 x-advance 0.0))))
262 (hash-set! table id char)))
263 nodes)
264 table))
265 (define (parse-kernings nodes)
266 (let ((table (make-hash-table)))
267 (for-each (lambda (node)
268 (let* ((first (attr node 'first string->character))
269 (second (attr node 'second string->character))
270 (x-offset (attr node 'amount string->number))
271 (inner-table (hash-ref table first)))
272 (if inner-table
273 (hash-set! inner-table second (vec2 x-offset 0.0))
274 (let ((inner-table (make-hash-table)))
275 (hash-set! inner-table second (vec2 x-offset 0.0))
276 (hash-set! table first inner-table)))))
277 nodes)
278 table))
a4670ba8 279 (let* ((info ((sxpath '(font info)) tree))
233d13ca
DT
280 (common ((sxpath '(font common)) tree))
281 (face (attr info 'face))
282 (bold? (attr info 'bold (const #t)))
283 (italic? (attr info 'italic (const #t)))
284 (line-height (attr common 'lineHeight string->number))
285 (image-width (attr common 'scaleW string->number))
286 (image-height (attr common 'scaleH string->number))
287 (pages (parse-pages ((sxpath '(font pages page)) tree)))
288 (chars (parse-chars ((sxpath '(font chars char)) tree)
289 pages
290 image-width
291 image-height
292 line-height))
293 (kernings (parse-kernings ((sxpath '(font kernings kerning)) tree))))
294 (make-font face bold? italic? line-height chars kernings)))
295
296(define (font-ref font char)
297 (hashv-ref (font-chars font) char))
298
fedd9bca 299(define draw-text*
45c5d680 300 (let ((cursor (vec2 0.0 0.0))
fedd9bca
DT
301 (rect (make-rect 0.0 0.0 0.0 0.0)))
302 (lambda* (font text matrix #:key (blend-mode 'alpha))
233d13ca
DT
303 ;; TODO: Respect kerning.
304 (define (render-char c)
acab405e
DT
305 ;; TODO: What if "?" isn't in the font?
306 (let* ((char (or (font-ref font c) (font-ref font #\?)))
fedd9bca 307 (texture (font-char-texture-region char))
233d13ca
DT
308 (dimensions (font-char-dimensions char))
309 (offset (font-char-offset char)))
fedd9bca
DT
310 (set-rect-x! rect (+ (vec2-x cursor) (vec2-x offset)))
311 (set-rect-y! rect (+ (vec2-y cursor) (vec2-y offset)))
312 (set-rect-width! rect (vec2-x dimensions))
313 (set-rect-height! rect (vec2-y dimensions))
314 (draw-sprite* texture rect matrix #:blend-mode blend-mode)
233d13ca 315 ;; Move forward to where the next character needs to be drawn.
45c5d680
DT
316 (set-vec2-x! cursor
317 (+ (vec2-x cursor)
233d13ca
DT
318 (vec2-x
319 (font-char-advance char))))))
fedd9bca
DT
320 (set-vec2-x! cursor 0.0)
321 (set-vec2-y! cursor 0.0)
322 (string-for-each render-char text))))
323
324(define %default-scale (vec2 1.0 1.0))
eb990ed5 325(define %null-vec2 (vec2 0.0 0.0))
fedd9bca
DT
326
327(define draw-text
328 (let ((matrix (make-null-matrix4)))
329 (lambda* (font
330 text
331 position
332 #:key
333 (origin %null-vec2)
334 (rotation 0)
335 (scale %default-scale)
336 (blend-mode 'alpha))
337 "Draw the string TEXT with the first character starting at
338POSITION using FONT."
339 (matrix4-2d-transform! matrix
340 #:origin origin
341 #:position position
342 #:rotation rotation
343 #:scale scale)
344 (draw-text* font text matrix #:blend-mode blend-mode))))