main
Matthew Butterick 6 years ago
parent a964bb10bb
commit c5c2eea752

@ -24,10 +24,10 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
[(? list?)
(for/list ([(op i) (in-indexed operands)])
(decodeOperands (list-ref type i) stream ret (list op)))]
[(hash-table 'decode proc) (proc stream ret operands)]
[(? xenomorphic?) (decode type stream #:parent ret operands)]
[(or 'number 'offset 'sid) (car operands)]
['boolean (if (car operands) #t #f)]
[_ operands]))
[_ operands]))
(define (encodeOperands type stream ctx operands)
(error 'cff-dict-encodeOperands-undefined))
@ -54,23 +54,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
(define b (read-byte stream))
(cond
[(< b 28)
#R b
(when (= b 12)
(set! b (bitwise-ior (arithmetic-shift b 8) (read-byte stream))))
(define field (hash-ref @fields b #false))
#R field
(unless field
(error 'cff-dict-decode (format "unknown operator: ~a" b)))
(define val (decodeOperands (third field) stream ret operands))
#R val
(unless (void? val)
;; ignoring PropertyDescriptor nonsense
(hash-set! ret (second field) val))
(set! operands null)]
[else
;; use `send` here to pass b as value arg
(set! operands (append operands (list (decode CFFOperand stream b))))])
(loop)))

@ -17,7 +17,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFFont.js
(augride [@decode decode])
(define (@decode stream parent)
(define cff-font (make-hasheq))
(hash-set! cff-font 'stream stream)
(for ([(k v) (in-hash (decode CFFTop stream))])
@ -41,30 +40,47 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFFont.js
(require rackunit racket/serialize racket/stream fontland/helper)
(define dir (deserialize (read (open-input-file fira-otf-directory-path))))
(define cff (hash-ref (hash-ref dir 'tables) 'CFF_))
(check-equal? (hash-ref cff 'length) 164604)
(define ip (open-input-file fira-otf-path))
(define cff-offset (hash-ref cff 'offset))
(check-equal? cff-offset 33472)
(define cff-length (hash-ref cff 'length))
(check-equal? cff-length 164604)
(define ip (open-input-file fira-otf-path))
(define cff-bytes (peek-bytes cff-length cff-offset ip))
(define cff-font (decode CFFFont cff-bytes))
(file-position ip cff-offset)
(define cff-font (decode CFFFont ip))
(check-equal? (file-position (hash-ref cff-font 'stream)) 74651)
(check-equal? (hash-ref cff-font 'version) 1)
(check-equal? (hash-ref cff-font 'hdrSize) 4)
(check-equal? (hash-ref cff-font 'offSize) 3)
(check-equal? (hash-ref cff-font 'nameIndex) '("FiraSans-Book"))
(check-equal? (length (hash-ref cff-font 'globalSubrIndex)) 820)
(check-equal?
(for/list ([h (in-list (hash-ref cff-font 'globalSubrIndex))])
(hash-ref h 'offset))
'(60105 60130 60218 60264 60303 60330 60361 60366 60387 60427 60433 60447 60454 60469 60500 60506 60512 60516 60545 60566 60581 60624 60637 60667 60679 60705 60715 60755 60776 60781 60839 60891 60897 60907 60914 60920 60938 60950 60976 60992 61005 61011 61032 61051 61067 61097 61111 61172 61272 61284 61359 61430 61489 61522 61526 61531 61535 61543 61565 61570 61575 61579 61601 61615 61629 61649 61654 61664 61842 61849 61858 61865 61895 61913 61920 61964 61977 61996 62074 62094 62102 62128 62132 62149 62160 62170 62197 62216 62225 62230 62237 62247 62256 62285 62332 62339 62347 62350 62375 62435 62479 62511 62539 62561 62585 62605 62621 62632 62711 62717 62733 62743 62783 62809 62818 62868 62905 62955 62965 62971 63034 63050 63059 63191 63237 63358 63394 63460 63465 63592 63716 63740 63866 63924 63947 64051 64075 64099 64120 64184 64245 64260 64374 64493 64515 64543 64585 64592 64597 64611 64622 64735 64738 64789 64797 64882 64920 65027 65054 65057 65069 65077 65113 65125 65222 65254 65275 65377 65480 65516 65524 65530 65550 65565 65569 65576 65673 65691 65760 65836 65854 65866 65873 65881 65895 65924 65929 65949 65970 66060 66093 66113 66132 66146 66151 66160 66165 66174 66185 66192 66210 66231 66255 66280 66288 66296 66301 66386 66395 66400 66446 66455 66537 66545 66550 66555 66636 66712 66722 66729 66748 66774 66788 66797 66810 66818 66841 66847 66853 66872 66877 66882 66887 66962 66988 66997 67008 67021 67027 67034 67040 67047 67110 67180 67218 67256 67325 67355 67369 67376 67390 67399 67403 67471 67478 67499 67520 67524 67550 67565 67579 67584 67651 67671 67679 67684 67749 67759 67772 67783 67790 67817 67883 67944 67967 67986 68049 68056 68090 68113 68132 68139 68149 68154 68159 68222 68226 68259 68262 68323 68326 68335 68372 68413 68420 68427 68435 68441 68446 68451 68462 68477 68489 68530 68535 68548 68553 68560 68567 68622 68638 68694 68748 68759 68764 68816 68862 68880 68885 68900 68907 68959 68988 69002 69011 69016 69028 69037 69089 69099 69115 69131 69143 69152 69160 69168 69174 69180 69202 69213 69218 69268 69318 69325 69374 69383 69402 69415 69422 69427 69434 69446 69488 69514 69529 69535 69582 69587 69603 69647 69667 69678 69684 69690 69700 69705 69710 69752 69795 69816 69860 69888 69898 69912 69921 69932 69936 69943 69948 69991 70002 70028 70041 70051 70057 70099 70130 70151 70166 70207 70219 70257 70279 70290 70300 70309 70316 70325 70333 70341 70346 70352 70357 70364 70401 70438 70475 70480 70487 70491 70497 70502 70532 70545 70552 70557 70562 70599 70616 70647 70651 70658 70665 70670 70706 70712 70737 70754 70766 70778 70786 70798 70804 70812 70817 70823 70858 70893 70904 70908 70913 70926 70933 70947 70954 70962 70968 70976 70981 70987 70996 71002 71007 71012 71018 71041 71074 71079 71111 71143 71175 71192 71196 71207 71215 71220 71226 71234 71247 71254 71261 71291 71308 71314 71322 71339 71345 71350 71354 71379 71395 71404 71413 71417 71422 71436 71458 71463 71479 71491 71501 71509 71521 71528 71535 71541 71547 71555 71561 71568 71575 71582 71588 71594 71599 71626 71640 71666 71692 71696 71704 71722 71735 71750 71759 71766 71781 71792 71797 71802 71808 71813 71818 71840 71865 71890 71894 71905 71910 71916 71927 71935 71942 71949 71956 71961 71966 71977 72001 72014 72036 72044 72056 72070 72076 72082 72087 72093 72100 72112 72135 72151 72155 72163 72172 72177 72182 72195 72217 72239 72261 72277 72297 72304 72324 72337 72350 72355 72365 72370 72377 72387 72394 72398 72405 72412 72417 72423 72429 72446 72460 72465 72485 72495 72514 72523 72530 72535 72546 72566 72586 72604 72624 72642 72651 72660 72666 72678 72684 72689 72698 72707 72714 72722 72730 72735 72740 72746 72752 72758 72774 72793 72799 72812 72825 72834 72839 72858 72862 72868 72877 72886 72895 72904 72913 72922 72931 72940 72946 72953 72960 72967 72973 72978 72985 73000 73007 73025 73043 73048 73066 73073 73089 73100 73111 73118 73124 73135 73140 73146 73152 73158 73163 73168 73173 73186 73200 73217 73234 73249 73264 73279 73286 73302 73318 73332 73348 73358 73374 73384 73390 73395 73403 73413 73422 73428 73438 73445 73452 73459 73465 73473 73479 73486 73491 73497 73505 73513 73521 73528 73533 73538 73545 73552 73559 73566 73572 73587 73592 73604 73619 73634 73649 73664 73679 73694 73709 73722 73735 73743 73748 73753 73758 73771 73784 73798 73804 73814 73827 73839 73843 73851 73860 73865 73872 73881 73890 73899 73908 73914 73920 73926 73932 73938 73943 73948 73953 73963 73976 73989 74002 74015 74028 74037 74050 74063 74076 74087 74098 74104 74110 74117 74123 74130 74137 74144 74149 74156 74161 74166 74171 74176 74188 74200 74212 74224 74236 74248 74260 74266 74278 74282 74294 74306 74313 74321 74328 74336 74342 74350 74357 74364 74370 74377 74385 74390 74396 74402 74408 74414 74420 74425 74430 74435 74440 74445 74450 74455 74466 74477 74488 74498 74509 74520 74531 74542 74553 74564 74575 74584 74593 74602 74611 74616 74621 74626 74631 74636 74641 74646))
(check-equal? (length (hash-ref cff-font 'stringIndex)) 2404)
; 'version string
#;(check-equal? (hash-ref (hash-ref cff-font 'topDict) 'version) 2401)
(check-equal?
(list-ref (hash-ref cff-font 'stringIndex) 2401)
"004.106")
; 'Notice string
(check-equal?
(list-ref (hash-ref cff-font 'stringIndex) 2402)
"Digitized data copyright \\(c\\) 2012-2015, The Mozilla Foundation and Telefonica S.A.")
; 'FullName string
(check-equal?
(list-ref (hash-ref cff-font 'stringIndex) 2403)
"Fira Sans Book")
(hash-ref (hash-ref cff-font 'topDict) 'Copyright))
(define top-dict (hash-ref cff-font 'topDict))
(check-equal? (hash-ref top-dict 'FontBBox) '(-167 -350 1360 1093))
(check-equal? (hash-ref top-dict 'version) 2792)
(check-equal? (hash-ref top-dict 'Notice) 2793)
(check-equal? (hash-ref top-dict 'FullName) 2794)
(check-equal? (hash-ref top-dict 'Weight) 388)
(define private (hash-ref top-dict 'Private))
(check-equal? (hash-ref private 'StdHW) 68)
(check-equal? (hash-ref private 'StdVW) 84)
(check-equal? (hash-ref private 'defaultWidthX) 0)
(check-equal? (hash-ref private 'nominalWidthX) 553)
(check-equal? (hash-ref private 'BlueScale) 0.037)
(check-equal? (hash-ref private 'BlueShift) 7)
(check-equal? (hash-ref private 'ExpansionFactor) 0.06)
)

@ -9,12 +9,11 @@
(define (getCFFVersion ctx)
(let loop ([ctx ctx])
(if (and ctx
(hash? ctx)
(hash-has-key? ctx 'hdrSize)
(not (hash-ref ctx 'hdrSize)))
(loop (hash-ref ctx 'parent))
(if ctx (hash-ref ctx 'x:version) -1))))
(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)

@ -32,19 +32,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFOperand.js
[(= value 30)
(for/fold ([strs null]
[break? #false]
#:result (* (string->number (string-append (reverse strs)) 1.0)))
#:result (* (string->number (apply string-append (reverse strs))) 1.0))
([i (in-naturals)]
#:break break?)
(define b (read-byte stream))
(define n1 (arithmetic-shift b -4))
(cond
[(= n1 FLOAT_EOF) (values strs 'break-now)]
[else
(let ([strs (cons (vector-ref FLOAT_LOOKUP n1) strs)])
(define n2 (bitwise-and b 15))
(cond
[(= n2 FLOAT_EOF (values strs 'break-now))]
[(= n2 FLOAT_EOF) (values strs 'break-now)]
[else
(let ([strs (cons (vector-ref FLOAT_LOOKUP n2) strs)])
(values strs #false))]))]))]))

@ -1,4 +1,4 @@
#lang racket/base
#lang debug racket/base
(require racket/class racket/list xenomorph)
(provide CFFPointer)
@ -9,39 +9,42 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFPointer.js
(define (CFFPointer type
#:offset-type [offset-type 'global]
#:relative-to [relative-to 'global]
#:lazy [lazy #false])
(x:pointer #:base-class CFFPointer%
#:type type
#:offset-type offset-type
(x:pointer #:type type
#:base-class CFFPointer%
#:relative-to relative-to
#:lazy lazy))
(define CFFPointer%
(class x:pointer%
(super-new)
(inherit-field offset-type)
(inherit-field type offset-type)
(define/override (decode stream parent operands)
(set! offset-type (class x:base%
(super-new)
(define/augment (decode . args) (first operands))))
(super decode stream parent operands))
(set! offset-type (make-object
(class x:base%
(super-new)
(define/augment (decode . args) (first operands)))))
(super decode stream parent))
(define/override (encode stream value ctx)
(cond
[(not stream)
;; compute the size (so ctx.pointerSize is correct)
(set! offset-type (class x:base%
(super-new)
(define/augment (size . args) 0)))
(set! offset-type (make-object
(class x:base%
(super-new)
(define/augment (size . args) 0))))
(send this size value ctx)
(Ptr 0)]
[else
(define ptr #false)
(set! offset-type (class x:base%
(super-new)
(define/augment (encode stream val) (set! ptr val))))
(set! offset-type (make-object
(class x:base%
(super-new)
(define/augment (encode stream val) (set! ptr val)))))
(super encode stream value ctx)
(Ptr ptr)]))))

@ -22,24 +22,25 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFPrivateDict.js
(loop (cdr operands))))]))))
(define CFFPrivateDict
;; key name type default
`((6 BlueValues delta #false)
(7 OtherBlues delta #false)
(8 FamilyBlues delta #false)
(9 FamilyOtherBlues delta #false)
((12 9) BlueScale number 0.039625)
((12 10) BlueShift number 7)
((12 11) BlueFuzz number 1)
(10 StdHW number #false)
(11 StdVW number #false)
((12 12) StemSnapH delta #false)
((12 13) StemSnapV delta #false)
((12 14) ForceBold boolean #false)
((12 17) LanguageGroup number 0)
((12 18) ExpansionFactor number 0.06)
((12 19) initialRandomSeed number 0)
(20 defaultWidthX number 0)
(21 nominalWidthX number 0)
(22 vsindex number 0)
(23 blend ,CFFBlendOp #false)
(19 Subrs ,(CFFPointer CFFIndex #:offset-type 'local) #false)))
(CFFDict
;; key name type default
`((6 BlueValues delta #false)
(7 OtherBlues delta #false)
(8 FamilyBlues delta #false)
(9 FamilyOtherBlues delta #false)
((12 9) BlueScale number 0.039625)
((12 10) BlueShift number 7)
((12 11) BlueFuzz number 1)
(10 StdHW number #false)
(11 StdVW number #false)
((12 12) StemSnapH delta #false)
((12 13) StemSnapV delta #false)
((12 14) ForceBold boolean #false)
((12 17) LanguageGroup number 0)
((12 18) ExpansionFactor number 0.06)
((12 19) initialRandomSeed number 0)
(20 defaultWidthX number 0)
(21 nominalWidthX number 0)
(22 vsindex number 0)
(23 blend ,CFFBlendOp #false)
(19 Subrs ,(CFFPointer (CFFIndex) #:relative-to 'local) #false))))

@ -18,8 +18,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFTop.js
(super-new)
(init-field [(@predefinedOps predefinedOps)]
[(@type type) #f])
(define/augment (decode stream parent operands)
(error 'predefined-op-decode-not-finished))
(augment [@decode decode])
(define (@decode stream parent operands)
(define idx (car operands))
(cond
[(and (< idx (length @predefinedOps)) (list-ref @predefinedOps idx))]
[else (decode @type stream #:parent parent operands)]))
(define/augment (size value ctx)
(error 'predefined-op-size-not-finished))
@ -115,9 +120,10 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFTop.js
(apply make-object
(class x:base%
(super-new)
(define/augment (decode stream parent operands)
(augment [@decode decode])
(define (@decode stream parent operands)
(hash-set! parent 'length (first operands))
(decode ptr stream parent (list (second operands)))))
(decode ptr stream #:parent parent (list (second operands)))))
args))
(define FontDict
@ -149,7 +155,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFTop.js
(14 XUID array #false)
(15 charset ,CFFCharset ,ISOAdobeCharset)
(16 Encoding ,CFFEncoding ,StandardEncoding)
(17 CharStrings ,(CFFPointer CFFIndex) #false)
(17 CharStrings ,(CFFPointer (CFFIndex)) #false)
(18 Private ,(CFFPrivateOp) #false)
((12 20) SyntheticBase number #false)
((12 21) PostScript sid #false)
@ -175,7 +181,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFTop.js
'nameIndex (CFFIndex (x:string #:length 'length))
'topDictIndex (CFFIndex CFFTopDict)
'stringIndex (CFFIndex (x:string #:length 'length))
'globalSubrIndex (CFFIndex))
'globalSubrIndex (CFFIndex)
)
#|
2 (dictify 'hdrSize uint8
@ -183,4 +190,4 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFTop.js
'topDict CFF2TopDict
'globalSubrIndex (CFFIndex))
|#
)))
)))
Loading…
Cancel
Save