Debugging font measurement bugs
When you find that the measurements that you are getting from measureText()
, fillTextBox()
or fitText()
are off and are causing layout issues, follow the debugging instructions on this page.
<div>
that contains the string and all the properties that you passed to measureText()
.
Sample markuptsx
import {AbsoluteFill } from 'remotion';constMyComp :React .FC = () => {constfontSize = 100;constfontWeight = 'bold';constfontFamily = 'Roboto';consttext = 'Hello World ';constletterSpacing =undefined ;constfontVariantNumeric =undefined ;consttextTransform =undefined ;return (<AbsoluteFill ><div id ="remotion-measurer"style ={{display : 'inline-block',whiteSpace : 'pre',fontSize ,fontWeight ,fontFamily ,letterSpacing ,fontVariantNumeric ,textTransform ,}}>{text }</div ></AbsoluteFill >);};
<div id="remotion-measurer">
node in the "Elements" tab.
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.
- If you don't want to measure whitespace. call
- Make sure the font is loaded when you are calling
measureText()
.- Use the
validateFontIsLoaded
option to ensure the font is loaded.
- Use the
- 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
orborder
on your text. Useoutline
instead ofborder
. - Don't multiply the font size with
useCurrentScale()
. 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.