Skip to content

Commit

Permalink
feat: the defaultFontFamily is not found in the OS and needs to be fa…
Browse files Browse the repository at this point in the history
…llback

Previously, resvg-js would not match any fonts in SVG text, even if there were fonts in the OS that could render them. This commit makes it possible to fallback properly.

```js
const resvg = new Resvg(svg, {
    font: {
      loadSystemFonts: true,
      defaultFontFamily: 'this-is-a-non-existent-font-family',
    },
  })
```
  • Loading branch information
yisibl committed Aug 7, 2023
1 parent d85973b commit 5496a80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
23 changes: 23 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ test('should be load custom fontDirs(no defaultFontFamily option)', (t) => {
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1726)
})

test('The defaultFontFamily is not found in the OS and needs to be fallback', (t) => {
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200" viewBox="0 0 300 200">
<text fill="blue" font-family="" font-size="100">
<tspan x="10" y="60%">Abc</tspan>
</text>
</svg>
`
const resvg = new Resvg(svg, {
font: {
loadSystemFonts: true,
defaultFontFamily: 'this-is-a-non-existent-font-family',
},
})
const pngData = resvg.render()
const originPixels = pngData.pixels.toJSON().data
// Find the number of blue `rgb(0,255,255)`pixels
const matchPixels = originPixels.join(',').match(/0,0,255/g)
console.info('✅ matchPixels length =', matchPixels?.length)
t.true(matchPixels !== null) // If the font is not matched, there are no blue pixels.
t.true((matchPixels?.length ?? 0) > 1500)
})

test('Async rendering', async (t) => {
const filePath = '../example/text.svg'
const svg = await fs.readFile(join(__dirname, filePath))
Expand Down
11 changes: 9 additions & 2 deletions src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,16 @@ fn set_wasm_font_families(
fontdb.set_monospace_family(&default_font_family);
}

/// 查询指定 font family 的字体是否存在,如果不存在则使用 fallback_font_family 代替。
#[cfg(not(target_arch = "wasm32"))]
fn find_and_debug_font_path(fontdb: &mut Database, font_family: &str) {
// 查找指定字体的路径
let query = Query {
families: &[Family::Name(font_family)],
..Query::default()
};

let now = std::time::Instant::now();
// 当前使用的字体是否存在
// 查询当前使用的字体是否存在
match fontdb.query(&query) {
Some(id) => {
let (src, index) = fontdb.face_source(id).unwrap();
Expand All @@ -195,6 +195,13 @@ fn find_and_debug_font_path(fontdb: &mut Database, font_family: &str) {
}
}
None => {
let fallback_font_family = "Arial".to_string();
fontdb.set_serif_family(&fallback_font_family);
fontdb.set_sans_serif_family(&fallback_font_family);
fontdb.set_cursive_family(&fallback_font_family);
fontdb.set_fantasy_family(&fallback_font_family);
fontdb.set_monospace_family(&fallback_font_family);

warn!(
"Warning: The default font-family '{}' not found.",
font_family
Expand Down

0 comments on commit 5496a80

Please sign in to comment.