Skip to content

Commit

Permalink
fix: Handle font-size correctly for element like heading (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
Onengakushimoto committed Apr 20, 2023
1 parent 416d54c commit 1faf9ca
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/handler/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,10 @@ export default function expand(
}

// Calculate the base font size.
let baseFontSize: number =
typeof transformedStyle.fontSize === 'number'
? transformedStyle.fontSize
: inheritedStyle.fontSize
if (typeof baseFontSize === 'string') {
try {
const parsed = new CssDimension(baseFontSize)
switch (parsed.unit) {
case 'em':
baseFontSize = parsed.value * (inheritedStyle.fontSize as number)
break
case 'rem':
baseFontSize = parsed.value * 16
break
}
} catch (err) {
baseFontSize = 16
}
}
const baseFontSize = calcBaseFontSize(
transformedStyle.fontSize,
inheritedStyle.fontSize as number
)
if (typeof transformedStyle.fontSize !== 'undefined') {
transformedStyle.fontSize = baseFontSize
}
Expand Down Expand Up @@ -393,6 +378,22 @@ export default function expand(
return transformedStyle
}

function calcBaseFontSize(size: number | string, inheritedSize: number) {
if (typeof size === 'number') return size

try {
const parsed = new CssDimension(size)
switch (parsed.unit) {
case 'em':
return parsed.value * inheritedSize
case 'rem':
return parsed.value * 16
}
} catch (err) {
return inheritedSize
}
}

/**
* @see https://github.com/RazrFalcon/resvg/issues/579
*/
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/font.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,39 @@ describe('Font', () => {

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should handle font-size correctly for element like heading', async () => {
const svgs = await Promise.all(
[20, '0.8em', '1.2rem'].map((fontSize) =>
satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize,
fontWeight: 600,
}}
>
<h1 style={{ color: 'red' }}>Hello, World</h1>
<h2 style={{ color: 'orange' }}>Hello, World</h2>
<h5 style={{ color: 'grey', fontSize: 20 }}>Hello, World</h5>
</div>,
{
width: 100,
height: 100,
fonts,
}
)
)
)

svgs.forEach((svg) => {
expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})
})

0 comments on commit 1faf9ca

Please sign in to comment.