introduce current-font-features

main
Matthew Butterick 6 years ago
parent 42a8698c3b
commit 4bb9f94a63

@ -14,11 +14,11 @@
ctm
ctm-stack
font-families
current-font-features
current-font-size
current-font
registered-fonts
line-gap
text-options
x
y
image-registry) #:transparent #:mutable)

@ -31,11 +31,11 @@
(define ctm default-ctm-value)
(define ctm-stack null)
(define font-families (make-hash))
(define current-font-features null) ; connotes default features
(define current-font-size 12)
(define current-font #false)
(define registered-fonts (make-hash))
(define line-gap 0)
(define text-options #false)
(define x 0)
(define y 0)
(define image-registry (make-hash))
@ -49,11 +49,11 @@
ctm
ctm-stack
font-families
current-font-features
current-font-size
current-font
registered-fonts
line-gap
text-options
x
y
image-registry))

@ -26,9 +26,9 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
#:when c)
v))
(define (to-hex . codePoints)
(define (to-hex . codepoints)
(string-append*
(for/list ([code (in-list codePoints)])
(for/list ([code (in-list codepoints)])
(~r code #:base 16 #:min-width 4 #:pad-string "0"))))
(define embedded-font%
@ -70,7 +70,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
;; called from text.rkt
(define/override (encode str [features null])
(define features-key (and features (sort features string<?)))
(define features-key (and features (sort features bytes<?)))
(hash-ref! encoding-cache (cons features-key str)
(λ ()
(define glyph-run (layout font str features-key))

@ -41,9 +41,23 @@
doc)
(define (font-size doc size)
(unless (and (number? size) (not (negative? size)))
(raise-argument-error 'font-size "non-negative number" size))
(set-$doc-current-font-size! doc size)
doc)
(define (font-features doc features [unfeatures null])
(unless (or (not features) (and (list? features) (andmap bytes? features)))
(raise-argument-error 'font-features "list of byte strings or #f" features))
(unless (and (list? unfeatures) (andmap bytes? unfeatures))
(raise-argument-error 'font-features "list of byte strings" unfeatures))
(set-$doc-current-font-features! doc
(and features
(sort (for/list ([f (in-list features)]
#:unless (memv f unfeatures))
f) bytes<?)))
doc)
(define (register-font doc name src)
(hash-set! ($doc-registered-fonts doc) name (make-hasheq (list (cons 'src src))))
doc)

@ -84,7 +84,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
;; 180321: the first call to this operation is very slow from Quad
;; 181126: because `encode` calls `layout`
(match-define (list encoded-char-strs positions)
(send ($doc-current-font doc) encode text (hash-ref options 'features #f)))
(send ($doc-current-font doc) encode text (hash-ref options 'features ($doc-current-font-features doc))))
(define scale (/ ($doc-current-font-size doc) 1000.0))
(define commands empty)
@ -148,5 +148,5 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(move-down doc #:factor -1))
(define (string-width doc str [options (mhash)])
(+ (send ($doc-current-font doc) string-width str ($doc-current-font-size doc) (hash-ref options 'features #f))
(+ (send ($doc-current-font doc) string-width str ($doc-current-font-size doc) (hash-ref options 'features ($doc-current-font-features doc)))
(* (hash-ref options 'characterSpacing 0) (sub1 (string-length str)))))

Loading…
Cancel
Save