fix(fig): use derived text line metrics

This commit is contained in:
Danila Poyarkov 2026-05-17 22:49:12 +03:00
parent 2072f2e916
commit ff0f072aef
2 changed files with 35 additions and 1 deletions

View file

@ -252,6 +252,12 @@ function convertCornerProps(
}
}
function importedTextLineHeight(nc: NodeChange): number | null {
const derivedLineHeight = nc.derivedTextData?.baselines?.[0]?.lineHeight
if (derivedLineHeight !== undefined && Number.isFinite(derivedLineHeight)) return derivedLineHeight
return convertLineHeight(nc.lineHeight, nc.fontSize)
}
function convertTextProps(
nc: NodeChange
): Pick<
@ -288,7 +294,7 @@ function convertTextProps(
textAutoResize: (nc.textAutoResize ?? 'NONE') as TextAutoResize,
textCase: (nc.textCase ?? 'ORIGINAL') as TextCase,
textDecoration: mapTextDecoration(nc.textDecoration as string),
lineHeight: convertLineHeight(nc.lineHeight, nc.fontSize),
lineHeight: importedTextLineHeight(nc),
letterSpacing: convertLetterSpacing(nc.letterSpacing, nc.fontSize),
maxLines: (nc.maxLines ?? null) as number | null,
styleRuns: importStyleRuns(nc),

View file

@ -21,6 +21,34 @@ describe('fig-import: text properties', () => {
expect(n.textAlignHorizontal).toBe('CENTER')
})
test('uses derived line metrics for imported Figma text rendering', () => {
const graph = importNodeChanges([
doc(),
canvas(),
node('TEXT', 10, 1, {
textData: { characters: 'Default' },
fontSize: 14,
lineHeight: { value: 36, units: 'PIXELS' },
derivedTextData: {
layoutSize: { x: 49, y: 14 },
baselines: [
{
firstCharacter: 0,
endCharacter: 7,
position: { x: 0, y: 12.09 },
width: 48.25,
lineY: 0,
lineHeight: 16.94,
lineAscent: 13
}
]
}
} as Partial<NodeChange>)
])
const n = graph.getChildren(graph.getPages()[0].id)[0]
expect(n.lineHeight).toBe(16.94)
})
test('font weight mapping', () => {
const cases = [
['Bold', 700],