Skip to content

Commit

Permalink
chore: new example/text.js
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Aug 6, 2023
1 parent eb9ad3a commit d85973b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
5 changes: 1 addition & 4 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,7 @@ test('should render using font buffer provided by options', async (t) => {
const resvg = new Resvg(svg, {
font: {
fontFiles: ['./__test__/Pacifico-Regular.ttf'],
// fontDirs: ['./__test__/'],
// loadSystemFonts: false,
loadSystemFonts: false,
defaultFontFamily: '',
},
logLevel: 'debug',
Expand All @@ -516,8 +515,6 @@ test('should render using font buffer provided by options', async (t) => {
const expectedResult = await jimp.read(Buffer.from(expectedResultBuffer.buffer))
const actualPng = await jimp.read(Buffer.from(renderedResult))

await fs.writeFile(join(__dirname, './options_font_buffer_expected_extend.png'), renderedResult)

t.is(jimp.diff(expectedResult, actualPng, 0.01).percent, 0) // 0 means similar, 1 means not similar
})

Expand Down
23 changes: 15 additions & 8 deletions example/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ 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>
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="200" viewBox="0 0 500 200">
<defs>
<linearGradient id="fill" x1="0%" y1="45%" x2="100%" y2="55%">
<stop stop-color="#7f00de" offset="0%"/>
<stop stop-color="#ff007e" offset="100%"/>
</linearGradient>
</defs>
<text fill="url(#fill)" font-family="serif" font-size="60">
<tspan x="40" y="80">竹外桃花三两枝</tspan>
<tspan x="40" y="160">Hello resvg-js</tspan>
</text>
</svg>
`
const t = performance.now()
const resvg = new Resvg(svg, {
background: 'pink',
background: '#fff',
font: {
fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'], // Load custom fonts.
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',
// defaultFontFamily: 'Source Han Serif CN Light',
},
logLevel: 'debug', // Default Value: error
})
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
const pngBuffer = resvg.render().asPng()
console.info('✨ Done in', performance.now() - t, 'ms')

await promises.writeFile(join(__dirname, './text2-out.png'), pngBuffer)
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.
2 changes: 1 addition & 1 deletion src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn set_font_families(font_options: &JsFontOptions, fontdb: &mut Database) {
fontdb.set_fantasy_family(&default_font_family);
fontdb.set_monospace_family(&default_font_family);

debug!("default_font_family = {}", default_font_family);
debug!("📝 defaultFontFamily = {}", default_font_family);

#[cfg(not(target_arch = "wasm32"))]
find_and_debug_font_path(fontdb, default_font_family.as_str())
Expand Down

0 comments on commit d85973b

Please sign in to comment.