diff --git a/src/handler/expand.ts b/src/handler/expand.ts index a11e8bba..cee21f5c 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,24 @@ 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 + break + case 'rem': + return parsed.value * 16 + break + } + } 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..e59d756e 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..92a151a1 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..ad8d40a4 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..2c3fb40b 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..539bdbf6 100644 --- a/test/font.test.tsx +++ b/test/font.test.tsx @@ -103,12 +103,47 @@ describe('Font', () => { Hello , { - width: 100, - height: 100, + width: 300, + height: 300, fonts: [montserratFont], } ) - expect(toImage(svg, 100)).toMatchImageSnapshot() + expect(toImage(svg, 300)).toMatchImageSnapshot() + }) + + it('should handle font-size correctly for element like heading', async () => { + const [pxSvg, emSvg, remSvg] = await Promise.all( + [20, '1.5em', '2rem'].map((fontSize) => + satori( +
+

Hello, World

+

Hello, World

+
Hello, World
+
, + { + width: 400, + height: 400, + fonts, + } + ) + ) + ) + + expect(toImage(pxSvg, 400)).toMatchImageSnapshot() + expect(toImage(emSvg, 400)).toMatchImageSnapshot() + expect(toImage(remSvg, 400)).toMatchImageSnapshot() }) })