Skip to content

🎥 Make videos programmatically with React

New article: Debugging font measurement bugs

Jonny Burgerd462a40a7011
diff --git a/packages/docs/docs/layout-utils/debug.mdx b/packages/docs/docs/layout-utils/debug.mdx
new file mode 100644
index 0000000000..85b80ccf16
--- /dev/null
+++ b/packages/docs/docs/layout-utils/debug.mdx
@@ -0,0 +1,73 @@
+---
+image: /generated/articles-docs-layout-utils-debug.png
+title: Debugging font measurement bugs
+sidebar_label: Debugging
+crumb: '@remotion/layout-utils'
+---
+
+When you find that the measurements that you are getting from [`measureText()`](/docs/layout-utils/measure-text), [`fillTextBox()`](/docs/layout-utils/fill-text-box) or [`fitText()`](/docs/layout-utils/fit-text) are off and are causing layout issues, follow the debugging instructions on this page.
+
+<Step>1</Step> Open your project in the Remotion Studio and select the
+composition in which you observe the issue.<br/>
+<Step>2</Step> Set the zoom to 100% to rule out any issues that arise from the
+zoom level.<br/>
+<Step>3</Step>Render at the bottom of your component a `<div>` that contains the string and all the properties that you passed to [`measureText()`](/docs/layout-utils/measure-text).
+
+```tsx twoslash title="Sample markup"
+import {AbsoluteFill} from 'remotion';
+
+const MyComp: React.FC = () => {
+ const fontSize = 100;
+ const fontWeight = 'bold';
+ const fontFamily = 'Roboto';
+ const text = 'Hello World ';
+ const letterSpacing = undefined;
+ const fontVariantNumeric = undefined;
+ const textTransform = undefined;
+
+ return (
+ <AbsoluteFill>
+ <div
+ id="remotion-measurer"
+ style={{
+ display: 'inline-block',
+ whiteSpace: 'pre',
+ fontSize,
+ fontWeight,
+ fontFamily,
+ letterSpacing,
+ fontVariantNumeric,
+ textTransform,
+ }}
+ >
+ {text}
+ </div>
+ </AbsoluteFill>
+ );
+};
+```
+
+<Step>4</Step> Open the Chrome DevTools and select the `<div id="remotion-measurer">` node in the "Elements" tab.<br/>
+<Step>5</Step> The node lights up, and you can see its measurements. See if there are any unexpected deviations.<br/>
+<Step>6</Step> Use the "Computed" tabs and go through all properties that may affect the layout, and compare them with the node in your composition that is causing issue. If the measurements are different, there will be at least 1 computed proeprty that has a different value.<br/>
+<Step>7</Step> Align your markup so that it has the same computed properties as the node in your composition.<br/>
+
+## Remember
+
+- The text gets measured with `whiteSpace: "pre"`, which includes whitespace.
+ - If you don't want to measure whitespace. call `.trim()` on your string.
+ - Make sure `whiteSpace: "pre"` is applied to your whole container, not just the individual words.
+ - Make sure you don't remove any whitespace while splitting your text that you leave in while measuring.
+- Make sure the font is loaded when you are calling `measureText()`.
+ - Use the `validateFontIsLoaded` option to ensure the font is loaded.
+- External styles may apply. See if you have CSS Resets, TailwindCSS stylesheets or other external resources messing up your layout.
+ - If this is the case, you can see it in the Computed Properties panel in the Chrome Dev Tools.
+- Don't use `padding` or `border` on your text. Use `outline` instead of `border`.
+- Don't multiply the font size with [`useCurrentScale()`](/docs/use-current-scale). It will lead to a broken layout.
+
+## See also
+
+You can also check out the source code of `measureText()`, paste and customize it in your project to aid debugging.
+
+- [Source code for `measureText()`](https://github.com/remotion-dev/remotion/blob/main/packages/layout-utils/src/layouts/measure-text.ts)
+- [Best practices](/docs/layout-utils/best-practices)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index cf007b0a28..290978ce0c 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -522,6 +522,7 @@ module.exports = {
'layout-utils/measure-text',
'layout-utils/fill-text-box',
'layout-utils/fit-text',
+ 'layout-utils/debug',
],
},
{
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 84214b1374..a3d9efe76d 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1679,6 +1679,13 @@ export const articles = [
compId: 'articles-docs-layout-utils-best-practices',
crumb: '@remotion/layout-utils',
},
+ {
+ id: 'layout-utils/debug',
+ title: 'Debugging font measurement bugs',
+ relativePath: 'docs/layout-utils/debug.mdx',
+ compId: 'articles-docs-layout-utils-debug',
+ crumb: '@remotion/layout-utils',
+ },
{
id: 'layout-utils/fill-text-box',
title: 'fillTextBox()',
diff --git a/packages/docs/static/generated/articles-docs-layout-utils-debug.png b/packages/docs/static/generated/articles-docs-layout-utils-debug.png
new file mode 100644
index 0000000000..318aabaa5d
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-layout-utils-debug.png differ