From 1b081dfe328bbad9a89f4fc20dbb0f42554bb813 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 15 Mar 2019 12:34:23 -0700 Subject: [PATCH] soft touchups --- fontland/fontland/cff-glyph.rkt | 28 +++---- fontland/fontland/subset.rkt | 82 +++++++++---------- fontland/fontland/table/cff/cff-dict.rkt | 23 +++--- fontland/fontland/table/cff/cff-font.rkt | 12 +-- .../fontland/table/cff/cff-private-dict.rkt | 12 +-- .../table/cff/cff-standard-strings.rkt | 2 +- 6 files changed, 68 insertions(+), 91 deletions(-) diff --git a/fontland/fontland/cff-glyph.rkt b/fontland/fontland/cff-glyph.rkt index 5dd6e570..610eea34 100644 --- a/fontland/fontland/cff-glyph.rkt +++ b/fontland/fontland/cff-glyph.rkt @@ -69,26 +69,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/CFFGlyph.js (define nStems 0) (define x 0) (define y 0) - (define usedGsubrs (make-hasheq)) - (define usedSubrs (make-hasheq)) + (define used-gsubrs (make-hasheq)) + (define used-subrs (make-hasheq)) (define open #false) (define gsubrs (match (hash-ref cff 'globalSubrIndex (vector)) [(list) (vector)] [vec vec])) - (define gsubrsBias (bias this gsubrs)) + (define gsubrs-bias (bias this gsubrs)) - (define privateDict (or (privateDictForGlyph cff (glyph-id this)) (make-hasheq))) + (define privateDict (or (private-dict-for-glyph cff (glyph-id this)) (make-hasheq))) (define subrs (match (hash-ref privateDict 'Subrs (vector)) [(list) (vector)] [vec vec])) - (define subrsBias (bias this subrs)) - - ;; skip variations shit - #;(define vstore (and (hash-ref* cff 'topDict 'vstore) - (hash-ref* cff 'topDict 'vstore))) - #;(define vsindex (hash-ref privateDict 'vsindex)) - #;(define variationProcessor ) + (define subrs-bias (bias this subrs)) (define (check-width) (unless width @@ -150,10 +144,10 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/CFFGlyph.js (path-bezierCurveTo path c1x c1y c2x c2y x y)))] [(10) ;; callsubr - (define index (+ (pop stack) subrsBias)) + (define index (+ (pop stack) subrs-bias)) (define subr (vector-ref subrs index)) (when subr - (hash-set! usedSubrs index #true) + (hash-set! used-subrs index #true) (define p (pos stream)) (define e end) (pos stream (index-item-offset subr)) @@ -267,10 +261,10 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/CFFGlyph.js (push stack (decode int16be stream))] [(29) ;; callgsubr - (define index (+ (pop stack) gsubrsBias)) + (define index (+ (pop stack) gsubrs-bias)) (define subr (vector-ref gsubrs index)) (when subr - (hash-set! usedGsubrs index #true) + (hash-set! used-gsubrs index #true) (define p (pos stream)) (define e end) (pos stream (index-item-offset subr)) @@ -480,7 +474,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/CFFGlyph.js (when open (path-closePath path)) - (set-cff-glyph-_usedSubrs! this usedSubrs) - (set-cff-glyph-_usedGsubrs! this usedGsubrs) + (set-cff-glyph-_usedSubrs! this used-subrs) + (set-cff-glyph-_usedGsubrs! this used-gsubrs) (set-cff-glyph-path! this path) path) diff --git a/fontland/fontland/subset.rkt b/fontland/fontland/subset.rkt index 629c3d17..10ad1500 100644 --- a/fontland/fontland/subset.rkt +++ b/fontland/fontland/subset.rkt @@ -79,14 +79,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js (for/list ([b (in-bytes bs)]) (~r #:base 16 b #:min-width 2 #:pad-string "0"))  " ")) -(define (subsetCharstrings this) +(define (subset-charstrings this) (set-cff-subset-charstrings! this null) (define gsubrs (make-hasheq)) (for ([gid (in-list (subset-glyphs this))]) (set-cff-subset-charstrings! this (append (cff-subset-charstrings this) - (list (getCharString (cff-subset-cff this) gid)))) + (list (get-char-string (cff-subset-cff this) gid)))) (define glyph (get-glyph (subset-font this) gid)) (unless (cff-glyph-path glyph) (getPath glyph)) ;; this causes the glyph to be parsed @@ -94,12 +94,12 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js (for ([subr (in-hash-keys (cff-glyph-_usedGsubrs glyph))]) (hash-set! gsubrs subr #true))) - (set-cff-subset-gsubrs! this (subsetSubrs + (set-cff-subset-gsubrs! this (subset-subrs this (hash-ref (cff-subset-cff this) 'globalSubrIndex) gsubrs))) -(define (subsetSubrs this subrs used) +(define (subset-subrs this subrs used) (for/vector ([(subr i) (in-indexed subrs)]) (cond [(hash-ref used i #false) @@ -108,34 +108,34 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js [else (bytes 11)]))) -(define (subsetFontdict this topDict) +(define (subset-font-dict this topDict) (error 'subsetFontdict-unimplemented)) -(define (createCIDFontdict this topDict) - (define used_subrs (make-hasheq)) +(define (create-cid-fontdict this top-dict) + (define used-subrs (make-hasheq)) (for ([gid (in-list (subset-glyphs this))]) (define glyph (get-glyph (subset-font this) gid)) (unless (cff-glyph-path glyph) (getPath glyph)) ;; this causes the glyph to be parsed (for ([subr (in-hash-keys (cff-glyph-_usedSubrs glyph))]) - (hash-set! used_subrs subr #true))) + (hash-set! used-subrs subr #true))) (define cff-topDict (hash-ref (cff-subset-cff this) 'topDict)) - (define privateDict (hash-copy (hash-ref cff-topDict 'Private (make-hasheq)))) + (define private-dict (hash-copy (hash-ref cff-topDict 'Private (make-hasheq)))) (when (and (hash-has-key? cff-topDict 'Private) (hash-has-key? (hash-ref cff-topDict 'Private) 'Subrs)) - (hash-set! privateDict 'Subrs (subsetSubrs this - (hash-ref (hash-ref cff-topDict 'Private) 'Subrs) - used_subrs))) - - (hash-set! topDict 'FDArray (list (dictify 'Private privateDict))) - (hash-set! topDict 'FDSelect (dictify 'version 3 - 'nRanges 1 - 'ranges (list (dictify 'first 0 'fd 0)) - 'sentinel (length (cff-subset-charstrings this)))) - (hash-ref topDict 'FDSelect)) + (hash-set! private-dict 'Subrs (subset-subrs this + (hash-ref (hash-ref cff-topDict 'Private) 'Subrs) + used-subrs))) + + (hash-set! top-dict 'FDArray (list (dictify 'Private private-dict))) + (hash-set! top-dict 'FDSelect (dictify 'version 3 + 'nRanges 1 + 'ranges (list (dictify 'first 0 'fd 0)) + 'sentinel (length (cff-subset-charstrings this)))) + (hash-ref top-dict 'FDSelect)) -(define (addString this [string #f]) +(define (add-string this [string #f]) (cond [(not string) #false] [else @@ -144,39 +144,34 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js (set-cff-subset-strings! this (append (cff-subset-strings this) (list string))) - (+ (vector-length standardStrings) (sub1 (length (cff-subset-strings this))))])) + (+ (vector-length standard-strings) (sub1 (length (cff-subset-strings this))))])) (define (cff-subset-encode this stream) - (subsetCharstrings this) + (subset-charstrings this) (define charset - (dictify 'version (if (> (length (cff-subset-charstrings this)) 255) - 2 - 1) + (dictify 'version (if (> (length (cff-subset-charstrings this)) 255) 2 1) 'ranges (list (dictify 'first 1 'nLeft (- (length (cff-subset-charstrings this)) 2))))) - - (define topDict (hash-copy (hash-ref (cff-subset-cff this) 'topDict))) - (hash-set*! topDict + + (define top-dict (hash-copy (hash-ref (cff-subset-cff this) 'topDict))) + (hash-set*! top-dict 'Private #false 'charset charset 'Encoding #false 'CharStrings (cff-subset-charstrings this)) - + (for ([key (in-list '(version Notice Copyright FullName FamilyName Weight PostScript BaseFontName FontName))]) - (hash-update! topDict key - (λ (tdk-val) (addString this (CFFont-string (cff-subset-cff this) tdk-val))))) - - (hash-set! topDict 'ROS (list (addString this "Adobe") - (addString this "Identity") - 0)) - (hash-set! topDict 'CIDCount (length (cff-subset-charstrings this))) + (hash-update! top-dict key + (λ (tdk-val) (add-string this (CFFont-string (cff-subset-cff this) tdk-val))))) + (hash-set! top-dict 'ROS (list (add-string this "Adobe") (add-string this "Identity") 0)) + (hash-set! top-dict 'CIDCount (length (cff-subset-charstrings this))) (if (hash-ref (cff-subset-cff this) 'isCIDFont) - (subsetFontdict this topDict) - (createCIDFontdict this topDict)) + (subset-font-dict this top-dict) + (create-cid-fontdict this top-dict)) (define top (mhasheq 'version 1 @@ -184,15 +179,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js 'offSize 4 'header (hash-ref (cff-subset-cff this) 'header #f) 'nameIndex (list (CFFFont-postscriptName (cff-subset-cff this))) - 'topDictIndex (list topDict) + 'topDictIndex (list top-dict) 'stringIndex (cff-subset-strings this) 'globalSubrIndex (cff-subset-gsubrs this))) - (for ([k (sort (dict-keys topDict) symbol= (hash-ref this 'version) 2) #false] - [(< sid (vector-length standardStrings)) (vector-ref standardStrings sid)] - [else (vector-ref (hash-ref this 'stringIndex) (- sid (vector-length standardStrings)))])) + [(< sid (vector-length standard-strings)) (vector-ref standard-strings sid)] + [else (vector-ref (hash-ref this 'stringIndex) (- sid (vector-length standard-strings)))])) (define (CFFFont-postscriptName this) (and (< (hash-ref this 'version) 2) (vector-ref (hash-ref this 'nameIndex) 0))) (define CFFFont (make-object CFFFont%)) -(define (getCharString cff-font glyph-id) +(define (get-char-string cff-font glyph-id) (define glyph-record (vector-ref (hash-ref (hash-ref cff-font 'topDict) 'CharStrings) glyph-id)) (pos (hash-ref cff-font 'stream) (index-item-offset glyph-record)) (read-bytes (index-item-length glyph-record) (hash-ref cff-font 'stream))) -(define (fdForGlyph this gid) +(define (fd-for-glyph this gid) (cond [(not (hash-has-key? (hash-ref this 'topDict) 'FDSelect)) #false] [else @@ -71,12 +71,12 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFFont.js [else (hash-ref (list-ref ranges mid) 'fd)])))] [default (error 'unknown-select-version)])])) -(define (privateDictForGlyph this gid) +(define (private-dict-for-glyph this gid) (cond [(and (hash-has-key? this 'topDict) (hash-has-key? (hash-ref this 'topDict) 'FDSelect) (hash-ref* this 'topDict 'FDSelect)) - (define fd (fdForGlyph this gid)) + (define fd (fd-for-glyph this gid)) (if (list-ref (hash-ref* this 'topDict 'FDArray) fd) (hash-ref (list-ref (hash-ref* 'topDict 'FDArray) fd) 'Private) #false)] diff --git a/fontland/fontland/table/cff/cff-private-dict.rkt b/fontland/fontland/table/cff/cff-private-dict.rkt index c2ce3893..b6b2d27d 100644 --- a/fontland/fontland/table/cff/cff-private-dict.rkt +++ b/fontland/fontland/table/cff/cff-private-dict.rkt @@ -10,16 +10,6 @@ approximates https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFPrivateDict.js |# -(define CFFBlendOp - (class x:base% - (define/augment (x:decode stream parent operands) - (match (reverse operands) - [(cons numBlends operands) - ;; TODO: actually blend. For now just consume the deltas - ;; since we don't use any of the values anyway. - (let loop ([operands operands]) - (when (> (length operands) numBlends) - (loop (cdr operands))))])))) (define CFFPrivateDict (CFFDict @@ -43,5 +33,5 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFPrivateDict.js (20 defaultWidthX number 0) (21 nominalWidthX number 0) (22 vsindex number 0) - (23 blend ,CFFBlendOp #false) + #;(23 blend boolean #false) (19 Subrs ,(CFFPointer (CFFIndex) #:relative-to 'local) ,(vector))))) \ No newline at end of file diff --git a/fontland/fontland/table/cff/cff-standard-strings.rkt b/fontland/fontland/table/cff/cff-standard-strings.rkt index df32dddc..00ea9d34 100644 --- a/fontland/fontland/table/cff/cff-standard-strings.rkt +++ b/fontland/fontland/table/cff/cff-standard-strings.rkt @@ -6,7 +6,7 @@ approximates https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFStandardStrings.js |# -(define standardStrings +(define standard-strings '#(".notdef" "space" "exclam" "quotedbl" "numbersign" "dollar" "percent" "ampersand" "quoteright" "parenleft" "parenright" "asterisk" "plus" "comma" "hyphen" "period" "slash" "zero" "one"