Skip to content

About

Font Manager for Koishi | Koishi 的字体管理器

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

Font Manager for Koishi

Koishi 的字体管理插件

插件列表

名称 版本 描述
fonts NPM 核心插件,提供字体注册、下载与管理
fonts4pptr NPM Puppeteer 适配器,自动为页面和画布注入字体

安装方式

在插件市场中搜索 fonts 或 fonts4pptr 安装,或使用包管理器手动安装:

yarn add koishi-plugin-fonts koishi-plugin-fonts4pptr

使用方式

注册字体

可以通过配置文件、manifest 文件或 API 注册字体。详细说明请参考文档。

# koishi.yml
plugins:
  fonts:
    root: data/fonts

puppeteer.page(options?)

加载 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'],
})

puppeteer.render(content, families?, callback?)

渲染 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'],
)

canvas.createCanvas(width, height, options?)

创建画布并注入字体。

  • 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 协议。

About

Font Manager for Koishi | Koishi 的字体管理器

Resources

Stars

2 stars

Watchers

1 watching

Forks

Contributors

Languages