@@ -2,20 +2,44 @@ const { Buffer } = require('buffer')
2
2
const { readFileSync, writeFileSync } = require ( 'fs' )
3
3
const { join } = require ( 'path' )
4
4
5
+ const { convertSVGTextToPath, GlobalFonts } = require ( '@napi-rs/canvas' )
6
+ const { render } = require ( '@resvg/resvg-js' )
7
+
5
8
const { FontKit } = require ( '../wasm-node' )
6
9
7
10
const FONT_PATH = join ( __dirname , 'OpenSans-Italic.ttf' )
11
+ const glyphName = 'B'
8
12
9
13
const data = readFileSync ( FONT_PATH )
10
14
const fontkit = new FontKit ( )
11
15
const key = fontkit . add_font_from_buffer ( data )
12
16
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 )
15
26
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">
17
28
<path d="${ svgPath } " fill="blue" />
18
29
</svg>
19
30
`
20
31
writeFileSync ( join ( __dirname , './out.svg' ) , Buffer . from ( svg ) )
21
32
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 )
0 commit comments