tests through 11 work

main
Matthew Butterick 7 years ago
parent fa104d0243
commit 4f33094e78

@ -6,4 +6,4 @@
(define gpos (· f GPOS))
(define gsub (· f GSUB))
(send (· gpos lookupList) get 9)
(send (· gpos lookupList) get 1)

@ -70,7 +70,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/OS2.js
(define len (· dir tables OS/2 length))
(check-equal? offset 360)
(check-equal? len 96)
(define ds (+DecodeStream (peek-bytes len offset ip)))
(define ds (open-input-bytes (peek-bytes len offset ip)))
(define version (send uint16be decode ds))
(send OS/2 force-version! version)
(define table-data (send OS/2 decode ds))

@ -8,7 +8,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/cvt.js
(define-subclass Struct (Rcvt_))
(define cvt_ (make-object Rcvt_
(define cvt_ (+Rcvt_
(dictify
'controlValues (+Array int16be))))
@ -23,10 +23,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/cvt.js
(set-port-position! ip 0)
(define table-bytes #"\0\24\0+\0S\0\0\0\20\377&\0\0\1\341\0\v\2\237\0\22\2\340\0\b")
(check-equal? table-bytes (peek-bytes len offset ip))
(define ds (+DecodeStream (peek-bytes len offset ip)))
(define ds (open-input-bytes (peek-bytes len offset ip)))
(define cvt-array '(20 43 83 0 16 -218 0 481 11 671 18 736 8))
(check-equal? (hash-ref (send cvt_ decode ds) 'controlValues) cvt-array)
(define es (+EncodeStream))
(send cvt_ encode es (mhash 'controlValues cvt-array))
(check-equal? (send es dump) table-bytes)
)
(check-equal? (dict-ref (decode cvt_ ds) 'controlValues) cvt-array)
(check-equal? (encode cvt_ (mhash 'controlValues cvt-array) #f) table-bytes))

@ -20,14 +20,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
(define (unescape-tag tag) (symbol-replace tag "_" " "))
(define-subclass Struct (RDirectory)
(define/augride (process this-res stream ctx)
(define/augride (post-decode this-res stream ctx)
(define new-tables-val (mhash))
(for ([table (in-list (· this-res tables))])
(hash-set! new-tables-val (escape-tag (· table tag)) table))
(dict-set! this-res 'tables new-tables-val)
this-res)
(define/override (preEncode this-val stream)
(define/override (pre-encode this-val stream)
(define tables (for/list ([(tag table) (in-hash (· this-val tables))])
(define table-codec (hash-ref table-codecs tag))
(mhash 'tag (unescape-tag tag)
@ -44,7 +44,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
'tables tables
'searchRange searchRange
'entrySelector (floor (/ searchRange (log 2)))
'rangeShift (- (* numTables 16) searchRange))))
'rangeShift (- (* numTables 16) searchRange))
this-val))
(define Directory (+RDirectory (dictify 'tag (+String 4)
'numTables uint16be
@ -55,7 +57,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
(define (directory-decode ip [options (mhash)])
(send Directory decode (+DecodeStream (port->bytes ip))))
(send Directory decode ip))
(define (file-directory-decode ps)
(directory-decode (open-input-file ps)))

@ -20,14 +20,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; This is the base class for all SFNT-based font formats in fontkit.
;; It supports TrueType, and PostScript glyphs, and several color glyph formats.
(define-subclass object% (TTFFont stream [_src #f])
(when stream (unless (DecodeStream? stream)
(raise-argument-error 'TTFFont "DecodeStream" stream)))
(unless (member (peek-bytes 4 0 (get-field _port stream)) (list #"true" #"OTTO" (bytes 0 1 0 0)))
(define-subclass object% (TTFFont port [_src #f])
(when port (unless (input-port? port)
(raise-argument-error 'TTFFont "input port" port)))
(unless (member (peek-bytes 4 0 port) (list #"true" #"OTTO" (bytes 0 1 0 0)))
(raise 'probe-fail))
;; skip variationCoords
(field [_directoryPos (send stream pos)]
(field [_directoryPos (pos port)]
[_tables (mhash)] ; holds decoded tables (loaded lazily)
[_glyphs (mhash)]
[_layoutEngine #f])
@ -46,8 +46,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define table (hash-ref (· this directory tables) tag))
(cond
[table
(send stream pos (· table offset))
stream]
(pos port (· table offset))
port]
[else #f]))
(define/public (_decodeTable table-tag)
@ -55,11 +55,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(λ () (raise-argument-error '_decodeTable "decodable table" table-tag))))
(define offset (· (hash-ref (· directory tables) table-tag) offset))
(define len (· (hash-ref (· directory tables) table-tag) length))
(send stream pos 0)
(send table-decoder decode (+DecodeStream (peek-bytes len offset (get-field _port stream))) this))
(pos port 0)
(decode table-decoder (open-input-bytes (peek-bytes len offset port)) #:parent this))
(define/public (_decodeDirectory)
(set! directory (send Directory decode stream (mhash '_startOffset 0)))
(set! directory (decode Directory port #:parent (mhash '_startOffset 0)))
directory)
(field [ft-library (FT_Init_FreeType)]
@ -301,7 +301,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; rather than use a `probe` function,
;; just try making a font with each format and see what happens
[font (in-value (with-handlers ([(curry eq? 'probe-fail) (λ (exn) #f)])
(make-object format (+DecodeStream buffer) filename)))]
(make-object format (open-input-bytes buffer) filename)))]
#:when font)
(if postscriptName
(send font getFont postscriptName) ; used to select from collection files like TTC

@ -25,377 +25,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/fpgm.js
(define len (· dir tables fpgm length))
(check-equal? offset 4140)
(check-equal? len 371)
(set-port-position! ip 0)
(define ds (+DecodeStream (peek-bytes len offset ip)))
(check-equal? (hash-ref (send fpgm decode ds) 'instructions) '(184
0
0
44
75
184
0
9
80
88
177
1
1
142
89
184
1
255
133
184
0
68
29
185
0
9
0
3
95
94
45
184
0
1
44
32
32
69
105
68
176
1
96
45
184
0
2
44
184
0
1
42
33
45
184
0
3
44
32
70
176
3
37
70
82
88
35
89
32
138
32
138
73
100
138
32
70
32
104
97
100
176
4
37
70
32
104
97
100
82
88
35
101
138
89
47
32
176
0
83
88
105
32
176
0
84
88
33
176
64
89
27
105
32
176
0
84
88
33
176
64
101
89
89
58
45
184
0
4
44
32
70
176
4
37
70
82
88
35
138
89
32
70
32
106
97
100
176
4
37
70
32
106
97
100
82
88
35
138
89
47
253
45
184
0
5
44
75
32
176
3
38
80
88
81
88
176
128
68
27
176
64
68
89
27
33
33
32
69
176
192
80
88
176
192
68
27
33
89
89
45
184
0
6
44
32
32
69
105
68
176
1
96
32
32
69
125
105
24
68
176
1
96
45
184
0
7
44
184
0
6
42
45
184
0
8
44
75
32
176
3
38
83
88
176
64
27
176
0
89
138
138
32
176
3
38
83
88
35
33
176
128
138
138
27
138
35
89
32
176
3
38
83
88
35
33
184
0
192
138
138
27
138
35
89
32
176
3
38
83
88
35
33
184
1
0
138
138
27
138
35
89
32
176
3
38
83
88
35
33
184
1
64
138
138
27
138
35
89
32
184
0
3
38
83
88
176
3
37
69
184
1
128
80
88
35
33
184
1
128
35
33
27
176
3
37
69
35
33
35
33
89
27
33
89
68
45
184
0
9
44
75
83
88
69
68
27
33
33
89
45)))
(check-equal? (pos ip 0) 0)
(check-equal? (dict-ref (send fpgm decode (peek-bytes len offset ip)) 'instructions) '(184 0 0 44 75 184 0 9 80 88 177 1 1 142 89 184 1 255 133 184 0 68 29 185 0 9 0 3 95 94 45 184 0 1 44 32 32 69 105 68 176 1 96 45 184 0 2 44 184 0 1 42 33 45 184 0 3 44 32 70 176 3 37 70 82 88 35 89 32 138 32 138 73 100 138 32 70 32 104 97 100 176 4 37 70 32 104 97 100 82 88 35 101 138 89 47 32 176 0 83 88 105 32 176 0 84 88 33 176 64 89 27 105 32 176 0 84 88 33 176 64 101 89 89 58 45 184 0 4 44 32 70 176 4 37 70 82 88 35 138 89 32 70 32 106 97 100 176 4 37 70 32 106 97 100 82 88 35 138 89 47 253 45 184 0 5 44 75 32 176 3 38 80 88 81 88 176 128 68 27 176 64 68 89 27 33 33 32 69 176 192 80 88 176 192 68 27 33 89 89 45 184 0 6 44 32 32 69 105 68 176 1 96 32 32 69 125 105 24 68 176 1 96 45 184 0 7 44 184 0 6 42 45 184 0 8 44 75 32 176 3 38 83 88 176 64 27 176 0 89 138 138 32 176 3 38 83 88 35 33 176 128 138 138 27 138 35 89 32 176 3 38 83 88 35 33 184 0 192 138 138 27 138 35 89 32 176 3 38 83 88 35 33 184 1 0 138 138 27 138 35 89 32 176 3 38 83 88 35 33 184 1 64 138 138 27 138 35 89 32 184 0 3 38 83 88 176 3 37 69 184 1 128 80 88 35 33 184 1 128 35 33 27 176 3 37 69 35 33 35 33 89 27 33 89 68 45 184 0 9 44 75 83 88 69 68 27 33 33 89 45)))

@ -18,9 +18,4 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/glyf.js
(check-equal? offset 4620)
(check-equal? len 34072)
(set-port-position! ip 0)
(define table-bytes (peek-bytes len offset ip))
(define ds (+DecodeStream table-bytes))
(define es (+EncodeStream))
(send glyf encode es empty)
#;(send es dump)
)
(define table-bytes (peek-bytes len offset ip)))

@ -42,19 +42,19 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/head.js
(define table-bytes #"\0\1\0\0\0\2\0\0.\252t<_\17<\365\0\t\3\350\0\0\0\0\316\3\301\261\0\0\0\0\316\3\304\364\377\36\377\24\4\226\3\324\0\2\0\t\0\2\0\0\0\0")
(set-port-position! ip 0)
(check-equal? (peek-bytes length offset ip) table-bytes)
(define table-data (send head decode (+DecodeStream table-bytes)))
(define table-data (send head decode table-bytes))
(check-equal? (· table-data unitsPerEm) 1000)
(check-equal? (· table-data yMin) -236)
(check-equal? (· table-data yMax) 980)
(check-equal? (· table-data xMax) 1174)
(check-equal? (· table-data xMin) -226)
(check-equal? (· table-data macStyle) (make-hash '((shadow . #f)
(extended . #f)
(condensed . #f)
(underline . #f)
(outline . #f)
(bold . #f)
(italic . #t))))
(check-equal? (· table-data macStyle) (make-hasheq '((shadow . #f)
(extended . #f)
(condensed . #f)
(underline . #f)
(outline . #f)
(bold . #f)
(italic . #t))))
(check-equal? (· table-data magicNumber) #x5F0F3CF5)
(check-equal? (· table-data indexToLocFormat) 0) ; used in loca table
(check-equal? (send head encode #f table-data) table-bytes))
(check-equal? (encode head table-data #f) table-bytes))

@ -32,7 +32,7 @@
(define table-bytes #"\0\1\0\0\3\324\377\22\0\0\4\311\377_\377`\4\251\0\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\345")
(set-port-position! ip 0)
(check-equal? (peek-bytes length offset ip) table-bytes)
(define table-data (send hhea decode (+DecodeStream table-bytes)))
(define table-data (decode hhea table-bytes))
(check-equal? (· table-data ascent) 980)
(check-equal? (· table-data descent) -238)
(check-equal? (· table-data numberOfMetrics) 229)

@ -34,11 +34,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/hmtx.js
(check-equal? hmtx-offset 456)
(check-equal? hmtx-length 916)
(define hmtx-bytes (peek-bytes hmtx-length hmtx-offset ip))
(define hmtx-data (send hmtx-test decode (+DecodeStream hmtx-bytes)))
(define hmtx-data (decode hmtx-test hmtx-bytes))
(check-equal? (send hmtx-test size) (* 229 (send HmtxEntry size)))
(define H-gid 41) (define OE-gid 142)
(check-equal? (send (· hmtx-data metrics) get H-gid) (mhasheq 'advance 738 'bearing 33))
(check-equal? (send (· hmtx-data metrics) get OE-gid) (mhasheq 'advance 993 'bearing 43))
(check-equal? (dump (send (· hmtx-data metrics) get H-gid)) (mhasheq 'advance 738 'bearing 33))
(check-equal? (dump (send (· hmtx-data metrics) get OE-gid)) (mhasheq 'advance 993 'bearing 43))
)

@ -12,7 +12,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js
|#
(define-subclass VersionedStruct (Rloca)
(define/augride (process res stream ctx)
(define/augride (post-decode res stream ctx)
;; in `xenomorph` `process` method, `res` is aliased as `this`
;;
(when (= 16bit-style (· res version))
@ -21,12 +21,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js
(dict-update! res 'offsets (λ (offsets) (map (curry * 2) offsets))))
res)
(define/override (preEncode this-val stream)
(define/override (pre-encode this-val stream)
;; this = val to be encoded
(loca-preEncode this-val stream)))
(loca-pre-encode this-val stream)
this-val))
;; make "static method"
(define (loca-preEncode this . args)
(define (loca-pre-encode this . args)
;; this = val to be encoded
(unless (dict-has-key? this 'version)
(dict-set! this 'version (if (> (last (· this offsets)) max-32-bit-value)
@ -49,7 +50,7 @@ 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)
(define ds (+DecodeStream (peek-bytes len offset ip)))
(define ds (peek-bytes len offset ip))
(check-equal?
(send loca encode #f (mhash 'version 0 'offsets '(0 76 156))) #"\0\0\0L\0\234")
(check-equal?

@ -33,6 +33,6 @@
(define maxp-bytes #"\0\1\0\0\0\345\0f\0\a\0O\0\4\0\1\0\0\0\0\0\n\0\0\2\0\1s\0\2\0\1")
(set-port-position! ip 0)
(check-equal? (peek-bytes maxp-length maxp-offset ip) maxp-bytes)
(define maxp-data (send maxp decode (+DecodeStream maxp-bytes)))
(define maxp-data (send maxp decode maxp-bytes))
(check-equal? (· maxp-data numGlyphs) 229)
(check-equal? (· maxp-data version) 65536))

@ -25,8 +25,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/post.js
1 null
2 (dictify 'numberOfGlyphs uint16be
'glyphNameIndex (+Array uint16be 'numberOfGlyphs)
;; this field causes problems due to deficiency in String class
;; 'names (+Array (+String uint8))
'names (+Array (+String uint8))
)
2.5 (dictify 'numberOfGlyphs uint16be
'offsets (+Array uint8))
@ -40,9 +39,10 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/post.js
(define len (· dir tables post length))
(check-equal? offset 41520)
(check-equal? len 514)
(define ds (+DecodeStream (peek-bytes len offset ip)))
(define version (send fixed32be decode ds)) ; version = 2
(define ds (open-input-bytes (peek-bytes len offset ip)))
(define version (decode fixed32be ds)) ; version = 2
(send post force-version! version)
(define table-data (send post decode ds))
(define table-data (decode post ds))
(check-equal? (· table-data underlineThickness) 58)
(check-equal? (· table-data underlinePosition) -178))
(check-equal? (· table-data underlinePosition) -178)
(check-equal? (· table-data names) '("periodcentered" "macron")))

@ -23,5 +23,5 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/prep.js
(set-port-position! ip 0)
(define table-bytes #"\270\0\0+\0\272\0\1\0\1\0\2+\1\272\0\2\0\1\0\2+\1\277\0\2\0C\0007\0+\0\37\0\23\0\0\0\b+\0\277\0\1\0\200\0i\0R\0;\0#\0\0\0\b+\0\272\0\3\0\5\0\a+\270\0\0 E}i\30D")
(check-equal? table-bytes (peek-bytes len offset ip))
(define ds (+DecodeStream (peek-bytes len offset ip)))
(check-equal? (hash-ref (send prep decode ds) 'controlValueProgram) '(184 0 0 43 0 186 0 1 0 1 0 2 43 1 186 0 2 0 1 0 2 43 1 191 0 2 0 67 0 55 0 43 0 31 0 19 0 0 0 8 43 0 191 0 1 0 128 0 105 0 82 0 59 0 35 0 0 0 8 43 0 186 0 3 0 5 0 7 43 184 0 0 32 69 125 105 24 68)))
(define ds (open-input-bytes (peek-bytes len offset ip)))
(check-equal? (dict-ref (decode prep ds) 'controlValueProgram) '(184 0 0 43 0 186 0 1 0 1 0 2 43 1 186 0 2 0 1 0 2 43 1 191 0 2 0 67 0 55 0 43 0 31 0 19 0 0 0 8 43 0 191 0 1 0 128 0 105 0 82 0 59 0 35 0 0 0 8 43 0 186 0 3 0 5 0 7 43 184 0 0 32 69 125 105 24 68)))

@ -14,10 +14,7 @@ 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 (+EncodeStream))
(send this encode s)
s)
(as-methods
includeGlyph))
@ -97,7 +94,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
;; name, cmap, OS/2, post
(define/contract (encode this stream)
(EncodeStream? . ->m . void?)
(output-port? . ->m . void?)
(set-field! glyf this empty)
(set-field! offset this 0)
(set-field! loca this (mhash 'offsets empty))
@ -115,7 +112,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
(dict-set! maxp 'numGlyphs (length (· this glyf)))
;; populate the new loca table
(dict-update! (· this loca) 'offsets (λ (vals) (append vals (list (· this offset)))))
(loca-preEncode (· this loca))
(loca-pre-encode (· this loca))
(define head (cloneDeep (send (· this font head) dump)))

@ -65,16 +65,16 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
(unfinished))
;; Parses a single glyph coordinate
(define/public (_parseGlyphCoord stream prev short same)
(unless (DecodeStream? stream)
(raise-argument-error '_parseGlyphCoord "DecodeStream" stream))
(define/public (_parseGlyphCoord port prev short same)
(unless (input-port? port)
(raise-argument-error '_parseGlyphCoord "input port" port))
(unless (number? prev)
(raise-argument-error '_parseGlyphCoord "number" prev))
(unless (and (boolean? short) (boolean? same))
(raise-argument-error '_parseGlyphCoord "booleans" (list short same)))
(+ prev (if short
((if (not same) - +) (send uint8 decode stream))
(if same 0 (send int16be decode stream)))))
((if (not same) - +) (decode uint8 port))
(if same 0 (decode int16be port)))))
;; Decodes the glyph data into points for simple glyphs,
@ -95,26 +95,26 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
[(? negative?) (_decodeComposite glyph stream startPos)])
glyph)))
(define/public (_decodeSimple glyph stream)
(define/public (_decodeSimple glyph port)
(unless (dict? glyph)
(raise-argument-error 'TTFGlyph-_decodeSimple "decoded RGlyfHeader" glyph))
(unless (DecodeStream? stream)
(raise-argument-error 'TTFGlyph-_decodeSimple "DecodeStream" stream))
(unless (input-port? port)
(raise-argument-error 'TTFGlyph-_decodeSimple "input port" port))
;; this is a simple glyph
(dict-set! glyph 'points empty)
(define endPtsOfContours (send (+Array uint16be (· glyph numberOfContours)) decode stream))
(dict-set! glyph 'instructions (send (+Array uint8be uint16be) decode stream))
(define endPtsOfContours (decode (+Array uint16be (· glyph numberOfContours)) port))
(dict-set! glyph 'instructions (decode (+Array uint8be uint16be) port))
(define numCoords (add1 (last endPtsOfContours)))
(define flags
(for*/lists (flags)
([i (in-naturals)]
#:break (= (length flags) numCoords)
[flag (in-value (send uint8 decode stream))]
[flag (in-value (decode uint8 port))]
[count (in-range (add1 (if (not (zero? (bitwise-and flag REPEAT)))
(send uint8 decode stream)
(decode uint8 port)
0)))])
flag))
@ -123,8 +123,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
(for/fold ([points empty] [px 0] [py 0])
([(flag i) (in-indexed flags)])
(define point (+Point (zero? (bitwise-and flag ON_CURVE)) (and (index-of endPtsOfContours i) #t) 0 0))
(define next-px (_parseGlyphCoord stream px (not (zero? (bitwise-and flag X_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_X)))))
(define next-py (_parseGlyphCoord stream py (not (zero? (bitwise-and flag Y_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_Y)))))
(define next-px (_parseGlyphCoord port px (not (zero? (bitwise-and flag X_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_X)))))
(define next-py (_parseGlyphCoord port py (not (zero? (bitwise-and flag Y_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_Y)))))
(set-field! x point next-px)
(set-field! y point next-py)
(values (cons point points) next-px next-py)))

@ -1,3 +1,3 @@
#lang reader (submod "private/racket.rkt" reader)
(r+p "private/stream.rkt")
(r+p "private/base.rkt")

@ -1,6 +1,7 @@
#lang reader (submod "private/racket.rkt" reader)
(r+p "array.rkt"
"base.rkt"
"bitfield.rkt"
"buffer.rkt"
"enum.rkt"

@ -9,7 +9,6 @@
"optional-test.rkt"
"pointer-test.rkt"
"reserved-test.rkt"
"stream-test.rkt"
"string-test.rkt"
"struct-test.rkt"
"versioned-struct-test.rkt")

Loading…
Cancel
Save