next: unify helper modules

main
Matthew Butterick 7 years ago
parent 55ad632e5d
commit 2d9c09c036

@ -3,21 +3,28 @@
(provide (all-defined-out))
#|
https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
|#
(define TableEntry (make-object RStruct
(list (cons 'tag (make-object RString 4))
(cons 'checkSum uint32be)
(cons 'offset uint32be)
(cons 'length uint32be))))
(dictify 'tag (make-object RString 4)
'checkSum uint32be
'offset uint32be
'length uint32be)))
(define Directory (make-object RStruct
(list (cons 'tag (make-object RString 4))
(cons 'numTables uint16be)
(cons 'searchRange uint16be)
(cons 'entrySelector uint16be)
(cons 'rangeShift uint16be)
;; todo next: derive the `14` from 'numTables
(cons 'tables (make-object RArray TableEntry 14)))))
(define-subclass RStruct (RDirectory)
(super-new)
(define/public (process)
'boom))
(define Directory (make-object RDirectory
(dictify 'tag (make-object RString 4)
'numTables uint16be
'searchRange uint16be
'entrySelector uint16be
'rangeShift uint16be
'tables (make-object RArray TableEntry 'numTables))))
(module+ test
(require rackunit)

@ -47,6 +47,7 @@
(define-hashifier mhash make-hash)
(define-hashifier mhasheq make-hasheq)
(define-hashifier mhasheqv make-hasheqv)
(define (dictify . xs) (listify xs))
(module+ test
(check-equal? (mhash 'k "v") (make-hash (list (cons 'k "v")))))

@ -8,12 +8,15 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|#
(define-subclass RStreamcoder (RArray type [length #f] [lengthType 'count])
(define/augment (decode stream [parent #f])
(caseq lengthType
[(count) (for/list ([i (in-range length)])
(send type decode stream this))]))
(let ([length (if length
(resolveLength length stream parent)
(send stream length))])
(caseq lengthType
[(count) (for/list ([i (in-range length)])
(send type decode stream this))])))
(define/public (size) (unfinished))

@ -72,6 +72,9 @@
(define-hashifier mhasheq make-hasheq)
(define-hashifier mhasheqv make-hasheqv)
(provide dictify)
(define (dictify . xs) (listify xs))
(define (port-position port)
(define-values (l c p) (port-next-location port))
p)

@ -6,6 +6,6 @@
(cond
[(number? length) length]
[(procedure? length) (length parent)]
[(and parent (string? length) (· parent length))]
[(and parent (symbol? length) (hash-ref parent length))] ; treat as key into RStruct parent
[(and stream (is-a? length Number) (send length decode stream))]
[else (raise-argument-error 'resolveLength "fixed-size item" length)]))
Loading…
Cancel
Save