Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Support wavy text-decoration (#8763)

Carlos Pelazasabd47649c34c
diff --git a/packages/docs/docs/client-side-rendering/limitations.mdx b/packages/docs/docs/client-side-rendering/limitations.mdx
index 629426b961..c893ad8ced 100644
--- a/packages/docs/docs/client-side-rendering/limitations.mdx
+++ b/packages/docs/docs/client-side-rendering/limitations.mdx
@@ -73,8 +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: 'green'}}>supported</a> for `solid`, `dotted`, `dashed`, and `double` `text-decoration-style` values on `underline`, `overline`, and `line-through` lines with `text-decoration-color` and `text-decoration-thickness`.
-`wavy` `text-decoration-style` values and pixel-perfect line positions are <a style={{color: 'red'}}>not supported</a>.
+The `text-decoration` property is <a style={{color: 'green'}}>supported</a> for `solid`, `dotted`, `dashed`, `double`, and `wavy` `text-decoration-style` values on `underline`, `overline`, and `line-through` lines with `text-decoration-color` and `text-decoration-thickness`.
+`wavy` lines are approximated with Bézier curves and may not exactly match the browser-native wave shape. 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/text-decoration.ts b/packages/web-renderer/src/drawing/text/text-decoration.ts
index 147cc47e7b..e1077b90cb 100644
--- a/packages/web-renderer/src/drawing/text/text-decoration.ts
+++ b/packages/web-renderer/src/drawing/text/text-decoration.ts
@@ -1,5 +1,10 @@
type TextDecorationLine = 'underline' | 'overline' | 'line-through';
-export type TextDecorationStyle = 'solid' | 'double' | 'dotted' | 'dashed';
+export type TextDecorationStyle =
+ | 'solid'
+ | 'double'
+ | 'dotted'
+ | 'dashed'
+ | 'wavy';
export type TextDecoration = {
lines: TextDecorationLine[];
@@ -20,12 +25,13 @@ const getDefaultTextDecorationThickness = (fontSizePx: number) => {
return Math.max(1, Number.isFinite(fontSizePx) ? fontSizePx / 16 : 1);
};
-const getTextDecorationStyle = (style: string): TextDecorationStyle | null => {
- if (style === 'wavy') {
- return null;
- }
-
- if (style === 'double' || style === 'dotted' || style === 'dashed') {
+const getTextDecorationStyle = (style: string): TextDecorationStyle => {
+ if (
+ style === 'double' ||
+ style === 'dotted' ||
+ style === 'dashed' ||
+ style === 'wavy'
+ ) {
return style;
}
@@ -42,9 +48,6 @@ export const parseTextDecoration = ({
const textDecorationStyle = getTextDecorationStyle(
style.getPropertyValue('text-decoration-style').trim(),
);
- if (textDecorationStyle === null) {
- return null;
- }
const textDecorationLine = style.getPropertyValue('text-decoration-line');
const lineParts = textDecorationLine.split(/\s+/);
@@ -169,6 +172,62 @@ const getTextDecorationLineDashPattern = (
return [];
};
+const drawWavyTextDecorationLine = ({
+ contextToDraw,
+ endX,
+ lineY,
+ startX,
+ thickness,
+}: {
+ contextToDraw: OffscreenCanvasRenderingContext2D;
+ endX: number;
+ lineY: number;
+ startX: number;
+ thickness: number;
+}) => {
+ // Approximates native wavy rendering with quadratic Bézier curves.
+ // A quadratic curve peaks at half the control point offset, so control
+ // points at 2 * amplitude produce waves that peak at one amplitude.
+ const step = Math.max(2, thickness);
+ const amplitude = step;
+ const halfWavelength = step * 2;
+ const wavelength = halfWavelength * 2;
+
+ // Decorations are drawn per word token. Anchoring the wave phase to an
+ // absolute x grid keeps the wave continuous across adjacent tokens
+ // instead of restarting at every word boundary.
+ const phaseStartX = Math.floor(startX / wavelength) * wavelength;
+
+ contextToDraw.save();
+ contextToDraw.beginPath();
+ contextToDraw.rect(
+ startX,
+ lineY - amplitude - thickness,
+ endX - startX,
+ (amplitude + thickness) * 2,
+ );
+ contextToDraw.clip();
+
+ contextToDraw.beginPath();
+ contextToDraw.moveTo(phaseStartX, lineY);
+
+ let x = phaseStartX;
+ let direction = 1;
+ while (x < endX) {
+ contextToDraw.quadraticCurveTo(
+ x + halfWavelength / 2,
+ lineY + direction * amplitude * 2,
+ x + halfWavelength,
+ lineY,
+ );
+ x += halfWavelength;
+ direction = -direction;
+ }
+
+ contextToDraw.stroke();
+ contextToDraw.restore();
+};
+
const strokeTextDecorationLine = ({
contextToDraw,
endX,
@@ -232,6 +291,18 @@ export const drawTextDecoration = ({
fontSizePx,
});
+ if (textDecoration.style === 'wavy') {
+ contextToDraw.setLineDash([]);
+ drawWavyTextDecorationLine({
+ contextToDraw,
+ endX,
+ lineY,
+ startX,
+ thickness: textDecoration.thickness,
+ });
+ continue;
+ }
+
if (textDecoration.style === 'double') {
contextToDraw.setLineDash([]);
strokeTextDecorationLine({
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index b1e1c3962d..fa4a175f30 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -76,6 +76,7 @@ import {textFixture} from './fixtures/text/text';
import {
textDecoration,
textDecorationStyles,
+ textDecorationWavy,
} from './fixtures/text/text-decoration';
import {textShadow} from './fixtures/text/text-shadow';
import {textShadowScale} from './fixtures/text/text-shadow-scale';
@@ -168,6 +169,7 @@ export const Root: React.FC = () => {
<Composition {...textTransform} />
<Composition {...textDecoration} />
<Composition {...textDecorationStyles} />
+ <Composition {...textDecorationWavy} />
<Composition {...fontStyle} />
<Composition {...fontVariantCaps} />
<Composition {...lineHeight} />
diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-chromium-darwin.png
index e9bbae3805..805addf67e 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-chromium-darwin.png and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-firefox-darwin.png
index 1dc6c48c3b..c91e45b417 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-firefox-darwin.png and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-webkit-darwin.png
index 5525a70b7f..89b98be163 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-webkit-darwin.png and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-styles-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-chromium-darwin.png
new file mode 100644
index 0000000000..734e3744cc
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-firefox-darwin.png
new file mode 100644
index 0000000000..41f437debb
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-webkit-darwin.png
new file mode 100644
index 0000000000..b18a1df0e0
Binary files /dev/null and b/packages/web-renderer/src/test/__screenshots__/text-decoration.test.tsx/text-decoration-wavy-webkit-darwin.png differ
diff --git a/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx b/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx
index 287dbb481e..880c42b6ba 100644
--- a/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx
+++ b/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx
@@ -172,7 +172,82 @@ const StylesComponent: React.FC = () => {
textDecorationThickness: 4,
}}
>
- Wavy skipped
+ Wavy underline
+ </p>
+ </AbsoluteFill>
+ );
+};
+
+const WavyComponent: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: 'white',
+ color: '#1d2330',
+ justifyContent: 'center',
+ padding: 24,
+ }}
+ >
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'wavy',
+ textDecorationColor: '#d13131',
+ textDecorationThickness: 4,
+ }}
+ >
+ Wavy across words
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'overline',
+ textDecorationStyle: 'wavy',
+ textDecorationColor: '#1976d2',
+ textDecorationThickness: 4,
+ }}
+ >
+ Wavy overline
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'line-through',
+ textDecorationStyle: 'wavy',
+ textDecorationColor: '#2b9a66',
+ textDecorationThickness: 3,
+ }}
+ >
+ Wavy line-through
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecoration: 'underline wavy #623aa2 6px',
+ }}
+ >
+ Wavy shorthand thick
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'wavy',
+ }}
+ >
+ Default thickness wave
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ color: '#7b2f91',
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'wavy',
+ textDecorationThickness: 4,
+ }}
+ >
+ Current color wave
</p>
</AbsoluteFill>
);
@@ -195,3 +270,12 @@ export const textDecorationStyles = {
fps: 30,
durationInFrames: 1,
} as const;
+
+export const textDecorationWavy = {
+ component: WavyComponent,
+ id: 'text-decoration-wavy',
+ width: 500,
+ height: 420,
+ 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.tsx
index 661a4cd3ef..72519f7321 100644
--- a/packages/web-renderer/src/test/text-decoration.test.tsx
+++ b/packages/web-renderer/src/test/text-decoration.test.tsx
@@ -4,6 +4,7 @@ import '../symbol-dispose';
import {
textDecoration,
textDecorationStyles,
+ textDecorationWavy,
} from './fixtures/text/text-decoration';
import {testImage} from './utils';
@@ -42,3 +43,21 @@ test('should render text-decoration-styles', async () => {
allowedMismatchedPixelRatio: 0.01,
});
});
+
+test('should render text-decoration-wavy', async () => {
+ const blob = await (
+ await renderStillOnWeb({
+ licenseKey: 'free-license',
+ composition: textDecorationWavy,
+ frame: 0,
+ inputProps: {},
+ })
+ ).blob({format: 'png'});
+
+ await testImage({
+ blob,
+ testId: 'text-decoration-wavy',
+ threshold: 0,
+ allowedMismatchedPixelRatio: 0.01,
+ });
+});