@cssints/fonts

Web fonts for cssints: font() reads the font files at build time and gives two members of a cssints/plugin scope(). family() emits the @font-face rules, a fallback face whose metrics match the font, and font-family. trim() removes the space above the cap height and below the baseline. The metrics come from the files (fontkitten). No script ships.

// fonts.ts: a build-time module (it runs while the build runs, and never reaches the browser)
import { font } from "@cssints/fonts" with { type: "cssints" };

export const body = font("Atkinson Hyperlegible", { provider: "fontsource", weights: [400, 700] });
export const brand = font("Brand", {
  provider: "fs",
  files: ["src/fonts/brand.woff2"],
  fallback: "serif",
});
// a component imports them from there
import { cn } from "cssints" with { type: "cssints" };
import { body } from "./fonts.ts" with { type: "cssints" };

export const text = body.family();
export const heading = cn(body.family(), body.trim({ lineHeight: 1.2 }));
@layer _.k {
  /* fontaine's formula (capsize's createFontStack): Arial scaled to the average width of the font, the vertical
     metrics of the font */
  @font-face {
    font-family: "Atkinson Hyperlegible Fallback";
    src: local("Arial"), local("Arimo"), local("Liberation Sans");
    size-adjust: 99.3717%;
    ascent-override: 95.6006%;
    descent-override: 29.1833%;
    line-gap-override: 0%;
  }
  /* one face per weight and subset of the package; Vite emits the files as assets */
  @font-face {
    font-family: "Atkinson Hyperlegible";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("/…/atkinson-hyperlegible-latin-400-normal.woff2") format("woff2");
    unicode-range: U+0000-00FF, …;
  }
  /* … latin-ext 400, latin 700, latin-ext 700 */
  ._fonts-native {
    text-box: trim-both cap alphabetic;
  }
  /* only when a target lacks text-box */
  @supports not (text-box: trim-both cap alphabetic) {
    ._fonts-native::before,
    ._fonts-native::after {
      content: "";
      display: table;
    }
    ._fonts-native::before {
      margin-bottom: var(--fonts-t);
    }
    ._fonts-native::after {
      margin-top: var(--fonts-b);
    }
  }
}
@layer _.a {
  ._a {
    font-family: "Atkinson Hyperlegible", "Atkinson Hyperlegible Fallback", sans-serif;
  }
  ._b {
    line-height: 1.2;
  }
  ._c {
    --fonts-t: -0.262em;
  }
  ._d {
    --fonts-b: -0.27em;
  }
}

Rules

Limits

cd packages/fonts && bun run test   # fixtures through the engine (output, errors, warnings, a build), and the type test via bun run typecheck

Credits

Copied, with the notice below:

Followed, not copied: the fallback face and the trim formulas of @capsizecss/core (the test checks the numbers against it), and fontaine (MIT, Daniel Roe) for the fallback face. The font files are read with fontkitten (MIT), a dependency.

The MIT notice of Capsize, Copyright (c) 2021 SEEK:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.