diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 06266fc0..7d171159 100755 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -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 = ` + + + + + + ` + 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( () => { diff --git a/example/text2-out.png b/example/text2-out.png index 293d9610..0fed6e5e 100644 Binary files a/example/text2-out.png and b/example/text2-out.png differ diff --git a/src/fonts.rs b/src/fonts.rs index b9738dc7..52210acc 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -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(),