Skip to content

Commit

Permalink
fix: make sure registered font Buffer is valid (#1014)
Browse files Browse the repository at this point in the history
* fix: make sure registered font Buffer is valid

* Clippy
  • Loading branch information
Brooooooklyn authored Mar 4, 2025
1 parent d1137a5 commit 529e96c
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 254 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde = "1"
serde_derive = "1"
serde_json = "1"
thiserror = "2"
uuid = { version = "1", features = ["v4"] }

[target.'cfg(not(target_os = "linux"))'.dependencies]
mimalloc = "0.1"
Expand Down
10 changes: 7 additions & 3 deletions __test__/global-fonts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'fs'
import { join } from 'path'
import test from 'ava'

import { GlobalFonts } from '../index'
import { GlobalFonts, FontKey } from '../index'

const fontPath = join(__dirname, 'fonts', 'SourceSerifPro-Regular.ttf')
const fontData = readFileSync(fontPath)
Expand All @@ -16,8 +16,12 @@ test('should be able to register font and test font existence', (t) => {
t.is(GlobalFonts.has('114514'), false)

if (!GlobalFonts.has('Source Serif Pro')) {
t.true(GlobalFonts.register(fontData))
const fontKey = GlobalFonts.register(fontData)
t.true(fontKey instanceof FontKey)
t.is(GlobalFonts.families.length, defaultCount + 1)
t.notThrows(() => {
GlobalFonts.remove(fontKey!)
})
} else {
t.is(GlobalFonts.families.length, defaultCount)
}
Expand All @@ -32,7 +36,7 @@ test('multiple identical fonts should only exist within one font family', (t) =>
})

test('return false if font path not existed', (t) => {
t.false(GlobalFonts.register(Buffer.from('whatever')))
t.is(GlobalFonts.register(Buffer.from('whatever')), null)
})

test('should be able to register font with name alias', (t) => {
Expand Down
8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,20 @@ export function createCanvas(width: number, height: number): Canvas

export function createCanvas(width: number, height: number, svgExportFlag: SvgExportFlag): SvgCanvas

export declare class FontKey {
// make it a unique type
private readonly key: symbol
}

interface IGlobalFonts {
readonly families: { family: string; styles: { weight: number; width: string; style: string }[] }[]
// return true if succeeded
register(font: Buffer, nameAlias?: string): boolean
register(font: Buffer, nameAlias?: string): FontKey | null
// absolute path
registerFromPath(path: string, nameAlias?: string): boolean
has(name: string): boolean
loadFontsFromDir(path: string): number
remove(key: FontKey): void
}

export const GlobalFonts: IGlobalFonts
Expand Down
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
Path: Path2D,
ImageData,
Image,
CanvasPattern,
FontKey,
GlobalFonts,
PathOp,
FillType,
Expand All @@ -28,9 +28,6 @@ const SvgExportFlag = {
RelativePathEncoding: 0x04,
}

// eslint-disable-next-line sonarjs/no-unused-collection
const Fonts = []

if (!('families' in GlobalFonts)) {
Object.defineProperty(GlobalFonts, 'families', {
get: function () {
Expand Down Expand Up @@ -113,4 +110,5 @@ module.exports = {
DOMMatrix,
DOMRect,
loadImage,
FontKey,
}
Loading

0 comments on commit 529e96c

Please sign in to comment.