From f20e937841f4c9178e56dfe18a05cce585b7b88b Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 15 Jun 2017 16:16:21 -0700 Subject: [PATCH] cleaner --- pitfall/fontkit/{cvt.rkt => cvt_.rkt} | 8 ++++---- pitfall/fontkit/directory.rkt | 25 +++++++++++++++++-------- pitfall/fontkit/font.rkt | 3 +-- pitfall/fontkit/subset.rkt | 2 +- pitfall/fontkit/subsetfont.rktd | Bin 0 -> 760 bytes pitfall/fontkit/tables.rkt | 2 +- 6 files changed, 24 insertions(+), 16 deletions(-) rename pitfall/fontkit/{cvt.rkt => cvt_.rkt} (81%) create mode 100644 pitfall/fontkit/subsetfont.rktd diff --git a/pitfall/fontkit/cvt.rkt b/pitfall/fontkit/cvt_.rkt similarity index 81% rename from pitfall/fontkit/cvt.rkt rename to pitfall/fontkit/cvt_.rkt index eb0b0093..2a7b1967 100644 --- a/pitfall/fontkit/cvt.rkt +++ b/pitfall/fontkit/cvt_.rkt @@ -6,9 +6,9 @@ approximates https://github.com/mbutterick/fontkit/blob/master/src/tables/cvt.js |# -(define-subclass Struct (Rcvt)) +(define-subclass Struct (Rcvt_)) -(define cvt (make-object Rcvt +(define cvt_ (make-object Rcvt_ (dictify 'controlValues (+Array int16be)))) @@ -25,8 +25,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/cvt.js (check-equal? table-bytes (peek-bytes len offset ip)) (define ds (+DecodeStream (peek-bytes len offset ip))) (define cvt-array '(20 43 83 0 16 -218 0 481 11 671 18 736 8)) - (check-equal? (hash-ref (send cvt decode ds) 'controlValues) cvt-array) + (check-equal? (hash-ref (send cvt_ decode ds) 'controlValues) cvt-array) (define es (+EncodeStream)) - (send cvt encode es (mhash 'controlValues cvt-array)) + (send cvt_ encode es (mhash 'controlValues cvt-array)) (check-equal? (send es dump) table-bytes) ) diff --git a/pitfall/fontkit/directory.rkt b/pitfall/fontkit/directory.rkt index 4a2a3e6c..88eaee3f 100644 --- a/pitfall/fontkit/directory.rkt +++ b/pitfall/fontkit/directory.rkt @@ -10,9 +10,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js (define-subclass Struct (RTableEntry) (define/override (preEncode this-val stream) (when (eq? (hash-ref this-val 'tag) 'cvt) - (hash-set! this-val 'tag '|cvt |)))) + (hash-set! this-val 'tag '|cvt |)))) -(define TableEntry (+RTableEntry +(define TableEntry (+Struct (dictify 'tag (+String 4) 'checkSum uint32be 'offset uint32be @@ -24,12 +24,21 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js (bytes-append bstr (make-bytes (- 4 mod) 0)) bstr)) +(define (symbol-replace sym this that) + (string->symbol (string-replace (symbol->string sym) this that))) + +(define (escape-tag tag) + (symbol-replace (if (string? tag) (string->symbol tag) tag) " " "_")) + +(define (unescape-tag tag) + (symbol-replace (if (string? tag) (string->symbol tag) tag) "_" " ")) + (define-subclass Struct (RDirectory) (define/override (process this-res stream) ;; in `restructure` `process` method, `res` is aliased as `this` (define new-tables-val (mhash)) (for ([table (in-list (· this-res tables))]) - (hash-set! new-tables-val (string->symbol (string-trim (· table tag))) table)) + (hash-set! new-tables-val (escape-tag (· table tag)) table)) (hash-set! this-res 'tables new-tables-val)) (define/override (preEncode this-val stream) @@ -41,9 +50,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js #:unless (hash-has-key? table-header-hash i)) (hash-set! table-header-hash i (let/cc k - (hash-set! offset-ks i k) - (mhash - 'tag tag)))) + (hash-set! offset-ks i k) + (mhash + 'tag (unescape-tag tag))))) (define table-headers (for/list ([i (in-range (length (hash-keys table-header-hash)))]) (hash-ref table-header-hash i))) @@ -52,7 +61,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js (define data-hash (mhash)) (for/fold ([current-offset table-header-size]) ([(table-header i) (in-indexed table-headers)]) - (define tag (· table-header tag)) + (define tag (escape-tag (· table-header tag))) (define bstr (hash-ref! data-hash i (λ () (define es (+EncodeStream)) @@ -64,7 +73,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js (cond [(hash-ref offset-ks i #f) => (λ (k) (hash-remove! offset-ks i) (k (mhash - 'tag (· table-header tag) + 'tag (unescape-tag (· table-header tag)) 'checkSum 0 'offset current-offset 'length (bytes-length bstr))))] diff --git a/pitfall/fontkit/font.rkt b/pitfall/fontkit/font.rkt index 0b7934da..733a5b00 100644 --- a/pitfall/fontkit/font.rkt +++ b/pitfall/fontkit/font.rkt @@ -282,6 +282,5 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js (define subset (make-object TTFSubset f)) (define es (+EncodeStream)) (send subset encode es) - (define ds (+DecodeStream (send es dump))) - (send Directory decode ds) + (check-equal? (send es dump) (file->bytes "subsetfont.rktd")) ) \ No newline at end of file diff --git a/pitfall/fontkit/subset.rkt b/pitfall/fontkit/subset.rkt index 1c1c41d9..04e8a84b 100644 --- a/pitfall/fontkit/subset.rkt +++ b/pitfall/fontkit/subset.rkt @@ -90,7 +90,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js 'hhea hhea 'loca (· this loca) 'maxp maxp - 'cvt (send (· this font) _getTable 'cvt) + 'cvt_ (send (· this font) _getTable 'cvt_) 'prep (send (· this font) _getTable 'prep) 'glyf (· this glyf) 'hmtx (· this hmtx) diff --git a/pitfall/fontkit/subsetfont.rktd b/pitfall/fontkit/subsetfont.rktd new file mode 100644 index 0000000000000000000000000000000000000000..f5dceb69aad7f31c99d007a2c9f0c8f638ab6835 GIT binary patch literal 760 zcmZ8fv2N2)6g|&&n-~UA=ulxHo@LshcGV3eK*ADEBoqlk&EU6?Mvc=1HNq+Zl>sRW zi-q_CBu41=*i3}fAXr$K_<*vpBSvKIvrSdi!#nq!d++h}eV*(+jbVZ#L9+XJyNB|5 zqhM$E`QRiU_x6($oDav7{fd85I2^|R`xU=Jv|r_q2(J#3opH(8GQgf}Kb!E9Yp)0| zCVO$^ue~RJ7p%w*P{TbK6*~iH)T<7q=s)rXV0_EF`Id1|)s&{?sXR2mf7jsMb2tCM z)PJeOcm4CN62JbY-X-He|3chUwd%eM>^5sqzdX$ClGHwNm