web-font/vendor/fonteditor-core
崮生(子虚) 168fc66544 feat(v1.9.0): OTF 子集化支持 + 法字笔画修复 + subsetCache 版本指纹
性能(vs v1.7.0 基线,基准测试全用例通过):
- 千字文 woff2 346ms → 9.8ms(35x)
- 8汉字 woff2 39.7ms → 2.8ms(14x)
- 千字文 ttf 6.0ms → 4.4ms
- 8汉字 ttf 2.7ms → 2.1ms

OTF 正确性:
- 修复 CFF.js readCFFIndexObject 漏读 off[idx+1] 导致子程序读成 0 字节,
  思源黑体「法」字三点水丢笔画的问题
- 修复非 subset 模式 cmap format12 崩溃(空数组误判为 subset)

缓存健壮性:
- subsetCache key 加版本指纹(package version + process.uptime),
  杜绝代码已修但缓存返回旧错误结果

基准测试:
- 新增白狐/思源 OTF 用例(含法海波等复杂笔画字)守护 OTF→TTF 转换
- 修复 calculateSSIM 的 width 推断 bug(sqrt 假设正方形)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 21:01:19 +08:00
..
2026-06-13 20:19:57 +08:00

fonteditor-core

FontEditor core functions

NPM version Downloads

Feature

Read and write sfnt font like ttf, woff, woff2, eot, svg, otf.

  • sfnt parse
  • read, write, transform fonts
    • ttf (read and write)
    • woff (read and write)
    • woff2 (read and write)
    • eot (read and write)
    • svg (read and write)
    • otf (only read and convert to ttf)
  • ttf glyph adjust
  • svg to glyph
  • ESM compatibility for modern bundlers (Webpack, Rollup, Vite, Next.js, etc.)
  • TypeScript support with type definitions

Usage

// read font file
import {createFont} from 'fonteditor-core';
import fs from 'fs';

const buffer = fs.readFileSync('font.ttf');
// read font data, support format:
// - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer
// - for svg, support string or Document(parsed svg)
const font = createFont(buffer, {
    // support ttf, woff, woff2, eot, otf, svg
    type: 'ttf',
    // only read `a`, `b` glyphs
    subset: [65, 66],
    // read font hinting tables, default false
    hinting: true,
    // read font kerning tables, default false
    kerning: true,
    // transform ttf compound glyph to simple
    compound2simple: true,
    // inflate function for woff
    inflate: undefined,
    // for svg path
    combinePath: false,
});
const fontObject = font.get();
console.log(Object.keys(fontObject));

/* => [ 'version',
  'numTables',
  'searchRenge',
  'entrySelector',
  'rengeShift',
  'head',
  'maxp',
  'glyf',
  'cmap',
  'name',
  'hhea',
  'post',
  'OS/2',
  'fpgm',
  'cvt',
  'prep'
]
*/

// write font file
const buffer = font.write({
    // support ttf, woff, woff2, eot, svg
    type: 'woff',
    // save font hinting tables, default false
    hinting: false,
    // save font kerning tables, default false
    kerning: false,
    // write glyf data when simple glyph has no contours, default false
    writeZeroContoursGlyfData: false,
    // deflate function for woff, eg. pako.deflate
    deflate: undefined,
    // for user to overwrite head.xMin, head.xMax, head.yMin, head.yMax, hhea etc.
    support: {head: {}, hhea: {}}
});
fs.writeFileSync('font.woff', buffer);

// to base64 str
font.toBase64({
    // support ttf, woff, woff2, eot, svg
    type: 'ttf'
});

// optimize glyphs
font.optimize()

// compound2simple
font.compound2simple()

// sort glyphs
font.sort()

// find glyphs
const result = font.find({
  unicode: [65]
});

const result = font.find({
  filter: function (glyf) {
    return glyf.name === 'icon'
  }
});

// merge another font object
font.merge(font1, {
  scale: 1
});

Modern ES Module Usage

This library supports both CommonJS and ES Modules. For detailed information on using with modern bundlers, please see ESM_USAGE.md.

// ESM import
import fonteditorCore, { createFont, woff2 } from 'fonteditor-core';

createFont(buffer, options);

woff2

Notice: woff2 use wasm build of google woff2, before read and write woff2, we should first call woff2.init().

import {createFont, woff2} from 'fonteditor-core';

// in nodejs
woff2.init().then(() => {
    // read woff2
    const font =  createFont(buffer, {
      type: 'woff2'
    });
    // write woff2
    const buffer = font.write({type: 'woff2'});
});

// in browser
woff2.init('/assets/woff2.wasm').then(() => {
    // read woff2
    const font = createFont();
    // write woff2
    const arrayBuffer = font.write({type: 'woff2'});
});

Demo

npm run dev

build

npm run build

test

npm run test

support

Node.js:>= 12.0

Browser: Chrome, Safari

License

MIT © Fonteditor