Skip to content

AI-native design editor. Open-source Figma alternative.

feat(layout): use opentype.js for accurate headless text measurement

Danila Poyarkove23b3f0ce154
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index ea20a55dc..866cb604b 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -16,6 +16,8 @@ import Yoga, {
} from 'yoga-layout'
import { resolveNodeLayoutDirection } from './text/direction'
+import { weightToStyle } from './text/fonts'
+import { measureTextWithOpenType } from './text/opentype'
import type { GridTrack, SceneGraph, SceneNode } from './scene-graph'
@@ -34,7 +36,14 @@ const GLYPH_WIDTH_FACTOR = 0.6
// this when available — this is only the fallback.
function estimateTextSize(node: SceneNode, maxWidth?: number): { width: number; height: number } {
const fontSize = node.fontSize || 14
+ const family = node.fontFamily || 'Inter'
+ const style = weightToStyle(node.fontWeight || 400, node.italic)
const text = node.text || ''
+
+ const explicitLineH = (node.lineHeight ?? 0) > 0 ? (node.lineHeight as number) : undefined
+ const measured = measureTextWithOpenType(text, fontSize, family, style, maxWidth, explicitLineH)
+ if (measured) return measured
+
const charWidth = fontSize * GLYPH_WIDTH_FACTOR
const singleLineWidth = Math.ceil(text.length * charWidth)
const lineH = (node.lineHeight ?? 0) > 0 ? (node.lineHeight as number) : Math.ceil(fontSize * 1.4)
diff --git a/packages/core/src/text/opentype.ts b/packages/core/src/text/opentype.ts
index 1630010d1..c62775048 100644
--- a/packages/core/src/text/opentype.ts
+++ b/packages/core/src/text/opentype.ts
@@ -23,7 +23,11 @@ interface OutlineGlyph {
interface OutlineFont {
unitsPerEm: number
+ ascender: number
+ descender: number
+ tables: { os2?: { sTypoLineGap?: number } }
stringToGlyphs(text: string): OutlineGlyph[]
+ getAdvanceWidth(text: string, fontSize: number): number
}
export interface GlyphOutlineProbe {
@@ -38,6 +42,43 @@ interface OpenTypeModule {
parse(buffer: ArrayBuffer): OutlineFont
}
+const parsedFontCache = new Map<string, OutlineFont>()
+
+function getParsedFont(family: string, style: string): OutlineFont | null {
+ const key = `${family}|${style}`
+ const cached = parsedFontCache.get(key)
+ if (cached) return cached
+ const bytes = getLoadedFontData(family, style)
+ if (!bytes) return null
+ const font = (OpenTypeSync as OpenTypeModule).parse(bytes.slice(0))
+ parsedFontCache.set(key, font)
+ return font
+}
+
+export function measureTextWithOpenType(
+ text: string,
+ fontSize: number,
+ family: string,
+ style: string,
+ maxWidth?: number,
+ lineHeight?: number
+): { width: number; height: number } | null {
+ const font = getParsedFont(family, style)
+ if (!font) return null
+
+ const scale = fontSize / font.unitsPerEm
+ const lineGap = font.tables.os2?.sTypoLineGap ?? 0
+ const lineH = lineHeight ?? Math.ceil((font.ascender - font.descender + lineGap) * scale)
+
+ const singleLineWidth = font.getAdvanceWidth(text, fontSize)
+
+ if (maxWidth && maxWidth > 0 && singleLineWidth > maxWidth) {
+ const lines = Math.ceil(singleLineWidth / maxWidth)
+ return { width: maxWidth, height: Math.ceil(lines * lineH) }
+ }
+ return { width: Math.ceil(singleLineWidth), height: lineH }
+}
+
let openTypeModulePromise: Promise<OpenTypeModule> | null = null
async function loadOpenTypeModule() {
@@ -65,10 +106,9 @@ export function getGlyphOutlineCommandsSync(
text: string,
fontSize: number
): Array<Array<string | number>> | null {
- const bytes = getLoadedFontData(family, style)
- if (!bytes) return null
+ const font = getParsedFont(family, style)
+ if (!font) return null
- const font = (OpenTypeSync as OpenTypeModule).parse(bytes.slice(0))
const glyphs = font.stringToGlyphs(text)
return glyphs.map((glyph) => commandsToFigmaNumbers(glyph.getPath(0, 0, fontSize).commands))
}