Skip to content

🎥 Make videos programmatically with React

@remotion/web-renderer: Support dotted, dashed and double text-decoration styles (#8733)

Matt Van Horn44f86e37c3f3
diff --git a/packages/docs/docs/client-side-rendering/limitations.mdx b/packages/docs/docs/client-side-rendering/limitations.mdx
index d4742d7bc5..629426b961 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 `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-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-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 5c496371f0..147cc47e7b 100644
--- a/packages/web-renderer/src/drawing/text/text-decoration.ts
+++ b/packages/web-renderer/src/drawing/text/text-decoration.ts
@@ -1,9 +1,11 @@
type TextDecorationLine = 'underline' | 'overline' | 'line-through';
+export type TextDecorationStyle = 'solid' | 'double' | 'dotted' | 'dashed';
export type TextDecoration = {
lines: TextDecorationLine[];
color: string;
thickness: number;
+ style: TextDecorationStyle;
};
const textDecorationLines: TextDecorationLine[] = [
@@ -18,6 +20,18 @@ 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') {
+ return style;
+ }
+
+ return 'solid';
+};
+
export const parseTextDecoration = ({
onlyBackgroundClipText,
style,
@@ -25,8 +39,10 @@ export const parseTextDecoration = ({
onlyBackgroundClipText: boolean;
style: CSSStyleDeclaration;
}): TextDecoration | null => {
- const textDecorationStyle = style.getPropertyValue('text-decoration-style');
- if (textDecorationStyle && textDecorationStyle !== 'solid') {
+ const textDecorationStyle = getTextDecorationStyle(
+ style.getPropertyValue('text-decoration-style').trim(),
+ );
+ if (textDecorationStyle === null) {
return null;
}
@@ -63,6 +79,7 @@ export const parseTextDecoration = ({
: style.color
: textDecorationColor,
thickness,
+ style: textDecorationStyle,
};
};
@@ -137,6 +154,38 @@ const getTextDecorationY = ({
return y - actualAscent * 0.35;
};
+const getTextDecorationLineDashPattern = (
+ style: TextDecorationStyle,
+ thickness: number,
+): number[] => {
+ if (style === 'dashed') {
+ return [thickness * 2, thickness];
+ }
+
+ if (style === 'dotted') {
+ return [thickness, thickness];
+ }
+
+ return [];
+};
+
+const strokeTextDecorationLine = ({
+ contextToDraw,
+ endX,
+ lineY,
+ startX,
+}: {
+ contextToDraw: OffscreenCanvasRenderingContext2D;
+ endX: number;
+ lineY: number;
+ startX: number;
+}) => {
+ contextToDraw.beginPath();
+ contextToDraw.moveTo(startX, lineY);
+ contextToDraw.lineTo(endX, lineY);
+ contextToDraw.stroke();
+};
+
export const drawTextDecoration = ({
contextToDraw,
fontSizePx,
@@ -183,10 +232,37 @@ export const drawTextDecoration = ({
fontSizePx,
});
- contextToDraw.beginPath();
- contextToDraw.moveTo(startX, lineY);
- contextToDraw.lineTo(endX, lineY);
- contextToDraw.stroke();
+ if (textDecoration.style === 'double') {
+ contextToDraw.setLineDash([]);
+ strokeTextDecorationLine({
+ contextToDraw,
+ endX,
+ lineY: lineY - textDecoration.thickness,
+ startX,
+ });
+ strokeTextDecorationLine({
+ contextToDraw,
+ endX,
+ lineY: lineY + textDecoration.thickness,
+ startX,
+ });
+ contextToDraw.setLineDash([]);
+ continue;
+ }
+
+ contextToDraw.setLineDash(
+ getTextDecorationLineDashPattern(
+ textDecoration.style,
+ textDecoration.thickness,
+ ),
+ );
+ strokeTextDecorationLine({
+ contextToDraw,
+ endX,
+ lineY,
+ startX,
+ });
+ contextToDraw.setLineDash([]);
}
}
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index 64d8b00f55..b1e1c3962d 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -73,7 +73,10 @@ 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 {
+ textDecoration,
+ textDecorationStyles,
+} 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';
@@ -164,6 +167,7 @@ export const Root: React.FC = () => {
<Composition {...letterSpacing} />
<Composition {...textTransform} />
<Composition {...textDecoration} />
+ <Composition {...textDecorationStyles} />
<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
new file mode 100644
index 0000000000..e9bbae3805
Binary files /dev/null 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
new file mode 100644
index 0000000000..1dc6c48c3b
Binary files /dev/null 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
new file mode 100644
index 0000000000..5525a70b7f
Binary files /dev/null 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/fixtures/text/text-decoration.tsx b/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx
index 21ca6c9639..287dbb481e 100644
--- a/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx
+++ b/packages/web-renderer/src/test/fixtures/text/text-decoration.tsx
@@ -9,6 +9,12 @@ const row: React.CSSProperties = {
margin: 0,
};
+const styleRow: React.CSSProperties = {
+ ...row,
+ fontSize: 30,
+ lineHeight: 1.35,
+};
+
const Component: React.FC = () => {
return (
<AbsoluteFill
@@ -70,6 +76,108 @@ const Component: React.FC = () => {
);
};
+const StylesComponent: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: 'white',
+ color: '#1d2330',
+ justifyContent: 'center',
+ padding: 24,
+ }}
+ >
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'dotted',
+ textDecorationColor: '#1976d2',
+ textDecorationThickness: 4,
+ }}
+ >
+ Dotted underline
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'dashed',
+ textDecorationColor: '#c56b12',
+ textDecorationThickness: 4,
+ }}
+ >
+ Dashed underline
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'double',
+ textDecorationColor: '#2b9a66',
+ textDecorationThickness: 3,
+ }}
+ >
+ Double underline
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'overline',
+ textDecorationStyle: 'dashed',
+ textDecorationColor: '#d13131',
+ textDecorationThickness: 6,
+ }}
+ >
+ Thick dashed overline
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'line-through',
+ textDecorationStyle: 'dotted',
+ textDecorationColor: '#623aa2',
+ textDecorationThickness: 4,
+ }}
+ >
+ Dotted line-through
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'line-through',
+ textDecorationStyle: 'double',
+ textDecorationColor: '#0b7fab',
+ textDecorationThickness: 4,
+ }}
+ >
+ Double line-through
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ color: '#7b2f91',
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'dashed',
+ textDecorationThickness: 4,
+ }}
+ >
+ Current color dash
+ </p>
+ <p
+ style={{
+ ...styleRow,
+ textDecorationLine: 'underline',
+ textDecorationStyle: 'wavy',
+ textDecorationColor: '#d13131',
+ textDecorationThickness: 4,
+ }}
+ >
+ Wavy skipped
+ </p>
+ </AbsoluteFill>
+ );
+};
+
export const textDecoration = {
component: Component,
id: 'text-decoration',
@@ -78,3 +186,12 @@ export const textDecoration = {
fps: 30,
durationInFrames: 1,
} as const;
+
+export const textDecorationStyles = {
+ component: StylesComponent,
+ id: 'text-decoration-styles',
+ 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 2f4f0320fa..661a4cd3ef 100644
--- a/packages/web-renderer/src/test/text-decoration.test.tsx
+++ b/packages/web-renderer/src/test/text-decoration.test.tsx
@@ -1,7 +1,10 @@
import {test} from 'vitest';
import {renderStillOnWeb} from '../render-still-on-web';
import '../symbol-dispose';
-import {textDecoration} from './fixtures/text/text-decoration';
+import {
+ textDecoration,
+ textDecorationStyles,
+} from './fixtures/text/text-decoration';
import {testImage} from './utils';
test('should render text-decoration', async () => {
@@ -21,3 +24,21 @@ test('should render text-decoration', async () => {
allowedMismatchedPixelRatio: 0.01,
});
});
+
+test('should render text-decoration-styles', async () => {
+ const blob = await (
+ await renderStillOnWeb({
+ licenseKey: 'free-license',
+ composition: textDecorationStyles,
+ frame: 0,
+ inputProps: {},
+ })
+ ).blob({format: 'png'});
+
+ await testImage({
+ blob,
+ testId: 'text-decoration-styles',
+ threshold: 0,
+ allowedMismatchedPixelRatio: 0.01,
+ });
+});