Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,46 @@ const { Buffer } = require('buffer')
const { readFileSync, writeFileSync } = require('fs')
const { join } = require('path')

const { convertSVGTextToPath, GlobalFonts } = require('@napi-rs/canvas')
const { render } = require('@resvg/resvg-js')

const { FontKit } = require('../wasm-node')

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

const data = readFileSync(FONT_PATH)
const fontkit = new FontKit()
const key = fontkit.add_font_from_buffer(data)
const font = fontkit.query(key)
const svgPath = font.glyph_path('A').to_string()
console.info('em-box = ', font.units_per_em)
const svgPath = font.glyph_path(glyphName).to_string()
const width = font.units_per_em // embox
const ascender = font.ascender
const descender = font.descender
const height = ascender - descender

console.info('em-box = ', width)
console.info('ascender = ', ascender)
console.info('descender = ', descender)

const svg = `<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
const svg = `<svg width="500" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
<path d="${svgPath}" fill="blue" />
</svg>
`
writeFileSync(join(__dirname, './out.svg'), Buffer.from(svg))
console.info(svg)

const svgText = `<svg width="500" height="500" xmlns="http://www.w3.org/2000/svg">
<text fill="green" font-family="Open Sans, Open Sans Italic" font-size="900">${glyphName}</text>
</svg>
`

GlobalFonts.registerFromPath(FONT_PATH)
const result = convertSVGTextToPath(svgText)
console.info('skr-canvas \n', result.toString('utf8'))

writeFileSync(join(__dirname, './skr-canvas.svg'), result)

const pngData = render(result.toString('utf8'))

writeFileSync(join(__dirname, './foo.png'), pngData)
Binary file added examples/foo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/out.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/skr-canvas.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"url": "[email protected]:alibaba/font-toolkit.git"
},
"devDependencies": {
"@napi-rs/canvas": "^0.1.21",
"@swc-node/register": "^1.4.2",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
Expand Down