Skip to content

Commit

Permalink
debug: add text demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Feb 13, 2023
1 parent 0adc081 commit 7064b13
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
32 changes: 32 additions & 0 deletions example/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { promises } = require('fs')
const { join } = require('path')
const { performance } = require('perf_hooks')

const { Resvg } = require('../index')

async function main() {
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<text fill="blue" font-family="serif" font-size="120">
<tspan x="40" y="143">水</tspan>
</text>
</svg>
`
const t = performance.now()
const resvg = new Resvg(svg, {
background: '#fff',
font: {
fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'], // Load custom fonts.
loadSystemFonts: false, // It will be faster to disable loading system fonts.
defaultFontFamily: 'Source Han Serif CN Light',
},
logLevel: 'debug', // Default Value: error
})
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('✨ Done in', performance.now() - t, 'ms')

await promises.writeFile(join(__dirname, './text2-out.png'), pngBuffer)
}

main()
Binary file added example/text2-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
// - `cursive` - Comic Sans MS
// - `fantasy` - Impact (Papyrus on macOS)
// - `monospace` - Courier New
if !font_options.default_font_family.is_empty() {
// If a default font family exists, set all other families to that family.
// This prevents fonts from not being rendered in SVG.
fontdb.set_serif_family(&font_options.default_font_family);
fontdb.set_sans_serif_family(&font_options.default_font_family);
fontdb.set_cursive_family(&font_options.default_font_family);
fontdb.set_fantasy_family(&font_options.default_font_family);
fontdb.set_monospace_family(&font_options.default_font_family);
} else {
fontdb.set_serif_family(&font_options.serif_family);
fontdb.set_sans_serif_family(&font_options.sans_serif_family);
fontdb.set_cursive_family(&font_options.cursive_family);
fontdb.set_fantasy_family(&font_options.fantasy_family);
fontdb.set_monospace_family(&font_options.monospace_family);
}
// if !font_options.default_font_family.is_empty() {
// // If a default font family exists, set all other families to that family.
// // This prevents fonts from not being rendered in SVG.
// fontdb.set_serif_family(&font_options.default_font_family);
// fontdb.set_sans_serif_family(&font_options.default_font_family);
// fontdb.set_cursive_family(&font_options.default_font_family);
// fontdb.set_fantasy_family(&font_options.default_font_family);
// fontdb.set_monospace_family(&font_options.default_font_family);
// } else {
fontdb.set_serif_family(&font_options.serif_family);
fontdb.set_sans_serif_family(&font_options.sans_serif_family);
fontdb.set_cursive_family(&font_options.cursive_family);
fontdb.set_fantasy_family(&font_options.fantasy_family);
fontdb.set_monospace_family(&font_options.monospace_family);
// }
debug!(
"Loaded {} font faces in {}ms.",
fontdb.len(),
Expand Down

0 comments on commit 7064b13

Please sign in to comment.