-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathcanvas-polyfill.js
More file actions
57 lines (52 loc) · 2.15 KB
/
Copy pathcanvas-polyfill.js
File metadata and controls
57 lines (52 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Polyfill OffscreenCanvas for Node.js environments where it's not available.
// Required by @chenglou/pretext which uses canvas measureText for text width.
if (typeof globalThis.OffscreenCanvas === 'undefined') {
try {
const { createCanvas, GlobalFonts } = await import('@napi-rs/canvas')
const { existsSync } = await import('fs')
const { fileURLToPath } = await import('url')
const { join, dirname } = await import('path')
// @napi-rs/canvas (Skia) does not automatically load system fonts on Linux.
// Fonts are bundled in badge-maker/fonts/ (run `node scripts/download-fonts.js`
// to populate the directory before running tests).
const fontsDir = join(
dirname(fileURLToPath(import.meta.url)),
'..',
'fonts',
)
// [filename, familyNameOverride]
// familyNameOverride replaces the font's own internal family name in the
// Skia registry, so that CSS font strings like 'bold 11px Helvetica' resolve
// to the bundled metric-compatible substitute (Liberation Sans).
const fonts = [
// Verdana — flat / plastic / for-the-badge measurement font
['Verdana.ttf'],
['Verdana_Bold.ttf'],
// Liberation Sans registered as Helvetica for social-badge measurement
['LiberationSans-Regular.ttf', 'Helvetica'],
['LiberationSans-Bold.ttf', 'Helvetica'],
// Liberation Sans also registered as Arial (font-family fallback in SVG)
['LiberationSans-Regular.ttf', 'Arial'],
['LiberationSans-Bold.ttf', 'Arial'],
// DejaVu Sans — fallback in badge font-family lists
['DejaVuSans.ttf'],
['DejaVuSans-Bold.ttf'],
]
for (const [filename, familyName] of fonts) {
const fontPath = join(fontsDir, filename)
if (existsSync(fontPath)) {
GlobalFonts.registerFromPath(fontPath, familyName)
}
}
globalThis.OffscreenCanvas = class OffscreenCanvas {
constructor(width, height) {
this._canvas = createCanvas(width, height)
}
getContext(type) {
return this._canvas.getContext(type)
}
}
} catch {
// @napi-rs/canvas not available; OffscreenCanvas must be provided by the runtime
}
}