allow percentage for font-tracking

main
Matthew Butterick 5 years ago
parent 0deb440ea0
commit 4cd69f71db

@ -16,16 +16,17 @@
(define (parse-dimension x)
(match x
[#false #false]
[(? number?) x]
[(? string? x)
(match (cdr (regexp-match #px"^(-?[0-9\\.]+)\\s*([a-z]+)$" (string-downcase x)))
[(list num-string unit)
[(? number? num) num]
[(? string? str)
(match (regexp-match #px"^(-?[0-9\\.]+)\\s*([a-z]+)$" (string-downcase str))
[#false str] ; a string other than a dimension string, so leave it
[(list _ num-string unit)
((match unit
[(regexp #rx"(pt|point)(s)?$") values]
[(regexp #rx"in(ch(es)?)?$") in->pts]
[(regexp #rx"cm$") (compose1 in->pts cm->in)]
[(regexp #rx"mm$") (compose1 in->pts cm->in mm->cm)]
[_ (raise-argument-error 'parse-dimension "dimension string" x)]) (string->number num-string))])]))
[_ (raise-argument-error 'parse-dimension "dimension string" str)]) (string->number num-string))])]))
(define (copy-block-attrs source-hash dest-hash)
(define new-hash (make-hasheq))

@ -76,7 +76,10 @@
(hash-set! attrs :font-path (font-attrs->path this-font-family this-bold this-italic))))
(define (parse-percentage pstr)
(/ (string->number (string-trim pstr "%")) 100.0))
(and
(string? pstr)
(string-suffix? pstr "%")
(/ (string->number (string-trim pstr "%")) 100.0)))
(define (adjuster-base attrs key adjustment-key default-value)
(define this-val (hash-ref! attrs key default-value))
@ -91,3 +94,10 @@
(define (resolve-line-height! attrs)
(adjuster-base attrs :line-height :line-height-adjust default-line-height))
(define (resolve-font-tracking! attrs)
;; if it's a percentage, we need to look at the font size.
;; if it's anything else, we're done.
(match (parse-percentage (hash-ref attrs :font-tracking #false))
[#false (void)]
[pct (hash-set! attrs :font-tracking (* pct (hash-ref attrs :font-size)))]))

@ -112,6 +112,7 @@
complete-every-path!
resolve-font-path!
resolve-font-size!
resolve-font-tracking!
resolve-line-height!
parse-font-features!))])
(proc attrs)))

Loading…
Cancel
Save