diff --git a/pitfall/fontkit/GPOS-test.rkt b/pitfall/fontkit/GPOS-test.rkt index a55a0893..69d31aad 100644 --- a/pitfall/fontkit/GPOS-test.rkt +++ b/pitfall/fontkit/GPOS-test.rkt @@ -3,30 +3,6 @@ (define fira-path "../pitfall/test/assets/fira.ttf") (define f (openSync fira-path)) -(define ds (send f _getTableStream 'GPOS)) - -#| -(file-position (· ds _port)) -(send uint32be decode (peek-bytes 4 0 (· ds _port))) ; version -(define ptr (send uint16be decode (peek-bytes 2 4 (· ds _port)))) -ptr ; scriptList pointer -(send uint16be decode (peek-bytes 2 ptr (· ds _port))) ; number of Scriptrecords - -(file-position (· ds _port) 0) -(define offset 692) -(send uint32be decode (peek-bytes 4 offset (· ds _port))) ; version -(define ptr2 (send uint16be decode (peek-bytes 2 (+ 4 offset) (· ds _port)))) -ptr2 ; scriptList pointer -(send uint16be decode (peek-bytes 2 (+ ptr2 offset) (· ds _port))) ; number of Scriptrecords -|# - (report 'start-decode) -(define h (send GPOS decode (send f _getTableStream 'GPOS))) -h - - - -;(check-equal? (· h version) #x00010000) - -;h -;(check-equal? (length (· h scriptList)) 4) \ No newline at end of file +(define gpos-hash (send GPOS decode (send f _getTableStream 'GPOS))) +gpos-hash diff --git a/pitfall/fontkit/GPOS.rkt b/pitfall/fontkit/GPOS.rkt index 6aa0cf45..23853cfe 100644 --- a/pitfall/fontkit/GPOS.rkt +++ b/pitfall/fontkit/GPOS.rkt @@ -100,7 +100,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js (define-subclass VersionedStruct (GPOSLookup-VersionedStruct)) (define GPOSLookup (+GPOSLookup-VersionedStruct - (λ (parent) (· parent lookupType)) + 'lookupType (dictify ;; Single Adjustment 1 (+VersionedStruct uint16be @@ -186,7 +186,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js (define gpos-common-dict (dictify 'scriptList (+Pointer uint16be ScriptList) ; pointer 'featureList (+Pointer uint16be FeatureList) ; pointer - ;'lookupList (+Pointer uint16be (LookupList GPOSLookup)) + 'lookupList (+Pointer uint16be (LookupList GPOSLookup)) )) ; pointer (define-subclass VersionedStruct (GPOS-VersionedStruct)) diff --git a/pitfall/fontkit/opentype.rkt b/pitfall/fontkit/opentype.rkt index 5e3a1cac..58afe7fa 100644 --- a/pitfall/fontkit/opentype.rkt +++ b/pitfall/fontkit/opentype.rkt @@ -34,9 +34,9 @@ (define Feature (+Struct (dictify - 'featureParams uint16be ; pointer - 'lookupCount uint16be - 'lookupListIndexes (+Array uint16be 'lookupCount)))) + 'featureParams uint16be ; pointer + 'lookupCount uint16be + 'lookupListIndexes (+Array uint16be 'lookupCount)))) (define FeatureRecord (+Struct (dictify 'tag (+String 4) @@ -47,14 +47,16 @@ (define LookupFlags (+Bitfield uint16be '(rightToLeft ignoreBaseGlyphs ignoreLigatures ignoreMarks useMarkFilteringSet #f markAttachmentType))) (define (LookupList SubTable) - (define Lookup (+Struct - (dictify - 'lookupType uint16be - 'flags LookupFlags - 'subTableCount uint16be - 'subTables (+Array (+Pointer uint16be SubTable) 'subTableCount) - 'markFilteringSet uint16be))) ; TODO: only present when flags says so ... - (+LazyArray (+Pointer uint16be Lookup) uint16be)) + (+Array ; originally LazyArray + (+Pointer uint16be (+Struct + (dictify + 'lookupType uint16be + 'flags LookupFlags + 'subTableCount uint16be + ;; 'subTables (+Array (+Pointer uint16be SubTable) 'subTableCount) + 'subTables (+Array uint16be 'subTableCount) + 'markFilteringSet uint16be))) + uint16be)) ;;############################################# ;; Contextual Substitution/Positioning Tables # diff --git a/pitfall/restructure/bitfield.rkt b/pitfall/restructure/bitfield.rkt index fd6ebd2f..b7ed5bba 100644 --- a/pitfall/restructure/bitfield.rkt +++ b/pitfall/restructure/bitfield.rkt @@ -12,7 +12,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee (define/augment (decode stream . args) (for*/fold ([flag-hash (mhash)]) ([val (in-value (send type decode stream))] - [(flag i) (in-indexed flags)]) + [(flag i) (in-indexed flags)] + #:when flag) (hash-set! flag-hash flag (bitwise-bit-set? val i)) flag-hash)) @@ -20,20 +21,20 @@ https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee (define/augment (encode stream flag-hash) (define bitfield-int (for/sum ([(flag i) (in-indexed flags)] - #:when (hash-ref flag-hash flag)) + #:when (and flag (hash-ref flag-hash flag))) (expt 2 i))) (send type encode stream bitfield-int))) (test-module (require "number.rkt" "stream.rkt") - (define bfer (+Bitfield uint16be '(bold italic underline outline shadow condensed extended))) + (define bfer (+Bitfield uint16be '(bold italic underline #f shadow condensed extended))) (define bf (send bfer decode (+DecodeStream #"\0\25"))) + (check-equal? (length (hash-keys bf)) 6) ; omits #f flag (check-true (hash-ref bf 'bold)) (check-true (hash-ref bf 'underline)) (check-true (hash-ref bf 'shadow)) (check-false (hash-ref bf 'italic)) - (check-false (hash-ref bf 'outline)) (check-false (hash-ref bf 'condensed)) (check-false (hash-ref bf 'extended)) diff --git a/pitfall/restructure/pointer.rkt b/pitfall/restructure/pointer.rkt index d4454068..ddb6934f 100644 --- a/pitfall/restructure/pointer.rkt +++ b/pitfall/restructure/pointer.rkt @@ -32,8 +32,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee (define/override (size [val #f] [ctx #f]) - (report* this offsetType type) - (report (send type size))) + (report* this offsetType type (send type size))) diff --git a/pitfall/restructure/struct.rkt b/pitfall/restructure/struct.rkt index 85c1223d..0dfdc765 100644 --- a/pitfall/restructure/struct.rkt +++ b/pitfall/restructure/struct.rkt @@ -66,9 +66,12 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (send dictvalue decode stream this))) (hash-set! res key val))) - (define/override (size [val (mhash)] [parent #f] [includePointers #t]) + (define/overment (size [input-hash (mhash)] [parent #f] [includePointers #t]) + (inner (void) size input-hash parent includePointers) (for/sum ([(key type) (in-hash fields)]) - (send type size (hash-ref val key #f))))) + (define val (hash-ref input-hash key #f)) + (define args (if val (list val) empty)) + (send type size . args)))) (test-module @@ -100,8 +103,8 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee (define-subclass Struct (VersionedStruct version-resolver [versions (dictify)]) (inherit-field res) - (unless ((disjoin integer? procedure? RestructureBase?) version-resolver) - (raise-argument-error 'VersionedStruct "integer, function, or Restructure object" version-resolver)) + (unless ((disjoin integer? procedure? RestructureBase? symbol?) version-resolver) + (raise-argument-error 'VersionedStruct "integer, function, symbol, or Restructure object" version-resolver)) (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-field fields key-index) @@ -109,15 +112,19 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee (define/public-final (force-version! version) (set! forced-version version)) + + (define/public (resolve-version [stream #f] [parent #f]) + (cond + [forced-version] ; for testing purposes: pass an explicit version + [(integer? version-resolver) version-resolver] + [(symbol? version-resolver) (hash-ref (· parent res) version-resolver)] + [(and (procedure? version-resolver) (positive? (procedure-arity version-resolver))) (version-resolver parent)] + [(RestructureBase? version-resolver) (send version-resolver decode stream)] + [else (raise-argument-error 'VersionedStruct:resolve-version "way of finding version" version-resolver)])) (define/override (decode stream [parent #f] [length 0]) (set! res (send this _setup stream parent length)) - (define version (cond - [forced-version] ; for testing purposes: pass an explicit version - [(integer? version-resolver) version-resolver] - [(procedure? version-resolver) (version-resolver parent)] - [(RestructureBase? version-resolver) (send version-resolver decode stream)] - [else (raise-argument-error 'VersionedStruct:decode "way of finding version" version-resolver)])) + (define version (resolve-version stream parent)) (hash-set! res 'version version) (define assocs (dict-ref versions version (λ () (raise-argument-error 'VersionedStruct:decode "valid version key" version)))) (send this update-fields! assocs) @@ -130,6 +137,12 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee (define/augment (encode stream input-hash [parent #f]) (define assocs (dict-ref versions (· input-hash version) (λ () (raise-argument-error 'VersionedStruct:encode "valid version key" version)))) + (send this update-fields! assocs)) + + + (define/augment (size [val (mhash)] [parent #f] [includePointers #t]) + (define version (resolve-version #f parent)) + (define assocs (dict-ref versions version (λ () (raise-argument-error 'VersionedStruct:size "valid version key" version)))) (send this update-fields! assocs))) (test-module @@ -150,5 +163,11 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee (send num-type size))) (define bs (apply bytes (for/list ([i (in-range struct-size)]) (random 256)))) - (check-equal? (send vs encode #f (send vs decode bs)) bs))) + (check-equal? (send vs encode #f (send vs decode bs)) bs)) + + (define s (+Struct (dictify 'a uint8 'b uint8 'c uint8))) + (check-equal? (send s size) 3) + (define vs (+VersionedStruct (λ (p) 2) (dictify 1 (dictify 'd s) 2 (dictify 'e s 'f s)))) + (check-equal? (send vs size) 6)) +