Koishi 的字体管理插件
| 名称 | 版本 | 描述 |
|---|---|---|
| fonts | 核心插件,提供字体注册、下载与管理 | |
| fonts4pptr | Puppeteer 适配器,自动为页面和画布注入字体 |
在插件市场中搜索 fonts 或 fonts4pptr 安装,或使用包管理器手动安装:
yarn add koishi-plugin-fonts koishi-plugin-fonts4pptr可以通过配置文件、manifest 文件或 API 注册字体。详细说明请参考文档。
# koishi.yml
plugins:
fonts:
root: data/fonts加载 fonts4pptr 后,ctx.puppeteer.page() 将自动获得字体注入能力。
- options.beforeGotoPage:
(page: Page) => Promise<void>页面跳转前回调,用于设置页面参数 - options.url:
string页面地址 - options.gotoOptions:
GotoOptions页面跳转选项 - options.content:
string要渲染的 HTML - options.families:
string[]字体家族名称列表 - 返回值:
Promise<Page>
const page = await ctx.puppeteer.page({
url: resolve(__dirname, 'index.html'),
content: '<div>Hello World</div>',
families: ['Noto Sans SC', 'GlowSansSC-Normal'],
})渲染 HTML 页面为图片,可指定渲染字体。
- content:
string要渲染的 HTML - families:
string[]字体家族名称列表 - callback:
(page, next) => Promise<string>回调函数 - 返回值:
string
const image = await ctx.puppeteer.render(
'<div style="font-size: 48px">Hello World</div>',
['Noto Sans SC'],
)创建画布并注入字体。
- width:
number宽度 - height:
number高度 - options.families:
string[]字体家族名称列表 - options.text:
string用于预加载字体的文本 - 返回值:
Promise<Canvas>
const canvas = await ctx.canvas.createCanvas(800, 600, {
families: ['Noto Sans SC', 'GlowSansSC-Normal'],
text: '预加载文本',
})
const ctx2d = canvas.getContext('2d')
ctx2d.font = '48px "Noto Sans SC"'
ctx2d.fillText('Hello', 10, 100)
const buffer = await canvas.toBuffer('image/png')以 ctx.puppeteer 或 ctx.canvas 为基础开发的插件无需直接依赖 fonts,只需声明 fonts4pptr:
export const inject = ['puppeteer', 'fonts4pptr']
// 或者
export const inject = ['canvas', 'fonts4pptr']当用户同时安装 fonts 和 fonts4pptr 后,你的插件调用 ctx.puppeteer.page({ families: [...] }) 即可自动获得字体支持。
本项目遵循 MIT 协议。