Skip to content

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

fix(clipboard): include glyph advances

Danila Poyarkov992be9b71334
diff --git a/packages/core/src/text/clipboard-derived-text.ts b/packages/core/src/text/clipboard-derived-text.ts
index d210a3fe5..bf0e72386 100644
--- a/packages/core/src/text/clipboard-derived-text.ts
+++ b/packages/core/src/text/clipboard-derived-text.ts
@@ -3,7 +3,7 @@ import type { SceneNode } from '#core/scene-graph'
import { buildDerivedTextData } from './derived-text-data'
import { normalizeFontFamily, weightToFigmaStyle, weightToStyle } from './fonts'
-import { getGlyphOutlineCommandsSync } from './opentype'
+import { getGlyphOutlineMetricsSync } from './opentype'
export interface ShapedClipboardText {
lineHeight: number
@@ -28,20 +28,22 @@ export async function buildDerivedTextDataV4(
const normalizedFamily = normalizeFontFamily(node.fontFamily)
const key = `${normalizedFamily}|${style}`
const lineHeightFallback = node.lineHeight ?? Math.ceil(node.fontSize * 1.2)
- const glyphCommandLists =
- getGlyphOutlineCommandsSync(node.fontFamily, style, node.text, node.fontSize) ?? []
+ const glyphMetrics = getGlyphOutlineMetricsSync(node.fontFamily, style, node.text, node.fontSize) ?? []
- const glyphs = glyphCommandLists.map((commands, index) => {
+ const fallbackAdvance = node.text.length > 0 ? node.width / Math.max(node.text.length, 1) : 0
+ const glyphs = glyphMetrics.map((glyph, index) => {
const shapedGlyph = shaped?.glyphs[index]
+ const fallbackX = glyph.x || index * fallbackAdvance
+ const fallbackGlyphAdvance = glyph.advance || fallbackAdvance
return {
- commands,
+ commands: glyph.commands,
position: {
- x: shapedGlyph?.x ?? 0,
+ x: shapedGlyph?.x ?? fallbackX,
y: shapedGlyph?.y ?? shaped?.baseline ?? lineHeightFallback
},
fontSize: node.fontSize,
firstCharacter: shapedGlyph?.firstCharacter ?? index,
- advance: shapedGlyph?.advance ?? 0,
+ advance: shapedGlyph?.advance ?? fallbackGlyphAdvance,
rotation: 0
}
})
@@ -64,6 +66,6 @@ export async function buildDerivedTextDataV4(
lineAscent: shaped?.lineAscent ?? Math.max(lineHeightFallback - node.fontSize * 0.2, 0),
logicalIndexToCharacterOffsetMap:
shaped?.logicalIndexToCharacterOffsetMap ??
- Array.from({ length: node.text.length + 1 }, () => 0)
+ Array.from({ length: node.text.length + 1 }, (_, index) => index * fallbackAdvance)
})
}
diff --git a/packages/core/src/text/opentype.ts b/packages/core/src/text/opentype.ts
index a3b610002..8e3d8331d 100644
--- a/packages/core/src/text/opentype.ts
+++ b/packages/core/src/text/opentype.ts
@@ -18,6 +18,7 @@ interface OutlinePath {
interface OutlineGlyph {
path: OutlinePath
+ advanceWidth?: number
getPath(x: number, y: number, fontSize: number): OutlinePath
}
@@ -93,17 +94,40 @@ function commandsToFigmaNumbers(commands: OutlineCommand[]): Array<string | numb
return result
}
-export function getGlyphOutlineCommandsSync(
+export interface GlyphOutlineMetrics {
+ commands: Array<string | number>
+ x: number
+ advance: number
+}
+
+export function getGlyphOutlineMetricsSync(
family: string,
style: string,
text: string,
fontSize: number
-): Array<Array<string | number>> | null {
+): GlyphOutlineMetrics[] | null {
const font = getParsedFont(family, style)
if (!font) return null
const glyphs = font.stringToGlyphs(text)
- return glyphs.map((glyph) => commandsToFigmaNumbers(glyph.getPath(0, 0, fontSize).commands))
+ let x = 0
+ const scale = fontSize / font.unitsPerEm
+ return glyphs.map((glyph) => {
+ const commands = commandsToFigmaNumbers(glyph.getPath(0, 0, fontSize).commands)
+ const advance = (glyph.advanceWidth ?? 0) * scale
+ const metrics = { commands, x, advance }
+ x += advance
+ return metrics
+ })
+}
+
+export function getGlyphOutlineCommandsSync(
+ family: string,
+ style: string,
+ text: string,
+ fontSize: number
+): Array<Array<string | number>> | null {
+ return getGlyphOutlineMetricsSync(family, style, text, fontSize)?.map((glyph) => glyph.commands) ?? null
}
export async function probeGlyphOutlineCommands(
diff --git a/tests/engine/editor/clipboard/derived-text.test.ts b/tests/engine/editor/clipboard/derived-text.test.ts
index a599bdf69..5d4556991 100644
--- a/tests/engine/editor/clipboard/derived-text.test.ts
+++ b/tests/engine/editor/clipboard/derived-text.test.ts
@@ -93,5 +93,8 @@ describe('clipboard derived text export', () => {
const derived = expectDefined(await buildDerivedTextDataV4(text, fontDigestMap), 'derived text')
expect(derived.fontMetaData[0].key.style).toBe('Semi Bold')
+ expect(derived.glyphs[1].position.x).toBeGreaterThan(0)
+ expect(derived.glyphs[1].advance).toBeGreaterThan(0)
+ expect(derived.logicalIndexToCharacterOffsetMap[1]).toBeGreaterThan(0)
})
})