Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the defaultFontFamily not working #208

Merged
merged 3 commits into from
Feb 15, 2023
Merged
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
23 changes: 23 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,29 @@ test('should return undefined if bbox is invalid', (t) => {
t.is(resvg.innerBBox(), undefined)
})

test('should be load custom fonts', (t) => {
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 resvg = new Resvg(svg, {
font: {
fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'],
loadSystemFonts: false,
defaultFontFamily: 'Source Han Serif CN Light',
},
logLevel: 'debug',
})
const pngData = resvg.render()
const originPixels = pngData.pixels.toJSON().data

// Find the number of blue `rgb(0,255,255)`pixels
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1726)
})

test('should throw because invalid SVG attribute (width attribute is 0)', (t) => {
const error = t.throws(
() => {
Expand Down
Binary file modified example/text-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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: 'pink',
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.
20 changes: 15 additions & 5 deletions src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
// - `cursive` - Comic Sans MS
// - `fantasy` - Impact (Papyrus on macOS)
// - `monospace` - Courier New
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