more refac

main
Matthew Butterick 7 years ago
parent 0ecd9fa581
commit 47e0e0504e

@ -21,11 +21,12 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
(define (unescape-tag tag) (symbol-replace tag "_" " "))
(define-subclass Struct (RDirectory)
(define/override (process this-res stream)
(define/augride (process this-res stream ctx)
(define new-tables-val (mhash))
(for ([table (in-list (dict-ref this-res 'tables))])
(hash-set! new-tables-val (escape-tag (dict-ref table 'tag)) table))
(dict-set! this-res 'tables new-tables-val))
(for ([table (in-list (· this-res tables))])
(hash-set! new-tables-val (escape-tag (· table tag)) table))
(dict-set! this-res 'tables new-tables-val)
this-res)
(define/override (preEncode this-val stream)
(define preamble-length 12)

@ -12,13 +12,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js
|#
(define-subclass VersionedStruct (Rloca)
(define/override (process res stream)
(define/augride (process res stream ctx)
;; in `restructure` `process` method, `res` is aliased as `this`
;;
(when (= 16bit-style (· res version))
;; in a 16bits-style loca table, actual 32bit offset values are divided by 2 (to fit into 16 bits)
;; so we re-inflate them.
(dict-update! res 'offsets (λ (offsets) (map (curry * 2) offsets)))))
(dict-update! res 'offsets (λ (offsets) (map (curry * 2) offsets))))
res)
(define/override (preEncode this-val stream)
;; this = val to be encoded

@ -51,7 +51,7 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee
(error 'swap-bytes-unimplemented))]
[else (error 'unsupported-string-encoding)])))
#;(test-module
(test-module
(define es (+EncodeStream))
(check-true (EncodeStream? es))
(send es write #"AB")

@ -256,7 +256,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
1 (dictify 'name (+StringT uint8 'utf8)
'age uint8
'gender uint8)))])
(set-field! process struct (λ (o stream) (ref-set! o 'processed "true")))
(set-field! process struct (λ (o stream ctx) (ref-set! o 'processed "true") o))
(let ([stream (+DecodeStream (+Buffer "\x00\x05devon\x15"))])
(check-equal? (send (send struct decode stream) kv) (mhasheq 'name "devon"
'processed "true"

@ -15,7 +15,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(unless (and (dict? versions) (andmap (λ (val) (or (dict? val) (Struct? val))) (map cdr versions)))
(raise-argument-error 'VersionedStruct "dict of dicts or Structs" versions))
(inherit _setup _parseFields process)
(inherit _setup _parse-fields process)
(inherit-field fields)
(field [forced-version #f]
[versionGetter void]
@ -42,7 +42,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
[else (send type decode stream)]))
(when (ref versions 'header)
(_parseFields stream res (ref versions 'header)))
(_parse-fields stream res (ref versions 'header)))
(define fields (or (ref versions (ref res 'version)) (raise-argument-error 'VersionedStruct:decode "valid version key" (cons version (· this versions)))))
@ -50,7 +50,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(cond
[(VersionedStruct? fields) (send fields decode stream parent)]
[else
(_parseFields stream res fields)
(_parse-fields stream res fields)
(process res stream)
res]))

Loading…
Cancel
Save