render: font: Allow rendering of substrings.
[chickadee.git] / chickadee / render / font.scm
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)
26 #:use-module (ice-9 match)
27 #:use-module (srfi srfi-1)
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
41 load-tile-font
42 font?
43 font-face
44 font-line-height
45 font-line-width
46 font-bold?
47 font-italic?
48 draw-text*
49 draw-text))
50
51 (define-record-type <font-char>
52 (make-font-char id texture-region offset dimensions advance)
53 font-char?
54 (id font-char-id)
55 (texture-region font-char-texture-region)
56 (offset font-char-offset)
57 (dimensions font-char-dimensions)
58 (advance font-char-advance))
59
60 (define-record-type <font>
61 (make-font face bold? italic? line-height chars kerning)
62 font?
63 (face font-face)
64 (bold? font-bold?)
65 (italic? font-italic?)
66 (line-height font-line-height)
67 (chars font-chars)
68 (kerning font-kerning))
69
70 (define (display-font font port)
71 (format port "#<font face: ~a line-height: ~d bold?: ~a italic?: ~a>"
72 (font-face font)
73 (font-line-height font)
74 (font-bold? font)
75 (font-italic? font)))
76
77 (set-record-type-printer! <font> display-font)
78
79 (define (font-line-width font text)
80 "Return the width of TEXT when rendered with FONT."
81 (let loop ((width 0.0)
82 (i 0))
83 (if (< i (string-length text))
84 (let ((char (or (font-ref font (string-ref text i))
85 (font-ref font #\?))))
86 (loop (+ width (vec2-x (font-char-advance char)))
87 (+ i 1)))
88 width)))
89
90 (define* (load-tile-font file tile-width tile-height characters #:key
91 (face "untitled") (margin 0) (spacing 0))
92 "Load the font named FACE from FILE, a bitmap image containing the
93 characters in the string CHARACTERS that are TILE-WIDTH by TILE-HEIGHT
94 pixels in size. The characters in the image *must* appear in the
95 order that they are specified in the character set or text will not
96 render properly. Optionally, each tile may have SPACING pixels of
97 horizontal and vertical space between surrounding tiles and the entire
98 image may have MARGIN pixels of empty space around its border."
99 (let* ((atlas (split-texture (load-image file) tile-width tile-height
100 #:margin margin
101 #:spacing spacing))
102 (chars
103 (let ((table (make-hash-table)))
104 (string-for-each-index
105 (lambda (i)
106 (hash-set! table (string-ref characters i)
107 (make-font-char (string-ref characters i)
108 (texture-atlas-ref atlas i)
109 (vec2 0.0 0.0)
110 (vec2 tile-width tile-height)
111 (vec2 tile-width 0.0))))
112 characters)
113 table))
114 ;; These fonts are by definition monospace fonts, so no
115 ;; kerning.
116 (kernings (make-hash-table)))
117 (make-font face #f #f tile-height chars kernings)))
118
119 (define (load-font file)
120 "Load the AngelCode formatted bitmap font within FILE. The file
121 extension must be either .xml or .fnt."
122 (cond
123 ((string-suffix? ".xml" file)
124 (parse-bmfont-sxml file (call-with-input-file file xml->sxml)))
125 ((string-suffix? ".fnt" file)
126 (parse-bmfont-sxml file (parse-fnt file)))
127 (else
128 (error "unknown bmfont file type: " file))))
129
130 (define (parse-fnt file)
131 (define (newline? char)
132 (eqv? char #\newline))
133 (define (whitespace? char)
134 (and (not (newline? char))
135 (char-set-contains? char-set:whitespace char)))
136 (define (letter? char)
137 (char-set-contains? char-set:letter char))
138 (define (consume-whitespace port)
139 (match (peek-char port)
140 ((? eof-object?) *unspecified*)
141 ((? whitespace?)
142 (read-char port)
143 (consume-whitespace port))
144 (_ *unspecified*)))
145 (define (read-tag port)
146 (list->symbol
147 (let loop ()
148 (match (peek-char port)
149 ((? letter? char)
150 (read-char port)
151 (cons char (loop)))
152 ((? whitespace? char)
153 '())))))
154 (define (read-key port)
155 (list->symbol
156 (let loop ()
157 (match (read-char port)
158 (#\= '())
159 ((? letter? char)
160 (cons char (loop)))))))
161 (define (read-quoted-string port)
162 (match (read-char port)
163 (#\" #t))
164 (list->string
165 (let loop ()
166 (match (read-char port)
167 (#\"
168 (if (or (whitespace? (peek-char port))
169 (newline? (peek-char port)))
170 '()
171 (cons #\" (loop))))
172 (char (cons char (loop)))))))
173 (define (read-unquoted-string port)
174 (list->string
175 (let loop ()
176 (match (peek-char port)
177 ((or (? whitespace?)
178 (? newline?))
179 '())
180 (char
181 (read-char port)
182 (cons char (loop)))))))
183 (define (read-value port)
184 (match (peek-char port)
185 (#\"
186 (read-quoted-string port))
187 (_ (read-unquoted-string port))))
188 (define (read-key/value-pair port)
189 (list (read-key port) (read-value port)))
190 (define (read-key/value-pairs port)
191 (cons '@
192 (let loop ()
193 (consume-whitespace port)
194 (match (peek-char port)
195 ((? newline?)
196 (read-char port)
197 '())
198 ((? letter?)
199 (cons (read-key/value-pair port)
200 (loop)))))))
201 (define (read-line port)
202 (list (read-tag port) (read-key/value-pairs port)))
203 `(*TOP*
204 (font
205 ,@(call-with-input-file file
206 (lambda (port)
207 (let loop ((pages '()))
208 (match (peek-char port)
209 ((? eof-object?)
210 `((pages (@ (count ,(number->string (length pages))))
211 ,@pages)))
212 ((? newline?)
213 (read-char port)
214 (loop pages))
215 ((? letter?)
216 (match (read-line port)
217 ((tag ('@ ('count count)))
218 (cons (cons* tag
219 `(@ (count ,count))
220 (list-tabulate (string->number count)
221 (lambda (i)
222 (read-line port))))
223 (loop pages)))
224 ((and ('page . _) page)
225 (loop (cons page pages)))
226 (exp (cons exp (loop pages))))))))))))
227
228 (define (parse-bmfont-sxml file tree)
229 (define directory (dirname file))
230 (define* (attr tree name #:optional (parse identity))
231 (let ((result ((sxpath `(@ ,name *text*)) tree)))
232 (if (null? result)
233 #f
234 (parse (car result)))))
235 (define (parse-pages nodes)
236 (let ((table (make-hash-table)))
237 (for-each (lambda (node)
238 (let* ((id (attr node 'id string->number))
239 (file (attr node 'file))
240 (texture (load-image
241 (string-append directory "/" file))))
242 (hash-set! table id texture)))
243 nodes)
244 table))
245 (define (string->character s)
246 (integer->char (string->number s)))
247 (define (parse-chars nodes pages image-width image-height line-height)
248 (define (x->s x)
249 (exact->inexact (/ x image-width)))
250 (define (y->t y)
251 (exact->inexact (/ y image-height)))
252 (let ((table (make-hash-table)))
253 (for-each (lambda (node)
254 (let* ((id (attr node 'id string->character))
255 (width (attr node 'width string->number))
256 (height (attr node 'height string->number))
257 (x (attr node 'x string->number))
258 ;; Invert the y axis. Our origin is the
259 ;; bottom-left corner, not top-left.
260 (y (- image-height height
261 (attr node 'y string->number)))
262 (x-offset (attr node 'xoffset string->number))
263 (y-offset (- line-height height
264 (attr node 'yoffset string->number)))
265 (x-advance (attr node 'xadvance string->number))
266 (page (or (attr node 'page string->number) 0))
267 (region (make-texture-region (hash-ref pages page)
268 (make-rect x y width height)))
269 (char (make-font-char id
270 region
271 (vec2 x-offset y-offset)
272 (vec2 width height)
273 (vec2 x-advance 0.0))))
274 (hash-set! table id char)))
275 nodes)
276 table))
277 (define (parse-kernings nodes)
278 (let ((table (make-hash-table)))
279 (for-each (lambda (node)
280 (let* ((first (attr node 'first string->character))
281 (second (attr node 'second string->character))
282 (x-offset (attr node 'amount string->number))
283 (inner-table (hash-ref table first)))
284 (if inner-table
285 (hash-set! inner-table second (vec2 x-offset 0.0))
286 (let ((inner-table (make-hash-table)))
287 (hash-set! inner-table second (vec2 x-offset 0.0))
288 (hash-set! table first inner-table)))))
289 nodes)
290 table))
291 (let* ((info ((sxpath '(font info)) tree))
292 (common ((sxpath '(font common)) tree))
293 (face (attr info 'face))
294 (bold? (attr info 'bold (const #t)))
295 (italic? (attr info 'italic (const #t)))
296 (line-height (attr common 'lineHeight string->number))
297 (image-width (attr common 'scaleW string->number))
298 (image-height (attr common 'scaleH string->number))
299 (pages (parse-pages ((sxpath '(font pages page)) tree)))
300 (chars (parse-chars ((sxpath '(font chars char)) tree)
301 pages
302 image-width
303 image-height
304 line-height))
305 (kernings (parse-kernings ((sxpath '(font kernings kerning)) tree))))
306 (make-font face bold? italic? line-height chars kernings)))
307
308 (define (font-ref font char)
309 (hashv-ref (font-chars font) char))
310
311 (define draw-text*
312 (let ((cursor (vec2 0.0 0.0))
313 (rect (make-rect 0.0 0.0 0.0 0.0)))
314 (lambda* (font text matrix #:key (blend-mode 'alpha)
315 (start 0) (end (string-length text)))
316 ;; TODO: Respect kerning.
317 (define (render-char c)
318 ;; TODO: What if "?" isn't in the font?
319 (let* ((char (or (font-ref font c) (font-ref font #\?)))
320 (texture (font-char-texture-region char))
321 (dimensions (font-char-dimensions char))
322 (offset (font-char-offset char)))
323 (set-rect-x! rect (+ (vec2-x cursor) (vec2-x offset)))
324 (set-rect-y! rect (+ (vec2-y cursor) (vec2-y offset)))
325 (set-rect-width! rect (vec2-x dimensions))
326 (set-rect-height! rect (vec2-y dimensions))
327 (draw-sprite* texture rect matrix #:blend-mode blend-mode)
328 ;; Move forward to where the next character needs to be drawn.
329 (set-vec2-x! cursor
330 (+ (vec2-x cursor)
331 (vec2-x
332 (font-char-advance char))))))
333 (set-vec2-x! cursor 0.0)
334 (set-vec2-y! cursor 0.0)
335 (string-for-each render-char text start end))))
336
337 (define %default-scale (vec2 1.0 1.0))
338 (define %null-vec2 (vec2 0.0 0.0))
339
340 (define draw-text
341 (let ((matrix (make-null-matrix4)))
342 (lambda* (font
343 text
344 position
345 #:key
346 (origin %null-vec2)
347 (rotation 0)
348 (scale %default-scale)
349 (blend-mode 'alpha)
350 (start 0)
351 (end (string-length text)))
352 "Draw the string TEXT with the first character starting at
353 POSITION using FONT."
354 (matrix4-2d-transform! matrix
355 #:origin origin
356 #:position position
357 #:rotation rotation
358 #:scale scale)
359 (draw-text* font text matrix #:blend-mode blend-mode
360 #:start start #:end end))))