stuck on test 12 / gpos-test

main
Matthew Butterick 7 years ago
parent ad60ed301c
commit e477bdbd49

@ -1,9 +1,13 @@
#lang fontkit/racket #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 fira-path "../pitfall/test/assets/fira.ttf")
(define f (openSync fira-path)) (define f (openSync fira-path))
(define gpos (· f GPOS)) (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)

@ -43,8 +43,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js
(define/public (size val ctx) (define/public (size val ctx)
(send (buildStruct ctx) size val ctx)) (send (buildStruct ctx) size val ctx))
(define/public (decode stream parent) (define/public (decode port parent)
(define res (send (buildStruct parent) decode stream parent)) (define res (send (buildStruct parent) decode port parent))
(dict-remove! res 'rel) (dict-remove! res 'rel)
res) res)

@ -43,7 +43,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define-table-getters) (define-table-getters)
(define/public (_getTableStream tag) (define/public (_getTableStream tag)
(define table (hash-ref (· this directory tables) tag)) (define table (dict-ref (· this directory tables) tag))
(cond (cond
[table [table
(pos port (· table offset)) (pos port (· table offset))

@ -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 (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 (as-methods
includeGlyph)) 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)) (match-define (list curOffset nextOffset) (take (drop (· this font loca offsets) gid) 2))
(define stream (send (· this font) _getTableStream 'glyf)) (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))) (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))) (when (and glyf (negative? (· glyf numberOfContours)))
(for ([component (in-list (· glyf components))]) (for ([component (in-list (· glyf components))])
(define gid (send this includeGlyph (· component glyphID))) (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 ;; 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)) (define gid (list-ref (· this glyphs) idx))
(send this _addGlyph gid)) (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))) (dict-set! maxp 'numGlyphs (length (· this glyf)))
;; populate the new loca table ;; populate the new loca table
(dict-update! (· this loca) 'offsets (λ (vals) (append vals (list (· this offset))))) (dict-update! (· this loca) 'offsets (λ (vals) (append vals (list (· this offset)))))
(loca-pre-encode (· this loca)) (loca-pre-encode (· this loca))
(define head (cloneDeep (dump (· this font head))))
(define head (cloneDeep (send (· this font head) dump)))
(dict-set! head 'indexToLocFormat (· this loca version)) (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))) (dict-set! hhea 'numberOfMetrics (length (· this hmtx metrics)))
(send Directory encode stream (send Directory encode stream
@ -141,4 +143,3 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
(void) (void)
) )

@ -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 ;; Nothing to do if there is no data for this glyph
(and (not (= glyfPos nextPos)) (and (not (= glyfPos nextPos))
(let () (let ()
(define stream (send _font _getTableStream 'glyf)) (define port (send _font _getTableStream 'glyf))
(send stream pos (+ (send stream pos) glyfPos)) (pos port (+ (pos port) glyfPos))
(define startPos (· stream pos)) (define startPos (pos port))
(define glyph (send GlyfHeader decode stream)) (define glyph (decode GlyfHeader port))
(match (· glyph numberOfContours) (match (· glyph numberOfContours)
[(? positive?) (_decodeSimple glyph stream)] [(? positive?) (_decodeSimple glyph port)]
[(? negative?) (_decodeComposite glyph stream startPos)]) [(? negative?) (_decodeComposite glyph port startPos)])
glyph))) glyph)))
(define/public (_decodeSimple glyph port) (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))) (values (cons point points) next-px next-py)))
(dict-set! glyph 'points (reverse points))) (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 ;; this is a composite glyph
(dict-set! glyph 'components empty) (dict-set! glyph 'components empty)
(define haveInstructions #f) (define haveInstructions #f)
@ -139,33 +139,33 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
(dict-set! glyph 'components (dict-set! glyph 'components
(for/list ([i (in-naturals)] (for/list ([i (in-naturals)]
#:break (zero? (bitwise-and flags MORE_COMPONENTS))) #:break (zero? (bitwise-and flags MORE_COMPONENTS)))
(set! flags (send uint16be decode stream)) (set! flags (send uint16be decode port))
(define gPos (- (send stream pos) offset)) (define gPos (- (pos port) offset))
(define glyphID (send uint16be decode stream)) (define glyphID (send uint16be decode port))
(unless haveInstructions (unless haveInstructions
(set! haveInstructions (not (zero? (bitwise-and flags WE_HAVE_INSTRUCTIONS))))) (set! haveInstructions (not (zero? (bitwise-and flags WE_HAVE_INSTRUCTIONS)))))
(match-define (match-define
(list dx dy) (list dx dy)
(let ([decoder (if (not (zero? (bitwise-and flags ARG_1_AND_2_ARE_WORDS))) int16be int8)]) (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)) (define component (+Component glyphID dx dy))
(set-field! pos component gPos) (set-field! pos component gPos)
(cond (cond
[(not (zero? (bitwise-and flags WE_HAVE_A_SCALE))) [(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! scaleX component scale)
(set-field! scaleY component scale)] (set-field! scaleY component scale)]
[(not (zero? (bitwise-and flags WE_HAVE_AN_X_AND_Y_SCALE))) [(not (zero? (bitwise-and flags WE_HAVE_AN_X_AND_Y_SCALE)))
(set-field! scaleX component (read-fixed14 stream)) (set-field! scaleX component (read-fixed14 port))
(set-field! scaleY component (read-fixed14 stream))] (set-field! scaleY component (read-fixed14 port))]
[(not (zero? (bitwise-and flags WE_HAVE_A_TWO_BY_TWO))) [(not (zero? (bitwise-and flags WE_HAVE_A_TWO_BY_TWO)))
(set-field! scaleX component (read-fixed14 stream)) (set-field! scaleX component (read-fixed14 port))
(set-field! scale01 component (read-fixed14 stream)) (set-field! scale01 component (read-fixed14 port))
(set-field! scale10 component (read-fixed14 stream)) (set-field! scale10 component (read-fixed14 port))
(set-field! scaleY component (read-fixed14 stream))]) (set-field! scaleY component (read-fixed14 port))])
component)) component))
haveInstructions)) haveInstructions))

@ -50,7 +50,7 @@
end) end)
(for ([(key val) (in-hash (hash-ref options 'info (hash)))]) ; if no 'info key, nothing will be copied from (hash) (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 the header
(write this (format "%PDF-~a" pdf-version)) ; PDF version (write this (format "%PDF-~a" pdf-version)) ; PDF version
@ -142,15 +142,15 @@
(flushPages this) (flushPages this)
(define _info (ref this)) (define _info (ref this))
(for ([(key val) (in-hash (· this info))]) (for ([(key val) (in-hash (· this info))])
;; upgrade string literal to String struct ;; upgrade string literal to String struct
(hash-set! (· _info payload) key (if (string? val) (String val) val))) (hash-set! (· _info payload) key (if (string? val) (String val) val)))
#;(report* (· this _offsets)) #;(report* (· this _offsets))
(· _info end) (· _info end)
(for ([font (in-hash-values (· this _fontFamilies))]) (for ([font (in-hash-values (· this _fontFamilies))])
(· font finalize)) (· font finalize))
#;(report* (· this _offsets)) #;(report* (· this _offsets))
@ -168,11 +168,15 @@
(this-write "xref") (this-write "xref")
(this-write (format "0 ~a" (add1 (length this-offsets)))) (this-write (format "0 ~a" (add1 (length this-offsets))))
(this-write "0000000000 65535 f ") (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)] (for ([offset (in-list this-offsets)]
[idx (in-list this-idxs)]) [idx (in-list this-idxs)])
(unless (number? offset) (this-write @string-append{@(~r offset #:min-width 10 #:pad-string "0") 00000 n }))
(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 "trailer") ;; trailer (this-write "trailer") ;; trailer
(this-write (convert (this-write (convert
(mhash 'Size (add1 (length this-offsets)) (mhash 'Size (add1 (length this-offsets))

@ -78,6 +78,8 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
(when isCFF (when isCFF
(hash-set! (· fontFile payload) 'Subtype "CIDFontType0C")) (hash-set! (· fontFile payload) 'Subtype "CIDFontType0C"))
(report* (· this subset))
(send fontFile end (send (send (· this subset) encodeStream) dump)) (send fontFile end (send (send (· this subset) encodeStream) dump))
(define familyClass (let ([val (if (send (· this font) has-table? 'OS/2) (define familyClass (let ([val (if (send (· this font) has-table? 'OS/2)

@ -17,5 +17,5 @@
(define-runtime-path this "test12rkt.pdf") (define-runtime-path this "test12rkt.pdf")
(make-doc this #f proc) (make-doc this #f proc)
(define-runtime-path that "test12crkt.pdf") #;(define-runtime-path that "test12crkt.pdf")
(make-doc that #t proc #:pdfkit #f) #;(make-doc that #t proc #:pdfkit #f)

@ -21,8 +21,7 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
(define/public-final (get index) (define/public-final (get index)
(unless (<= 0 index (sub1 len)) (unless (<= 0 index (sub1 len))
#;(raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index) (raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" (sub1 len)) index))
(void))
(ref! item-cache index (λ () (ref! item-cache index (λ ()
(define orig-pos (pos port)) (define orig-pos (pos port))
(pos port (+ starting-pos (* (send type size #f ctx) index))) (pos port (+ starting-pos (* (send type size #f ctx) index)))

@ -91,7 +91,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define ctx (mhasheq 'parent parent (define ctx (mhasheq 'parent parent
'val val 'val val
'pointerSize 0)) '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)) (send type size (and val (ref val key)) ctx))
(if include-pointers (· ctx pointerSize) 0))) (if include-pointers (· ctx pointerSize) 0)))

Loading…
Cancel
Save