main
Matthew Butterick 5 years ago
parent 7afe01683e
commit 2cff00a143

@ -20,20 +20,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
(values key field))]) (values key field))])
(define (decodeOperands type stream ret operands) (define (decodeOperands type stream ret operands)
(cond (match type
[(list? type) [(? list?)
(for/list ([(op i) (in-indexed operands)]) (for/list ([(op i) (in-indexed operands)])
(decodeOperands (list-ref type i) stream ret (list op)))] (decodeOperands (list-ref type i) stream ret (list op)))]
[(hash-ref type 'decode #f) => (λ (proc) (proc stream ret operands))] [(hash-table 'decode proc) (proc stream ret operands)]
[else (case type [(or 'number 'offset 'sid) (car operands)]
[(number offset sid) (car operands)] ['boolean (if (car operands) #t #f)]
[(boolean) (if (car operands) #t #f)] [_ operands]))
[else operands])]))
(define (encodeOperands type stream ctx operands) (define (encodeOperands type stream ctx operands)
(error 'cff-dict-encodeOperands-undefined)) (error 'cff-dict-encodeOperands-undefined))
(define/augment (decode stream parent) (augment [@decode decode])
(define (@decode stream parent)
(define end (+ (pos stream) (hash-ref parent 'length))) (define end (+ (pos stream) (hash-ref parent 'length)))
(define ret (make-hash)) (define ret (make-hash))
(define operands null) (define operands null)
@ -53,7 +53,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
[(< b 28) [(< b 28)
(when (= b 12) (when (= b 12)
(set! b (bitwise-ior (arithmetic-shift b 8) (read-byte stream)))) (set! b (bitwise-ior (arithmetic-shift b 8) (read-byte stream))))
(define field (hash-ref @fields b #false)) (define field (hash-ref @fields b #false))
(unless field (unless field
(error 'cff-dict-decode (format "unknown operator: ~a" b))) (error 'cff-dict-decode (format "unknown operator: ~a" b)))
@ -64,8 +63,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
(hash-set! ret (second field) val)) (hash-set! ret (second field) val))
(set! operands null)] (set! operands null)]
[else [else
(set! operands (append operands (list (decode CFFOperand stream b))))]) ;; use `send` here to pass b as value arg
(loop)))) (set! operands (append operands (list (send CFFOperand decode stream b))))])
(loop)))
ret)
(define/augment (size dict parent [includePointers #true]) (define/augment (size dict parent [includePointers #true])
(error 'cff-dict-size-undefined)) (error 'cff-dict-size-undefined))
@ -73,4 +75,4 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
(define/augment (encode stream dict parent) (define/augment (encode stream dict parent)
(error 'cff-dict-encode-undefined)))) (error 'cff-dict-encode-undefined))))
(define (CFFDict [ops null]) (make-object CFFDict% ops)) (define (CFFDict [ops null]) #R (make-object CFFDict% ops))

@ -48,10 +48,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFFont.js
(define ip (open-input-file fira-otf-path)) (define ip (open-input-file fira-otf-path))
(define cff-bytes (peek-bytes cff-length cff-offset ip)) (define cff-bytes (peek-bytes cff-length cff-offset ip))
(define cff-font (decode CFFFont cff-bytes)) (define cff-font (decode CFFFont cff-bytes))
(check-equal? (hash-ref cff-font 'length) 13) (check-equal? (hash-ref cff-font 'version) 1)
(check-equal? (hash-ref cff-font 'hdrSize) 4) (check-equal? (hash-ref cff-font 'hdrSize) 4)
(check-equal? (hash-ref cff-font 'offSize) 3) (check-equal? (hash-ref cff-font 'offSize) 3)
(check-equal? (hash-ref cff-font 'nameIndex) '("FiraSans-Book")) (check-equal? (hash-ref cff-font 'nameIndex) '("FiraSans-Book"))
(check-equal? (hash-ref cff-font 'length) (string-length (car (hash-ref cff-font 'nameIndex)))) (check-equal? (take-right (hash-ref cff-font 'stringIndex) 2)
cff-font (list
"Digitized data copyright \\(c\\) 2012-2015, The Mozilla Foundation and Telefonica S.A."
"Fira Sans Book"))
(hash-ref cff-font 'topDict)
(check-equal? (length (hash-ref cff-font 'globalSubrIndex)) 820)
) )

@ -21,7 +21,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFOperand.js
(class x:base% (class x:base%
(super-new) (super-new)
(define (decode stream value) (augment [@decode decode])
(define (@decode stream value)
(cond (cond
[(<= 32 value 246) (- value 139)] [(<= 32 value 246) (- value 139)]
[(<= 247 value 250) (+ (* (- value 247) 256) (read-byte stream) 108)] [(<= 247 value 250) (+ (* (- value 247) 256) (read-byte stream) 108)]
@ -48,7 +49,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFOperand.js
(let ([strs (cons (vector-ref FLOAT_LOOKUP n2) strs)]) (let ([strs (cons (vector-ref FLOAT_LOOKUP n2) strs)])
(values strs #false))]))]))])) (values strs #false))]))]))]))
(define (size value-arg) (define/augment (size value-arg)
;; if the value needs to be forced to the largest size (32 bit) ;; if the value needs to be forced to the largest size (32 bit)
;; e.g. for unknown pointers, set to 32768 ;; e.g. for unknown pointers, set to 32768
(define value (if (hash-ref value-arg 'forceLarge #f) 32768 value-arg)) (define value (if (hash-ref value-arg 'forceLarge #f) 32768 value-arg))
@ -62,7 +63,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFOperand.js
[(<= -32768 value 32767) 3] [(<= -32768 value 32767) 3]
[else 5])) [else 5]))
(define (encode stream value) (augment [@encode encode])
(define (@encode stream value)
;; if the value needs to be forced to the largest size (32 bit) ;; if the value needs to be forced to the largest size (32 bit)
;; e.g. for unknown pointers, save the old value and set to 32768 ;; e.g. for unknown pointers, save the old value and set to 32768
(define val (string->number (format "~a" value))) (define val (string->number (format "~a" value)))
@ -107,5 +109,4 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFOperand.js
(encode uint32be val stream)])))) (encode uint32be val stream)]))))
(define (CFFOperand . args) (define CFFOperand (make-object CFFOperand%))
(apply make-object CFFOperand% args))

@ -174,12 +174,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFTop.js
'offSize uint8 'offSize uint8
'nameIndex (CFFIndex (x:string #:length 'length)) 'nameIndex (CFFIndex (x:string #:length 'length))
'topDictIndex (CFFIndex CFFTopDict) 'topDictIndex (CFFIndex CFFTopDict)
;;'stringIndex (CFFIndex (x:string #:length 'length)) 'stringIndex (CFFIndex (x:string #:length 'length))
;;'globalSubrIndex (CFFIndex) 'globalSubrIndex (CFFIndex))
)
2 (dictify 'hdrSize uint8 #|
2 (dictify 'hdrSize uint8
'length uint16be 'length uint16be
;;'topDict CFF2TopDict 'topDict CFF2TopDict
;;'globalSubrIndex (CFFIndex) 'globalSubrIndex (CFFIndex))
)))) |#
)))
Loading…
Cancel
Save