Skip to content

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

refactor(core): split text data serialization

Danila Poyarkov06dce623db68
diff --git a/packages/core/src/canvas/text.ts b/packages/core/src/canvas/text.ts
index 5ea99436e..2d80f41cb 100644
--- a/packages/core/src/canvas/text.ts
+++ b/packages/core/src/canvas/text.ts
@@ -195,65 +195,76 @@ function textDecorationValue(ck: CanvasKit, decoration: string): number {
}
}
-function addStyledRuns(
+function styleRunColor(
+ ck: CanvasKit,
+ style: SceneNode['styleRuns'][number]['style'],
+ baseColor: Float32Array
+): Float32Array {
+ const visibleFill = style.fills?.find((fill) => fill.visible && fill.type === 'SOLID')
+ if (!visibleFill) return baseColor
+ const color = resolveRGBAForPreview(visibleFill.color).color
+ return ck.Color4f(color.r, color.g, color.b, color.a * visibleFill.opacity)
+}
+
+function pushStyleRun(
r: TextRenderer,
builder: ReturnType<CanvasKit['ParagraphBuilder']['MakeFromFontProvider']>,
node: SceneNode,
+ run: SceneNode['styleRuns'][number],
baseColor: Float32Array,
baseFontSize: number,
fontFamilies: (primary: string, weight: number, italic?: boolean) => string[],
halfLeading: boolean
): void {
const ck = r.ck
+ const style = run.style
+ const runLineHeight = style.lineHeight !== undefined ? style.lineHeight : node.lineHeight
+ const runFontSize = style.fontSize ?? baseFontSize
+
+ builder.pushStyle(
+ new ck.TextStyle({
+ color: styleRunColor(ck, style, baseColor),
+ fontFamilies: fontFamilies(
+ style.fontFamily ?? (node.fontFamily || DEFAULT_FONT_FAMILY),
+ style.fontWeight ?? node.fontWeight,
+ style.italic ?? node.italic
+ ),
+ fontSize: runFontSize,
+ fontStyle: {
+ weight: { value: style.fontWeight ?? node.fontWeight } as FontWeight,
+ slant: (style.italic ?? node.italic) ? ck.FontSlant.Italic : ck.FontSlant.Upright
+ },
+ fontVariations: textFontVariations(style.fontVariations ?? node.fontVariations),
+ fontFeatures: textFontFeatures(style.fontFeatures ?? node.fontFeatures),
+ letterSpacing: style.letterSpacing ?? (node.letterSpacing || 0),
+ decoration: textDecorationValue(ck, style.textDecoration ?? node.textDecoration),
+ heightMultiplier: runLineHeight ? runLineHeight / runFontSize : undefined,
+ halfLeading
+ })
+ )
+}
+
+function addStyledRuns(
+ r: TextRenderer,
+ builder: ReturnType<CanvasKit['ParagraphBuilder']['MakeFromFontProvider']>,
+ node: SceneNode,
+ baseColor: Float32Array,
+ baseFontSize: number,
+ fontFamilies: (primary: string, weight: number, italic?: boolean) => string[],
+ halfLeading: boolean
+): void {
const text = node.text
let pos = 0
for (const run of node.styleRuns) {
- if (pos < run.start) {
- builder.addText(text.slice(pos, run.start))
- }
- const s = run.style
- const runLineHeight = s.lineHeight !== undefined ? s.lineHeight : node.lineHeight
- const runFontSize = s.fontSize ?? baseFontSize
-
- let runColor = baseColor
- if (s.fills) {
- const visibleFill = s.fills.find((f) => f.visible && f.type === 'SOLID')
- if (visibleFill) {
- const c = resolveRGBAForPreview(visibleFill.color).color
- runColor = ck.Color4f(c.r, c.g, c.b, c.a * visibleFill.opacity)
- }
- }
-
- builder.pushStyle(
- new ck.TextStyle({
- color: runColor,
- fontFamilies: fontFamilies(
- s.fontFamily ?? (node.fontFamily || DEFAULT_FONT_FAMILY),
- s.fontWeight ?? node.fontWeight,
- s.italic ?? node.italic
- ),
- fontSize: runFontSize,
- fontStyle: {
- weight: { value: s.fontWeight ?? node.fontWeight } as FontWeight,
- slant: (s.italic ?? node.italic) ? ck.FontSlant.Italic : ck.FontSlant.Upright
- },
- fontVariations: textFontVariations(s.fontVariations ?? node.fontVariations),
- fontFeatures: textFontFeatures(s.fontFeatures ?? node.fontFeatures),
- letterSpacing: s.letterSpacing ?? (node.letterSpacing || 0),
- decoration: textDecorationValue(ck, s.textDecoration ?? node.textDecoration),
- heightMultiplier: runLineHeight ? runLineHeight / runFontSize : undefined,
- halfLeading
- })
- )
+ if (pos < run.start) builder.addText(text.slice(pos, run.start))
+ pushStyleRun(r, builder, node, run, baseColor, baseFontSize, fontFamilies, halfLeading)
builder.addText(text.slice(run.start, run.start + run.length))
builder.pop()
pos = run.start + run.length
}
- if (pos < text.length) {
- builder.addText(text.slice(pos))
- }
+ if (pos < text.length) builder.addText(text.slice(pos))
}
export function buildParagraph(
diff --git a/packages/core/src/figma-api/proxy.ts b/packages/core/src/figma-api/proxy.ts
index b6ed48882..e82c0297a 100644
--- a/packages/core/src/figma-api/proxy.ts
+++ b/packages/core/src/figma-api/proxy.ts
@@ -5,7 +5,6 @@ import {
setNodeStrokeOkHCL
} from '#core/color/okhcl'
import type { OkHCLColor, OkHCLPayload } from '#core/color/okhcl'
-/* eslint-disable max-lines -- Figma Plugin API proxy; FigmaAPI already in separate file */
import type {
SceneGraph,
SceneNode,
diff --git a/packages/core/src/kiwi/fig/codec/index.ts b/packages/core/src/kiwi/fig/codec/index.ts
index 74a28a315..18d43c39d 100644
--- a/packages/core/src/kiwi/fig/codec/index.ts
+++ b/packages/core/src/kiwi/fig/codec/index.ts
@@ -1,4 +1,3 @@
-/* eslint-disable max-lines -- codec types and encode/decode logic are tightly coupled */
/**
* Message Encoding/Decoding for Figma Multiplayer
*
diff --git a/packages/core/src/kiwi/fig/node-change/serialize.ts b/packages/core/src/kiwi/fig/node-change/serialize.ts
index 43a053ae3..82a20ccaa 100644
--- a/packages/core/src/kiwi/fig/node-change/serialize.ts
+++ b/packages/core/src/kiwi/fig/node-change/serialize.ts
@@ -14,19 +14,19 @@ export {
export { buildFontDigestMap } from './font-digests'
import type { NodeChange, Paint, VariableConsumptionEntry } from '#core/kiwi/fig/codec'
-import type { SceneGraph, SceneNode, CharacterStyleOverride } from '#core/scene-graph'
+import type { SceneGraph, SceneNode } from '#core/scene-graph'
import type { Color, GUID, Matrix } from '#core/types'
import { guidToString, stringToGuid, VARIABLE_BINDING_FIELDS } from './convert'
import { sceneNodeToKiwiWithContext, type KiwiNodeChange } from './export-node'
import { applyFontFeaturesToKiwi } from './font-features'
-import { stringToFigmaAxisTag } from './font-variations'
import {
BOUND_VARIABLES_PLUGIN_KEY,
LAYOUT_DIRECTION_PLUGIN_KEY,
TEXT_DIRECTION_PLUGIN_KEY,
upsertPluginData
} from './plugin-data'
+import { exportTextData, fontVariationToKiwi } from './text-data-export'
export function mapToFigmaType(type: SceneNode['type']): string {
switch (type) {
@@ -178,73 +178,6 @@ function buildDerivedTextData(
})
}
-function fontVariationToKiwi(variation: SceneNode['fontVariations'][number]) {
- const axisTag = stringToFigmaAxisTag(variation.axis)
- return axisTag === undefined
- ? { axisName: variation.axis, value: variation.value }
- : { axisTag, axisName: variation.axis, value: variation.value }
-}
-
-function exportTextData(node: SceneNode): NodeChange['textData'] {
- const runs = node.styleRuns
- if (runs.length === 0) {
- return { characters: node.text, lines: textLines(node.text) }
- }
-
- const charIds = Array.from<number>({ length: node.text.length }).fill(0)
- const styleMap = new Map<string, { id: number; style: CharacterStyleOverride }>()
- let nextId = 1
-
- for (const run of runs) {
- const key = JSON.stringify(run.style)
- let entry = styleMap.get(key)
- if (!entry) {
- entry = { id: nextId++, style: run.style }
- styleMap.set(key, entry)
- }
- for (let i = run.start; i < run.start + run.length && i < charIds.length; i++) {
- charIds[i] = entry.id
- }
- }
-
- const overrideTable: NodeChange[] = []
- for (const { id, style } of styleMap.values()) {
- const override: Record<string, unknown> = { styleID: id }
- const weight = style.fontWeight ?? node.fontWeight
- const italic = style.italic ?? node.italic
- override.fontName = {
- family: normalizeFontFamily(style.fontFamily ?? node.fontFamily),
- style: weightToFigmaStyle(weight, italic),
- postscript: ''
- }
- if (style.fontSize !== undefined) override.fontSize = style.fontSize
- if (style.fontVariations && style.fontVariations.length > 0) {
- override.fontVariations = style.fontVariations.map(fontVariationToKiwi)
- }
- if (style.fontFeatures && style.fontFeatures.length > 0) {
- applyFontFeaturesToKiwi(override as NodeChange, style.fontFeatures)
- }
- if (style.letterSpacing !== undefined) {
- override.letterSpacing = { value: style.letterSpacing, units: 'PIXELS' }
- }
- if (style.lineHeight !== undefined && style.lineHeight !== null) {
- override.lineHeight = { value: style.lineHeight, units: 'PIXELS' }
- }
- if (style.textDecoration) override.textDecoration = style.textDecoration
- if (style.fills && style.fills.length > 0) {
- override.fillPaints = style.fills.map(fillToKiwiPaint)
- }
- overrideTable.push(override as NodeChange)
- }
-
- return {
- characters: node.text,
- lines: textLines(node.text),
- characterStyleIDs: charIds,
- styleOverrideTable: overrideTable
- }
-}
-
export function safeColor(c: Color | Omit<Color, 'a'>): Color {
return { r: c.r, g: c.g, b: c.b, a: 'a' in c ? c.a : 1 }
}
@@ -317,7 +250,7 @@ function serializeTextProps(
style: weightToFigmaStyle(node.fontWeight, node.italic),
postscript: ''
}
- nc.textData = exportTextData(node)
+ nc.textData = exportTextData(node, textLines, fillToKiwiPaint)
if (node.fontVariations.length > 0) {
nc.fontVariations = node.fontVariations.map(fontVariationToKiwi)
}
diff --git a/packages/core/src/kiwi/fig/node-change/text-data-export.ts b/packages/core/src/kiwi/fig/node-change/text-data-export.ts
new file mode 100644
index 000000000..29118fc8c
--- /dev/null
+++ b/packages/core/src/kiwi/fig/node-change/text-data-export.ts
@@ -0,0 +1,92 @@
+import type { NodeChange, Paint } from '#core/kiwi/fig/codec'
+import type { CharacterStyleOverride, SceneNode } from '#core/scene-graph'
+import { normalizeFontFamily, weightToFigmaStyle } from '#core/text/fonts'
+
+import { applyFontFeaturesToKiwi } from './font-features'
+import { stringToFigmaAxisTag } from './font-variations'
+
+export function fontVariationToKiwi(variation: SceneNode['fontVariations'][number]) {
+ const axisTag = stringToFigmaAxisTag(variation.axis)
+ return axisTag === undefined
+ ? { axisName: variation.axis, value: variation.value }
+ : { axisTag, axisName: variation.axis, value: variation.value }
+}
+
+function textStyleOverrideToKiwi(
+ id: number,
+ style: CharacterStyleOverride,
+ node: SceneNode,
+ fillToKiwiPaint: (fill: SceneNode['fills'][number]) => Paint
+): NodeChange {
+ const override: Record<string, unknown> = { styleID: id }
+ const weight = style.fontWeight ?? node.fontWeight
+ const italic = style.italic ?? node.italic
+ override.fontName = {
+ family: normalizeFontFamily(style.fontFamily ?? node.fontFamily),
+ style: weightToFigmaStyle(weight, italic),
+ postscript: ''
+ }
+ if (style.fontSize !== undefined) override.fontSize = style.fontSize
+ if (style.fontVariations && style.fontVariations.length > 0) {
+ override.fontVariations = style.fontVariations.map(fontVariationToKiwi)
+ }
+ if (style.fontFeatures && style.fontFeatures.length > 0) {
+ applyFontFeaturesToKiwi(override as NodeChange, style.fontFeatures)
+ }
+ if (style.letterSpacing !== undefined) {
+ override.letterSpacing = { value: style.letterSpacing, units: 'PIXELS' }
+ }
+ if (style.lineHeight !== undefined && style.lineHeight !== null) {
+ override.lineHeight = { value: style.lineHeight, units: 'PIXELS' }
+ }
+ if (style.textDecoration) override.textDecoration = style.textDecoration
+ if (style.fills && style.fills.length > 0) {
+ override.fillPaints = style.fills.map(fillToKiwiPaint)
+ }
+ return override as NodeChange
+}
+
+function collectTextStyleOverrides(node: SceneNode): {
+ charIds: number[]
+ styleMap: Map<string, { id: number; style: CharacterStyleOverride }>
+} {
+ const charIds = Array.from<number>({ length: node.text.length }).fill(0)
+ const styleMap = new Map<string, { id: number; style: CharacterStyleOverride }>()
+ let nextId = 1
+
+ for (const run of node.styleRuns) {
+ const key = JSON.stringify(run.style)
+ let entry = styleMap.get(key)
+ if (!entry) {
+ entry = { id: nextId++, style: run.style }
+ styleMap.set(key, entry)
+ }
+ for (let i = run.start; i < run.start + run.length && i < charIds.length; i++) {
+ charIds[i] = entry.id
+ }
+ }
+
+ return { charIds, styleMap }
+}
+
+export function exportTextData(
+ node: SceneNode,
+ textLines: (text: string) => Array<{ characters: string; styleID: number }>,
+ fillToKiwiPaint: (fill: SceneNode['fills'][number]) => Paint
+): NodeChange['textData'] {
+ if (node.styleRuns.length === 0) {
+ return { characters: node.text, lines: textLines(node.text) }
+ }
+
+ const { charIds, styleMap } = collectTextStyleOverrides(node)
+ const overrideTable = [...styleMap.values()].map(({ id, style }) =>
+ textStyleOverrideToKiwi(id, style, node, fillToKiwiPaint)
+ )
+
+ return {
+ characters: node.text,
+ lines: textLines(node.text),
+ characterStyleIDs: charIds,
+ styleOverrideTable: overrideTable
+ }
+}