From 0c509f61a3edcb2bc4d3f305def55e3d4bd3c34f Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 3 Jul 2017 08:34:49 -0700 Subject: [PATCH] use pointers in directory --- pitfall/fontkit/directory.rkt | 51 ++++++++++------------------------- pitfall/fontkit/subset.rkt | 2 +- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/pitfall/fontkit/directory.rkt b/pitfall/fontkit/directory.rkt index a86ddcaf..c755dfe9 100644 --- a/pitfall/fontkit/directory.rkt +++ b/pitfall/fontkit/directory.rkt @@ -11,7 +11,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js (define TableEntry (+Struct (dictify 'tag (+String 4) 'checkSum uint32be - 'offset uint32be + 'offset (+Pointer uint32be 'void (mhash 'type 'global)) 'length uint32be))) ;; for stupid tags like 'cvt ' @@ -29,53 +29,30 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js this-res) (define/override (preEncode this-val stream) - (define preamble-length 12) - (define table-header-size (+ preamble-length - (* (length (hash-keys (· this-val tables))) (send TableEntry size)))) + (define tables (for/list ([(tag table) (in-hash (· this-val tables))]) + (mhash 'tag (unescape-tag tag) + 'checkSum 0 + 'offset (+VoidPointer (hash-ref table-codecs tag) table) + 'length (send (hash-ref table-codecs tag) size table)))) - (define-values (table-headers table-datas _) - (for/lists (ths tds lens) - ([(tag table) (in-hash (· this-val tables))]) - - (define table-data - (let ([es (+EncodeStream)]) - (send (hash-ref table-codecs tag) encode es table) - (send es dump))) - - (define table-header (mhash - 'tag (unescape-tag tag) - 'checkSum 0 - 'offset (apply + (cons table-header-size lens)) - 'length (bytes-length table-data))) - - (values table-header table-data (bytes-length table-data)))) - - - (define numTables (length table-headers)) + (define numTables (length tables)) (define searchRange (* (floor (log numTables 2)) 16)) (hash-set*! this-val 'tag "true" 'numTables numTables - 'tables table-headers + 'tables tables 'searchRange searchRange 'entrySelector (floor (/ searchRange (log 2))) - 'rangeShift (- (* numTables 16) searchRange) - 'data table-datas))) + 'rangeShift (- (* numTables 16) searchRange)))) -(define directory-common-dict (dictify 'tag (+String 4) +(define Directory (+RDirectory (dictify 'tag (+String 4) 'numTables uint16be 'searchRange uint16be 'entrySelector uint16be 'rangeShift uint16be - 'tables (+Array TableEntry 'numTables))) - -(define Directory (+RDirectory directory-common-dict)) + 'tables (+Array TableEntry 'numTables)))) -;; we don't know what tables we might get -;; so we represent as generic Buffer type, -;; and convert the tables to bytes manually in preEncode -(define EncodableDirectory (+RDirectory (append directory-common-dict (list (cons 'data (+Array (+RBuffer))))))) (define (directory-decode ip [options (mhash)]) (send Directory decode (+DecodeStream (port->bytes ip)))) @@ -84,6 +61,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js (directory-decode (open-input-file ps))) #;(test-module - (define ip (open-input-file charter-path)) - (define decoded-dir (deserialize (read (open-input-file charter-directory-path)))) - (check-equal? (directory-decode ip) decoded-dir)) \ No newline at end of file + (define ip (open-input-file charter-path)) + (define decoded-dir (deserialize (read (open-input-file charter-directory-path)))) + (check-equal? (directory-decode ip) decoded-dir)) \ No newline at end of file diff --git a/pitfall/fontkit/subset.rkt b/pitfall/fontkit/subset.rkt index 4dbe3c34..15c78b7d 100644 --- a/pitfall/fontkit/subset.rkt +++ b/pitfall/fontkit/subset.rkt @@ -124,7 +124,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js (define hhea (cloneDeep (send (· this font hhea) kv))) (dict-set! hhea 'numberOfMetrics (length (· this hmtx metrics))) - (send EncodableDirectory encode stream + (send Directory encode stream (mhash 'tables (mhash 'head head