🎥 Make videos programmatically with React
`@remotion/web-renderer`: Account for line-height < 1 in bounding rect
JonnyBurger4888cc120779
diff --git a/packages/web-renderer/src/get-biggest-bounding-client-rect.ts b/packages/web-renderer/src/get-biggest-bounding-client-rect.tsindex c17375af12..d4b7ef395e 100644--- a/packages/web-renderer/src/get-biggest-bounding-client-rect.ts+++ b/packages/web-renderer/src/get-biggest-bounding-client-rect.ts@@ -2,6 +2,50 @@ import {parseBoxShadow} from './drawing/draw-box-shadow';import {parseOutlineOffset, parseOutlineWidth} from './drawing/draw-outline';import {skipToNextNonDescendant} from './walk-tree';+/**+ * Calculates the vertical text overflow when line-height is less than 1.+ * When line-height < 1, text ascenders/descenders can extend beyond the element's bounding box.+ * Returns the extra padding needed on top and bottom.+ */+const getTextOverflowForLineHeight = (+ computedStyle: CSSStyleDeclaration,+): {top: number; bottom: number} => {+ const {lineHeight} = computedStyle;+ const fontSize = parseFloat(computedStyle.fontSize);++ if (!fontSize || isNaN(fontSize)) {+ return {top: 0, bottom: 0};+ }++ let lineHeightValue: number;++ if (lineHeight === 'normal') {+ // 'normal' is typically around 1.2, no overflow+ return {top: 0, bottom: 0};+ }++ if (lineHeight.endsWith('px')) {+ lineHeightValue = parseFloat(lineHeight) / fontSize;+ } else {+ // Unitless value or percentage+ lineHeightValue = parseFloat(lineHeight);+ if (lineHeight.endsWith('%')) {+ lineHeightValue /= 100;+ }+ }++ if (isNaN(lineHeightValue) || lineHeightValue >= 1) {+ return {top: 0, bottom: 0};+ }++ // When line-height < 1, the line box is smaller than the font-size.+ // Text can overflow by approximately (1 - lineHeight) * fontSize total,+ // split between top and bottom.+ const overflow = (1 - lineHeightValue) * fontSize;++ return {top: overflow, bottom: overflow};+};+export const getBiggestBoundingClientRect = (element: HTMLElement | SVGElement,) => {@@ -20,6 +64,9 @@ export const getBiggestBoundingClientRect = (const outlineOffset = parseOutlineOffset(computedStyle.outlineOffset);const rect = (treeWalker.currentNode as Element).getBoundingClientRect();+ // Calculate text overflow for elements with line-height < 1+ const textOverflow = getTextOverflowForLineHeight(computedStyle);+// Calculate box shadow extensionsconst shadows = parseBoxShadow(computedStyle.boxShadow);let shadowLeft = 0;@@ -54,7 +101,7 @@ export const getBiggestBoundingClientRect = ();mostTop = Math.min(mostTop,- rect.top - outlineOffset - outlineWidth - shadowTop,+ rect.top - outlineOffset - outlineWidth - shadowTop - textOverflow.top,);mostRight = Math.max(mostRight,@@ -62,7 +109,11 @@ export const getBiggestBoundingClientRect = ();mostBottom = Math.max(mostBottom,- rect.bottom + outlineOffset + outlineWidth + shadowBottom,+ rect.bottom ++ outlineOffset ++ outlineWidth ++ shadowBottom ++ textOverflow.bottom,);if (computedStyle.overflow === 'hidden') {diff --git a/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-chromium-darwin.pngnew file mode 100644index 0000000000..907025bd7dBinary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-chromium-darwin.png differdiff --git a/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-firefox-darwin.pngnew file mode 100644index 0000000000..d89ddf558cBinary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-firefox-darwin.png differdiff --git a/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-webkit-darwin.pngnew file mode 100644index 0000000000..800e0c2d69Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/filter-line-height-clip.test.tsx/filter-line-height-clip-webkit-darwin.png differdiff --git a/packages/web-renderer/src/test/filter-line-height-clip.test.tsx b/packages/web-renderer/src/test/filter-line-height-clip.test.tsxnew file mode 100644index 0000000000..61cffeaade--- /dev/null+++ b/packages/web-renderer/src/test/filter-line-height-clip.test.tsx@@ -0,0 +1,18 @@+import {test} from 'vitest';+import {renderStillOnWeb} from '../render-still-on-web';+import '../symbol-dispose';+import {filterLineHeightClip} from './fixtures/text/filter-line-height-clip';+import {testImage} from './utils';++test('should render filter-line-height-clip', async () => {+ const blob = await (+ await renderStillOnWeb({+ licenseKey: 'free-license',+ composition: filterLineHeightClip,+ frame: 0,+ inputProps: {},+ })+ ).blob({format: 'png'});++ await testImage({blob, testId: 'filter-line-height-clip'});+});diff --git a/packages/web-renderer/src/test/fixtures/text/filter-line-height-clip.tsx b/packages/web-renderer/src/test/fixtures/text/filter-line-height-clip.tsxnew file mode 100644index 0000000000..c5a8434ae5--- /dev/null+++ b/packages/web-renderer/src/test/fixtures/text/filter-line-height-clip.tsx@@ -0,0 +1,222 @@+import React from 'react';+import {AbsoluteFill} from 'remotion';++const Component: React.FC = () => {+ return (+ <AbsoluteFill+ style={{+ backgroundColor: '#222',+ justifyContent: 'center',+ alignItems: 'center',+ }}+ >+ <div+ style={{+ position: 'absolute',+ left: '50%',+ top: '50%',+ textAlign: 'center',+ padding: 40,+ userSelect: 'none',+ touchAction: 'none',+ border: 'none',+ boxShadow: 'none',+ zIndex: 10,+ borderRadius: 4,+ fontSize: 21,+ overflowWrap: 'break-word',+ wordBreak: 'break-word',+ lineHeight: 0.9,+ transform: 'translate3d(-50%, -50%, 0px)',+ display: 'flex',+ placeItems: 'center',+ justifyContent: 'center',+ }}+ >+ <div+ id="video-player-container"+ style={{+ display: 'flex',+ justifyContent: 'center',+ alignItems: 'center',+ pointerEvents: 'none',+ }}+ >+ <div+ style={{+ direction: 'ltr',+ left: '50%',+ top: '50%',+ }}+ >+ <div+ id="word-wrapper"+ style={{+ display: 'flex',+ whiteSpace: 'nowrap',+ flexDirection: 'column',+ filter:+ 'drop-shadow(rgb(255, 255, 255) 0px 0px 100px) drop-shadow(rgba(0, 0, 0, 0.35) 5px 5px 15px)',+ }}+ >+ <div+ style={{+ width: '100%',+ position: 'relative',+ textAlign: 'center',+ }}+ >+ <div+ data-line-id="9"+ data-line-active="true"+ style={{+ display: 'block',+ position: 'relative',+ fontFamily: 'sans-serif',+ fontSize: 32,+ lineHeight: 0.9,+ color: 'rgb(255, 255, 255)',+ textAlign: 'center',+ textShadow: 'rgba(0, 0, 0, 0.35) 5px 5px 15px',+ height: 'unset',+ opacity: 1,+ }}+ >+ <span+ style={{+ position: 'relative',+ display: 'inline-block',+ whiteSpace: 'pre',+ }}+ >+ <span+ aria-hidden="true"+ style={{+ position: 'absolute',+ top: '50%',+ left: '50%',+ width: '120%',+ height: '80%',+ transform: 'translate(-50%, -50%)',+ filter: 'blur(10px)',+ pointerEvents: 'none',+ zIndex: 0,+ }}+ />+ <span+ data-word-id="19"+ style={{+ fontFamily: 'sans-serif',+ fontSize: 63,+ color: 'rgb(255, 255, 255)',+ display: 'inline-block',+ textAlign: 'center',+ textShadow: 'none',+ fontWeight: 800,+ lineHeight: 0.9,+ filter: 'drop-shadow(rgba(0, 0, 0, 0.35) 5px 5px 15px)',+ opacity: 1,+ whiteSpace: 'pre',+ }}+ >+ announced by+ </span>+ </span>+ <span+ style={{+ position: 'relative',+ display: 'inline-block',+ whiteSpace: 'pre',+ }}+ >+ <span+ aria-hidden="true"+ style={{+ position: 'absolute',+ top: '50%',+ left: '50%',+ width: '120%',+ height: '80%',+ transform: 'translate(-50%, -50%)',+ filter: 'blur(10px)',+ pointerEvents: 'none',+ zIndex: 0,+ }}+ />+ <span+ data-word-id="-1"+ style={{+ fontFamily: 'sans-serif',+ fontSize: 63,+ color: 'rgb(255, 255, 255)',+ display: 'inline-block',+ textAlign: 'center',+ textShadow: 'none',+ fontWeight: 800,+ lineHeight: 0.9,+ filter: 'drop-shadow(rgba(0, 0, 0, 0.35) 5px 5px 15px)',+ opacity: 1,+ whiteSpace: 'pre',+ }}+ >+ {' '}+ </span>+ </span>+ <span+ style={{+ position: 'relative',+ display: 'inline-block',+ whiteSpace: 'pre',+ }}+ >+ <span+ aria-hidden="true"+ style={{+ position: 'absolute',+ top: '50%',+ left: '50%',+ width: '120%',+ height: '80%',+ transform: 'translate(-50%, -50%)',+ filter: 'blur(10px)',+ pointerEvents: 'none',+ zIndex: 0,+ }}+ />+ <span+ data-word-id="20"+ style={{+ fontFamily: 'sans-serif',+ fontSize: 63,+ color: 'rgb(255, 255, 255)',+ display: 'inline-block',+ textAlign: 'center',+ textShadow: 'none',+ fontWeight: 800,+ lineHeight: 0.9,+ filter: 'drop-shadow(rgba(0, 0, 0, 0.35) 5px 5px 15px)',+ opacity: 1,+ whiteSpace: 'pre',+ }}+ >+ a+ </span>+ </span>+ </div>+ </div>+ </div>+ </div>+ </div>+ </div>+ </AbsoluteFill>+ );+};++export const filterLineHeightClip = {+ component: Component,+ id: 'filter-line-height-clip',+ width: 800,+ height: 400,+ fps: 25,+ durationInFrames: 1,+} as const;