Skip to content

Commit 0ae442e

Browse files
committed
test: add glyph path diff
1 parent 087bf3e commit 0ae442e

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

examples/basic.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,44 @@ const { Buffer } = require('buffer')
22
const { readFileSync, writeFileSync } = require('fs')
33
const { join } = require('path')
44

5+
const { convertSVGTextToPath, GlobalFonts } = require('@napi-rs/canvas')
6+
const { render } = require('@resvg/resvg-js')
7+
58
const { FontKit } = require('../wasm-node')
69

710
const FONT_PATH = join(__dirname, 'OpenSans-Italic.ttf')
11+
const glyphName = 'B'
812

913
const data = readFileSync(FONT_PATH)
1014
const fontkit = new FontKit()
1115
const key = fontkit.add_font_from_buffer(data)
1216
const font = fontkit.query(key)
13-
const svgPath = font.glyph_path('A').to_string()
14-
console.info('em-box = ', font.units_per_em)
17+
const svgPath = font.glyph_path(glyphName).to_string()
18+
const width = font.units_per_em // embox
19+
const ascender = font.ascender
20+
const descender = font.descender
21+
const height = ascender - descender
22+
23+
console.info('em-box = ', width)
24+
console.info('ascender = ', ascender)
25+
console.info('descender = ', descender)
1526

16-
const svg = `<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
27+
const svg = `<svg width="500" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
1728
<path d="${svgPath}" fill="blue" />
1829
</svg>
1930
`
2031
writeFileSync(join(__dirname, './out.svg'), Buffer.from(svg))
2132
console.info(svg)
33+
34+
const svgText = `<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
35+
<text fill="green" font-family="Open Sans, Open Sans Italic" font-size="900">${glyphName}</text>
36+
</svg>
37+
`
38+
39+
GlobalFonts.registerFromPath(FONT_PATH)
40+
const result = convertSVGTextToPath(svgText)
41+
console.info('skr-canvas \n', result.toString('utf8'))
42+
43+
const pngData = render(result.toString('utf8'))
44+
45+
writeFileSync(join(__dirname, './foo.png'), pngData)

examples/out.svg

Lines changed: 2 additions & 2 deletions
Loading

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"url": "[email protected]:alibaba/font-toolkit.git"
3232
},
3333
"devDependencies": {
34+
"@napi-rs/canvas": "^0.1.21",
3435
"@swc-node/register": "^1.4.2",
3536
"@typescript-eslint/eslint-plugin": "^5.15.0",
3637
"@typescript-eslint/parser": "^5.15.0",

0 commit comments

Comments
 (0)