diff --git a/pitfall/fontkit/glyf.rkt b/pitfall/fontkit/glyf.rkt new file mode 100644 index 00000000..ca5266b2 --- /dev/null +++ b/pitfall/fontkit/glyf.rkt @@ -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)) + ) diff --git a/pitfall/fontkit/subset.rkt b/pitfall/fontkit/subset.rkt index c9023713..ead3c243 100644 --- a/pitfall/fontkit/subset.rkt +++ b/pitfall/fontkit/subset.rkt @@ -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) ))) diff --git a/pitfall/fontkit/tables.rkt b/pitfall/fontkit/tables.rkt index b42e703e..8dc9ffb9 100644 --- a/pitfall/fontkit/tables.rkt +++ b/pitfall/fontkit/tables.rkt @@ -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) \ No newline at end of file +(define-table-decoders table-decoders maxp hhea head loca prep fpgm hmtx cvt glyf) \ No newline at end of file diff --git a/pitfall/restructure/buffer.rkt b/pitfall/restructure/buffer.rkt new file mode 100644 index 00000000..70100dc9 --- /dev/null +++ b/pitfall/restructure/buffer.rkt @@ -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)) \ No newline at end of file diff --git a/pitfall/restructure/main.rkt b/pitfall/restructure/main.rkt index dc5b67e7..0bb766a8 100644 --- a/pitfall/restructure/main.rkt +++ b/pitfall/restructure/main.rkt @@ -5,4 +5,5 @@ "string.rkt" "array.rkt" "bitfield.rkt" - "stream.rkt") \ No newline at end of file + "stream.rkt" + "buffer.rkt") \ No newline at end of file diff --git a/pitfall/restructure/stream.rkt b/pitfall/restructure/stream.rkt index b9abf8e5..db7f7ac5 100644 --- a/pitfall/restructure/stream.rkt +++ b/pitfall/restructure/stream.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")) diff --git a/pitfall/restructure/utils.rkt b/pitfall/restructure/utils.rkt index 01a59ddb..dbbc5c2b 100644 --- a/pitfall/restructure/utils.rkt +++ b/pitfall/restructure/utils.rkt @@ -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)])) \ No newline at end of file + [else (raise-argument-error 'resolveLength "not a fixed size" len)])) \ No newline at end of file