From d6fff41bb8b3631faa5031ec3058f095dcee1549 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 17 Dec 2018 11:42:51 -0800 Subject: [PATCH] new notation --- fontland/fontland/directory.rkt | 7 +++---- fontland/fontland/font.rkt | 4 ++-- fontland/fontland/table-stream.rkt | 5 +---- fontland/fontland/table/loca.rkt | 19 ++++++++----------- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/fontland/fontland/directory.rkt b/fontland/fontland/directory.rkt index 5cf4cad8..6b10abb2 100644 --- a/fontland/fontland/directory.rkt +++ b/fontland/fontland/directory.rkt @@ -53,16 +53,15 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js 'rangeShift (- (* numTables 16) searchRange)) this-val) -(define Directory (+xstruct 'tag (+xsymbol #:length 4) +(define Directory (+xstruct #:pre-encode directory-pre-encode + #:post-decode directory-post-decode + 'tag (+xsymbol #:length 4) 'numTables uint16be 'searchRange uint16be 'entrySelector uint16be 'rangeShift uint16be 'tables (+xarray #:type table-entry #:length 'numTables))) -(set-pre-encode! Directory directory-pre-encode) -(set-post-decode! Directory directory-post-decode) - (define (directory-decode ip [options (mhash)]) (decode Directory ip)) diff --git a/fontland/fontland/font.rkt b/fontland/fontland/font.rkt index bf0e8188..34ec8a25 100644 --- a/fontland/fontland/font.rkt +++ b/fontland/fontland/font.rkt @@ -8,6 +8,7 @@ "db.rkt" "struct.rkt" "table-stream.rkt" + racket/class xenomorph racket/match sugar/unstable/dict @@ -35,8 +36,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define (+ttf-font port [decoded-tables (mhash)] [src (path->string (object-name port))] - [directory (delay (parameterize ([current-parent (mhash '_startOffset 0)]) - (xdecode Directory port)))] + [directory (delay (decode Directory port #:parent (mhash '_startOffset 0)))] [ft-face (delay (and src (FT_New_Face (force ft-library) src)))] [hb-font (delay (and src (hb_ft_font_create (force ft-face))))] [hb-buf (delay (hb_buffer_create))] diff --git a/fontland/fontland/table-stream.rkt b/fontland/fontland/table-stream.rkt index 2356fdfa..5285bd59 100644 --- a/fontland/fontland/table-stream.rkt +++ b/fontland/fontland/table-stream.rkt @@ -24,8 +24,6 @@ (hash-has-key? (· directory tables) (match tag [(? bytes?) (string->symbol (bytes->string/latin-1 tag))] [_ tag]))) - - (define (get-table this table-tag) (unless (has-table? this table-tag) @@ -48,6 +46,5 @@ (pos (ttf-font-port this) (· table offset)) (define table-bytes (open-input-bytes (peek-bytes (· table length) 0 (ttf-font-port this)))) (define table-decoder (hash-ref table-codecs table-tag)) - (parameterize ([current-parent this]) - (decode table-decoder table-bytes))) + (decode table-decoder table-bytes #:parent this)) diff --git a/fontland/fontland/table/loca.rkt b/fontland/fontland/table/loca.rkt index ce151565..dc4450a7 100644 --- a/fontland/fontland/table/loca.rkt +++ b/fontland/fontland/table/loca.rkt @@ -37,24 +37,21 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js val) (define loca (+xversioned-struct + #:pre-encode loca-pre-encode + #:post-decode loca-post-decode ;; todo: address ugliness to cross-ref head table from ttffont (λ (o) (hash-ref (force (ttf-font-get-head-table-proc o)) 'indexToLocFormat)) (dictify 0 (dictify 'offsets (+xarray #:type uint16be)) 1 (dictify 'offsets (+xarray #:type uint32be))))) -(set-pre-encode! loca loca-pre-encode) -(set-post-decode! loca loca-post-decode) - (define loca-v0 (+xversioned-struct - ;; todo: address ugliness to cross-ref head table from ttffont - 0 - (dictify - 0 (dictify 'offsets (+xarray #:type uint16be)) - 1 (dictify 'offsets (+xarray #:type uint32be))))) - -(set-pre-encode! loca-v0 loca-pre-encode) -(set-post-decode! loca-v0 loca-post-decode) + #:pre-encode loca-pre-encode + #:post-decode loca-post-decode + 0 + (dictify + 0 (dictify 'offsets (+xarray #:type uint16be)) + 1 (dictify 'offsets (+xarray #:type uint32be))))) (module+ test (require rackunit racket/serialize)