Skip to content

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

Consolidate font weight mapping and FIG_KIWI_VERSION

Danila Poyarkove3ae61e99d83
diff --git a/packages/core/src/clipboard.ts b/packages/core/src/clipboard.ts
index 598cd30af..238159fa4 100644
--- a/packages/core/src/clipboard.ts
+++ b/packages/core/src/clipboard.ts
@@ -2,7 +2,7 @@ import { inflateSync, deflateSync } from 'fflate'
import { initCodec, getCompiledSchema, getSchemaBytes } from './kiwi/codec'
import { decodeBinarySchema, compileSchema, ByteBuffer } from './kiwi/kiwi-schema'
-import { sceneNodeToKiwi } from './kiwi-serialize'
+import { sceneNodeToKiwi, FIG_KIWI_VERSION } from './kiwi-serialize'
import { decodeVectorNetworkBlob } from './vector'
@@ -90,7 +90,7 @@ async function parseFigKiwiWithZstd(
function buildFigKiwi(schemaDeflated: Uint8Array, dataRaw: Uint8Array): Uint8Array {
const dataDeflated = deflateSync(dataRaw)
- const FIG_KIWI_VERSION = 106
+
const total = 8 + 4 + 4 + schemaDeflated.length + 4 + dataDeflated.length
const out = new Uint8Array(total)
diff --git a/packages/core/src/fig-export.ts b/packages/core/src/fig-export.ts
index c103d4493..ca892f647 100644
--- a/packages/core/src/fig-export.ts
+++ b/packages/core/src/fig-export.ts
@@ -2,7 +2,7 @@ import { zipSync, deflateSync } from 'fflate'
import { IS_TAURI } from './constants'
import { initCodec, getCompiledSchema, getSchemaBytes } from './kiwi/codec'
-import { sceneNodeToKiwi, fractionalPosition } from './kiwi-serialize'
+import { sceneNodeToKiwi, fractionalPosition, FIG_KIWI_VERSION } from './kiwi-serialize'
import { renderThumbnail } from './render-image'
import type { SkiaRenderer } from './renderer'
@@ -20,7 +20,7 @@ const THUMBNAIL_1X1 = Uint8Array.from(
type KiwiNodeChange = NodeChange & Record<string, unknown>
function buildFigKiwi(schemaDeflated: Uint8Array, dataCompressed: Uint8Array): Uint8Array {
- const FIG_KIWI_VERSION = 106
+
const total = 8 + 4 + 4 + schemaDeflated.length + 4 + dataCompressed.length
const out = new Uint8Array(total)
const view = new DataView(out.buffer)
diff --git a/packages/core/src/fonts.ts b/packages/core/src/fonts.ts
index 3f1b4df7b..c811dcbeb 100644
--- a/packages/core/src/fonts.ts
+++ b/packages/core/src/fonts.ts
@@ -133,14 +133,16 @@ export async function ensureNodeFont(family: string, weight: number): Promise<vo
await loadFont(family, style)
}
-function weightToStyle(weight: number): string {
- if (weight <= 100) return 'Thin'
- if (weight <= 200) return 'ExtraLight'
- if (weight <= 300) return 'Light'
- if (weight <= 400) return 'Regular'
- if (weight <= 500) return 'Medium'
- if (weight <= 600) return 'SemiBold'
- if (weight <= 700) return 'Bold'
- if (weight <= 800) return 'ExtraBold'
- return 'Black'
+export function weightToStyle(weight: number, italic = false): string {
+ let label = 'Regular'
+ if (weight <= 100) label = 'Thin'
+ else if (weight <= 200) label = 'ExtraLight'
+ else if (weight <= 300) label = 'Light'
+ else if (weight <= 500) label = 'Medium'
+ else if (weight <= 600) label = 'SemiBold'
+ else if (weight <= 700) label = 'Bold'
+ else if (weight <= 800) label = 'ExtraBold'
+ else if (weight >= 900) label = 'Black'
+ if (italic) label += ' Italic'
+ return label
}
diff --git a/packages/core/src/kiwi-serialize.ts b/packages/core/src/kiwi-serialize.ts
index f9cd93e1b..6767fec73 100644
--- a/packages/core/src/kiwi-serialize.ts
+++ b/packages/core/src/kiwi-serialize.ts
@@ -1,4 +1,7 @@
+export const FIG_KIWI_VERSION = 106
+
import { encodeVectorNetworkBlob } from './vector'
+import { weightToStyle } from './fonts'
import type { SceneGraph, SceneNode, CharacterStyleOverride } from './scene-graph'
import type { NodeChange, Paint } from './kiwi/codec'
@@ -48,19 +51,7 @@ export function fractionalPosition(index: number): string {
return String.fromCharCode('!'.charCodeAt(0) + index)
}
-function fontStyleLabel(weight: number, italic: boolean): string {
- let label = 'Regular'
- if (weight >= 900) label = 'Black'
- else if (weight >= 800) label = 'ExtraBold'
- else if (weight >= 700) label = 'Bold'
- else if (weight >= 600) label = 'SemiBold'
- else if (weight >= 500) label = 'Medium'
- else if (weight >= 300) label = 'Light'
- else if (weight >= 200) label = 'ExtraLight'
- else if (weight >= 100) label = 'Thin'
- if (italic) label += ' Italic'
- return label
-}
+
function exportTextData(node: SceneNode): NodeChange['textData'] {
const runs = node.styleRuns
@@ -91,7 +82,7 @@ function exportTextData(node: SceneNode): NodeChange['textData'] {
const italic = style.italic ?? node.italic
override.fontName = {
family: style.fontFamily ?? node.fontFamily,
- style: fontStyleLabel(weight, italic),
+ style: weightToStyle(weight, italic),
postscript: ''
}
if (style.fontSize !== undefined) override.fontSize = style.fontSize
@@ -204,7 +195,7 @@ export function sceneNodeToKiwi(
nc.fontSize = node.fontSize
nc.fontName = {
family: node.fontFamily,
- style: fontStyleLabel(node.fontWeight, node.italic),
+ style: weightToStyle(node.fontWeight, node.italic),
postscript: ''
}
nc.textData = exportTextData(node)