AI-native design editor. Open-source Figma alternative.
feat(canvas): flatten loaded text outlines
Danila Poyarkovfa3f60d10c81
diff --git a/packages/core/src/canvas/boolean.ts b/packages/core/src/canvas/boolean.tsindex 0d756925f..28bd64022 100644--- a/packages/core/src/canvas/boolean.ts+++ b/packages/core/src/canvas/boolean.ts@@ -1,6 +1,8 @@import type { Canvas, Path, PathOp } from 'canvaskit-wasm'import type { SceneGraph, SceneNode } from '#core/scene-graph'+import { getGlyphOutlineMetricsSync, type OutlineCommand } from '#core/text/opentype'+import { fontManager, weightToStyle } from '#core/text/fonts'import { makeArcPath } from './fills'import type { SkiaRenderer } from './renderer'@@ -41,13 +43,15 @@ function hasVisibleImageFill(node: SceneNode): boolean {return node.fills.some((fill) => fill.visible && fill.type === 'IMAGE')}+function canMakeTextSourcePath(node: SceneNode): boolean {+ if (node.type !== 'TEXT' || !node.text) return false+ const style = weightToStyle(node.fontWeight)+ return fontManager.loadedData(node.fontFamily, style) !== null+}+export function canMakeBooleanSourcePath(node: SceneNode): boolean {- return (- node.type !== 'TEXT' &&- node.type !== 'SECTION' &&- node.type !== 'COMPONENT_SET' &&- !hasVisibleImageFill(node)- )+ if (node.type === 'TEXT') return canMakeTextSourcePath(node) && !hasVisibleImageFill(node)+ return node.type !== 'SECTION' && node.type !== 'COMPONENT_SET' && !hasVisibleImageFill(node)}export function canMakeBooleanSourceNode(node: SceneNode, graph: SceneGraph): boolean {@@ -67,7 +71,54 @@ function lineStrokePath(r: SkiaRenderer, node: SceneNode): Path | null {return path.stroke({ width: stroke?.weight ?? 1 })}+function appendOutlineCommand(path: Path, command: OutlineCommand, xOffset: number, yOffset: number): void {+ switch (command.type) {+ case 'M':+ path.moveTo((command.x ?? 0) + xOffset, (command.y ?? 0) + yOffset)+ break+ case 'L':+ path.lineTo((command.x ?? 0) + xOffset, (command.y ?? 0) + yOffset)+ break+ case 'C':+ path.cubicTo(+ (command.x1 ?? 0) + xOffset,+ (command.y1 ?? 0) + yOffset,+ (command.x2 ?? 0) + xOffset,+ (command.y2 ?? 0) + yOffset,+ (command.x ?? 0) + xOffset,+ (command.y ?? 0) + yOffset+ )+ break+ case 'Q':+ path.quadTo(+ (command.x1 ?? 0) + xOffset,+ (command.y1 ?? 0) + yOffset,+ (command.x ?? 0) + xOffset,+ (command.y ?? 0) + yOffset+ )+ break+ case 'Z':+ path.close()+ break+ }+}++function textOutlinePath(r: SkiaRenderer, node: SceneNode): Path | null {+ if (!node.text) return null+ const fontSize = node.fontSize+ const style = weightToStyle(node.fontWeight)+ const glyphs = getGlyphOutlineMetricsSync(node.fontFamily, style, node.text, fontSize)+ if (!glyphs) return null++ const path = new r.ck.Path()+ for (const glyph of glyphs) {+ for (const command of glyph.commands) appendOutlineCommand(path, command, glyph.x, fontSize)+ }+ return path+}+function baseShapePath(r: SkiaRenderer, node: SceneNode): Path | null {+ if (node.type === 'TEXT') return textOutlinePath(r, node)if (node.type === 'LINE') return lineStrokePath(r, node)if (node.type === 'ELLIPSE' && node.arcData) return makeArcPath(r, node)diff --git a/tests/engine/editor/structure/boolean.test.ts b/tests/engine/editor/structure/boolean.test.tsindex 20a629642..075323bb3 100644--- a/tests/engine/editor/structure/boolean.test.ts+++ b/tests/engine/editor/structure/boolean.test.ts@@ -26,7 +26,7 @@ describe('booleanOperationSelected', () => {test('does not wrap unsupported text nodes', () => {const editor = createEditor()const pageId = editor.state.currentPageId- const first = editor.graph.createNode('TEXT', pageId, { text: 'Nope' })+ const first = editor.graph.createNode('TEXT', pageId, { text: 'Nope', fontFamily: 'Definitely Missing Font' })const second = editor.graph.createNode('RECTANGLE', pageId)editor.select([first.id, second.id])diff --git a/tests/engine/editor/structure/flatten.test.ts b/tests/engine/editor/structure/flatten.test.tsindex 235b8f87a..217e2aaf7 100644--- a/tests/engine/editor/structure/flatten.test.ts+++ b/tests/engine/editor/structure/flatten.test.ts@@ -4,6 +4,7 @@ import { initCanvasKit } from '#cli/headless'import { SkiaRenderer } from '#core/canvas'import { BLACK, TRANSPARENT } from '#core/constants'import { createEditor } from '#core/editor'+import { fontManager } from '#core/text/fonts'async function createEditorWithRenderer() {const ck = await initCanvasKit()@@ -15,6 +16,11 @@ async function createEditorWithRenderer() {return { editor, surface }}+async function loadInterRegular() {+ const data = await Bun.file('public/Inter-Regular.ttf').arrayBuffer()+ fontManager.markLoaded('Inter', 'Regular', data)+}+describe('flattenSelected', () => {test('converts selected shapes into a vector path', async () => {const { editor, surface } = await createEditorWithRenderer()@@ -38,10 +44,34 @@ describe('flattenSelected', () => {surface.delete()})+ test('flattens loaded text as outlines', async () => {+ await loadInterRegular()+ const { editor, surface } = await createEditorWithRenderer()+ const pageId = editor.state.currentPageId+ const text = editor.graph.createNode('TEXT', pageId, {+ text: 'Hi',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 32,+ width: 80,+ height: 40+ })++ editor.select([text.id])+ editor.flattenSelected()++ const [vectorId] = [...editor.state.selectedIds]+ const vector = editor.graph.getNode(vectorId)+ expect(vector?.type).toBe('VECTOR')+ expect(vector?.vectorNetwork?.vertices.length).toBeGreaterThan(0)+ expect(editor.graph.getNode(text.id)).toBeUndefined()+ surface.delete()+ })+test('does not flatten unsupported text nodes', async () => {const { editor, surface } = await createEditorWithRenderer()const pageId = editor.state.currentPageId- const text = editor.graph.createNode('TEXT', pageId, { text: 'Nope' })+ const text = editor.graph.createNode('TEXT', pageId, { text: 'Nope', fontFamily: 'Definitely Missing Font' })const rect = editor.graph.createNode('RECTANGLE', pageId)editor.select([text.id, rect.id])