You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/fontland/fontland/table/cff/cff-index.rkt

124 lines
4.1 KiB
Racket

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

#lang debug racket
(require racket/class racket/match xenomorph sugar/unstable/dict)
(provide CFFIndex)
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFIndex.js
|#
(define CFFIndex%
(class x:base%
(super-new)
(init-field [(@type type) #f])
(define (getCFFVersion ctx)
(let loop ([ctx ctx])
(cond
[(and ctx (hash? ctx) (not (hash-ref ctx 'hdrSize #f)))
(loop (hash-ref ctx 'x:parent))]
[(and ctx (hash-ref ctx 'x:version #f))]
[else -1])))
(augride [@decode decode])
(define (@decode stream parent)
(match (decode (if (>= (getCFFVersion parent) 2) uint32be uint16be) stream)
[0 null]
[count (define offSize (decode uint8 stream))
(define offsetType (match offSize
[1 uint8]
[2 uint16be]
[3 uint24be]
[4 uint32be]
[_ (error 'bad-offset-size-in-CFFIndex)]))
(define startPos (+ (pos stream) (* (add1 count) offSize) -1))
(for/fold ([vals null]
[start (send offsetType decode stream)]
#:result (begin0 (reverse vals) (pos stream (+ startPos start))))
([i (in-range count)])
(define end (send offsetType decode stream))
(define val
(cond
[@type
(define apos (pos stream))
(pos stream (+ startPos start))
(hash-set! parent 'length (- end start))
(begin0
(send @type decode stream parent)
(pos stream apos))]
[else
(hasheq 'offset (+ startPos start)
'length (- end start))]))
(values (cons val vals) end))]))
(augride [@size size])
(define (@size arr parent)
(define size 2)
(cond
[(zero? (length arr)) size]
[else
(define type (or @type (x:buffer)))
;; find maximum offset to determinine offset type
(define offset 1)
(for ([(item i) (in-indexed arr)])
(set! offset (+ offset (send type size item parent))))
(define offsetType
(cond
[(<= offset #xff) uint8]
[(<= offset #xffff) uint16be]
[(<= offset #xffffff) uint24be]
[(<= offset #xffffffff) uint32be]
[else (error 'CFFIndex-size (format "bad offset: ~a" offset))]))
(set! size (+ size 1 (* (send offsetType size) (add1 (length arr)))))
(set! size (+ size (sub1 offset)))
size]))
(define/augride (encode arr stream parent)
#R arr
(encode uint16be (length arr) stream)
(cond
[(zero? (length arr))]
[else
(define type (or @type (x:buffer)))
;; find maximum offset to detminine offset type
(define sizes null)
(define offset 1)
(for ([item (in-list arr)])
(define s (send @type size item parent))
(set! sizes (append sizes (list s)))
(set! offset (+ offset s)))
(define offsetType
(cond
[(<= offset #xff)
uint8]
[(<= offset #xffff)
uint16]
[(<= offset #xffffff)
uint24]
[(<= offset #xffffffff)
uint32]
[else
(error 'cff-index-encode-bad-offset!)]))
;; write offset size
(encode uint8 (size offsetType) stream)
;; write elements
(set! offset 1)
(encode offsetType offset stream)
(for ([size (in-list sizes)])
(set! offset (+ offset size))
(encode offsetType offset stream))
(for ([item (in-list arr)])
(encode type item stream #:parent parent))]))))
(define (CFFIndex [type #f])
(new CFFIndex% [type type]))