Skip to content

Commit 063de5c

Browse files
committed
GLCORE: distinct datatype for glyphs
- enables easier runtime type checks - precomputes and caches texcoords for _way_ faster rendering
1 parent 47ad4ea commit 063de5c

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

modules/ln_glgui/primitives.scm

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,27 +216,79 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216216
;;; Reducing the computational complexity of font operations improves
217217
;;; rendering time.
218218

219+
(define-type ln-ttf:glyph
220+
macros: prefix: macro-
221+
desc ;; for now legacy: (key (texcoord1..4) X Y Z)
222+
width
223+
height
224+
texture
225+
texcoords ;; generic 4 element vector of flownums
226+
rect-texcoords ;; 4x2 element f32vector
227+
;; order is sorta important here
228+
offsetx
229+
advancex
230+
offsety
231+
)
232+
233+
(define (ln-ttf:glyph? obj) (macro-ln-ttf:glyph? obj))
234+
(define (ttf:glyph-desc obj) (macro-ln-ttf:glyph-desc obj))
235+
(define (ttf:glyph-width obj) (macro-ln-ttf:glyph-width obj))
236+
(define (ttf:glyph-height obj) (macro-ln-ttf:glyph-height obj))
237+
(define (ttf:glyph-image obj) (macro-ln-ttf:glyph-texture obj))
238+
(define (ttf:glyph-texcoords obj) (macro-ln-ttf:glyph-texcoords obj))
239+
(define (ttf:glyph-rect-texcoords obj) (macro-ln-ttf:glyph-rect-texcoords obj))
240+
(define (ttf:glyph-offsetx obj) (macro-ln-ttf:glyph-offsetx obj))
241+
(define (ttf:glyph-advancex obj) (macro-ln-ttf:glyph-advancex obj))
242+
(define (ttf:glyph-offsety obj) (macro-ln-ttf:glyph-offsety obj))
243+
219244
(define-type ln-ttf:font
220245
macros: prefix: macro-
246+
;; TBD: get rid of the key doubling - requires changes to call sites
221247
desc ;; for now the legacy description of a font as a assoc-list
222-
char->desc-table
248+
char->glyph-table
223249
)
224250

225251
(define (ln-ttf:font? obj) (macro-ln-ttf:font? obj))
226252

227253
(define find-font/desc)
228254

229-
(define (ln-ttf:font-ref font char) ;; -> glyph
255+
(define (ln-ttf:font-ref font char) ;; -> legacy glyph
230256
(cond
231257
((macro-ln-ttf:font? font) ;; TBD: leave this as the only case
232-
(table-ref (macro-ln-ttf:font-char->desc-table font) char #f))
258+
(let ((entry (table-ref (macro-ln-ttf:font-char->glyph-table font) char #f)))
259+
(and entry (macro-ln-ttf:glyph-desc entry))))
233260
(else (ln-ttf:font-ref (find-font font) char))))
234261

262+
(define (MATURITY+1:ln-ttf:font-ref font char) ;; -> glyph
263+
(cond
264+
((macro-ln-ttf:font? font) ;; TBD: leave this as the only case
265+
(table-ref (macro-ln-ttf:font-char->glyph-table font) char #f))
266+
(else (error "illegal arguments" MATURITY+1:ln-ttf:font-ref font char))))
267+
235268
(define (make-ln-ttf:font/desc fnt)
236-
(let ((font-table (list->table
237-
(let ((double-the-key (lambda (x) (cons (car x) x))))
238-
;; TBD: get rid of the doubling - requires changes to call sites
239-
(map double-the-key fnt)))))
269+
(define (convert desc g)
270+
(receive (img offsetx advancex offsety) (apply values g)
271+
(receive (width height texture x0 y0 x1 y1) (apply values img)
272+
(let ((texcoords (vector x0 y0 x1 y1))
273+
(rect-texcoords
274+
(f32vector
275+
x0 y1
276+
x1 y1
277+
x0 y0
278+
x1 y0)))
279+
(macro-make-ln-ttf:glyph
280+
desc width height texture
281+
texcoords rect-texcoords
282+
offsetx advancex offsety)))))
283+
(let ((font-table
284+
(list->table
285+
(let ((convert
286+
(lambda (x)
287+
;; TBD: get rid of the key doubling - requires changes to call sites
288+
(let ((k (car x))
289+
(v (cdr x)))
290+
(cons k (convert x v))))))
291+
(map convert fnt)))))
240292
(macro-make-ln-ttf:font fnt font-table)))
241293

242294
(define find-font

0 commit comments

Comments
 (0)