bug in encoding empty glyf table

main
Matthew Butterick 8 years ago
parent ec675b61fa
commit 677a4291f1

@ -0,0 +1,23 @@
#lang fontkit/racket
(require restructure)
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/tables/glyf.js
|#
(define-subclass Struct (Rglyf))
(define glyf (+Array (+Buffer)))
(test-module
(define ip (open-input-file charter-path))
(define dir (deserialize (read (open-input-file charter-directory-path))))
(define offset (· dir tables glyf offset))
(define len (· dir tables glyf length))
(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))
)

@ -91,9 +91,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
'loca (· this loca)
'maxp maxp
'cvt (send (· this font) _getTable 'cvt)
;; todo: cvt
'prep (send (· this font) _getTable 'prep)
;; 'glyf (· this glyf)
'glyf (· this glyf)
'hmtx (· this hmtx)
'fpgm (send (· this font) _getTable 'fpgm)
)))

@ -9,4 +9,4 @@
(test-module (require (submod TABLE-ID-STRING test) ...))
(define ID (make-hasheq (map cons (list 'TABLE-ID ...) (list TABLE-ID ...)))))))
(define-table-decoders table-decoders maxp hhea head loca prep fpgm hmtx cvt)
(define-table-decoders table-decoders maxp hhea head loca prep fpgm hmtx cvt glyf)

@ -0,0 +1,36 @@
#lang restructure/racket
(require "number.rkt" "utils.rkt")
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee
|#
(define-subclass RestructureBase (Buffer [_length #f])
(define/override (decode stream [parent #f])
(define len (resolveLength _length stream parent))
(send stream readBuffer len))
(define/override (size [val #f] [parent #f])
(when val (unless (bytes? val)
(raise-argument-error 'Buffer:size "bytes" val)))
(if val
(bytes-length val)
(resolveLength _length val parent)))
(define/override (encode stream buf parent)
(when (Number? _length)
(send _length encode stream (bytes-length buf)))
(send stream writeBuffer buf)))
#;(test-module
(require "stream.rkt")
(define stream (+DecodeStream #"\2BCDEF"))
(define S (+String uint8 'utf8))
(check-equal? (send S decode stream) "BC")
(define os (+EncodeStream))
(send S encode os "Mike")
(check-equal? (send os dump) #"\4Mike")
(check-equal? (send (+String) size "foobar") 6))

@ -5,4 +5,5 @@
"string.rkt"
"array.rkt"
"bitfield.rkt"
"stream.rkt")
"stream.rkt"
"buffer.rkt")

@ -34,7 +34,10 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee
(define/public-final (write val)
(unless (bytes? val)
(raise-argument-error 'EncodeStream:write "bytes" val))
(void (write-bytes val (· this _port)))))
(void (write-bytes val (· this _port))))
(define/public-final (writeBuffer buffer)
(write buffer)))
(test-module
(define es (+EncodeStream))
@ -83,7 +86,10 @@ https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
(define bytes-remaining (- length (port-position _port)))
(when (> count bytes-remaining)
(raise-argument-error 'DecodeStream:read (format "byte count not more than bytes remaining = ~a" bytes-remaining) count))
(read-bytes count _port)))
(read-bytes count _port))
(define/public-final (readBuffer count)
(read count)))
(test-module
(define ds (+DecodeStream #"ABCD"))

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