render: sprite: Use a single buffer for unbatched sprite mesh.
[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
649fe208 45 font-line-width
233d13ca
DT
46 font-bold?
47 font-italic?
fedd9bca 48 draw-text*
233d13ca
DT
49 draw-text))
50
51(define-record-type <font-char>
45c5d680 52 (make-font-char id texture-region offset dimensions advance)
233d13ca
DT
53 font-char?
54 (id font-char-id)
45c5d680 55 (texture-region font-char-texture-region)
233d13ca
DT
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
649fe208
DT
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 #\?))))
c9582b3f 86 (loop (+ width (vec2-x (font-char-advance char)))
649fe208
DT
87 (+ i 1)))
88 width)))
89
b61558f5
DT
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
93characters in the string CHARACTERS that are TILE-WIDTH by TILE-HEIGHT
94pixels in size. The characters in the image *must* appear in the
95order that they are specified in the character set or text will not
96render properly. Optionally, each tile may have SPACING pixels of
97horizontal and vertical space between surrounding tiles and the entire
98image 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
233d13ca 119(define (load-font file)
a4670ba8
DT
120 "Load the AngelCode formatted bitmap font within FILE. The file
121extension 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)
233d13ca
DT
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))
cbf59a78 258 (y (attr node 'y string->number))
233d13ca
DT
259 (x-offset (attr node 'xoffset string->number))
260 (y-offset (- line-height height
261 (attr node 'yoffset string->number)))
262 (x-advance (attr node 'xadvance string->number))
263 (page (or (attr node 'page string->number) 0))
45c5d680 264 (region (make-texture-region (hash-ref pages page)
fedd9bca 265 (make-rect x y width height)))
233d13ca 266 (char (make-font-char id
233d13ca
DT
267 region
268 (vec2 x-offset y-offset)
269 (vec2 width height)
270 (vec2 x-advance 0.0))))
271 (hash-set! table id char)))
272 nodes)
273 table))
274 (define (parse-kernings nodes)
275 (let ((table (make-hash-table)))
276 (for-each (lambda (node)
277 (let* ((first (attr node 'first string->character))
278 (second (attr node 'second string->character))
279 (x-offset (attr node 'amount string->number))
280 (inner-table (hash-ref table first)))
281 (if inner-table
282 (hash-set! inner-table second (vec2 x-offset 0.0))
283 (let ((inner-table (make-hash-table)))
284 (hash-set! inner-table second (vec2 x-offset 0.0))
285 (hash-set! table first inner-table)))))
286 nodes)
287 table))
a4670ba8 288 (let* ((info ((sxpath '(font info)) tree))
233d13ca
DT
289 (common ((sxpath '(font common)) tree))
290 (face (attr info 'face))
291 (bold? (attr info 'bold (const #t)))
292 (italic? (attr info 'italic (const #t)))
293 (line-height (attr common 'lineHeight string->number))
294 (image-width (attr common 'scaleW string->number))
295 (image-height (attr common 'scaleH string->number))
296 (pages (parse-pages ((sxpath '(font pages page)) tree)))
297 (chars (parse-chars ((sxpath '(font chars char)) tree)
298 pages
299 image-width
300 image-height
301 line-height))
302 (kernings (parse-kernings ((sxpath '(font kernings kerning)) tree))))
303 (make-font face bold? italic? line-height chars kernings)))
304
305(define (font-ref font char)
306 (hashv-ref (font-chars font) char))
307
fedd9bca 308(define draw-text*
45c5d680 309 (let ((cursor (vec2 0.0 0.0))
fedd9bca 310 (rect (make-rect 0.0 0.0 0.0 0.0)))
552f0615
DT
311 (lambda* (font text matrix #:key (blend-mode 'alpha)
312 (start 0) (end (string-length text)))
233d13ca
DT
313 ;; TODO: Respect kerning.
314 (define (render-char c)
acab405e
DT
315 ;; TODO: What if "?" isn't in the font?
316 (let* ((char (or (font-ref font c) (font-ref font #\?)))
fedd9bca 317 (texture (font-char-texture-region char))
233d13ca
DT
318 (dimensions (font-char-dimensions char))
319 (offset (font-char-offset char)))
fedd9bca
DT
320 (set-rect-x! rect (+ (vec2-x cursor) (vec2-x offset)))
321 (set-rect-y! rect (+ (vec2-y cursor) (vec2-y offset)))
322 (set-rect-width! rect (vec2-x dimensions))
323 (set-rect-height! rect (vec2-y dimensions))
324 (draw-sprite* texture rect matrix #:blend-mode blend-mode)
233d13ca 325 ;; Move forward to where the next character needs to be drawn.
45c5d680
DT
326 (set-vec2-x! cursor
327 (+ (vec2-x cursor)
233d13ca
DT
328 (vec2-x
329 (font-char-advance char))))))
3b847cdf 330 (set-vec2! cursor 0.0 0.0)
552f0615 331 (string-for-each render-char text start end))))
fedd9bca
DT
332
333(define %default-scale (vec2 1.0 1.0))
eb990ed5 334(define %null-vec2 (vec2 0.0 0.0))
fedd9bca
DT
335
336(define draw-text
337 (let ((matrix (make-null-matrix4)))
338 (lambda* (font
339 text
340 position
341 #:key
342 (origin %null-vec2)
343 (rotation 0)
344 (scale %default-scale)
552f0615
DT
345 (blend-mode 'alpha)
346 (start 0)
347 (end (string-length text)))
fedd9bca
DT
348 "Draw the string TEXT with the first character starting at
349POSITION using FONT."
350 (matrix4-2d-transform! matrix
351 #:origin origin
352 #:position position
353 #:rotation rotation
354 #:scale scale)
552f0615
DT
355 (draw-text* font text matrix #:blend-mode blend-mode
356 #:start start #:end end))))