From 2bb9a0afefe513d05c1ba32a7f65f842e64d7d69 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 11 Jun 2017 21:17:18 -0700 Subject: [PATCH] more --- pitfall/fontkit/font.rkt | 4 +- pitfall/fontkit/loca.rkt | 235 +-------------------------------- pitfall/fontkit/subset.rkt | 2 +- pitfall/restructure/struct.rkt | 22 +-- 4 files changed, 20 insertions(+), 243 deletions(-) diff --git a/pitfall/fontkit/font.rkt b/pitfall/fontkit/font.rkt index 6a2543b9..7487ae10 100644 --- a/pitfall/fontkit/font.rkt +++ b/pitfall/fontkit/font.rkt @@ -37,7 +37,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define offset (· (hash-ref (· directory tables) table-tag) offset)) (define len (· (hash-ref (· directory tables) table-tag) length)) (set-port-position! stream 0) - (send table-decoder decode (make-object RDecodeStream (peek-bytes len offset stream)) this length)) + (send table-decoder decode (+DecodeStream (peek-bytes len offset stream)) this length)) (define/public (_decodeDirectory) (set! directory (directory-decode stream (mhash '_startOffset 0))) @@ -280,6 +280,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (check-exn exn:fail:contract? (λ () (send f _getTable 'nonexistent-table-tag))) #;(send f _getTable 'maxp) (define subset (make-object TTFSubset f)) - (send subset encode (make-object REncodeStream)) + (send subset encode (+EncodeStream)) ) \ No newline at end of file diff --git a/pitfall/fontkit/loca.rkt b/pitfall/fontkit/loca.rkt index 721df794..0b317623 100644 --- a/pitfall/fontkit/loca.rkt +++ b/pitfall/fontkit/loca.rkt @@ -50,237 +50,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js (define len (· dir tables loca length)) (check-equal? offset 38692) (check-equal? len 460) - (set-port-position! ip 0) (define ds (+DecodeStream (peek-bytes len offset ip))) - (define table-data (send loca decode ds #:version 0)) + (send loca force-version! 0) + (define table-data (send loca decode ds)) (check-equal? (length (· table-data offsets)) 230) - (check-equal? (· table-data offsets) '(0 - 0 - 0 - 136 - 296 - 500 - 864 - 1168 - 1548 - 1628 - 1716 - 1804 - 1944 - 2048 - 2128 - 2176 - 2256 - 2312 - 2500 - 2596 - 2788 - 3052 - 3168 - 3396 - 3624 - 3732 - 4056 - 4268 - 4424 - 4564 - 4640 - 4728 - 4804 - 5012 - 5384 - 5532 - 5808 - 6012 - 6212 - 6456 - 6672 - 6916 - 7204 - 7336 - 7496 - 7740 - 7892 - 8180 - 8432 - 8648 - 8892 - 9160 - 9496 - 9764 - 9936 - 10160 - 10312 - 10536 - 10780 - 10992 - 11148 - 11216 - 11272 - 11340 - 11404 - 11444 - 11524 - 11820 - 12044 - 12216 - 12488 - 12728 - 12932 - 13324 - 13584 - 13748 - 13924 - 14128 - 14232 - 14592 - 14852 - 15044 - 15336 - 15588 - 15776 - 16020 - 16164 - 16368 - 16520 - 16744 - 16984 - 17164 - 17320 - 17532 - 17576 - 17788 - 17896 - 18036 - 18284 - 18552 - 18616 - 18988 - 19228 - 19512 - 19712 - 19796 - 19976 - 20096 - 20160 - 20224 - 20536 - 20836 - 20876 - 21000 - 21200 - 21268 - 21368 - 21452 - 21532 - 21720 - 21908 - 22036 - 22244 - 22664 - 22872 - 22932 - 22992 - 23088 - 23220 - 23268 - 23372 - 23440 - 23600 - 23752 - 23868 - 23988 - 24084 - 24184 - 24224 - 24548 - 24788 - 25012 - 25292 - 25716 - 25884 - 26292 - 26396 - 26540 - 26796 - 27172 - 27488 - 27512 - 27536 - 27560 - 27584 - 27912 - 27936 - 27960 - 27984 - 28008 - 28032 - 28056 - 28080 - 28104 - 28128 - 28152 - 28176 - 28200 - 28224 - 28248 - 28272 - 28296 - 28320 - 28344 - 28368 - 28392 - 28416 - 28440 - 28464 - 28488 - 28512 - 28536 - 28560 - 28968 - 28992 - 29016 - 29040 - 29064 - 29088 - 29112 - 29136 - 29160 - 29184 - 29208 - 29232 - 29256 - 29280 - 29304 - 29328 - 29352 - 29376 - 29400 - 29424 - 29448 - 29472 - 29496 - 29520 - 29824 - 30164 - 30220 - 30652 - 30700 - 30956 - 31224 - 31248 - 31332 - 31488 - 31636 - 31916 - 32104 - 32176 - 32484 - 32744 - 32832 - 32956 - 33248 - 33664 - 33884 - 34048 - 34072))) + (check-equal? (· table-data offsets) '(0 0 0 136 296 500 864 1168 1548 1628 1716 1804 1944 2048 2128 2176 2256 2312 2500 2596 2788 3052 3168 3396 3624 3732 4056 4268 4424 4564 4640 4728 4804 5012 5384 5532 5808 6012 6212 6456 6672 6916 7204 7336 7496 7740 7892 8180 8432 8648 8892 9160 9496 9764 9936 10160 10312 10536 10780 10992 11148 11216 11272 11340 11404 11444 11524 11820 12044 12216 12488 12728 12932 13324 13584 13748 13924 14128 14232 14592 14852 15044 15336 15588 15776 16020 16164 16368 16520 16744 16984 17164 17320 17532 17576 17788 17896 18036 18284 18552 18616 18988 19228 19512 19712 19796 19976 20096 20160 20224 20536 20836 20876 21000 21200 21268 21368 21452 21532 21720 21908 22036 22244 22664 22872 22932 22992 23088 23220 23268 23372 23440 23600 23752 23868 23988 24084 24184 24224 24548 24788 25012 25292 25716 25884 26292 26396 26540 26796 27172 27488 27512 27536 27560 27584 27912 27936 27960 27984 28008 28032 28056 28080 28104 28128 28152 28176 28200 28224 28248 28272 28296 28320 28344 28368 28392 28416 28440 28464 28488 28512 28536 28560 28968 28992 29016 29040 29064 29088 29112 29136 29160 29184 29208 29232 29256 29280 29304 29328 29352 29376 29400 29424 29448 29472 29496 29520 29824 30164 30220 30652 30700 30956 31224 31248 31332 31488 31636 31916 32104 32176 32484 32744 32832 32956 33248 33664 33884 34048 34072))) diff --git a/pitfall/fontkit/subset.rkt b/pitfall/fontkit/subset.rkt index 7f36b4c1..c7f6d058 100644 --- a/pitfall/fontkit/subset.rkt +++ b/pitfall/fontkit/subset.rkt @@ -59,7 +59,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js ;; name, cmap, OS/2, post (define/contract (encode this stream) - ((is-a?/c REncodeStream) . ->m . void?) + (EncodeStream? . ->m . void?) (set-field! glyf this empty) (set-field! offset this 0) (set-field! loca this (mhash 'offsets empty)) diff --git a/pitfall/restructure/struct.rkt b/pitfall/restructure/struct.rkt index af03429d..7758decd 100644 --- a/pitfall/restructure/struct.rkt +++ b/pitfall/restructure/struct.rkt @@ -9,13 +9,13 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (define-subclass Streamcoder (Struct [assocs (dictify)]) (field [key-index (map car assocs)] ; store the original key order - [struct-types (mhash)]) + [fields (mhash)]) (for ([(k v) (in-dict assocs)]) - (hash-set! struct-types k v)) + (hash-set! fields k v)) (define/augride (decode stream [parent #f] [length 0]) (define res (_setup stream parent length)) - (_parseFields stream res struct-types) + (_parseFields stream res fields) #;(hash-set! (hash-ref res '_props) '_currentOffset (· stream pos)) (send this process res stream) res) @@ -30,7 +30,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (send this preEncode input-hash stream) (for* ([key (in-list key-index)] ; iterate over original keys in order - [struct-type (in-value (hash-ref struct-types key))] + [struct-type (in-value (hash-ref fields key))] [value-to-encode (in-value (hash-ref input-hash key))]) (send struct-type encode stream value-to-encode))) @@ -55,7 +55,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (hash-set! res key val))) (define/override (size [val (mhash)] [parent #f] [includePointers #t]) - (for/sum ([(key type) (in-hash struct-types)] + (for/sum ([(key type) (in-hash fields)] #:when (hash-has-key? val key)) (send type size (hash-ref val key))))) @@ -67,16 +67,22 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee |# (define-subclass Struct (VersionedStruct type [versions (dictify)]) - (define/override (decode stream [parent #f] [length 0] #:version [maybe-version #f]) + (inherit-field fields key-index) + (field [forced-version #f]) + + (define/public (force-version! version) + (set! forced-version version)) + + (define/override (decode stream [parent #f] [length 0]) (define res (send this _setup stream parent length)) (define version (cond - [maybe-version] ; for testing purposes: pass an explicit version + [forced-version] ; for testing purposes: pass an explicit version [(procedure? type) (type parent)] [(RestructureBase? type) (send type decode stream)] [else (raise-argument-error 'decode "way of finding version" type)])) (hash-set! res 'version version) (set-field! fields this (dict-ref versions version (λ () (raise-argument-error 'RVersionedStruct:decode "valid version key" version)))) - (send this make-key-index! (· this fields)) + (set-field! key-index this (map car (· this fields))) (cond [(VersionedStruct? (· this fields)) (send (· this fields) decode stream parent)] [else