Fonts
A FontFace loads a font file with @font-face, and its family() is the value a font-family declaration takes.
Loading a font file
A FontFace is one font file of a family, written as an @font-face rule. Global::font adds it to a Global, and FontFace::family, fn family(&self) -> FontFamily, returns the value font-family takes, so the name in the rule and the name in the style are the same string:
use hypersig::html::{em, p};
use hypersig::style::value::{FontFamily, quoted, url};
use hypersig::style::{Element, FontFace, Global, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Blog {}
// One variable file for weights 100 to 900, and its italic face.
let inter = FontFace::new("Inter", url("/fonts/inter.woff2")).weights(100, 900);
let inter_italic = FontFace::new("Inter", url("/fonts/inter-italic.woff2"))
.weights(100, 900)
.italic();
let html = Page::new("Blog", Blog {})
.global(
Global::preflight()
.rule(
Element::Body,
Style::new().font_family(inter.family().fallback(FontFamily::SansSerif)),
)
// Each character comes from the first family in the list that has it.
.rule(
Element::Code,
Style::new().font_family(
FontFamily::from(quoted("JetBrains Mono")).fallback(FontFamily::Monospace),
),
)
.rule(Element::Kbd, Style::new().font_family(FontFamily::UiMonospace))
.font(inter)
.font(inter_italic),
)
.child(p().child(em().text("Hello")))
.render();Besides the reset, the page gets:
body { font-family: "Inter", sans-serif; line-height: 1.5 }
code { font-family: "JetBrains Mono", monospace }
kbd { font-family: ui-monospace }
@font-face { font-family: "Inter"; src: url("/fonts/inter.woff2"); font-display: swap; font-weight: 100 900 }
@font-face { font-family: "Inter"; src: url("/fonts/inter-italic.woff2"); font-display: swap; font-weight: 100 900; font-style: italic }The methods
| Method | What it writes |
|---|---|
new(family: &'static str, src: Url) -> FontFace | font-family as a quoted string, src: url(..), and font-display: swap. The browser recognizes the format from the file, and WOFF2 is the usual choice. |
weights(self, min: u16, max: u16) -> FontFace | font-weight: <min> <max>, the range a variable font covers. One weight is weights(400, 400). |
italic(self) -> FontFace | font-style: italic, which marks the file as the italic face of the family. |
Two faces with the same family name and different font-style are one family to the browser. <em> above is drawn from the italic file, and the rest from the upright one.
The URL of the file
src is a Url made by url, const fn url(href: &'static str) -> Url, so a plain string is a compile error:
use hypersig::style::FontFace;
// error[E0308]: mismatched types: expected `Url`, found `&str`
let inter = FontFace::new("Inter", "/fonts/inter.woff2");url panics on an empty URL, a scheme other than http or https, or a space or a control character. In a const, the panic is a compile error:
use hypersig::style::value::{Url, url};
// error[E0080]: evaluation panicked: a URL a page loads is a path or an `http` or `https` URL, with no space or control character
const INTER: Url = url("/fonts/Inter Variable.woff2");A path that starts with a single / names a file the application serves, and the page writes it under the base its routes are declared under, so under Base::new("/app") the file /fonts/inter.woff2 loads from /app/fonts/inter.woff2. URLs and strings covers url in full.
Fallback fonts
A FontFamily is a generic keyword such as FontFamily::SystemUi, the quoted name family() returns, or a name from quoted. FontFamily::fallback adds the next family of a list, so inter.family().fallback(FontFamily::SansSerif) writes "Inter", sans-serif. The browser takes each character from the first family that has it, so the <code> rule of the first sample falls back to monospace where JetBrains Mono is not installed. A list should end in a generic family, which every browser has, and nothing checks that it does.
What the types leave unchecked
- Nothing checks that the file exists. While the file loads, and for good if it does not,
font-display: swapshows the text in the next font of the list, or in the browser's default font. - The weight range, at run time.
weightspanics where it is called on a range in reverse, such asweights(900, 100), or on a weight outside the 1 to 1000 that CSS allows. It is not aconst fn, so this is a panic when the line runs, not a compile error. - The browser downloads a font only when text on the page uses its family. A
FontFacethat no rule or style names costs nothing, and nothing reports it.
Next
Stacks starts the Layout pages, with the functions that return the styles of common layouts.