Skip to content

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

fix(layout): use stored .fig dimensions for headless text measurement

Danila Poyarkov226542cfcfe0
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index 5ba8605c0..ea20a55dc 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -422,12 +422,22 @@ function configureChildAsLeaf(yogaChild: YogaNode, child: SceneNode, parent: Sce
if (needsMeasureFunc) {
configureTextLeaf(yogaChild, child, parent)
} else if (isText && !globalTextMeasurer && child.textAutoResize !== 'NONE') {
- // No CanvasKit — use rough estimate so HUG containers don't inherit
- // the 100×100 default SceneNode size. See estimateTextSize above.
+ // No CanvasKit — prefer stored dimensions from .fig import (Figma's
+ // ground truth) over the rough character-count estimate. Only fall back
+ // to estimateTextSize for newly-created nodes that still carry the
+ // 100×100 default SceneNode size.
+ const hasStoredSize =
+ child.width > 0 && child.height > 0 && !(child.width === 100 && child.height === 100)
+
if (child.textAutoResize === 'WIDTH_AND_HEIGHT') {
- const est = estimateTextSize(child)
- yogaChild.setWidth(est.width)
- yogaChild.setHeight(est.height)
+ if (hasStoredSize) {
+ yogaChild.setWidth(child.width)
+ yogaChild.setHeight(child.height)
+ } else {
+ const est = estimateTextSize(child)
+ yogaChild.setWidth(est.width)
+ yogaChild.setHeight(est.height)
+ }
} else if (child.textAutoResize === 'HEIGHT') {
const stretches =
child.layoutAlignSelf === 'STRETCH' ||
@@ -435,8 +445,12 @@ function configureChildAsLeaf(yogaChild: YogaNode, child: SceneNode, parent: Sce
if (!(!isRow && stretches)) {
yogaChild.setWidth(child.width)
}
- const est = estimateTextSize(child, child.width)
- yogaChild.setHeight(est.height)
+ if (hasStoredSize) {
+ yogaChild.setHeight(child.height)
+ } else {
+ const est = estimateTextSize(child, child.width)
+ yogaChild.setHeight(est.height)
+ }
}
} else {
configureNonTextLeaf(yogaChild, child, isRow, stretchCross)
diff --git a/tests/engine/layout.test.ts b/tests/engine/layout.test.ts
index d541a6c6e..459c91963 100644
--- a/tests/engine/layout.test.ts
+++ b/tests/engine/layout.test.ts
@@ -1193,7 +1193,7 @@ describe('Auto Layout', () => {
expect(updatedArrow2.x).toBe(190)
})
- test('without measurer, text uses estimated size instead of 100x100 default', () => {
+ test('without measurer, text preserves stored dimensions from .fig import', () => {
const graph = new SceneGraph()
const pid = pageId(graph)
@@ -1219,14 +1219,43 @@ describe('Auto Layout', () => {
const children = graph.getChildren(frame.id)
const updatedText = children[0]
- // Rough estimate: ~0.6 × fontSize × charCount, not the 100×100 default
+ expect(updatedText.width).toBe(200)
+ expect(updatedText.height).toBe(20)
+ })
+
+ test('without measurer, text with 100x100 default falls back to estimate', () => {
+ const graph = new SceneGraph()
+ const pid = pageId(graph)
+
+ const frame = autoFrame(graph, pid, {
+ width: 300,
+ height: 40,
+ layoutMode: 'HORIZONTAL',
+ primaryAxisSizing: 'FIXED',
+ counterAxisSizing: 'FIXED',
+ primaryAxisAlign: 'CENTER',
+ })
+
+ graph.createNode('TEXT', frame.id, {
+ width: 100,
+ height: 100,
+ text: 'Test',
+ fontSize: 14,
+ textAutoResize: 'WIDTH_AND_HEIGHT' as const,
+ })
+
+ setTextMeasurer(null)
+ computeAllLayouts(graph)
+
+ const children = graph.getChildren(frame.id)
+ const updatedText = children[0]
expect(updatedText.width).toBeLessThan(100)
expect(updatedText.width).toBeGreaterThan(0)
expect(updatedText.height).toBeLessThan(100)
expect(updatedText.height).toBeGreaterThan(0)
})
- test('without measurer, HEIGHT text estimates multi-line wrapping', () => {
+ test('without measurer, HEIGHT text preserves stored height from .fig import', () => {
const graph = new SceneGraph()
const pid = pageId(graph)
@@ -1240,7 +1269,7 @@ describe('Auto Layout', () => {
graph.createNode('TEXT', frame.id, {
width: 269,
- height: 20,
+ height: 66,
text: 'GDP Growth Exceeds Expectations at 3.1% in Q2 Report That Nobody Expected',
fontSize: 15,
lineHeight: 22,
@@ -1252,11 +1281,39 @@ describe('Auto Layout', () => {
const children = graph.getChildren(frame.id)
const updatedText = children[0]
- // 74 chars × 15 × 0.6 = 666px single line, in 269px → ~3 lines × 22px = 66px
- expect(updatedText.height).toBeGreaterThan(22)
+ expect(updatedText.height).toBe(66)
expect(updatedText.width).toBe(269)
})
+ test('without measurer, HEIGHT text with 100x100 default falls back to estimate', () => {
+ const graph = new SceneGraph()
+ const pid = pageId(graph)
+
+ const frame = autoFrame(graph, pid, {
+ width: 269,
+ height: 400,
+ layoutMode: 'VERTICAL',
+ primaryAxisSizing: 'FIXED',
+ counterAxisSizing: 'FIXED',
+ })
+
+ graph.createNode('TEXT', frame.id, {
+ width: 100,
+ height: 100,
+ text: 'GDP Growth Exceeds Expectations at 3.1% in Q2 Report That Nobody Expected',
+ fontSize: 15,
+ lineHeight: 22,
+ textAutoResize: 'HEIGHT' as const,
+ })
+
+ setTextMeasurer(null)
+ computeAllLayouts(graph)
+
+ const children = graph.getChildren(frame.id)
+ const updatedText = children[0]
+ expect(updatedText.height).toBeGreaterThan(22)
+ })
+
test('text with w="fill" in flex="col" stretches to parent width', () => {
const graph = new SceneGraph()
const pid = pageId(graph)