soft touchups

main
Matthew Butterick 5 years ago
parent 1677a04dcc
commit 1b081dfe32

@ -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)

@ -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<?)])
(define val (dict-ref topDict k))
(unless (or (list? val) (dict? val))
k
val))
(for ([k (in-list (sort (dict-keys top-dict) symbol<?))])
(match (dict-ref top-dict k)
[(or (? list? (? dict?))) k]
[val val]))
(encode CFFTop top stream))

@ -74,14 +74,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
(let ([b (if (= b 12)
(bitwise-ior (arithmetic-shift b 8) (read-byte stream))
b)])
(define field (hash-ref @fields b #false))
(unless field
(error 'cff-dict-decode (format "unknown operator: ~a" b)))
(define val (decode-operands (third field) stream ret operands))
(unless (void? val)
(hash-set! ret (second field) val))
(loop null))]
(match (hash-ref @fields b #false)
[#false (error 'cff-dict-decode (format "unknown operator: ~a" b))]
[field
(define val (decode-operands (third field) stream ret operands))
(unless (void? val)
(hash-set! ret (second field) val))
(loop null)]))]
[else
(loop (append operands (list (decode CFFOperand stream b))))]))))
@ -96,12 +95,12 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFDict.js
(+ (for*/sum ([k (in-list (sort (dict-keys @fields) <))]
[field (in-value (dict-ref @fields k))]
[val (in-value (dict-ref dict (list-ref field 1) #false))]
#:unless (or (not val) (equal? val (list-ref field 3))))
(define operands (encode-operands (list-ref field 2) #false ctx val))
[val (in-value (dict-ref dict (second field) #false))]
#:when (and val (not (equal? val (fourth field)))))
(define operands (encode-operands (third field) #false ctx val))
(define operand-size (for/sum ([op (in-list operands)])
(size CFFOperand op)))
(define key (if (list? (car field)) (car field) (list (car field))))
(define key (if (list? (first field)) (first field) (list (first field))))
(+ operand-size (length key)))
(if include-pointers (hash-ref ctx x:pointer-size-key) 0)))

@ -39,20 +39,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFFont.js
(cond
[(not sid) #false]
[(>= (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)]

@ -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)))))

@ -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"

Loading…
Cancel
Save