From 732bd7f6160ce9da4cc3063b3f2b47b3d204f446 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Fri, 13 Sep 2024 09:58:57 -0700 Subject: [PATCH] fix: size text with computed styles even when hidden (#8572) * fix: size text with computed styles even when hidden * refactor: remove unneeded try/catch. --- core/utils/dom.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/core/utils/dom.ts b/core/utils/dom.ts index e318e7e91..87019dbb2 100644 --- a/core/utils/dom.ts +++ b/core/utils/dom.ts @@ -208,16 +208,14 @@ export function getTextWidth(textElement: SVGTextElement): number { } } - // Attempt to compute fetch the width of the SVG text element. - try { - width = textElement.getComputedTextLength(); - } catch (e) { - // In other cases where we fail to get the computed text. Instead, use an - // approximation and do not cache the result. At some later point in time - // when the block is inserted into the visible DOM, this method will be - // called again and, at that point in time, will not throw an exception. - return textElement.textContent!.length * 8; - } + // Compute the width of the SVG text element. + const style = window.getComputedStyle(textElement); + width = getFastTextWidthWithSizeString( + textElement, + style.fontSize, + style.fontWeight, + style.fontFamily, + ); // Cache the computed width and return. if (cacheWidths) {