-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.ts
More file actions
126 lines (111 loc) · 3.71 KB
/
bench.ts
File metadata and controls
126 lines (111 loc) · 3.71 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* eslint-disable @typescript-eslint/no-unused-expressions */
import fs from 'fs'
import { bench, group, run } from 'mitata'
import opentype from 'opentype.js'
import path from 'path'
import { createSubsetEngine } from './bindings/javascript/wasm'
import { ttf2woff } from './deps/ttf2woff'
const wasmPath = path.join(__dirname, 'zig-out', 'ttf.wasm')
const ttfPath = path.join(__dirname, 'fonts', 'LXGWBright-Light.ttf')
const fontData = new Uint8Array(fs.readFileSync(ttfPath))
const wasmBinary = fs.readFileSync(wasmPath)
const testText = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789汉字测试段落The quick brown fox jumps over the lazy dog.'
const shortText = 'Hello World'
const longText =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789汉字测试段落The quick brown fox jumps over the lazy dog.这是一个很长的测试文本,包含了中英文混合的内容,用于测试字体子集生成的性能。Lorem ipsum dolor sit amet, consectetur adipiscing elit. 中文测试内容包括常用汉字和标点符号。'
const wasmEngine = createSubsetEngine(wasmBinary)
wasmEngine.loadFont(fontData)
const otFont = opentype.parse(fs.readFileSync(ttfPath).buffer)
function createOpentypeSubset(font: opentype.Font, text: string): ArrayBuffer {
const originalWarn = console.warn
console.warn = () => {}
const glyphs = []
const notdefGlyph = font.glyphs.get(0)
glyphs.push(notdefGlyph)
for (const char of text) {
const glyphIndex = font.charToGlyphIndex(char)
if (glyphIndex > 0) {
const glyph = font.glyphs.get(glyphIndex)
if (glyph && !glyphs.find((g) => g.index === glyph.index)) {
glyphs.push(glyph)
}
}
}
try {
const subsetFont = new opentype.Font({
familyName: font.names.fontFamily.en,
styleName: font.names.fontSubfamily.en,
unitsPerEm: font.unitsPerEm,
ascender: font.ascender,
descender: font.descender,
glyphs
})
return subsetFont.toArrayBuffer()
} finally {
console.warn = originalWarn
}
}
group('get glyph id for codepoint', () => {
bench('WASM', () => {
for (const char of testText) {
const codePoint = char.codePointAt(0)!
wasmEngine.getGlyphIdForCodepoint(codePoint)
}
})
bench('opentype.js', () => {
for (const char of testText) {
const codePoint = char.codePointAt(0)!
otFont.charToGlyphIndex(String.fromCodePoint(codePoint))
}
})
})
group('get glyph name', () => {
const glyphIds = Array.from({ length: 100 }, (_, i) => i + 1)
bench('WASM', () => {
for (const id of glyphIds) {
wasmEngine.getGlyphName(id)
}
})
bench('opentype.js', () => {
for (const id of glyphIds) {
otFont.glyphs.get(id)?.name
}
})
})
group('subset generation - short text', () => {
bench('WASM (instance method)', () => {
const engine = createSubsetEngine(wasmBinary)
engine.loadFont(fontData)
engine.createSubset()
engine.addTextToSubset(shortText)
engine.generateSubsetFont()
engine.destroy()
})
bench('opentype.js', () => {
createOpentypeSubset(otFont, shortText)
})
})
group('subset generation - long text', () => {
bench('WASM (instance method)', () => {
const engine = createSubsetEngine(wasmBinary)
engine.loadFont(fontData)
engine.createSubset()
engine.addTextToSubset(longText)
engine.generateSubsetFont()
engine.destroy()
})
bench('opentype.js', () => {
createOpentypeSubset(otFont, longText)
})
})
group('woff generation', () => {
bench('WASM', async () => {
const engine = createSubsetEngine(wasmBinary)
await engine.ttfToWoff(fontData)
engine.destroy()
})
bench('javascript', () => {
ttf2woff(fontData)
})
})
run().catch(console.error)