Skip to content

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

fix(kiwi): improve figma export fidelity

Danila Poyarkov63d3f2e0cb48
diff --git a/packages/core/assets/Inter-Medium.ttf b/packages/core/assets/Inter-Medium.ttf
new file mode 100644
index 000000000..4a57a1a72
Binary files /dev/null and b/packages/core/assets/Inter-Medium.ttf differ
diff --git a/packages/core/assets/Inter-SemiBold.ttf b/packages/core/assets/Inter-SemiBold.ttf
new file mode 100644
index 000000000..67fda287e
Binary files /dev/null and b/packages/core/assets/Inter-SemiBold.ttf differ
diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.ts
index 8625759c3..f3caf68e3 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -588,7 +588,7 @@ export function renderText(r: SkiaRenderer, canvas: Canvas, node: SceneNode): vo
}
if (r.fontsLoaded && r.fontProvider) {
const paragraph = r.buildParagraph(node, r.fillPaint.getColor())
- canvas.drawParagraph(paragraph, 0, 0)
+ canvas.drawParagraph(paragraph, 0, -1)
paragraph.delete()
} else if (r.textFont) {
canvas.drawText(text, 0, node.fontSize || r.DEFAULT_FONT_SIZE, r.fillPaint, r.textFont)
diff --git a/packages/core/src/kiwi/instance-overrides/index.ts b/packages/core/src/kiwi/instance-overrides/index.ts
index 0f30aac82..b78f31eb9 100644
--- a/packages/core/src/kiwi/instance-overrides/index.ts
+++ b/packages/core/src/kiwi/instance-overrides/index.ts
@@ -10,6 +10,7 @@ export type {
SymbolOverride
} from './types'
+import { copyFills } from '../../scene-graph/copy'
import { guidToString } from '../convert'
import { applyConstraintScaling } from './constraints'
import { applyDerivedSymbolData } from './dsd'
@@ -39,16 +40,30 @@ function buildKiwiPropertyNodes(
if (!node?.componentId) continue
const comp = graph.getNode(node.componentId)
if (!comp) continue
- const hasDiffFills = nc.fillPaints !== undefined && node.fills !== comp.fills
const hasDiffRadius =
(nc.cornerRadius !== undefined || nc.rectangleCornerRadiiIndependent !== undefined) &&
node.cornerRadius !== comp.cornerRadius
const hasDiffVisible = nc.visible === false && comp.visible
- if (hasDiffFills || hasDiffRadius || hasDiffVisible) result.add(nodeId)
+ if (hasDiffRadius || hasDiffVisible) result.add(nodeId)
}
return result
}
+function propagateResolvedFills(graph: SceneGraph, protectedNodes: Set<string>): void {
+ for (let pass = 0; pass < 10; pass++) {
+ let changed = false
+ for (const node of graph.getAllNodes()) {
+ if (!node.componentId) continue
+ const source = graph.getNode(node.componentId)
+ if (!source || source.fills === node.fills) continue
+ if (protectedNodes.has(node.id) && !protectedNodes.has(source.id)) continue
+ graph.updateNode(node.id, { fills: copyFills(source.fills) })
+ changed = true
+ }
+ if (!changed) return
+ }
+}
+
function buildOverrideContext(
graph: SceneGraph,
changeMap: Map<string, InstanceNodeChange>,
@@ -139,5 +154,6 @@ export function populateAndApplyOverrides(
}
applyDerivedSymbolData(ctx)
+ propagateResolvedFills(graph, new Set([...ctx.kiwiPropertyNodes, ...overriddenNodes]))
applyConstraintScaling(ctx)
}
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index 866cb604b..712d97c14 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -29,6 +29,12 @@ export type TextMeasurer = (
let globalTextMeasurer: TextMeasurer | null = null
const GLYPH_WIDTH_FACTOR = 0.6
+const yogaConfig = Yoga.Config.create()
+yogaConfig.setPointScaleFactor(0)
+
+function createYogaNode(): YogaNode {
+ return Yoga.Node.create(yogaConfig)
+}
// Rough estimate for text size when CanvasKit/font is not available.
// DO NOT REMOVE: without this, text nodes keep their 100×100 default size
@@ -151,7 +157,7 @@ function configureAsGrid(
}
function createGridChildNode(child: SceneNode): YogaNode {
- const yogaChild = Yoga.Node.create()
+ const yogaChild = createYogaNode()
if (!child.visible) {
yogaChild.setDisplay(Display.None)
} else {
@@ -184,14 +190,14 @@ function buildGridTree(
frame: SceneNode,
inheritedDirection: 'LTR' | 'RTL'
): YogaNode {
- const root = Yoga.Node.create()
+ const root = createYogaNode()
const direction = resolveNodeLayoutDirection(frame, inheritedDirection)
configureAsGrid(root, frame, direction)
const children = graph.getChildren(frame.id)
for (const child of children) {
if (child.layoutPositioning === 'ABSOLUTE') {
- const yogaChild = Yoga.Node.create()
+ const yogaChild = createYogaNode()
configureAbsoluteChild(yogaChild, child)
root.insertChild(yogaChild, root.getChildCount())
} else {
@@ -218,7 +224,7 @@ function buildYogaTree(
frame: SceneNode,
inheritedDirection: 'LTR' | 'RTL'
): YogaNode {
- const root = Yoga.Node.create()
+ const root = createYogaNode()
const direction = resolveNodeLayoutDirection(frame, inheritedDirection)
if (frame.primaryAxisSizing === 'FIXED') {
@@ -234,7 +240,7 @@ function buildYogaTree(
const children = graph.getChildren(frame.id)
for (const child of children) {
- const yogaChild = Yoga.Node.create()
+ const yogaChild = createYogaNode()
if (child.layoutPositioning === 'ABSOLUTE') {
configureAbsoluteChild(yogaChild, child)
@@ -355,7 +361,7 @@ function configureChildAsGrid(
const grandchildren = graph.getChildren(child.id)
for (const gc of grandchildren) {
if (gc.layoutPositioning === 'ABSOLUTE') {
- const yogaGC = Yoga.Node.create()
+ const yogaGC = createYogaNode()
configureAbsoluteChild(yogaGC, gc)
yogaChild.insertChild(yogaGC, yogaChild.getChildCount())
} else {
@@ -402,7 +408,7 @@ function configureChildAsAutoLayout(
const grandchildren = graph.getChildren(child.id)
for (const gc of grandchildren) {
- const yogaGC = Yoga.Node.create()
+ const yogaGC = createYogaNode()
if (gc.layoutPositioning === 'ABSOLUTE') {
configureAbsoluteChild(yogaGC, gc)
} else if (!gc.visible) {
diff --git a/packages/core/src/text/fonts.ts b/packages/core/src/text/fonts.ts
index aa4e93b3e..9efeab812 100644
--- a/packages/core/src/text/fonts.ts
+++ b/packages/core/src/text/fonts.ts
@@ -59,6 +59,8 @@ export async function listFamilies(): Promise<string[]> {
const BUNDLED_FONTS: Record<string, string> = {
'Inter|Regular': '/Inter-Regular.ttf',
+ 'Inter|Medium': '/Inter-Medium.ttf',
+ 'Inter|SemiBold': '/Inter-SemiBold.ttf',
'Noto Naskh Arabic|Regular': '/NotoNaskhArabic-Regular.ttf'
}
diff --git a/public/Inter-Medium.ttf b/public/Inter-Medium.ttf
new file mode 100644
index 000000000..4a57a1a72
Binary files /dev/null and b/public/Inter-Medium.ttf differ
diff --git a/public/Inter-SemiBold.ttf b/public/Inter-SemiBold.ttf
new file mode 100644
index 000000000..67fda287e
Binary files /dev/null and b/public/Inter-SemiBold.ttf differ