AI-native design editor. Open-source Figma alternative.
fix(fig): keep a text's own line height when a range sets its first line's (#1451)
IvyTechnx0bcf21003eac
diff --git a/changes/1451/fixed.md b/changes/1451/fixed.mdnew file mode 100644index 000000000..a883f8acb--- /dev/null+++ b/changes/1451/fixed.md@@ -0,0 +1 @@+- Text whose first line has a line height of its own keeps the text's line height when a `.fig` file is reopened, instead of taking the first line's.diff --git a/packages/core/tests/io/formats/fig/roundtrip/text/styles.test.ts b/packages/core/tests/io/formats/fig/roundtrip/text/styles.test.tsindex 603fbdf4e..0b4ccf29c 100644--- a/packages/core/tests/io/formats/fig/roundtrip/text/styles.test.ts+++ b/packages/core/tests/io/formats/fig/roundtrip/text/styles.test.ts@@ -6,6 +6,7 @@ import { exportFigFile, parseFigFile } from '@open-pencil/core/io/formats/fig'import { fontManager } from '@open-pencil/core/text'import { initCodec } from '@open-pencil/kiwi/fig/codec'import { SceneGraph } from '@open-pencil/scene-graph'+import { pixelLength } from '@open-pencil/scene-graph/text'beforeAll(async () => {await initCodec()@@ -40,3 +41,27 @@ test('keeps auto line height on text that mixes font sizes', async () => {// Its lines differ in height; fixing both at the first one's cut off the second.expect(text.lineHeight).toBeNull()})++test('keeps its own line height on text whose first line is a range with another', async () => {+ const graph = new SceneGraph()+ for (const lineHeight of [pixelLength(20), null]) {+ graph.createNode('TEXT', graph.getPages()[0].id, {+ text: 'First\nSecond',+ fontFamily: 'Inter',+ fontSize: 24,+ lineHeight: pixelLength(40),+ width: 300,+ height: 60,+ textAutoResize: 'WIDTH_AND_HEIGHT',+ styleRuns: [{ start: 0, length: 6, style: { lineHeight } }]+ })+ }++ const reopened = await parseFigFile((await exportFigFile(graph)).slice().buffer)+ const texts = [...reopened.getAllNodes()].filter((node) => node.type === 'TEXT')+ // Each first line was laid out at its range's line height; the second one has the text's.+ expect(texts.map((text) => [text.lineHeight, text.styleRuns[0]?.style.lineHeight])).toEqual([+ [pixelLength(40), pixelLength(20)],+ [pixelLength(40), null]+ ])+})diff --git a/packages/fig/src/node-change/convert.ts b/packages/fig/src/node-change/convert.tsindex 3b965e41d..4a807ead3 100644--- a/packages/fig/src/node-change/convert.ts+++ b/packages/fig/src/node-change/convert.ts@@ -332,21 +332,35 @@ function convertPolygonProps(* A text's line height. Figma writes auto as 100 percent, and OpenPencil once wrote none for* it; a text in auto lines up each line at its own font's height. Figma saves each line at the* larger of its line height and its tallest font's height, so a first line shorter than the- * text's height in pixels was laid out at another one, which a style the text uses set.+ * text's height in pixels was laid out at another one, which a style the text uses set. A first+ * line with a range of its own line height was laid out at the range's, not the text's.*/function importedTextLineHeight(nc: NodeChange): TextLength | null {if (!nc.lineHeight || isFigmaAutoLineHeight(nc.lineHeight)) return null- const derivedLineHeight = nc.derivedTextData?.baselines?.[0]?.lineHeight+ const firstLine = nc.derivedTextData?.baselines?.[0]if (nc.lineHeight.units === 'PIXELS' &&- derivedLineHeight !== undefined &&- Number.isFinite(derivedLineHeight) &&- derivedLineHeight < nc.lineHeight.value+ firstLine !== undefined &&+ Number.isFinite(firstLine.lineHeight) &&+ firstLine.lineHeight < nc.lineHeight.value &&+ !rangeSetsLineHeight(nc, firstLine.firstCharacter, firstLine.endCharacter))- return pixelLength(derivedLineHeight)+ return pixelLength(firstLine.lineHeight)return convertLineHeight(nc.lineHeight)}+/** Whether a character from `start` up to `end` has a line height from its range's style. */+function rangeSetsLineHeight(nc: NodeChange, start: number, end: number): boolean {+ const ids = new Set(+ (nc.textData?.styleOverrideTable ?? [])+ .filter((style) => style.lineHeight)+ .map((style) => style.styleID as number | undefined)+ )+ return (nc.textData?.characterStyleIDs ?? [])+ .slice(start, end)+ .some((id) => id !== 0 && ids.has(id))+}+type TextProps = Pick<SceneNode,| 'text'