🎥 Make videos programmatically with React
@remotion/web-renderer: Add text-decoration support (#8689)
Jonny Burger9f1dfb68684a
diff --git a/packages/docs/docs/client-side-rendering/limitations.mdx b/packages/docs/docs/client-side-rendering/limitations.mdxindex 5545b48bc8..d4742d7bc5 100644--- a/packages/docs/docs/client-side-rendering/limitations.mdx+++ b/packages/docs/docs/client-side-rendering/limitations.mdx@@ -73,7 +73,8 @@ The `font-kerning` property is <a style={{color: 'green'}}>supported</a>.The `font-stretch` property is <a style={{color: 'green'}}>supported</a>.The `direction` HTML attribute is <a style={{color: 'green'}}>supported</a>.The `writing-mode` property is <a style={{color: 'red'}}>not supported</a>.-The `text-decoration` property is <a style={{color: 'red'}}>not supported</a>.+The `text-decoration` property is <a style={{color: 'green'}}>supported</a> for solid `underline`, `overline`, and `line-through` lines with `text-decoration-color` and `text-decoration-thickness`.+Other `text-decoration-style` values and pixel-perfect line positions are <a style={{color: 'red'}}>not supported</a>.The `text-shadow` property is <a style={{color: 'green'}}>supported</a>.The `-webkit-text-stroke`, `-webkit-text-stroke-color`, `-webkit-text-stroke-width`, `paint-order` properties are <a style={{color: 'green'}}>supported</a>.diff --git a/packages/web-renderer/src/drawing/text/draw-text.ts b/packages/web-renderer/src/drawing/text/draw-text.tsindex 239ed40601..edaed82e39 100644--- a/packages/web-renderer/src/drawing/text/draw-text.ts+++ b/packages/web-renderer/src/drawing/text/draw-text.ts@@ -6,6 +6,7 @@ import {applyTextTransform} from './apply-text-transform';import {findWords} from './find-line-breaks.text';import {parsePaintOrder} from './parse-paint-order';import {parseTextShadow} from './parse-text-shadow';+import {drawTextDecoration, getTextDecorations} from './text-decoration';export const drawText = ({span,@@ -77,6 +78,12 @@ export const drawText = ({contextToDraw.letterSpacing = letterSpacing;contextToDraw.wordSpacing = wordSpacing;+ const textDecorations = getTextDecorations({+ computedStyle,+ onlyBackgroundClipText,+ span,+ });+const strokeWidth = parseFloat(webkitTextStrokeWidth);const hasStroke = strokeWidth > 0;if (hasStroke) {@@ -147,6 +154,16 @@ export const drawText = ({drawFill();drawStroke();}++ drawTextDecoration({+ contextToDraw,+ fontSizePx,+ measurements,+ parentRect,+ textDecorations,+ token,+ y,+ });}span.textContent = originalText;diff --git a/packages/web-renderer/src/drawing/text/text-decoration.ts b/packages/web-renderer/src/drawing/text/text-decoration.tsnew file mode 100644index 0000000000..5c496371f0--- /dev/null+++ b/packages/web-renderer/src/drawing/text/text-decoration.ts@@ -0,0 +1,194 @@+type TextDecorationLine = 'underline' | 'overline' | 'line-through';++export type TextDecoration = {+ lines: TextDecorationLine[];+ color: string;+ thickness: number;+};++const textDecorationLines: TextDecorationLine[] = [+ 'underline',+ 'overline',+ 'line-through',+];++const currentColorValues = new Set(['currentcolor', 'currentColor']);++const getDefaultTextDecorationThickness = (fontSizePx: number) => {+ return Math.max(1, Number.isFinite(fontSizePx) ? fontSizePx / 16 : 1);+};++export const parseTextDecoration = ({+ onlyBackgroundClipText,+ style,+}: {+ onlyBackgroundClipText: boolean;+ style: CSSStyleDeclaration;+}): TextDecoration | null => {+ const textDecorationStyle = style.getPropertyValue('text-decoration-style');+ if (textDecorationStyle && textDecorationStyle !== 'solid') {+ return null;+ }++ const textDecorationLine = style.getPropertyValue('text-decoration-line');+ const lineParts = textDecorationLine.split(/\s+/);+ const lines = textDecorationLines.filter((line) => lineParts.includes(line));++ if (lines.length === 0) {+ return null;+ }++ const textDecorationThickness = style.getPropertyValue(+ 'text-decoration-thickness',+ );+ const thicknessValue = parseFloat(textDecorationThickness);+ const thickness = Number.isFinite(thicknessValue)+ ? thicknessValue+ : getDefaultTextDecorationThickness(parseFloat(style.fontSize));++ if (thickness <= 0) {+ return null;+ }++ const textDecorationColor = style.getPropertyValue('text-decoration-color');++ return {+ lines,+ color:+ onlyBackgroundClipText ||+ !textDecorationColor ||+ currentColorValues.has(textDecorationColor)+ ? onlyBackgroundClipText+ ? 'black'+ : style.color+ : textDecorationColor,+ thickness,+ };+};++export const getTextDecorations = ({+ computedStyle,+ onlyBackgroundClipText,+ span,+}: {+ computedStyle: CSSStyleDeclaration;+ onlyBackgroundClipText: boolean;+ span: HTMLSpanElement;+}) => {+ const decorations: TextDecoration[] = [];+ const spanDecoration = parseTextDecoration({+ onlyBackgroundClipText,+ style: computedStyle,+ });++ if (spanDecoration) {+ decorations.push(spanDecoration);+ }++ let parent = span.parentElement;++ while (parent) {+ const parentDecoration = parseTextDecoration({+ onlyBackgroundClipText,+ style: getComputedStyle(parent),+ });++ if (parentDecoration) {+ decorations.push(parentDecoration);+ }++ parent = parent.parentElement;+ }++ return decorations;+};++const getTextDecorationY = ({+ line,+ measurements,+ y,+ thickness,+ fontSizePx,+}: {+ line: TextDecorationLine;+ measurements: TextMetrics;+ y: number;+ thickness: number;+ fontSizePx: number;+}) => {+ const fontAscent =+ measurements.fontBoundingBoxAscent ||+ measurements.actualBoundingBoxAscent ||+ fontSizePx;+ const fontDescent =+ measurements.fontBoundingBoxDescent ||+ measurements.actualBoundingBoxDescent ||+ fontSizePx * 0.2;+ const actualAscent = measurements.actualBoundingBoxAscent || fontAscent;++ if (line === 'underline') {+ return y + Math.max(thickness, fontDescent * 0.4);+ }++ if (line === 'overline') {+ return y - fontAscent + thickness / 2;+ }++ return y - actualAscent * 0.35;+};++export const drawTextDecoration = ({+ contextToDraw,+ fontSizePx,+ measurements,+ parentRect,+ textDecorations,+ token,+ y,+}: {+ contextToDraw: OffscreenCanvasRenderingContext2D;+ fontSizePx: number;+ measurements: TextMetrics;+ parentRect: DOMRect;+ textDecorations: TextDecoration[];+ token: {+ rect: DOMRect;+ };+ y: number;+}) => {+ if (textDecorations.length === 0) {+ return;+ }++ const startX = token.rect.left - parentRect.x;+ const endX = token.rect.right - parentRect.x;++ if (endX <= startX) {+ return;+ }++ contextToDraw.save();+ contextToDraw.lineCap = 'butt';++ for (const textDecoration of textDecorations) {+ contextToDraw.strokeStyle = textDecoration.color;+ contextToDraw.lineWidth = textDecoration.thickness;++ for (const line of textDecoration.lines) {+ const lineY = getTextDecorationY({+ line,+ measurements,+ y,+ thickness: textDecoration.thickness,+ fontSizePx,+ });++ contextToDraw.beginPath();+ contextToDraw.moveTo(startX, lineY);+ contextToDraw.lineTo(endX, lineY);+ contextToDraw.stroke();+ }+ }++ contextToDraw.restore();+};diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsxindex ebedc025ae..0cb0892696 100644--- a/packages/web-renderer/src/test/Root.tsx+++ b/packages/web-renderer/src/test/Root.tsx@@ -72,6 +72,7 @@ import {fontVariantCaps} from './fixtures/text/font-variant-caps';import {letterSpacing} from './fixtures/text/letter-spacing';import {paragraphs} from './fixtures/text/paragraphs';import {textFixture} from './fixtures/text/text';+import {textDecoration} from './fixtures/text/text-decoration';import {textShadow} from './fixtures/text/text-shadow';import {textShadowScale} from './fixtures/text/text-shadow-scale';import {textTransform} from './fixtures/text/text-transform';@@ -161,6 +162,7 @@ export const Root: React.FC = () => {<Composition {...paragraphs} /><Composition {...letterSpacing} /><Composition {...textTransform} />+ <Composition {...textDecoration} /><Composition {...fontStyle} /><Composition {...fontVariantCaps} /><Composition {...lineHeight} />diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-chromium-darwin.pngnew file mode 100644index 0000000000..2f1c6cef0cBinary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-chromium-darwin.png differdiff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-firefox-darwin.pngnew file mode 100644index 0000000000..a6ec253e21Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-firefox-darwin.png differdiff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-webkit-darwin.pngnew file mode 100644index 0000000000..f96761e2e4Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-webkit-darwin.png differdiff --git a/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx b/packages/web-renderer/src/test/fixtures/text/text-decoration.tsxnew file mode 100644index 0000000000..21ca6c9639--- /dev/null+++ b/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx@@ -0,0 +1,80 @@+import React from 'react';+import {AbsoluteFill} from 'remotion';++const row: React.CSSProperties = {+ fontFamily: 'Arial, sans-serif',+ fontSize: 38,+ fontWeight: 700,+ lineHeight: 1.45,+ margin: 0,+};++const Component: React.FC = () => {+ return (+ <AbsoluteFill+ style={{+ backgroundColor: 'white',+ color: '#1d2330',+ justifyContent: 'center',+ padding: 32,+ }}+ >+ <p+ style={{+ ...row,+ textDecorationLine: 'underline',+ }}+ >+ Underlined text+ </p>+ <p+ style={{+ ...row,+ textDecorationLine: 'overline',+ textDecorationColor: '#2b9a66',+ textDecorationThickness: 5,+ }}+ >+ Green overline+ </p>+ <p+ style={{+ ...row,+ textDecorationLine: 'line-through',+ textDecorationColor: '#d13131',+ textDecorationThickness: 4,+ }}+ >+ Line-through+ </p>+ <p+ style={{+ ...row,+ color: '#623aa2',+ textDecorationLine: 'underline overline line-through',+ textDecorationThickness: 3,+ }}+ >+ Combined lines+ </p>+ <p+ dir="rtl"+ style={{+ ...row,+ textDecoration: 'underline solid #0b7fab 4px',+ }}+ >+ Ù…Ø±ØØ¨Ø§ بالعالم+ </p>+ </AbsoluteFill>+ );+};++export const textDecoration = {+ component: Component,+ id: 'text-decoration',+ width: 500,+ height: 360,+ fps: 30,+ durationInFrames: 1,+} as const;diff --git a/packages/web-renderer/src/test/text-decoration.test.tsx b/packages/web-renderer/src/test/text-decoration.test.tsxnew file mode 100644index 0000000000..2f4f0320fa--- /dev/null+++ b/packages/web-renderer/src/test/text-decoration.test.tsx@@ -0,0 +1,23 @@+import {test} from 'vitest';+import {renderStillOnWeb} from '../render-still-on-web';+import '../symbol-dispose';+import {textDecoration} from './fixtures/text/text-decoration';+import {testImage} from './utils';++test('should render text-decoration', async () => {+ const blob = await (+ await renderStillOnWeb({+ licenseKey: 'free-license',+ composition: textDecoration,+ frame: 0,+ inputProps: {},+ })+ ).blob({format: 'png'});++ await testImage({+ blob,+ testId: 'text-decoration',+ threshold: 0,+ allowedMismatchedPixelRatio: 0.01,+ });+});