diff --git a/src/handler/expand.ts b/src/handler/expand.ts index a11e8bba..72669b39 100644 --- a/src/handler/expand.ts +++ b/src/handler/expand.ts @@ -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 } @@ -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 */ diff --git a/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-family-fallback-1-snap.png b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-family-fallback-1-snap.png index 0aa275c2..edb0dfcd 100644 Binary files a/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-family-fallback-1-snap.png and b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-family-fallback-1-snap.png differ diff --git a/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-1-snap.png b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-1-snap.png new file mode 100644 index 00000000..47f12b6e Binary files /dev/null and b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-1-snap.png differ diff --git a/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-2-snap.png b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-2-snap.png new file mode 100644 index 00000000..f90fe9ee Binary files /dev/null and b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-2-snap.png differ diff --git a/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-3-snap.png b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-3-snap.png new file mode 100644 index 00000000..35b5230e Binary files /dev/null and b/test/__image_snapshots__/font-test-tsx-test-font-test-tsx-font-should-handle-font-size-correctly-for-element-like-heading-3-snap.png differ diff --git a/test/font.test.tsx b/test/font.test.tsx index 5c96dc31..33cdcd98 100644 --- a/test/font.test.tsx +++ b/test/font.test.tsx @@ -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( +
+

Hello, World

+

Hello, World

+
Hello, World
+
, + { + width: 100, + height: 100, + fonts, + } + ) + ) + ) + + svgs.forEach((svg) => { + expect(toImage(svg, 100)).toMatchImageSnapshot() + }) + }) })