plane truth

main
Matthew Butterick 6 years ago
parent 332884f7ba
commit f493e041cf

@ -40,7 +40,6 @@
id
ascender
descender
upm
line-gap
bbox
ref

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

@ -24,12 +24,11 @@
(define kern-pairs (make-hash kps))
(define ascender (string->number (hash-ref attributes 'Ascender "0")))
(define descender (string->number (hash-ref attributes 'Descender "0")))
(define upm 1000)
(define bbox (for/list ([attr (in-list (string-split (hash-ref attributes 'FontBBox)))])
(or (string->number attr) 0)))
(define line-gap (- (third bbox) (first bbox) ascender descender))
(sfont
name id ascender descender upm line-gap bbox #f #f sfont-embed sfont-encode sfont-measure-string
name id ascender descender line-gap bbox #f #f sfont-embed sfont-encode sfont-measure-string
attributes glyph-widths kern-pairs))
(define (sfont-embed sf)

@ -48,7 +48,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(define (add-text doc x y str features)
(match-define (list encoded-char-strs positions) (encode (pdf-current-font doc) str features))
(define scale (/ (pdf-current-font-size doc) #;(pdf-font-upm (pdf-current-font doc)) 1000.0))
(define scale (/ (pdf-current-font-size doc) 1000.0))
(define commands empty)
;; Adds a segment of text to the TJ command buffer

Loading…
Cancel
Save