You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/fontland/fontland/subset.rkt

308 lines
12 KiB
Racket

6 years ago
#lang debug racket/base
6 years ago
(require racket/serialize
racket/list
racket/match
sugar/unstable/dict
"table/loca.rkt"
6 years ago
"table-stream.rkt"
6 years ago
"directory.rkt"
6 years ago
"struct.rkt"
6 years ago
fontland/glyph
fontland/ttf-glyph
5 years ago
fontland/cff-glyph
xenomorph
racket/dict
fontland/table/cff/cff-font
fontland/table/cff/cff-top
fontland/table/cff/cff-standard-strings)
6 years ago
5 years ago
(provide subset +subset
5 years ago
ttf-subset +ttf-subset ttf-subset?
cff-subset +cff-subset cff-subset?
5 years ago
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))
6 years ago
#|
approximates
5 years ago
https://github.com/mbutterick/fontkit/blob/master/src/subset/Subset.js
6 years ago
|#
6 years ago
(define (+subset font [glyphs empty] [mapping (mhash)])
(define ss (subset font glyphs mapping))
6 years ago
(subset-add-glyph! ss 0)
6 years ago
ss)
(define (encode-to-port ss)
(define p (open-output-bytes))
5 years ago
((if (cff-subset? ss)
cff-subset-encode
ttf-subset-encode) ss p)
6 years ago
p)
5 years ago
(define (subset-add-glyph! ss glyph-or-gid) ; fka `includeGlyph`
6 years ago
(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
(hash-ref! (subset-mapping ss) new-gid
6 years ago
(λ ()
(set-subset-glyphs! ss (append (subset-glyphs ss) (list new-gid)))
6 years ago
(sub1 (length (subset-glyphs ss))))))
6 years ago
5 years ago
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js
|#
(define (+cff-subset font [glyphs empty] [mapping (mhash)]
5 years ago
[cff (get-table font 'CFF_)]
5 years ago
[strings #f]
[charstrings #f]
[gsubrs #f])
(define ss (cff-subset font glyphs mapping cff strings charstrings gsubrs))
(subset-add-glyph! ss 0)
ss)
(require racket/format racket/string)
(define (bytes->hexes bs)
(string-join
(for/list ([b (in-bytes bs)])
(~r #:base 16 b #:min-width 2 #:pad-string "0"))  " "))
5 years ago
(define (subset-charstrings this)
5 years ago
(set-cff-subset-charstrings! this null)
5 years ago
(define gsubrs (make-hasheq))
(for ([gid (in-list (subset-glyphs this))])
(set-cff-subset-charstrings!
this
(append (cff-subset-charstrings this)
5 years ago
(list (get-char-string (cff-subset-cff this) gid))))
(define glyph (get-glyph (subset-font this) gid))
5 years ago
(unless (cff-glyph-path glyph) (getPath glyph)) ;; this causes the glyph to be parsed
5 years ago
(for ([subr (in-hash-keys (cff-glyph-_usedGsubrs glyph))])
(hash-set! gsubrs subr #true)))
5 years ago
(set-cff-subset-gsubrs! this (subset-subrs
this
(hash-ref (cff-subset-cff this) 'globalSubrIndex)
gsubrs)))
5 years ago
(define (subset-subrs this subrs used)
(for/vector ([(subr i) (in-indexed subrs)])
(cond
[(hash-ref used i #false)
(pos (hash-ref (cff-subset-cff this) 'stream) (index-item-offset subr))
(read-bytes (index-item-length subr) (hash-ref (cff-subset-cff this) 'stream))]
[else (bytes 11)])))
5 years ago
(define (subset-font-dict this topDict)
(error 'subsetFontdict-unimplemented))
5 years ago
(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))
5 years ago
(unless (cff-glyph-path glyph) (getPath glyph)) ;; this causes the glyph to be parsed
(for ([subr (in-hash-keys (cff-glyph-_usedSubrs glyph))])
5 years ago
(hash-set! used-subrs subr #true)))
(define cff-topDict (hash-ref (cff-subset-cff this) 'topDict))
5 years ago
(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))
5 years ago
(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))
5 years ago
(define (add-string this [string #f])
(cond
[(not string) #false]
[else
(unless (cff-subset-strings this)
(set-cff-subset-strings! this null))
(set-cff-subset-strings! this
(append (cff-subset-strings this) (list string)))
5 years ago
(+ (vector-length standard-strings) (sub1 (length (cff-subset-strings this))))]))
(define (cff-subset-encode this stream)
5 years ago
(subset-charstrings this)
(define charset
5 years ago
(dictify 'version (if (> (length (cff-subset-charstrings this)) 255) 2 1)
'ranges (list (dictify 'first 1 'nLeft (- (length (cff-subset-charstrings this)) 2)))))
5 years ago
(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))
5 years ago
(for ([key (in-list '(version Notice Copyright FullName
FamilyName Weight PostScript
BaseFontName FontName))])
5 years ago
(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)
5 years ago
(subset-font-dict this top-dict)
(create-cid-fontdict this top-dict))
(define top
(mhasheq 'version 1
'hdrSize (hash-ref (cff-subset-cff this) 'hdrSize)
'offSize 4
'header (hash-ref (cff-subset-cff this) 'header #f)
'nameIndex (list (CFFFont-postscriptName (cff-subset-cff this)))
5 years ago
'topDictIndex (list top-dict)
'stringIndex (cff-subset-strings this)
'globalSubrIndex (cff-subset-gsubrs this)))
5 years ago
(for ([k (in-list (sort (dict-keys top-dict) symbol<?))])
(match (dict-ref top-dict k)
[(or (? list? (? dict?))) k]
[val val]))
5 years ago
5 years ago
(encode CFFTop top stream))
5 years ago
#;(module+ test
(require "font.rkt" "helper.rkt")
(define otf (open-font (path->string fira-otf-path)))
(define cffss (+cff-subset otf))
cffss)
5 years ago
6 years ago
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
|#
6 years ago
(struct ttf-subset subset (glyf offset loca hmtx) #:transparent #:mutable)
6 years ago
6 years ago
(define (+ttf-subset font [glyphs empty] [mapping (mhash)]
[glyf #f]
[offset #f]
[loca #f]
[hmtx #f])
6 years ago
(define ss (ttf-subset font glyphs mapping glyf offset loca hmtx))
6 years ago
(subset-add-glyph! ss 0)
6 years ago
ss)
6 years ago
6 years ago
6 years ago
(define (ttf-subset-add-glyph ss gid)
6 years ago
;; glyph-decode unpacks the `glyf` table data corresponding to a certin gid.
6 years ago
;; here, it's not necessary for non-composite glyphs
;; because they just get copied entirely into the subset.
;; it's just used to detect composite glyphs and handle them specially.
6 years ago
;; so an optimization would be to detect composite / noncomposite without full glyph-decode.
6 years ago
(define glyph (get-glyph (subset-font ss) gid))
6 years ago
(define ttf-glyf-data (glyph-decode glyph))
6 years ago
6 years ago
;; get the offset to the glyph from the loca table
6 years ago
(match-define (list this-offset next-offset)
6 years ago
(take (drop (hash-ref (get-table (subset-font ss) 'loca) 'offsets) gid) 2))
6 years ago
6 years ago
(define port (get-table-stream (subset-font ss) 'glyf))
(pos port (+ (pos port) this-offset))
6 years ago
(define glyf-bytes (read-bytes (- next-offset this-offset) port))
6 years ago
;; if it is a compound glyph, include its components
6 years ago
(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))))
;; `loca` table v0 stores offsets as half of actual value
;; so we need an even number of bytes to encode
(define glyf-bytes-even (if (odd? (bytes-length glyf-bytes))
(bytes-append glyf-bytes #"0")
glyf-bytes))
6 years ago
(set-ttf-subset-glyf! ss (append (ttf-subset-glyf ss) (list glyf-bytes-even)))
6 years ago
(hash-update! (ttf-subset-loca ss) 'offsets
(λ (os)
(append os (list (ttf-subset-offset ss)))))
(hash-update! (ttf-subset-hmtx ss) 'metrics
(λ (ms) (append ms
(list (mhash 'advance (glyph-advance-width glyph)
6 years ago
'bearing (hash-ref (get-glyph-metrics glyph) 'leftBearing))))))
(set-ttf-subset-offset! ss (+ (ttf-subset-offset ss) (bytes-length glyf-bytes-even)))
6 years ago
(sub1 (length (ttf-subset-glyf ss))))
6 years ago
;; tables required by PDF spec:
;; head, hhea, loca, maxp, cvt, prep, glyf, hmtx, fpgm
;; additional tables required for standalone fonts:
;; name, cmap, OS/2, post
6 years ago
(define (clone-deep val) (deserialize (serialize val)))
6 years ago
5 years ago
(define (ttf-subset-encode ss port)
6 years ago
(set-ttf-subset-glyf! ss empty)
(set-ttf-subset-offset! ss 0)
(set-ttf-subset-loca! ss (mhash 'offsets empty))
(set-ttf-subset-hmtx! ss (mhash 'metrics empty 'bearings empty))
6 years ago
;; include all the glyphs used in the document
;; not using `in-list` because we need to support adding more
;; glyphs to the array as component glyphs are discovered & enqueued
(for ([idx (in-naturals)]
6 years ago
#:break (= idx (length (subset-glyphs ss))))
(define gid (list-ref (subset-glyphs ss) idx))
(ttf-subset-add-glyph ss gid))
6 years ago
6 years ago
(define new-maxp-table (clone-deep (get-maxp-table (subset-font ss))))
6 years ago
(dict-set! new-maxp-table 'numGlyphs (length (ttf-subset-glyf ss)))
6 years ago
;; populate the new loca table
6 years ago
(dict-update! (ttf-subset-loca ss) 'offsets (λ (vals) (append vals (list (ttf-subset-offset ss)))))
(loca-pre-encode (ttf-subset-loca ss))
6 years ago
6 years ago
(define new-head-table (clone-deep (get-head-table (subset-font ss))))
(dict-set! new-head-table 'indexToLocFormat (dict-ref (ttf-subset-loca ss) x:version-key))
6 years ago
6 years ago
(define new-hhea-table (clone-deep (get-hhea-table (subset-font ss))))
6 years ago
(dict-set! new-hhea-table 'numberOfMetrics (length (dict-ref (ttf-subset-hmtx ss) 'metrics)))
(define new-tables
(filter cdr (dictify 'head new-head-table
'hhea new-hhea-table
'loca (ttf-subset-loca ss)
'maxp new-maxp-table
'cvt_ (and (has-table? (subset-font ss) 'cvt_)
(get-cvt_-table (subset-font ss)))
'prep (and (has-table? (subset-font ss) 'prep)
(get-prep-table (subset-font ss)))
'glyf (ttf-subset-glyf ss)
'hmtx (ttf-subset-hmtx ss)
'fpgm (and (has-table? (subset-font ss) 'fpgm)
(get-fpgm-table (subset-font ss))))))
6 years ago
5 years ago
(encode directory (mhash 'tables new-tables) port)
6 years ago
(void))
6 years ago