got first two parts of gpos

main
Matthew Butterick 8 years ago
parent a028c6fd95
commit bf3470d477

@ -5,14 +5,24 @@
(define f (openSync fira-path))
(define ds (send f _getTableStream 'GPOS))
#|
(file-position (· ds _port))
(peek-bytes 4 0 (· ds _port)) ; version
(peek-bytes 2 4 (· ds _port)) ; scriptList pointer
(peek-bytes 2 10 (· ds _port)) ; number of Scriptrecords
(send uint16be decode #"\0\n")
(define h (send GPOS decode ds))
(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

@ -42,7 +42,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js
(send (buildStruct ctx) size val ctx))
(define/override (decode stream parent)
(define/augride (decode stream parent)
(define res (send (buildStruct parent) decode stream parent))
(hash-remove! res 'rel)
res)
@ -185,8 +185,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js
;; GPOSLookup.versions[9].extension.type = GPOSLookup;
(define gpos-common-dict (dictify 'scriptList (+Pointer uint16be ScriptList) ; pointer
;'featureList (+Pointer uint16be FeatureList) ; pointer
;'lookupList (+Pointer uint16be (+LookupList GPOSLookup))
'featureList (+Pointer uint16be FeatureList) ; pointer
;'lookupList (+Pointer uint16be (LookupList GPOSLookup))
)) ; pointer
(define-subclass VersionedStruct (GPOS-VersionedStruct))

@ -14,7 +14,7 @@
(define LangSysRecord (+Struct
(dictify 'tag (+String 4)
'langSys (+Pointer uint16be LangSysTable (mhash 'type 'parent)))))
'langSys (+Pointer uint16be LangSysTable 'parent))))
(define Script (+Struct
(dictify 'defaultLangSys (+Pointer uint16be LangSysTable)
@ -24,13 +24,42 @@
(define-subclass Struct (ScriptRecord-Struct))
(define ScriptRecord (+ScriptRecord-Struct
(dictify 'tag (+String 4)
'script uint16be #;(+Pointer uint16be Script (mhash 'type 'parent)))))
(define-subclass Array (ScriptRecordArray)
(define/override (decode stream ctx)
(define val (super decode stream ctx))
(report (· this _len))
val))
(define ScriptList (+ScriptRecordArray ScriptRecord uint16be))
'script (+Pointer uint16be Script 'parent))))
(define ScriptList (+Array ScriptRecord uint16be))
;;#######################
;; Features and Lookups #
;;#######################
(define Feature (+Struct (dictify
'featureParams uint16be ; pointer
'lookupCount uint16be
'lookupListIndexes (+Array uint16be 'lookupCount))))
(define FeatureRecord (+Struct (dictify
'tag (+String 4)
'feature (+Pointer uint16be Feature 'parent))))
(define FeatureList (+Array FeatureRecord uint16be))
(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))
;;#############################################
;; Contextual Substitution/Positioning Tables #
;;#############################################
(define LookupRecord (+Struct
(dictify
@ -61,6 +90,10 @@
'lookupRecords (+Array LookupRecord 'lookupCount)))))
;;######################################################
;; Chaining Contextual Substitution/Positioning Tables #
;;######################################################
(define ChainingContext
(+VersionedStruct
uint16be

@ -45,7 +45,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/post.js
(check-equal? offset 41520)
(check-equal? len 514)
(define ds (+DecodeStream (peek-bytes len offset ip)))
(define version (send fixed32be decode ds))
(define version (send fixed32be decode ds)) ; version = 2
(send post force-version! version)
(define table-data (send post decode ds))
(check-equal? (· table-data underlineThickness) 58)

@ -8,7 +8,6 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|#
(define-subclass Streamcoder (Array type [_length #f] [lengthType 'count])
(field [_len #f])
(define/augride (decode stream [parent #f])
(let ([len (cond
@ -20,7 +19,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(unless (andmap (λ (x) (and x (number? x))) (list num denom))
(raise-argument-error 'Array:decode "valid length and size" (list num denom)))
(floor (/ (send stream length) (send type size)))])])
(set! _len len)
(caseq lengthType
[(count) (for/list ([i (in-range len)])
(send type decode stream this))])))

@ -1,11 +1,17 @@
#lang racket/base
(require racket/class)
(require racket/class sugar/debug)
(provide (all-defined-out))
(define RestructureBase
(class object%
(super-new)
(abstract decode)
(field [starting-offset #f]
[parent #f])
(define/pubment (decode stream . args)
(set! starting-offset (and (object? stream) (send stream pos)))
(set! parent (and (pair? args) (is-a? (car args) RestructureBase) (car args)))
#;(report* starting-offset parent (and parent (get-field starting-offset parent)))
(inner (void) decode stream . args))
(abstract encode)
(abstract size)
(define/public (process . args) (void))

@ -8,7 +8,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee
|#
(define-subclass RestructureBase (Buffer [_length #xffff])
(define/override (decode stream [parent #f])
(define/augride (decode stream [parent #f])
(define len (resolveLength _length stream parent))
(send stream readBuffer len))

@ -6,19 +6,23 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
|#
(define-subclass RestructureBase (Pointer offsetType type [options (mhash)])
(define-subclass RestructureBase (Pointer offsetType type [scope 'local])
(and (symbol? scope) (caseq scope
[(local parent) 'yay]
[else (raise-argument-error 'Pointer "local or parent" scope)]))
(define/override (decode stream ctx)
(report (file-position (· stream _port)))
(define/augride (decode stream ctx)
#;(report* (· this starting-offset) (· this parent starting-offset))
(define offset (send offsetType decode stream ctx))
(define ptr offset)
(report* offset ptr)
(define ptr (+ offset (caseq scope
[(local) (· this parent starting-offset)]
[(parent) (· this parent parent starting-offset)])))
#;(report* offset ptr)
(cond
[type (define orig-pos (send stream pos))
(send stream pos ptr)
(define val (send type decode stream ctx))
(send stream pos orig-pos)
(report* options)
val]
[else ptr]))
@ -26,8 +30,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(define/override (encode stream val)
(error 'Pointer-encode-not-done))
(define/override (size val)
(error 'Pointer-size-not-done))
(define/override (size [val #f] [ctx #f])
(report* this offsetType type)
(report (send type size)))

@ -113,8 +113,7 @@ https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
;; Streamcoder is a helper class that checks / converts stream arguments before decode / encode
;; not a subclass of DecodeStream or EncodeStream, however.
(define-subclass RestructureBase (Streamcoder)
(define/overment (decode x . args)
(define/augment (decode x . args)
(define stream (if (bytes? x) (+DecodeStream x) x))
(unless (DecodeStream? stream)
(raise-argument-error 'Streamcoder:decode "bytes or DecodeStream" x))
@ -132,7 +131,7 @@ https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
(test-module
(define-subclass Streamcoder (Dummy)
(define/augment (decode stream) "foo")
(define/augment (decode stream . args) "foo")
(define/augment (encode stream val) "bar")
(define/override (size) 42))

@ -8,6 +8,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|#
(define-subclass Streamcoder (Struct [assocs (dictify)])
(field [res #f])
(unless (assocs? assocs)
(raise-argument-error 'Struct "assocs" assocs))
(field [key-index #f] ; store the original key order
@ -30,9 +32,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(update-fields! assocs)
(define/augride (decode stream [parent #f] [length 0])
(define res (_setup stream parent length))
(_parseFields stream res fields)
#;(hash-set! (hash-ref res '_props) '_currentOffset (· stream pos))
(set! res (_setup stream parent length))
(_parseFields stream fields)
(send this process res stream)
res)
@ -54,23 +55,15 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(send struct-type encode stream value-to-encode)))
(define/public-final (_setup stream parent length)
(define res (mhasheq))
;; define hidden properties
#;(hash-set! res '_props
(mhasheq 'parent (mhasheq 'value parent)
'_startOffset (mhasheq 'value (· stream pos))
'_currentOffset (mhasheq 'value 0 'writable #t)
'_length (mhasheq 'value length)))
res)
(mhasheq))
(define/public-final (_parseFields stream res fields)
(define/public-final (_parseFields stream fields)
(for ([key (in-list key-index)])
(define dictvalue (dict-ref fields key))
(define val
(if (procedure? dictvalue)
(dictvalue res)
(send dictvalue decode stream res)))
(send dictvalue decode stream this)))
(hash-set! res key val)))
(define/override (size [val (mhash)] [parent #f] [includePointers #t])
@ -106,6 +99,7 @@ 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 (and (dict? versions) (andmap (λ (val) (or (dict? val) (Struct? val))) (map cdr versions)))
@ -117,7 +111,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(set! forced-version version))
(define/override (decode stream [parent #f] [length 0])
(define res (send this _setup stream parent length))
(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]
@ -130,7 +124,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(cond
[(VersionedStruct? fields) (send fields decode stream parent)]
[else
(send this _parseFields stream res fields)
(send this _parseFields stream fields)
(send this process res stream)
res]))
@ -157,3 +151,4 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(define bs (apply bytes (for/list ([i (in-range struct-size)])
(random 256))))
(check-equal? (send vs encode #f (send vs decode bs)) bs)))

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