CLI và thư viện Node
Gói fontstay là bộ máy điều chỉnh của trang này không cần trình duyệt: một công cụ dòng lệnh và một thư viện Node đọc các tệp phông chữ và in ra cùng các quy tắc @font-face và bản xuất như ứng dụng. Nó không cần DOM và không gửi yêu cầu mạng nào, nên phù hợp với quy trình build và CI.
Cài đặt
Gói chưa được phát hành lên npm. Nó yêu cầu Node 24 trở lên; hãy build nó từ một bản checkout của kho mã. Các ví dụ bên dưới viết fontstay thay cho node packages/fontstay/dist/bin.js.
git clone https://github.com/fogrew/fontstay.dev.git
cd fontstay.dev
pnpm install
pnpm cli:build
node packages/fontstay/dist/bin.js --versionLệnh
fit đọc một hoặc nhiều tệp phông chữ (WOFF2, WOFF, TTF, OTF) và in CSS phông chữ dự phòng. Nhiều tệp được gộp thành một stylesheet, như trong ứng dụng. mcp khởi động một máy chủ MCP trên stdio cho các tác nhân lập trình và không nhận tùy chọn nào.
Điều chỉnh một phông chữ cho Windows và macOS, không có các face Kirin và Hy Lạp. Đầu ra là cùng văn bản với bản xuất CSS trong ứng dụng.
$ fontstay fit font.woff2 --systems windows,macos --no-scripts
@font-face {
font-family: "Source Sans Pro Fallback";
src: local("Arial"), local("ArialMT");
size-adjust: 93.697%;
ascent-override: 105.0194%;
descent-override: 29.1365%;
line-gap-override: 0%;
}
body {
font-family: "Source Sans Pro", "Source Sans Pro Fallback", sans-serif;
/* Safari has no vertical overrides: match the x-height and fix the line box */
font-size-adjust: 0.48;
line-height: 1.4;
}
.fonts-loading {
letter-spacing: -0.0003em;
word-spacing: -0.0025em;
}fontstay fit Inter.woff2 -o fallbacks.css
fontstay fit regular.woff2 bold.woff2 --format tailwind
fontstay fit font.woff2 -a 'last 2 chrome versions, os windows' --jsonTùy chọn
| Tùy chọn | Ý nghĩa |
|---|---|
-s, --systems <list> | Các hệ thống cần điều chỉnh: windows, macos, ios, android, linux, chromeos. Mặc định: mọi hệ thống có ít nhất 1% tỷ lệ trong đối tượng người dùng. |
-a, --audience <query> | Truy vấn Browserslist, có thể kèm các phần “os <system>” và “not os <system>”. Mặc định: baseline được hỗ trợ rộng rãi. |
-f, --format <format> | Định dạng đầu ra, xem bên dưới. Mặc định: css. |
-k, --kind <kind> | Họ chung của các phông chữ dự phòng: sans-serif, serif, monospace hoặc cursive. Mặc định: nhận diện từ phông chữ. |
--locale <en|ru> | Hồ sơ tần suất của phép điều chỉnh: en hoặc ru. Mặc định: en. |
--unicode-range | Thêm unicode-range với các glyph mà mỗi phông chữ phủ. |
--no-scripts | Không tạo các face riêng cho Kirin và Hy Lạp. |
-o, --out <file> | Ghi đầu ra vào một tệp thay vì stdout. |
--json | In một báo cáo JSON thay vì văn bản của tệp. |
-h, --help / -v, --version | Hiển thị trợ giúp hoặc phiên bản. |
Định dạng đầu ra
--format nhận một trong các giá trị này. Chúng khớp với các định dạng xuất của ứng dụng và bắt đầu từ cùng một phép điều chỉnh.
Chạy lệnh này cho từng định dạng với phông chữ Fira Sans mẫu. Đầu ra hiển thị lấy từ CLI đã build.
Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format cssĐầu ra của CLI
@font-face {
font-family: "Fira Sans Fallback";
src: local("Trebuchet MS"), local("TrebuchetMS");
size-adjust: 100.3942%;
ascent-override: 93.1328%;
descent-override: 26.3959%;
line-gap-override: 0%;
}
body {
font-family: "Fira Sans", "Fira Sans Fallback", sans-serif;
/* Safari has no vertical overrides: match the x-height and fix the line box */
font-size-adjust: 0.527;
line-height: 1.4;
}
.fonts-loading {
letter-spacing: -0.0019em;
}Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format variablesĐầu ra của CLI
@font-face {
font-family: "Fira Sans Fallback";
src: local("Trebuchet MS"), local("TrebuchetMS");
size-adjust: 100.3942%;
ascent-override: 93.1328%;
descent-override: 26.3959%;
line-gap-override: 0%;
}
:root {
--font-body: "Fira Sans", "Fira Sans Fallback", sans-serif;
}
body {
font-family: var(--font-body);
/* Safari has no vertical overrides: match the x-height and fix the line box */
font-size-adjust: 0.527;
line-height: 1.4;
}Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format tailwindĐầu ra của CLI
@import "tailwindcss";
@font-face {
font-family: "Fira Sans Fallback";
src: local("Trebuchet MS"), local("TrebuchetMS");
size-adjust: 100.3942%;
ascent-override: 93.1328%;
descent-override: 26.3959%;
line-gap-override: 0%;
}
@theme {
--font-sans: "Fira Sans", "Fira Sans Fallback", sans-serif;
}Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format nextĐầu ra của CLI
// Put FiraSans-Regular.ttf into ./fonts/ and the @font-face rules from the CSS export into your global CSS.
import localFont from 'next/font/local';
export const font = localFont({
src: './fonts/FiraSans-Regular.ttf',
display: 'swap',
adjustFontFallback: false,
fallback: ['Fira Sans Fallback', 'sans-serif'],
variable: '--font-body',
});Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format nuxtĐầu ra của CLI
// Add the @font-face rules from the CSS export to a stylesheet listed in `css`.
export default defineNuxtConfig({
modules: ['@nuxt/fonts'],
fonts: {
families: [
{
name: 'Fira Sans',
src: '/fonts/FiraSans-Regular.ttf',
fallbacks: ['Fira Sans Fallback', 'sans-serif'],
},
],
},
});Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format astroĐầu ra của CLI
// Add the @font-face rules from the CSS export to your global styles.
import { defineConfig } from 'astro/config';
export default defineConfig({
fonts: [
{
name: 'Fira Sans',
cssVariable: '--font-body',
provider: 'local',
options: {
variants: [{ weight: 400, style: 'normal', src: ['./src/assets/fonts/FiraSans-Regular.ttf'] }],
},
fallbacks: ['Fira Sans Fallback', 'sans-serif'],
},
],
});Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format wordpressĐầu ra của CLI
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 3,
"settings": {
"typography": {
"fontFamilies": [
{
"name": "Fira Sans",
"slug": "fira-sans",
"fontFamily": "\"Fira Sans\", \"Fira Sans Fallback\", sans-serif",
"fontFace": [
{
"fontFamily": "Fira Sans",
"fontWeight": "400",
"fontStyle": "normal",
"src": [
"file:./assets/fonts/FiraSans-Regular.ttf"
]
}
]
}
]
}
}
}Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format fontaineĐầu ra của CLI
// Put FiraSans-Regular.ttf into ./public/fonts/ and declare their @font-face rules in your CSS. In Nuxt, put the plugin under vite.plugins.
import { FontaineTransform } from 'fontaine';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
FontaineTransform.vite({
fallbacks: {
'Fira Sans': ['Trebuchet MS'],
},
resolvePath: (id) => new URL(`./public${id}`, import.meta.url),
}),
],
});Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format preloadĐầu ra của CLI
<link rel="preload" href="/fonts/FiraSans-Regular.ttf" as="font" type="font/ttf" crossorigin>Lệnh
fontstay fit tests/fixtures/fonts/FiraSans-Regular.ttf --systems windows --no-scripts --format loadingĐầu ra của CLI
const root = document.documentElement;
const done = () => root.classList.remove('fonts-loading');
root.classList.add('fonts-loading');
document.fonts.load('1em "Fira Sans"').then(done, done);Đầu ra và báo cáo
Văn bản của tệp đi vào stdout. Với --json, CLI in một báo cáo thay thế, gồm các khóa file (đầu ra theo định dạng đã chọn), fonts (họ phông chữ, loại nhận diện được, độ phủ glyph và phông chữ dự phòng chọn cho mỗi hệ thống của từng phông chữ), shares (tỷ lệ đối tượng người dùng của mỗi hệ thống, tính theo phần trăm), systems, uncovered (các hệ thống trong đối tượng người dùng không có phông chữ dự phòng) và audience (truy vấn đã xác định và số trình duyệt khớp). Các tỷ lệ phụ thuộc vào dữ liệu trình duyệt tại thời điểm chạy.
Cảnh báo đi vào stderr, nên không bao giờ lẫn vào đầu ra: một phông chữ mà các phông chữ dự phòng chỉ có chung dưới 90% glyph với nó, và các hệ thống không có phông chữ dự phòng nào. Với --out, CLI cũng báo tệp đã ghi trên stderr.
Thư viện Node
fit nhận byte phông chữ và fitFiles đọc tệp. Cả hai trả về CSS, đầu ra theo định dạng yêu cầu, một báo cáo cho mỗi phông chữ và các tỷ lệ đối tượng người dùng. Lỗi ném ra FitRunError (invalid-audience, no-systems, no-fit) hoặc FontParseError (ví dụ invalid-font). Các kiểu nằm trong index.d.ts.
import { fit, fitFiles } from 'fontstay';
import { readFile } from 'node:fs/promises';
const { css, fonts, shares } = await fit({
fonts: [{ fileName: 'font.woff2', data: await readFile('font.woff2') }],
audience: 'defaults',
systems: ['windows', 'macos'],
});
const tailwind = (await fitFiles(['font.woff2'], { format: 'tailwind' })).file.text;Mã thoát
| Mã | Ý nghĩa |
|---|---|
0 | Thành công. |
1 | Thất bại: tệp không đọc được, phông chữ không hợp lệ, đối tượng người dùng không hợp lệ. |
2 | Lỗi cách dùng: lệnh không xác định, thiếu tệp phông chữ, giá trị tùy chọn không hợp lệ. |