|
|
|
@ -12,11 +12,24 @@
|
|
|
|
|
xenomorph
|
|
|
|
|
racket/dict)
|
|
|
|
|
|
|
|
|
|
(provide subset +subset ttf-subset +ttf-subset subset-add-glyph! encode-to-port create-subset)
|
|
|
|
|
(provide subset +subset
|
|
|
|
|
ttf-subset +ttf-subset
|
|
|
|
|
cff-subset +cff-subset
|
|
|
|
|
subset-add-glyph! encode-to-port create-subset)
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
|
from
|
|
|
|
|
https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
(define (create-subset font)
|
|
|
|
|
((if (has-table? font #"CFF_")
|
|
|
|
|
+cff-subset
|
|
|
|
|
+ttf-subset) font))
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
|
approximates
|
|
|
|
|
https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js
|
|
|
|
|
https://github.com/mbutterick/fontkit/blob/master/src/subset/Subset.js
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
; glyphs = list of glyph ids in the subset
|
|
|
|
@ -33,7 +46,7 @@ https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js
|
|
|
|
|
(subset-encode ss p)
|
|
|
|
|
p)
|
|
|
|
|
|
|
|
|
|
(define (subset-add-glyph! ss glyph-or-gid)
|
|
|
|
|
(define (subset-add-glyph! ss glyph-or-gid) ; fka `includeGlyph`
|
|
|
|
|
(define new-gid ((if (glyph? glyph-or-gid) glyph-id values) glyph-or-gid))
|
|
|
|
|
;; put the new glyph at the end of `glyphs`,
|
|
|
|
|
;; and put its index in the mapping
|
|
|
|
@ -43,6 +56,28 @@ https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js
|
|
|
|
|
(sub1 (length (subset-glyphs ss))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
|
approximates
|
|
|
|
|
https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
(struct cff-subset subset (cff strings charstrings gsubrs) #:transparent #:mutable)
|
|
|
|
|
|
|
|
|
|
(define (+cff-subset font [glyphs empty] [mapping (mhash)]
|
|
|
|
|
[cff #f]
|
|
|
|
|
[strings #f]
|
|
|
|
|
[charstrings #f]
|
|
|
|
|
[gsubrs #f])
|
|
|
|
|
(define ss (cff-subset font glyphs mapping cff strings charstrings gsubrs))
|
|
|
|
|
(subset-add-glyph! ss 0)
|
|
|
|
|
ss)
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
(require "font.rkt" "helper.rkt")
|
|
|
|
|
(define otf (open-font (path->string fira-otf-path)))
|
|
|
|
|
(define cffss (+cff-subset otf))
|
|
|
|
|
cffss)
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
|
approximates
|
|
|
|
|
https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
|
|
|
|
@ -59,10 +94,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
|
|
|
|
|
(subset-add-glyph! ss 0)
|
|
|
|
|
ss)
|
|
|
|
|
|
|
|
|
|
(define (create-subset font)
|
|
|
|
|
((if (has-table? font #"CFF_")
|
|
|
|
|
(error 'cff-fonts-unsupported)
|
|
|
|
|
+ttf-subset) font))
|
|
|
|
|
|
|
|
|
|
(define (ttf-subset-add-glyph ss gid)
|
|
|
|
|
;; glyph-decode unpacks the `glyf` table data corresponding to a certin gid.
|
|
|
|
@ -84,9 +115,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
|
|
|
|
|
;; if it is a compound glyph, include its components
|
|
|
|
|
(when (and ttf-glyf-data (negative? (hash-ref ttf-glyf-data 'numberOfContours)))
|
|
|
|
|
(for ([ttf-glyph-component (in-list (hash-ref ttf-glyf-data 'components))])
|
|
|
|
|
(define gid (subset-add-glyph! ss (ttf-glyph-component-glyph-id ttf-glyph-component)))
|
|
|
|
|
;; note: this (ttf-glyph-component-pos component) is correct. It's a field of a Component object, not a port
|
|
|
|
|
(bytes-copy! glyf-bytes (ttf-glyph-component-pos ttf-glyph-component) (encode uint16be gid #f))))
|
|
|
|
|
(define gid (subset-add-glyph! ss (ttf-glyph-component-glyph-id ttf-glyph-component)))
|
|
|
|
|
;; note: this (ttf-glyph-component-pos component) is correct. It's a field of a Component object, not a port
|
|
|
|
|
(bytes-copy! glyf-bytes (ttf-glyph-component-pos ttf-glyph-component) (encode uint16be gid #f))))
|
|
|
|
|
|
|
|
|
|
(set-ttf-subset-glyf! ss (append (ttf-subset-glyf ss) (list glyf-bytes)))
|
|
|
|
|
(hash-update! (ttf-subset-loca ss) 'offsets
|
|
|
|
@ -117,8 +148,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
|
|
|
|
|
;; glyphs to the array as component glyphs are discovered & enqueued
|
|
|
|
|
(for ([idx (in-naturals)]
|
|
|
|
|
#:break (= idx (length (subset-glyphs ss))))
|
|
|
|
|
(define gid (list-ref (subset-glyphs ss) idx))
|
|
|
|
|
(ttf-subset-add-glyph ss gid))
|
|
|
|
|
(define gid (list-ref (subset-glyphs ss) idx))
|
|
|
|
|
(ttf-subset-add-glyph ss gid))
|
|
|
|
|
|
|
|
|
|
(define new-maxp-table (clone-deep (get-maxp-table (subset-font ss))))
|
|
|
|
|
(dict-set! new-maxp-table 'numGlyphs (length (ttf-subset-glyf ss)))
|
|
|
|
@ -146,7 +177,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
|
|
|
|
|
'fpgm (get-fpgm-table (subset-font ss))))
|
|
|
|
|
(for ([(k v) (in-dict kvs)]
|
|
|
|
|
#:unless v)
|
|
|
|
|
(error 'encode (format "missing value for ~a" k)))
|
|
|
|
|
(error 'encode (format "missing value for ~a" k)))
|
|
|
|
|
(make-hasheq kvs)))
|
|
|
|
|
|
|
|
|
|
(encode Directory (mhash 'tables new-tables) port)
|
|
|
|
|