Extract font-stretch when converting pango font names to CSS.

Also wrap font-family value in quotes and lower-case the fallback
generic sans-serif font-family for CSS conformance (though browsers
don't seem to care).
This commit is contained in:
John Ralls 2020-10-26 11:48:22 -07:00
parent 676cc337dc
commit 4ddd28a5d9
2 changed files with 17 additions and 7 deletions

View File

@ -37,6 +37,7 @@
(define (font-name-to-style-info font-name)
(let* ((font-style "")
(font-weight "")
(font-stretch "")
(idx (string-index-right font-name #\space))
(font-size (substring font-name (1+ idx) (string-length font-name)))
(font-name (string-take font-name idx)))
@ -54,9 +55,18 @@
(set! font-style "font-style: oblique; ")
(set! font-name (string-strip font-name " oblique"))))
(string-append "font-family: " font-name ", Sans-Serif; "
(cond
((string-contains-ci font-name " expanded")
(set! font-stretch "font-stretch: expanded; ")
(set! font-name (string-strip font-name " expanded")))
((string-contains-ci font-name " condensed")
(set! font-stretch "font-stretch: condensed; ")
(set! font-name (string-strip font-name " condensed"))))
(string-append "font-family: \"" font-name "\", sans-serif; "
"font-size: " font-size "pt; "
font-style font-weight)))
font-style font-weight font-stretch)))
;; Registers font options
(define (register-font-options options)

View File

@ -14,23 +14,23 @@
(test-begin "font-name-to-style-info")
(test-equal "basic"
"font-family: Courier Regular, Sans-Serif; font-size: 20pt; "
"font-family: \"Courier Regular\", sans-serif; font-size: 20pt; "
(font-name-to-style-info "Courier Regular 20"))
(test-equal "basic size 50"
"font-family: Courier Regular, Sans-Serif; font-size: 50pt; "
"font-family: \"Courier Regular\", sans-serif; font-size: 50pt; "
(font-name-to-style-info "Courier Regular 50"))
(test-equal "basic size 50 bold"
"font-family: Courier, Sans-Serif; font-size: 50pt; font-weight: bold; "
"font-family: \"Courier\", sans-serif; font-size: 50pt; font-weight: bold; "
(font-name-to-style-info "Courier bold 50"))
(test-equal "basic size 50 italic"
"font-family: Courier, Sans-Serif; font-size: 50pt; font-style: italic; "
"font-family: \"Courier\", sans-serif; font-size: 50pt; font-style: italic; "
(font-name-to-style-info "Courier italic 50"))
(test-equal "basic size 15 oblique"
"font-family: Courier, Sans-Serif; font-size: 15pt; font-style: oblique; "
"font-family: \"Courier\", sans-serif; font-size: 15pt; font-style: oblique; "
(font-name-to-style-info "Courier oblique 15"))
(test-end "font-name-to-style-info"))