@cssints/fonts

中文文档
0.35.1发布于 2026/11/07MITTypeScriptES Module构建来源已验证近 30 天下载 7379

Fonts for cssints: @font-face rules with a metric-matched fallback face, and cap-height trimming

原始使用文档
文档目录3 节
  1. Rules
  2. Limits
  3. Credits

@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

  • Providers. fontsource reads the @fontsource/<family> package from the node_modules of the working directory ("Atkinson Hyperlegible" is @fontsource/atkinson-hyperlegible): its .woff2 files, one face per weight, style and subset, each with the subset's unicode-range. weights and styles must be among those of the package (400 and normal by default). fs takes font files (.woff2, .woff, .ttf, .otf, relative to the working directory), one face each, with the weight and style read from the file.
  • URLs. A face's src is the absolute path of the file. Vite's CSS pipeline turns it into an asset in a build (/assets/…-[hash].woff2) and into /@fs/… in dev.
  • Fallback face. fallback is a generic family (sans-serif, serif or monospace; by default the category of the fontsource package, else sans-serif). Its local fonts are Arial, Times New Roman or Courier New, with their metric-compatible clones (Arimo, Tinos, Cousine, the Liberation fonts). The face scales them to the font's average latin width (size-adjust) and overrides ascent, descent and line-gap with the font's. This is fontaine's formula. The numbers are those of @capsizecss/core createFontStack (checked in the test). The metrics come from the 400 normal face (else the first) of the latin subset.
  • trim(). trim() sets line-height (normal, or the number given) and the class of the kernel text-box: trim-both cap alphabetic (Chrome 133, Safari 18.2, Firefox 154). When a target lacks text-box, the sheet gets capsize's fallback: ::before and ::after with negative margins, in em, from the metrics of the font and the line height (the numbers of @capsizecss/core createStyleObject, checked in the test). The fallback is plain CSS, so every target gets it (Firefox 120 too), and there is no build warning. It is inside @supports not, so a browser with text-box does not trim twice. When every target has text-box, the fallback is not in the sheet.
  • Errors, at build time: a package that is not installed (the message names it), a weight or a style the package does not have, a file that cannot be read, a font collection, and a lineHeight that is not a number (also a type error).
  • Cache. A file is parsed once per modification time in a process.

Limits

  • lineHeight is a number (a multiple of the font size). A length would make the trim depend on the font size.
  • The trim is right for the web font. The fallback face matches ascent and descent, but not the cap height, so the trim is off by a little until the font loads.
  • Not yet: variable fonts (@fontsource-variable/*), remote providers (unifont, Google Fonts), the subsets option, and font-display other than swap.
  • In dev, an edited file of fs updates the CSS of the modules that use it (dependsOn of cssints/plugin). The files of fontsource are in node_modules, which Vite does not watch.
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:

  • The characters and weights of the average width (xWidthAvg): the latin table of @capsizecss/unpack (MIT, SEEK).
  • The metrics of Arial, Times New Roman and Courier New from @capsizecss/metrics (MIT, SEEK).

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.

版本与发布记录

@cssints/fonts 共发布 43 个版本,首次发布于 2026/10/11;过去 12 个月发布了 43 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.35.1latest2026/11/073 天前
0.35.02026/11/073 天前
0.34.02026/11/073 天前
0.33.02026/11/046 天前
0.32.02026/11/046 天前
0.31.02026/11/047 天前

关于 @cssints/fonts 的常见问题

@cssints/fonts 是什么?

Fonts for cssints: @font-face rules with a metric-matched fallback face, and cap-height trimming

如何安装 @cssints/fonts?

在项目目录运行 npm install @cssints/fonts,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cssints(^0.35.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cssints/fonts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cssints/fonts 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cssints/fonts 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cssints/fonts 可以商用吗?

@cssints/fonts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cssints/fonts 的下载量有多少?

根据 npm 官方下载统计,@cssints/fonts 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,379 次,日均约 246 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cssints/fonts 还在维护吗?

最新版本 0.35.1 发布于 2026/11/07(3 天前);至今共发布 43 个版本,其中过去 12 个月发布了 43 个;首次发布于 2026/10/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。