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

feat: improve custom loaded fonts #209

Merged
merged 13 commits into from
Aug 21, 2023
Merged

feat: improve custom loaded fonts #209

merged 13 commits into from
Aug 21, 2023

Conversation

yisibl
Copy link
Member

@yisibl yisibl commented Feb 14, 2023

The defaultFontFamily option can now be omitted when loading custom fonts. We'll read the font-family from the incoming fonts and set it to the default.

This feature is also available in Wasm (previously, loading fonts via the fontBuffers option was supported in Wasm).

const resvg = new Resvg(svg, {
    background: '#fff',
    font: {
      fontDirs: ['./__test__/'], // Load custom fonts from this directory.
      fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'], // Load custom fonts from this file.
      loadSystemFonts: false, // It will be faster to disable loading system fonts.
      // defaultFontFamily: 'Source Han Serif CN Light',
    },
    logLevel: 'debug', // Default Value: error
  })
  const pngBuffer = resvg.render().asPng()
node example/text

[2023-08-06T15:40:50Z DEBUG resvg_js::fonts] 📝 default_font_family = Source Han Serif CN Light
[2023-08-06T15:40:50Z DEBUG resvg_js::fonts] Font './example/SourceHanSerifCN-Light-subset.ttf':0 found in 0.008ms.
[2023-08-06T15:40:50Z DEBUG resvg_js::fonts] Loaded 2 font faces in 0.425ms.
[2023-08-06T15:40:50Z WARN  usvg_text_layout] Fallback from Source Han Serif CN Light to Pacifico.

text2-out

@vercel
Copy link

vercel bot commented Feb 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
resvg-js ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 20, 2023 2:57pm

yisibl added 2 commits August 6, 2023 20:42
When `loadSystemFonts: true`, the `defaultFontFamily` option can also be omitted.
…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',
    },
  })
```
@zimond zimond merged commit 8d5cd92 into main Aug 21, 2023
@yisibl yisibl deleted the better-load-custom-font branch August 21, 2023 06:49
yisibl added a commit that referenced this pull request Aug 21, 2023
* feat: improve custom loaded fonts

* feat: refactoring and adding test cases

When `loadSystemFonts: true`, the `defaultFontFamily` option can also be omitted.

* feat(wasm): support for getting the default font-family from `fontsBuffers`

* chore: new example/text.js

* feat: the defaultFontFamily is not found in the OS and needs to be fallback

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',
    },
  })
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants