You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/quad/quadwriter/font.rkt

85 lines
3.6 KiB
Racket

5 years ago
#lang debug racket/base
(require racket/runtime-path
racket/string
racket/path
racket/match
"attrs.rkt")
5 years ago
(provide (all-defined-out))
(define-runtime-path quadwriter-fonts-dir "fonts")
(define-runtime-path default-font-face "fonts/default/SourceSerifPro-Regular.otf")
5 years ago
(define default-font-family "text")
5 years ago
(define default-font-size 12)
5 years ago
(define default-line-height 16)
5 years ago
(define default-font-color "black")
5 years ago
(define default-font-features (list (cons #"tnum" 1)))
5 years ago
(define font-paths (make-hash))
5 years ago
(define top-font-directory "fonts")
(define font-file-extensions '(#".otf" #".ttf" #".woff"))
5 years ago
(define (setup-font-path-table! base-path)
;; rules for font naming
;; "fonts" subdirectory on top
;; family directories inside: each named with font family name
;; this makes it possible to give font families generic names (e.g., "body-text")
;; and change the font files without disturbing anything else.
5 years ago
(hash-clear! font-paths)
(define-values (dir path _) (split-path base-path))
5 years ago
(define doc-fonts-dir (build-path dir top-font-directory))
;; run doc-fonts-dir first because earlier fonts take precedence (using hash-ref! below)
(for* ([fonts-dir (in-list (list doc-fonts-dir quadwriter-fonts-dir))]
5 years ago
#:when (directory-exists? fonts-dir)
[font-family-subdir (in-directory fonts-dir)]
#:when (directory-exists? font-family-subdir)
[font-path (in-directory font-family-subdir)]
5 years ago
#:when (member (path-get-extension font-path) font-file-extensions))
5 years ago
(match-define (list font-path-string family-name)
5 years ago
(for/list ([x (list font-path font-family-subdir)])
(path->string (find-relative-path fonts-dir x))))
(define path-parts (for/list ([part (in-list (explode-path (string->path (string-downcase font-path-string))))])
(path->string part)))
5 years ago
(define key
5 years ago
(cons (string-downcase family-name)
(cond
[(member "bold-italic" path-parts) 'bi]
[(member "bold" path-parts) 'b]
[(member "italic" path-parts) 'i]
[else 'r])))
5 years ago
;; only set value if there's not one there already.
;; this means that we only use the first eligible font we find.
(hash-ref! font-paths key font-path)))
(define (font-attrs->path font-family bold italic)
;; find the font-path corresponding to a certain family name and style.
5 years ago
(define key (cons (string-downcase font-family)
5 years ago
(cond
[(and bold italic) 'bi]
[bold 'b]
[italic 'i]
[else 'r])))
(define regular-key (cons font-family 'r))
(cond
[(hash-ref font-paths key #false)]
[(hash-ref font-paths regular-key #false)]
[else default-font-face]))
(define (resolve-font-path! attrs)
(define this-font-family (hash-ref! attrs :font-family default-font-family))
(unless (complete-path? this-font-family)
(define this-bold (hash-ref! attrs :font-bold #false))
(define this-italic (hash-ref! attrs :font-italic #false))
(hash-set! attrs :font-path (font-attrs->path this-font-family this-bold this-italic))))
5 years ago
(define (parse-percentage pstr)
(/ (string->number (string-trim pstr "%")) 100.0))
(define (resolve-font-size! attrs)
(define this-font-size (hash-ref! attrs :font-size default-font-size))
(define this-font-size-adjust (parse-percentage (hash-ref! attrs :font-size-adjust "100%")))
5 years ago
;; we bake the adjustment into the font size...
(hash-set! attrs :font-size (* this-font-size this-font-size-adjust))
5 years ago
;; and then set the adjustment back to 100% (since it's now accounted for)
(hash-set! attrs :font-size-adjust "100%"))