npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

leafer-x-webfont

v0.2.0

Published

LeaferJS 字体插件 —— Text 元素自动按需加载字体子集(6 字 ≈ 6KB),让画布与导出图用上任意中文字体

Readme

leafer-x-webfont

LeaferJS 中文字体插件 —— 画布里的 Text 用什么字,就只加载那几个字(6 字 ≈ 6KB,而非 16MB)。

为什么需要它

Leafer 的 Text 元素渲染时直接拼 canvas.font = fontFamily,依赖浏览器字体系统。 中文字体动辄 10MB+,FontFace 注册又慢又耗流量,海报/设计器场景根本没法用。

本插件订阅画布内 Text 的 text / fontFamily 变化,只对实际用到的字符调用 webfont 子集化 API,注册 KB 级子集字体后自动重渲染画布:

new Text({ text: '静心茶舍', fontFamily: '令东齐伋复刻体.ttf' })
→ 服务端裁剪 → 返回 ~6KB 子集 → FontFace 注册 → 画布自动重绘

安装

npm install leafer-x-webfont

使用

import { Leafer, Text } from 'leafer-ui'
import { WebFontPlugin } from 'leafer-x-webfont'

const leafer = new Leafer({ view: window })

// 一行接入
const webfont = new WebFontPlugin(leafer)

leafer.add(new Text({ text: '静心茶舍', fontFamily: '令东齐伋复刻体.ttf', fontSize: 64 }))
// 字体到位后画布自动重渲染

导出图片前

await webfont.ready()          // 等待所有已用字符的子集注册完成
const blob = await leafer.export('png', { pixelRatio: 2 })

配置项

new WebFontPlugin(leafer, {
  baseUrl: 'https://webfont.shenzilong.cn', // 自部署时改这里
  outType: 'woff2',
  debounceMs: 120,
  watch: true,        // 持续监听画布变化;静态海报导出可关掉
  debug: false,
  resolveFont: null,  // 自定义 fontFamily 解析规则,返回 null 跳过
})

特性

  • 零配置:任意 fontFamily(含 xxx.ttf 文件名写法)自动识别,字体不存在时静默回退
  • 增量去重:同一字体下字符集只增不减,改一个字只多发一个字符的子集请求
  • 失败记忆:字体不含的字符自动记入失败集,不反复 404
  • 导出友好:webfont.ready() 保证 leafer.export() 时字体已注册

本地开发

# 仓库根目录
pnpm install
pnpm dev            # 起前后端

# 打开 demo(插件源码直引,无需构建)
open packages/leafer-x-webfont/demo/index.html

License

MIT