Skip to content

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

Merge master into image fill hash fix

Danila Poyarkovbb6c537b8e98
diff --cc packages/core/src/bytes/hex.ts
index 000000000,000000000..e594cc38b
new file mode 100644
--- /dev/null
+++ b/packages/core/src/bytes/hex.ts
@@@ -1,0 -1,0 +1,21 @@@
++export function hexToBytes(hex: string): Uint8Array {
++ if (hex.length % 2 !== 0) {
++ throw new Error('Hex string must contain an even number of characters')
++ }
++
++ const bytes = new Uint8Array(hex.length / 2)
++ for (let i = 0; i < bytes.length; i++) {
++ const byte = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16)
++ if (Number.isNaN(byte)) throw new Error('Hex string contains invalid characters')
++ bytes[i] = byte
++ }
++ return bytes
++}
++
++export function bytesToHex(bytes: Uint8Array): string {
++ let hex = ''
++ for (const byte of bytes) {
++ hex += byte.toString(16).padStart(2, '0')
++ }
++ return hex
++}
diff --cc packages/core/src/kiwi/binary/variable-bindings.ts
index cea3bc41f,cea3bc41f..b09d9331c
--- a/packages/core/src/kiwi/binary/variable-bindings.ts
+++ b/packages/core/src/kiwi/binary/variable-bindings.ts
@@@ -1,3 -1,3 +1,4 @@@
++import { hexToBytes } from '#core/bytes/hex'
import type { GUID } from '#core/types'
import type { NodeChange, Paint } from './codec'
@@@ -87,7 -87,7 +88,7 @@@ export function encodeNodeChangeWithVar
hex = injectVariableBinding(hex, '2701', strokeBinding)
}
-- return new Uint8Array(hex.match(/.{2}/g)?.map((b) => parseInt(b, 16)) ?? [])
++ return hexToBytes(hex)
}
function injectVariableBinding(hex: string, marker: string, binding: { variableID: GUID }): string {
diff --cc packages/core/src/kiwi/node-change/serialize.ts
index a31d0886d,4e64a52a9..de5cb73b4
--- a/packages/core/src/kiwi/node-change/serialize.ts
+++ b/packages/core/src/kiwi/node-change/serialize.ts
@@@ -1,6 -1,7 +1,8 @@@
- import { buildDerivedTextData as buildSharedDerivedTextData } from '#core/text/derived-text-data'
- import { normalizeFontFamily, weightToStyle } from '#core/text/fonts'
- import { getGlyphOutlineCommandsSync } from '#core/text/opentype'
++import { hexToBytes } from '#core/bytes/hex'
+ import { buildDerivedTextData as buildSharedDerivedTextData } from '#core/text/derived-text/data'
+ import { normalizeFontFamily, weightToFigmaStyle, weightToStyle } from '#core/text/fonts'
+ import { encodePathCommandsBlob } from '#core/kiwi/node-change/path-commands'
+ import { getGlyphOutlineMetricsSync } from '#core/text/opentype'
import { encodeVectorNetworkBlob, buildStyleOverrideTable } from '#core/vector'
export {
buildFigKiwi,
diff --cc tests/engine/io/fig/export/images.test.ts
index 000000000,2c299d069..0c0643ebe
mode 000000,100644..100644
--- a/tests/engine/io/fig/export/images.test.ts
+++ b/tests/engine/io/fig/export/images.test.ts
@@@ -1,0 -1,112 +1,123 @@@
+ import { beforeAll, describe, expect, test } from 'bun:test'
+
+ import { unzipSync } from 'fflate'
+
+ import { exportFigFile, FigmaAPI, initCodec, parseFigFile, SceneGraph } from '@open-pencil/core'
+
+ import { expectDefined } from '#tests/helpers/assert'
+
+ describe('fig export/import with images', () => {
+ beforeAll(async () => {
+ await initCodec()
+ })
+
+ test('exported zip contains images entries', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const figma = new FigmaAPI(graph)
+
+ const bytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x00, 0x01, 0x02, 0x03])
+ const { hash } = figma.createImage(bytes)
+
+ graph.createNode('RECTANGLE', page.id, {
+ name: 'ImageNode',
+ width: 100,
+ height: 100,
+ fills: [
+ {
+ type: 'IMAGE',
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true,
+ imageHash: hash,
+ imageScaleMode: 'FILL'
+ }
+ ]
+ })
+
+ const zip = await exportFigFile(graph)
+ const entries = unzipSync(zip)
+
+ expect(entries[`images/${hash}`]).toBeDefined()
+ expect(new Uint8Array(entries[`images/${hash}`])).toEqual(bytes)
+ })
+
+ test('graph without images has no images entries', async () => {
+ const graph = new SceneGraph()
+ graph.createNode('RECTANGLE', graph.getPages()[0].id, {
+ name: 'Plain',
+ width: 50,
+ height: 50
+ })
+
+ const zip = await exportFigFile(graph)
+ const entries = unzipSync(zip)
+ const imageKeys = Object.keys(entries).filter((k) => k.startsWith('images/'))
+
+ expect(imageKeys).toHaveLength(0)
+ })
+
+ test('round-trip preserves images', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const figma = new FigmaAPI(graph)
+
+ const bytes1 = new Uint8Array([11, 22, 33, 44, 55])
+ const bytes2 = new Uint8Array([66, 77, 88, 99])
+ const { hash: hash1 } = figma.createImage(bytes1)
+ const { hash: hash2 } = figma.createImage(bytes2)
+
+ graph.createNode('RECTANGLE', page.id, {
+ name: 'Img1',
+ width: 100,
+ height: 100,
+ fills: [
+ {
+ type: 'IMAGE',
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true,
+ imageHash: hash1,
+ imageScaleMode: 'FILL'
+ }
+ ]
+ })
+ graph.createNode('ELLIPSE', page.id, {
+ name: 'Img2',
+ width: 80,
+ height: 80,
+ fills: [
+ {
+ type: 'IMAGE',
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true,
+ imageHash: hash2,
+ imageScaleMode: 'FIT'
+ }
+ ]
+ })
+
+ const zip = await exportFigFile(graph)
+ const restored = await parseFigFile(zip.buffer as ArrayBuffer)
+
+ expect(restored.images.size).toBe(2)
+ expect(new Uint8Array(expectDefined(restored.images.get(hash1), 'restored image 1'))).toEqual(
+ bytes1
+ )
+ expect(new Uint8Array(expectDefined(restored.images.get(hash2), 'restored image 2'))).toEqual(
+ bytes2
+ )
++
++ const restoredImg1 = expectDefined(
++ [...restored.getAllNodes()].find((node) => node.name === 'Img1'),
++ 'restored image node 1'
++ )
++ const restoredImg2 = expectDefined(
++ [...restored.getAllNodes()].find((node) => node.name === 'Img2'),
++ 'restored image node 2'
++ )
++ expect(restoredImg1.fills[0].imageHash).toBe(hash1)
++ expect(restoredImg2.fills[0].imageHash).toBe(hash2)
+ })
+ })