|
|
|
@ -42,7 +42,6 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
|
|
|
|
|
(define unicode (mhasheq 0 '(0))) ; always include the missing glyph (gid = 0)
|
|
|
|
|
(define widths (mhasheq 0 (glyph-advance-width (get-glyph font 0))))
|
|
|
|
|
(define name (font-postscript-name font))
|
|
|
|
|
(define upm (font-units-per-em font))
|
|
|
|
|
(define scale (/ 1000 (font-units-per-em font)))
|
|
|
|
|
(define ascender (* (font-ascent font) scale))
|
|
|
|
|
(define descender (* (font-descent font) scale))
|
|
|
|
@ -50,20 +49,9 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
|
|
|
|
|
(define line-gap (* (font-linegap font) scale))
|
|
|
|
|
(define encoding-cache (make-hash)) ; needs to be per font, not in top level of module
|
|
|
|
|
(efont
|
|
|
|
|
name id ascender descender upm line-gap bbox #f #f efont-embedded efont-encode efont-measure-string
|
|
|
|
|
name id ascender descender line-gap bbox #f #f efont-embedded efont-encode efont-measure-string
|
|
|
|
|
font subset unicode widths scale encoding-cache))
|
|
|
|
|
|
|
|
|
|
(define (efont-measure-string ef str size [features null])
|
|
|
|
|
; #f disables features ; null enables default features ; list adds features
|
|
|
|
|
(define scale (/ size (pdf-font-upm ef) 1.0))
|
|
|
|
|
;; use `encode` because it's cached.
|
|
|
|
|
;; we assume that the side effects of `encode`
|
|
|
|
|
;; (e.g., appending to `widths` and `unicode`)
|
|
|
|
|
;; are ok because every string that gets measured is going to be encoded eventually
|
|
|
|
|
(match-define (list _ posns) (efont-encode ef str features))
|
|
|
|
|
(define width (for/sum ([p (in-vector posns)]) (glyph-position-x-advance p)))
|
|
|
|
|
(* width scale))
|
|
|
|
|
|
|
|
|
|
(define (efont-encode ef str [features-in null])
|
|
|
|
|
(define features (sort (remove-duplicates features-in) bytes<? #:key car))
|
|
|
|
|
(hash-ref! (efont-encoding-cache ef) (cons str features)
|
|
|
|
@ -89,6 +77,16 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
|
|
|
|
|
(hash-ref! (efont-unicode ef) gid (λ () (glyph-codepoints glyph))))
|
|
|
|
|
(list subset-idxs new-positions))))
|
|
|
|
|
|
|
|
|
|
(define (efont-measure-string ef str size [features null])
|
|
|
|
|
;; #f disables features ; null enables default features ; list adds features
|
|
|
|
|
;; use `encode` because it's cached.
|
|
|
|
|
;; we assume that the side effects of `encode`
|
|
|
|
|
;; (e.g., appending to `widths` and `unicode`)
|
|
|
|
|
;; are ok because every string that gets measured is going to be encoded eventually
|
|
|
|
|
(define width (for/sum ([p (in-vector (glyphrun-positions (layout (efont-font ef) str #:features features)))]) (glyph-position-x-advance p)))
|
|
|
|
|
(define scale (/ size (+ (font-units-per-em (efont-font ef)) 0.0)))
|
|
|
|
|
(* width scale))
|
|
|
|
|
|
|
|
|
|
(define (efont-embedded ef)
|
|
|
|
|
(define isCFF (has-table? (efont-font ef) 'CFF_))
|
|
|
|
|
(define font-file (make-ref))
|
|
|
|
|