use pointers in directory

main
Matthew Butterick 7 years ago
parent 7239b72eea
commit 0c509f61a3

@ -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))
(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))

@ -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

Loading…
Cancel
Save