From e477bdbd4987e473e8196cfe7f2d7fc7603c37a4 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 4 Jul 2017 18:06:47 -0700 Subject: [PATCH] stuck on test 12 / gpos-test --- pitfall/fontkit/GPOS-test.rkt | 10 +++++-- pitfall/fontkit/GPOS.rkt | 4 +-- pitfall/fontkit/font.rkt | 2 +- pitfall/fontkit/subset.rkt | 17 +++++------ pitfall/fontkit/ttfglyph.rkt | 36 ++++++++++++------------ pitfall/pitfall/document.rkt | 18 +++++++----- pitfall/pitfall/embedded.rkt | 2 ++ pitfall/pitfall/test/test12.rkt | 4 +-- pitfall/xenomorph/private/lazy-array.rkt | 3 +- pitfall/xenomorph/private/struct.rkt | 3 +- 10 files changed, 55 insertions(+), 44 deletions(-) diff --git a/pitfall/fontkit/GPOS-test.rkt b/pitfall/fontkit/GPOS-test.rkt index b863cd09..9c66c9c5 100644 --- a/pitfall/fontkit/GPOS-test.rkt +++ b/pitfall/fontkit/GPOS-test.rkt @@ -1,9 +1,13 @@ #lang fontkit/racket -(require fontkit rackunit xenomorph racket/serialize) +(require fontkit "subset.rkt" rackunit xenomorph racket/serialize) (define fira-path "../pitfall/test/assets/fira.ttf") (define f (openSync fira-path)) (define gpos (· f GPOS)) -(define gsub (· f GSUB)) +(define ttfs (make-object TTFSubset f)) +(send ttfs encodeStream) + +#;(define gsub (· f GSUB)) + +#;(get (· gpos lookupList) 7) -(send (· gpos lookupList) get 1) diff --git a/pitfall/fontkit/GPOS.rkt b/pitfall/fontkit/GPOS.rkt index 91305940..cbfdffea 100644 --- a/pitfall/fontkit/GPOS.rkt +++ b/pitfall/fontkit/GPOS.rkt @@ -43,8 +43,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js (define/public (size val ctx) (send (buildStruct ctx) size val ctx)) - (define/public (decode stream parent) - (define res (send (buildStruct parent) decode stream parent)) + (define/public (decode port parent) + (define res (send (buildStruct parent) decode port parent)) (dict-remove! res 'rel) res) diff --git a/pitfall/fontkit/font.rkt b/pitfall/fontkit/font.rkt index 2c56ba5c..ddeff548 100644 --- a/pitfall/fontkit/font.rkt +++ b/pitfall/fontkit/font.rkt @@ -43,7 +43,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define-table-getters) (define/public (_getTableStream tag) - (define table (hash-ref (· this directory tables) tag)) + (define table (dict-ref (· this directory tables) tag)) (cond [table (pos port (· table offset)) diff --git a/pitfall/fontkit/subset.rkt b/pitfall/fontkit/subset.rkt index b217e655..354b4b1b 100644 --- a/pitfall/fontkit/subset.rkt +++ b/pitfall/fontkit/subset.rkt @@ -14,7 +14,10 @@ https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js (send this includeGlyph 0) ; always include the missing glyph in subset - + (define/public (encodeStream) + (define s (open-output-bytes)) + (send this encode s) + s) (as-methods includeGlyph)) @@ -65,7 +68,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js (match-define (list curOffset nextOffset) (take (drop (· this font loca offsets) gid) 2)) (define stream (send (· this font) _getTableStream 'glyf)) - (send stream pos (+ (send stream pos) curOffset)) + (pos (+ (pos stream) curOffset)) (define buffer (send stream readBuffer (- nextOffset curOffset))) @@ -73,7 +76,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js (when (and glyf (negative? (· glyf numberOfContours))) (for ([component (in-list (· glyf components))]) (define gid (send this includeGlyph (· component glyphID))) - (bytes-copy! buffer (· component pos) (send uint16be encode #f gid)))) + (bytes-copy! buffer (pos component) (send uint16be encode #f gid)))) ;; skip variation shit @@ -108,17 +111,16 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js (define gid (list-ref (· this glyphs) idx)) (send this _addGlyph gid)) - (define maxp (cloneDeep (send (· this font maxp) dump))) + (define maxp (cloneDeep (dump (· this font maxp)))) (dict-set! maxp 'numGlyphs (length (· this glyf))) ;; populate the new loca table (dict-update! (· this loca) 'offsets (λ (vals) (append vals (list (· this offset))))) (loca-pre-encode (· this loca)) - - (define head (cloneDeep (send (· this font head) dump))) + (define head (cloneDeep (dump (· this font head)))) (dict-set! head 'indexToLocFormat (· this loca version)) - (define hhea (cloneDeep (send (· this font hhea) dump))) + (define hhea (cloneDeep (dump (· this font hhea)))) (dict-set! hhea 'numberOfMetrics (length (· this hmtx metrics))) (send Directory encode stream @@ -141,4 +143,3 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js (void) ) - diff --git a/pitfall/fontkit/ttfglyph.rkt b/pitfall/fontkit/ttfglyph.rkt index 095becf6..223d5cd6 100644 --- a/pitfall/fontkit/ttfglyph.rkt +++ b/pitfall/fontkit/ttfglyph.rkt @@ -86,13 +86,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js ;; Nothing to do if there is no data for this glyph (and (not (= glyfPos nextPos)) (let () - (define stream (send _font _getTableStream 'glyf)) - (send stream pos (+ (send stream pos) glyfPos)) - (define startPos (· stream pos)) - (define glyph (send GlyfHeader decode stream)) + (define port (send _font _getTableStream 'glyf)) + (pos port (+ (pos port) glyfPos)) + (define startPos (pos port)) + (define glyph (decode GlyfHeader port)) (match (· glyph numberOfContours) - [(? positive?) (_decodeSimple glyph stream)] - [(? negative?) (_decodeComposite glyph stream startPos)]) + [(? positive?) (_decodeSimple glyph port)] + [(? negative?) (_decodeComposite glyph port startPos)]) glyph))) (define/public (_decodeSimple glyph port) @@ -130,7 +130,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js (values (cons point points) next-px next-py))) (dict-set! glyph 'points (reverse points))) - (define/public (_decodeComposite glyph stream [offset 0]) + (define/public (_decodeComposite glyph port [offset 0]) ;; this is a composite glyph (dict-set! glyph 'components empty) (define haveInstructions #f) @@ -139,33 +139,33 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js (dict-set! glyph 'components (for/list ([i (in-naturals)] #:break (zero? (bitwise-and flags MORE_COMPONENTS))) - (set! flags (send uint16be decode stream)) - (define gPos (- (send stream pos) offset)) - (define glyphID (send uint16be decode stream)) + (set! flags (send uint16be decode port)) + (define gPos (- (pos port) offset)) + (define glyphID (send uint16be decode port)) (unless haveInstructions (set! haveInstructions (not (zero? (bitwise-and flags WE_HAVE_INSTRUCTIONS))))) (match-define (list dx dy) (let ([decoder (if (not (zero? (bitwise-and flags ARG_1_AND_2_ARE_WORDS))) int16be int8)]) - (list (send decoder decode stream) (send decoder decode stream)))) + (list (send decoder decode port) (send decoder decode port)))) (define component (+Component glyphID dx dy)) (set-field! pos component gPos) (cond [(not (zero? (bitwise-and flags WE_HAVE_A_SCALE))) - (define scale (read-fixed14 stream)) + (define scale (read-fixed14 port)) (set-field! scaleX component scale) (set-field! scaleY component scale)] [(not (zero? (bitwise-and flags WE_HAVE_AN_X_AND_Y_SCALE))) - (set-field! scaleX component (read-fixed14 stream)) - (set-field! scaleY component (read-fixed14 stream))] + (set-field! scaleX component (read-fixed14 port)) + (set-field! scaleY component (read-fixed14 port))] [(not (zero? (bitwise-and flags WE_HAVE_A_TWO_BY_TWO))) - (set-field! scaleX component (read-fixed14 stream)) - (set-field! scale01 component (read-fixed14 stream)) - (set-field! scale10 component (read-fixed14 stream)) - (set-field! scaleY component (read-fixed14 stream))]) + (set-field! scaleX component (read-fixed14 port)) + (set-field! scale01 component (read-fixed14 port)) + (set-field! scale10 component (read-fixed14 port)) + (set-field! scaleY component (read-fixed14 port))]) component)) haveInstructions)) diff --git a/pitfall/pitfall/document.rkt b/pitfall/pitfall/document.rkt index 080ac0b8..450218e2 100644 --- a/pitfall/pitfall/document.rkt +++ b/pitfall/pitfall/document.rkt @@ -50,7 +50,7 @@ end) (for ([(key val) (in-hash (hash-ref options 'info (hash)))]) ; if no 'info key, nothing will be copied from (hash) - (hash-set! info key val)) + (hash-set! info key val)) ;; Write the header (write this (format "%PDF-~a" pdf-version)) ; PDF version @@ -142,15 +142,15 @@ (flushPages this) (define _info (ref this)) (for ([(key val) (in-hash (· this info))]) - ;; upgrade string literal to String struct - (hash-set! (· _info payload) key (if (string? val) (String val) val))) + ;; upgrade string literal to String struct + (hash-set! (· _info payload) key (if (string? val) (String val) val))) #;(report* (· this _offsets)) (· _info end) (for ([font (in-hash-values (· this _fontFamilies))]) - (· font finalize)) + (· font finalize)) #;(report* (· this _offsets)) @@ -168,11 +168,15 @@ (this-write "xref") (this-write (format "0 ~a" (add1 (length this-offsets)))) (this-write "0000000000 65535 f ") + (let ([missing-offsets (for/list ([offset (in-list this-offsets)] + [idx (in-list this-idxs)] + #:unless (number? offset)) + idx)]) + (unless (empty? missing-offsets) + (raise-argument-error 'document:end "numerical offsets" missing-offsets))) (for ([offset (in-list this-offsets)] [idx (in-list this-idxs)]) - (unless (number? offset) - (raise-argument-error 'document:end (format "numerical offset for ref ~a" idx) offset)) - (this-write @string-append{@(~r offset #:min-width 10 #:pad-string "0") 00000 n })) + (this-write @string-append{@(~r offset #:min-width 10 #:pad-string "0") 00000 n })) (this-write "trailer") ;; trailer (this-write (convert (mhash 'Size (add1 (length this-offsets)) diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index 549abddb..5fae17db 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -78,6 +78,8 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee (when isCFF (hash-set! (· fontFile payload) 'Subtype "CIDFontType0C")) + (report* (· this subset)) + (send fontFile end (send (send (· this subset) encodeStream) dump)) (define familyClass (let ([val (if (send (· this font) has-table? 'OS/2) diff --git a/pitfall/pitfall/test/test12.rkt b/pitfall/pitfall/test/test12.rkt index f27bdd98..d68cb6bd 100644 --- a/pitfall/pitfall/test/test12.rkt +++ b/pitfall/pitfall/test/test12.rkt @@ -17,5 +17,5 @@ (define-runtime-path this "test12rkt.pdf") (make-doc this #f proc) -(define-runtime-path that "test12crkt.pdf") -(make-doc that #t proc #:pdfkit #f) \ No newline at end of file +#;(define-runtime-path that "test12crkt.pdf") +#;(make-doc that #t proc #:pdfkit #f) \ No newline at end of file diff --git a/pitfall/xenomorph/private/lazy-array.rkt b/pitfall/xenomorph/private/lazy-array.rkt index a267e9b2..af69bb9d 100644 --- a/pitfall/xenomorph/private/lazy-array.rkt +++ b/pitfall/xenomorph/private/lazy-array.rkt @@ -21,8 +21,7 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee (define/public-final (get index) (unless (<= 0 index (sub1 len)) - #;(raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index) - (void)) + (raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" (sub1 len)) index)) (ref! item-cache index (λ () (define orig-pos (pos port)) (pos port (+ starting-pos (* (send type size #f ctx) index))) diff --git a/pitfall/xenomorph/private/struct.rkt b/pitfall/xenomorph/private/struct.rkt index 1f5bdf7c..edf83aab 100644 --- a/pitfall/xenomorph/private/struct.rkt +++ b/pitfall/xenomorph/private/struct.rkt @@ -91,7 +91,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (define ctx (mhasheq 'parent parent 'val val 'pointerSize 0)) - (+ (for/sum ([(key type) (in-dict fields)]) + (+ (for/sum ([(key type) (in-dict fields)] + #:when (object? type)) (send type size (and val (ref val key)) ctx)) (if include-pointers (· ctx pointerSize) 0)))