AI-native design editor. Open-source Figma alternative.
import { afterAll, describe, test } from 'bun:test'
import { SkiaRenderer } from '@open-pencil/core/canvas'
import { initCanvasKit } from '@open-pencil/core/io/formats/raster'
import type { SceneGraph, SceneNode, Stroke } from '@open-pencil/scene-graph'
import { expectDefined } from '#core-tests/helpers/assert'
import { expectFigmaRow, renderRow } from '#core-tests/helpers/figma-rows'
const ck = await initCanvasKit()
const renderer = new SkiaRenderer(ck, expectDefined(ck.MakeSurface(1, 1)))
afterAll(() => renderer.destroy())
/** Rows of alpha from Figma 126 exports of a 12 px blue stroke on the shapes below. */
const FIGMA = {
starInside: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 0, 0, 32, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 191, 0, 0, 223, 255, 255, 255, 255, 255, 176, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
starOutside: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
polygonInside: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
polygonOutside: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 255, 255, 255, 255, 255, 255, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
arcInside: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
arcOutside: [0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0]
}
/** A 160 px shape at (120, 70) without a fill, stroked 12 px blue with `align`. */
function stroked(type: SceneNode['type'], props: Partial<SceneNode>, align: Stroke['align']) {
return (graph: SceneGraph, hostId: string) => {
graph.createNode(type, hostId, {
x: 120,
y: 70,
width: 160,
height: 160,
fills: [],
strokeWeight: 12,
strokes: [
{
type: 'SOLID',
color: { r: 0, g: 0, b: 1, a: 1 },
opacity: 1,
visible: true,
weight: 12,
align
}
],
...props
})
}
}
const STAR = { pointCount: 5, starInnerRadius: 0.4 }
const TRIANGLE = { pointCount: 3 }
const HALF_PIE = { arcData: { startingAngle: 0, endingAngle: Math.PI, innerRadius: 0 } }
// An inside stroke stays within the shape and an outside one around it, as Figma draws them; an
// inside stroke used to be clipped to the layer's box, so a star or polygon showed it both ways.
describe('a stroke inside or outside a shape follows its outline', () => {
test('on a star', () => {
expectFigmaRow(renderRow(ck, renderer, stroked('STAR', STAR, 'INSIDE'), 180, 100, 300), FIGMA.starInside)
expectFigmaRow(renderRow(ck, renderer, stroked('STAR', STAR, 'OUTSIDE'), 180, 100, 300), FIGMA.starOutside)
})
test('on a polygon', () => {
expectFigmaRow(renderRow(ck, renderer, stroked('POLYGON', TRIANGLE, 'INSIDE'), 150, 100, 300), FIGMA.polygonInside)
expectFigmaRow(renderRow(ck, renderer, stroked('POLYGON', TRIANGLE, 'OUTSIDE'), 150, 100, 300), FIGMA.polygonOutside)
})
test('on an arc', () => {
expectFigmaRow(renderRow(ck, renderer, stroked('ELLIPSE', HALF_PIE, 'INSIDE'), 145, 100, 300), FIGMA.arcInside)
expectFigmaRow(renderRow(ck, renderer, stroked('ELLIPSE', HALF_PIE, 'OUTSIDE'), 145, 100, 300), FIGMA.arcOutside)
})
})