
Product
Introducing the Alert Details Page: A Better Way to Explore Alerts
Socket's new Alert Details page is designed to surface more context, with a clearer layout, reachability dependency chains, and structured review.
cn-font-split
Advanced tools
划时代的字体切割工具,CJK与任何字符!支持 otf、ttf、woff2 字体多线程切割,完美地细颗粒度地进行包大小控制。A revolutionary font subetter that supports CJK and any characters! It enables multi-threaded subset of otf, ttf, and woff2 fonts, allowing for precise control over package size.

| 中文网字计划 | Github | 在线使用 |
|---|
cn-font-split 是 中文网字计划 所使用的字体分包工具,通过高性能的各种技术将庞大的字体包拆分为适合网络分发的版本。经过七个大版本的字体研究与代码迭代,这项技术在我们的网站中得到了充分的应用,实现了中文字体在 Web 领域的加载速度与效率的双飞跃。
cn-font-split 不仅支持中文,针对于中韩日文字、少数民族文字、阿拉伯文等皆有优化,可以根据实际字体包内字符进行智能地分包。
WebAssembly 或者 Rust FFI 实现, 原生运行分包,进入秒级构建;7.0 版本更改了一些使用方式,请阅读文档进行修改。
Opentype Feature 支持情况 支持 95 | 部分支持 9| 等待测试 20
# 国内请设置环境变量, windows 用 set
export CN_FONT_SPLIT_GH_HOST=https://ik.imagekit.io/github
# set CN_FONT_SPLIT_GH_HOST=https://ik.imagekit.io/github # windows
pnpm i cn-font-split
推荐使用默认导入方式,cn-font-split 会自动检测运行环境(Node.js、Bun、Deno)并选择最优的实现方式:
import { fontSplit } from 'cn-font-split';
// 或者
import fontSplit from 'cn-font-split';
// Node.js 示例
import fs from 'fs';
const inputBuffer = new Uint8Array(
fs.readFileSync('../demo/public/SmileySans-Oblique.ttf').buffer,
);
// Bun 示例
// const inputBuffer = await Bun.file('../demo/public/SmileySans-Oblique.ttf').arrayBuffer();
console.time('font-split');
await fontSplit({
input: inputBuffer,
outDir: './dist/font',
});
console.timeEnd('font-split');
如果需要直接使用特定环境的实现,可以使用以下方式:
Node.js 版本:
import { fontSplit } from 'cn-font-split/dist/node/index';
Bun 版本:
import { fontSplit } from 'cn-font-split/dist/bun/index';
Deno 版本:
import { fontSplit } from 'cn-font-split/dist/deno/index';
import { fontSplit } from 'cn-font-split';
await fontSplit({
input: inputBuffer,
outDir: './dist/font',
previewImage: {
name: 'preview', // 文件名称
text: '中文网字计划\nThe Chinese Web Font Project', // 需要渲染的字
},
testHtml: true,
reporter: true,
});
只有 input 和 outDir 是必须的参数,其他参数均为可选参数,没有特殊需求不建议修改。
import { fontSplit } from 'cn-font-split';
await fontSplit({
input: inputBuffer, // 输入的字体缓冲区
outDir: './dist/font', // 输出目录
// subsets: [ // 手动分包范围,一般而言不需要手动配置
// [65,66,67], // 第一个分包,对照: 65(A)、66(B)、67(C)
// [102,103,104], // 第二个分包,对照: 102(f)、103(g)、104(h)
// ],
// css: { // CSS 输出产物配置,一般而言不需要手动配置
// fontFamily: 'Test Sans', // 输出 css 产物的 font-family 名称
// fontWeight: '400', // 字重: 400 (常规)、700(粗体), 详细可见 https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
// fontStyle: 'normal', // 字体样式: normal (常规)、italic (斜体)。可见 https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-style
// fontDisplay: 'swap', // 字体显示策略,推荐 swap。可见 https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display
// localFamily: ['Test Sans'], // 本地字体族名称。可见 https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face
// commentUnicodes: false, // 在 CSS 中添加 Unicode 码点注释
// compress: true // 压缩生成的 CSS 产物
// },
// languageAreas: false, // 是否启用语言区域优化,将同一语言的字符分到一起
// autoSubset: true, // 当分包超过指定大小时是否自动拆分
// fontFeature: true, // 是否保留字体特性(如 Code 字体的连字、字距调整等)
// reduceMins: true, // 是否减少碎片分包,合并小分包以减少请求数,一般不需要修改
previewImage: {
name: 'preview', // 预览图片的文件名
text: '中文网字计划\nThe Chinese Web Font Project', // 预览图片的文本内容
},
// chunkSize: 70 * 1024, // 单个分片目标大小
// chunkSizeTolerance: 1 * 1024, // 分片容差,一般不需要修改
// maxAllowSubsetsCount: 10, // 最大允许分包数量,可能会和 chunkSize 冲突
testHtml: true, // 是否生成测试 HTML 文件
reporter: true, // 是否生成 reporter.bin 文件
// 自定义分包输出的文件名为 6 位短哈希,或者使用自增索引: '[index].[ext]'
renameOutputFont: '[hash:6].[ext]',
// 不在控制台打印多余的日志信息
silent: true,
});
虽然在浏览器,但是速度极快。因为是 Wasm,所以 JS 环境基本都可以运行,我们甚至有一个 deno 版本的服务器。
# 首先安装 wasm 版本
cn-font-split i wasm32-wasip1
cn-font-split ls
import { fontSplit, StaticWasm } from 'cn-font-split/dist/wasm/index.js';
import wasmBuffer from 'cn-font-split/dist/libffi-wasm32-wasip1.wasm?url';
// 你的字体
const input = await fetch(
'https://jsdelivr.deno.dev/gh/KonghaYao/cn-font-split/packages/demo/public/SmileySans-Oblique.ttf',
).then((res) => res.arrayBuffer());
// 只需要初始化一次
const wasm = new StaticWasm(wasmBuffer);
const data = await fontSplit(
{
input: new Uint8Array(input),
outDir: './dist',
},
wasm.WasiHandle,
{
logger(str, type) {
console.log(str);
},
},
);
console.log(data);
// { name: string, data: Uint8Array }[]
| 参数名 | 描述 |
|---|---|
| CN_FONT_SPLIT_BIN | 二进制动态链接库的地址 |
| CN_FONT_SPLIT_GH_HOST | GitHub 域名(代理用) |
| CN_FONT_SPLIT_PLATFORM | 覆盖默认判断的平台 |
Apache-2.0
FAQs
划时代的字体切割工具,CJK与任何字符!支持 otf、ttf、woff2 字体多线程切割,完美地细颗粒度地进行包大小控制。A revolutionary font subetter that supports CJK and any characters! It enables multi-threaded subset of otf, ttf, and woff2 fonts, allowing for precise control over package size.
The npm package cn-font-split receives a total of 13,892 weekly downloads. As such, cn-font-split popularity was classified as popular.
We found that cn-font-split demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket's new Alert Details page is designed to surface more context, with a clearer layout, reachability dependency chains, and structured review.

Product
Campaign-level threat intelligence in Socket now shows when active supply chain attacks affect your repositories and packages.

Research
Malicious PyPI package sympy-dev targets SymPy users, a Python symbolic math library with 85 million monthly downloads.