AI-native design editor. Open-source Figma alternative.
import type {
DerivedTextGlyph,
GeometryPath,
SceneNode,
TextPathData
} from '@open-pencil/scene-graph'
import { transformGeometryPaths } from '@open-pencil/scene-graph/copy'
import { geometryBlobBounds } from '@open-pencil/scene-graph/geometry'
/**
* Shift geometry command blobs in node space. Used when we grow the layout box
* left/top so content must move with the new origin (parent-space art stays put).
*
* Delegates to transformGeometryPaths so CLOSE (command 0) and path-level paint
* metadata stay intact — a hand-rolled walker that broke on CLOSE left stroke
* silhouettes only partially shifted (DomeSticker white outline desynced).
*/
function translateGeometryPaths(paths: GeometryPath[], dx: number, dy: number): GeometryPath[] {
if (dx === 0 && dy === 0) return paths
return transformGeometryPaths(paths, 1, 0, 0, 1, dx, dy)
}
interface BoxOverflow {
left: number
top: number
right: number
bottom: number
}
/** How far painted geometry + glyph pads spill past the width×height box. */
function measurePathTextOverflow(
props: Partial<SceneNode>,
width: number,
height: number
): BoxOverflow {
const paths: GeometryPath[] = [...(props.fillGeometry ?? []), ...(props.strokeGeometry ?? [])]
const geom = paths.length > 0 ? geometryBlobBounds(paths) : null
let minX = geom?.x ?? 0
let minY = geom?.y ?? 0
let maxX = geom ? geom.x + geom.width : width
let maxY = geom ? geom.y + geom.height : height
// Baselines only — pad with fontSize so ascent/side-bearings are covered.
for (const g of (props.derivedTextGlyphs as DerivedTextGlyph[] | null) ?? []) {
const pad = g.fontSize || 0
minX = Math.min(minX, g.x - pad * 0.25)
minY = Math.min(minY, g.y - pad)
maxX = Math.max(maxX, g.x + pad)
maxY = Math.max(maxY, g.y + pad * 0.35)
}
const pad = 1
return {
left: Math.max(0, -minX + (minX < 0 ? pad : 0)),
top: Math.max(0, -minY + (minY < 0 ? pad : 0)),
right: Math.max(0, maxX - width + (maxX > width ? pad : 0)),
bottom: Math.max(0, maxY - height + (maxY > height ? pad : 0))
}
}
/** Shift local geometry + glyph baselines by (dx, dy) after the origin moved. */
function shiftPathTextGeometry(props: Partial<SceneNode>, dx: number, dy: number): void {
if (props.strokeGeometry && props.strokeGeometry.length > 0) {
props.strokeGeometry = translateGeometryPaths(props.strokeGeometry, dx, dy)
}
if (props.fillGeometry && props.fillGeometry.length > 0) {
props.fillGeometry = translateGeometryPaths(props.fillGeometry, dx, dy)
}
if (props.derivedTextGlyphs?.length) {
props.derivedTextGlyphs = props.derivedTextGlyphs.map((g) => ({
...g,
x: g.x + dx,
y: g.y + dy
}))
}
}
/**
* Figma often sizes the TEXT_PATH layout box tighter than the painted outlines
* (DomeSticker strokeGeometry minX ≈ -37). That is fine inside Figma's own
* painter, but our frames honor `clipsContent` against child *layout* extents
* during resize/cull — overflowing lettering on the left/bottom got clipped.
*
* Grow width/height to cover strokeGeometry + glyph pads, then shift local
* geometry by (dx, dy) and compensate with x/y so the design does not jump.
*/
export function expandPathTextLayoutBox(
props: Partial<SceneNode> & { nodeType: string },
data: TextPathData | null
): void {
if (props.nodeType !== 'TEXT' || !data) return
props.textPathData = data
const width = props.width ?? 0
const height = props.height ?? 0
// The layout path (rawNodeFields vectorData) maps onto the ORIGINAL Figma
// box; anchor it before the box grows so reflow can evaluate the path in
// current node-local coordinates.
props.textPathBox = { x: 0, y: 0, width, height }
const overflow = measurePathTextOverflow(props, width, height)
if (overflow.left === 0 && overflow.top === 0 && overflow.right === 0 && overflow.bottom === 0) {
return
}
// New origin is (oldOrigin - (overflowLeft, overflowTop)) in parent space;
// add the same delta to local geometry so world positions are unchanged.
const dx = overflow.left
const dy = overflow.top
props.x = (props.x ?? 0) - dx
props.y = (props.y ?? 0) - dy
props.width = width + overflow.left + overflow.right
props.height = height + overflow.top + overflow.bottom
if (dx !== 0 || dy !== 0) {
props.textPathBox = { x: dx, y: dy, width, height }
shiftPathTextGeometry(props, dx, dy)
}
}