AI-native design editor. Open-source Figma alternative.
test(core): cover italic outlines and ligature spacing (#1650)
Danila Poyarkov9ce0b1903337
diff --git a/packages/core/tests/canvas/text/placed.test.ts b/packages/core/tests/canvas/text/placed.test.tsindex 4e7bce13f..4fd56a3c9 100644--- a/packages/core/tests/canvas/text/placed.test.ts+++ b/packages/core/tests/canvas/text/placed.test.ts@@ -165,6 +165,40 @@ describe('the font saved glyphs are outlined from', () => {])})+ test('spaces a reshaped ligature once, as the paragraph draws it', () => {+ const text = 'a->b'+ const spacing = 10+ // The width the paragraph draws the arrow ligature at, with `letterSpacing`.+ const drawnArrow = (letterSpacing: number) => {+ const textStyle = { fontFamilies: [FAMILY], fontSize: FONT_SIZE, letterSpacing }+ const builder = ck.ParagraphBuilder.MakeFromFontProvider(+ new ck.ParagraphStyle({ textStyle }),+ variableProvider+ )+ builder.addText(text)+ const paragraph = builder.build()+ builder.delete()+ try {+ paragraph.layout(1000)+ const [run] = paragraph.getShapedLines()[0].runs+ expect(Array.from(run.offsets)).toEqual([0, 1, 3, 4])+ return run.positions[4] - run.positions[2]+ } finally {+ paragraph.delete()+ }+ }+ // The static file draws the arrow's characters apart, so the run is shaped again.+ const reshapedArrow = (letterSpacing: number) =>+ placed(text, staticProvider, [{ name: 'calt', value: 0 }], {+ letterSpacing: pixelLength(letterSpacing)+ })[1]++ expect(reshapedArrow(spacing).firstCharacter).toBe(1)+ expect(reshapedArrow(spacing).id).toBe(variableSource().shape(text, {})[1].id)+ expect(drawnArrow(spacing) - drawnArrow(0)).toBeCloseTo(spacing, 3)+ expect(reshapedArrow(spacing).advance - reshapedArrow(0).advance).toBeCloseTo(spacing, 3)+ })+test('shapes the run itself with the features the text sets', () => {const source = variableSource()// With contextual alternates off in the text too, the arrow stays its three characters.diff --git a/packages/core/tests/text/outlines.test.ts b/packages/core/tests/text/outlines.test.tsindex 17e537b4a..f1aed8281 100644--- a/packages/core/tests/text/outlines.test.ts+++ b/packages/core/tests/text/outlines.test.ts@@ -12,6 +12,9 @@ beforeAll(async () => {const data = await Bun.file('packages/core/assets/InterVariable.ttf').arrayBuffer()await fontManager.markLoaded('Inter', 'Regular', data)await fontManager.markLoaded('Inter', 'Bold', data)+ const italic = await Bun.file('packages/core/assets/InterVariable-Italic.ttf').arrayBuffer()+ await fontManager.markLoaded('Inter', 'Regular Italic', italic)+ await fontManager.markLoaded('Inter', 'Bold Italic', italic)})function text(props: Partial<SceneNode>): SceneNode {@@ -74,4 +77,33 @@ describe('textNodeToOutlineLayout', () => {expect(lineCount(text({ width, textAutoResize: 'HEIGHT' }))).toBe(1)expect(lineCount(text({ width, letterSpacing, textAutoResize: 'HEIGHT' }))).toBe(2)})++ test('outlines italic text from the italic file', () => {+ const outline = (italic: boolean) =>+ textNodeToOutlineLayout(text({ text: 'l', italic, fontStyle: italic ? 'Italic' : 'Regular' }))+ ?.glyphs[0]?.commands+ const upright = outline(false)+ const slanted = outline(true)++ expect(upright).toBeDefined()+ expect(slanted).toBeDefined()+ expect(slanted).not.toEqual(upright)+ })++ test('wraps italic text by the advances of its weight', () => {+ const italic = { italic: true }+ const regular = oneLineWidth({ ...italic, fontWeight: 400, fontStyle: 'Italic' })+ const bold = oneLineWidth({ ...italic, fontWeight: 700, fontStyle: 'Bold Italic' })+ expect(bold).toBeGreaterThan(regular)+ const width = (regular + bold) / 2++ expect(+ lineCount(text({ ...italic, fontWeight: 400, fontStyle: 'Italic', width, textAutoResize: 'HEIGHT' }))+ ).toBe(1)+ expect(+ lineCount(+ text({ ...italic, fontWeight: 700, fontStyle: 'Bold Italic', width, textAutoResize: 'HEIGHT' })+ )+ ).toBe(2)+ })})