Skip to content

Commit

Permalink
test: add load custom font test
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Feb 14, 2023
1 parent 1cc5234 commit 8cad124
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
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', async (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/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 8cad124

Please sign in to comment.