Skip to content

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

fix: rounded rectangle corners and board section titles, as FigJam draws them (#1674)

Danila Poyarkov4ee95fabbc90
diff --git a/packages/core/src/canvas/index.ts b/packages/core/src/canvas/index.ts
index 335e48f6d..5b00cbc95 100644
--- a/packages/core/src/canvas/index.ts
+++ b/packages/core/src/canvas/index.ts
@@ -16,7 +16,7 @@ export {
export { computeGuideRedline } from './guides/redlines'
export { hitTestGuides, type GuideHit } from './guides/hit-test'
export type { GuideOverlayState, GuidePreview, GuideSelection } from './guides/types'
-export { canvasLabelForeground } from './labels/color'
+export { canvasLabelForeground, outlineColorInMode } from './labels/color'
export type { ImagePreview, ImagePreviewDecoder } from './images/previews'
export {
SkiaRenderer,
diff --git a/packages/core/src/canvas/labels/color.ts b/packages/core/src/canvas/labels/color.ts
index 67061df7a..bfa292baf 100644
--- a/packages/core/src/canvas/labels/color.ts
+++ b/packages/core/src/canvas/labels/color.ts
@@ -1,3 +1,4 @@
+import type { SceneGraph, SceneNode, Stroke } from '@open-pencil/scene-graph'
import { compositeOver, readableForeground } from '@open-pencil/scene-graph/color'
import { WHITE } from '@open-pencil/scene-graph/constants'
import type { Color } from '@open-pencil/scene-graph/primitives'
@@ -5,3 +6,14 @@ import type { Color } from '@open-pencil/scene-graph/primitives'
export function canvasLabelForeground(background: Color, canvasBackground: Color = WHITE): Color {
return readableForeground(compositeOver(background, canvasBackground))
}
+
+/** An outline's colour in the document, its variable's value in the active mode when it has one. */
+export function outlineColorInMode(
+ graph: SceneGraph,
+ node: SceneNode,
+ stroke: Stroke,
+ index: number
+): Color {
+ const variableId = node.boundVariables[`strokes/${index}/color`]
+ return (variableId && graph.resolveColorVariableForNode(node.id, variableId)) || stroke.color
+}
diff --git a/packages/core/src/canvas/labels/style.ts b/packages/core/src/canvas/labels/style.ts
index f47642632..9c6fbefef 100644
--- a/packages/core/src/canvas/labels/style.ts
+++ b/packages/core/src/canvas/labels/style.ts
@@ -1,17 +1,38 @@
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
import { compositeOver } from '@open-pencil/scene-graph/color'
+import { figJamSectionTitleColors } from '@open-pencil/scene-graph/figjam'
import Matrix from '@open-pencil/scene-graph/matrix'
import type { Color, Vector } from '@open-pencil/scene-graph/primitives'
import type { SkiaRenderer } from '#core/canvas/renderer'
import type { RotationPreview, SceneGeometry } from '#core/geometry'
-import { canvasLabelForeground } from './color'
+import { canvasLabelForeground, outlineColorInMode } from './color'
import type { LabelHitOptions } from './hit-test'
import type { LabelLayout, LabelTextMetrics } from './layout'
import { frameLabelPlacement } from './transform'
+/** FigJam's title pill takes a darker edge of its own colour. */
+const FIGJAM_TITLE_EDGE = 0.2
+
export function sectionLabelColors(r: SkiaRenderer, graph: SceneGraph, node: SceneNode) {
+ const figJam =
+ r.sectionTitles === 'outline'
+ ? figJamSectionTitleColors(node, (stroke, index) =>
+ outlineColorInMode(graph, node, stroke, index)
+ )
+ : null
+ if (figJam) {
+ const text = figJam.lightText ? 1 : 0
+ const outline = node.strokes[figJam.strokeIndex]
+ return {
+ // The outline's colour as drawn, in the canvas's colour space; the pill itself is opaque.
+ background: { ...r.resolveStrokeColor(outline, figJam.strokeIndex, node, graph), a: 1 },
+ border: r.ck.Color4f(0, 0, 0, FIGJAM_TITLE_EDGE),
+ hover: r.ck.Color4f(text, text, text, 0.08),
+ foreground: r.ck.Color4f(text, text, text, 1)
+ }
+ }
const background: Color =
node.fills.length > 0 && node.fills[0].visible
? r.resolveFillColor(node.fills[0], 0, node, graph)
diff --git a/packages/core/src/canvas/renderer.ts b/packages/core/src/canvas/renderer.ts
index 111e1f889..75386737d 100644
--- a/packages/core/src/canvas/renderer.ts
+++ b/packages/core/src/canvas/renderer.ts
@@ -146,6 +146,8 @@ export class SkiaRenderer {
pageColor = CANVAS_BG_COLOR
/** The board grid the page's dots follow, in canvas units, or null for a page without dots. */
pageDotGrid: number | null = null
+ /** What section titles are coloured from, as the document's profile says. */
+ sectionTitles: 'fill' | 'outline' = 'fill'
showRulers = true
rulerTheme: RulerTheme | null = null
/** Selection chrome, set from editor state each frame through {@link setSelectionTheme}. */
diff --git a/packages/core/src/canvas/renderer/pipeline.ts b/packages/core/src/canvas/renderer/pipeline.ts
index 0ba282e70..fe8b1fe9f 100644
--- a/packages/core/src/canvas/renderer/pipeline.ts
+++ b/packages/core/src/canvas/renderer/pipeline.ts
@@ -105,6 +105,7 @@ export function renderFromEditorState(
const profile = resolveEditorProfile(state)
const { editChrome, live, edits } = profile
r.pageDotGrid = profile.dotGrid
+ r.sectionTitles = profile.sectionTitles
r.editHandles = edits
render(
r,
diff --git a/packages/core/src/editor/shapes/with-text.ts b/packages/core/src/editor/shapes/with-text.ts
index f0f7f706f..2181db9de 100644
--- a/packages/core/src/editor/shapes/with-text.ts
+++ b/packages/core/src/editor/shapes/with-text.ts
@@ -12,7 +12,7 @@ import {
type FigJamLineStyle,
type FigJamTextSize
} from '@open-pencil/scene-graph/figjam'
-import { shapeWithTextFrameFields, shapeWithTextLabel } from '@open-pencil/scene-graph/generated'
+import { shapeWithTextKindFields, shapeWithTextLabel } from '@open-pencil/scene-graph/generated'
import type { ShapeWithTextType } from '@open-pencil/scene-graph/shape-with-text'
import { recordSubtreeEdit } from '#core/editor/components/slots/history'
@@ -61,10 +61,7 @@ export function createShapeWithTextActions(ctx: EditorContext, nodes: NodeAction
'Change shape',
changing.map((shape) => shape.id),
(shape) => {
- nodes.updateNode(shape.id, {
- shapeWithTextType: kind,
- ...shapeWithTextFrameFields(kind, shape.width, shape.height)
- })
+ nodes.updateNode(shape.id, shapeWithTextKindFields(shape, kind))
}
)
}
diff --git a/packages/core/src/editor/views/profile.ts b/packages/core/src/editor/views/profile.ts
index d2f26ec5f..89382b6cf 100644
--- a/packages/core/src/editor/views/profile.ts
+++ b/packages/core/src/editor/views/profile.ts
@@ -21,6 +21,7 @@ type DocumentBehaviour = Pick<
| 'dotGrid'
| 'pixelGrid'
| 'framesOpen'
+ | 'sectionTitles'
| 'lint'
| 'rulers'
| 'panelsOpen'
@@ -38,6 +39,7 @@ const DOCUMENT_BEHAVIOURS: Readonly<Record<DocumentKind, DocumentBehaviour>> = {
dotGrid: null,
pixelGrid: true,
framesOpen: true,
+ sectionTitles: 'fill',
lint: true,
rulers: true,
panelsOpen: true
@@ -50,6 +52,7 @@ const DOCUMENT_BEHAVIOURS: Readonly<Record<DocumentKind, DocumentBehaviour>> = {
dotGrid: BOARD_GRID,
pixelGrid: false,
framesOpen: false,
+ sectionTitles: 'outline',
lint: false,
rulers: false,
panelsOpen: false
diff --git a/packages/core/src/editor/views/types.ts b/packages/core/src/editor/views/types.ts
index 7eb2045b7..d3b6ec5fd 100644
--- a/packages/core/src/editor/views/types.ts
+++ b/packages/core/src/editor/views/types.ts
@@ -59,6 +59,11 @@ export interface EditorProfile {
* board it selects the frame, and double-click or Enter goes inside.
*/
framesOpen: boolean
+ /**
+ * What a section's title is coloured from: its fill, as in Figma Design, or its outline, as a
+ * FigJam board draws it.
+ */
+ sectionTitles: 'fill' | 'outline'
/** Whether design lint checks the document; a FigJam board has none. */
lint: boolean
/** Whether the canvas shows rulers, from which guides are dragged; a FigJam board has none. */
diff --git a/packages/core/src/figma-api/shape-with-text.ts b/packages/core/src/figma-api/shape-with-text.ts
index 2ef71fffd..6ccadce2e 100644
--- a/packages/core/src/figma-api/shape-with-text.ts
+++ b/packages/core/src/figma-api/shape-with-text.ts
@@ -1,5 +1,5 @@
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
-import { createShapeWithText, shapeWithTextFrameFields } from '@open-pencil/scene-graph/generated'
+import { createShapeWithText, shapeWithTextKindFields } from '@open-pencil/scene-graph/generated'
import {
isShapeWithTextType,
SHAPE_WITH_TEXT_TYPES,
@@ -57,10 +57,7 @@ export function installShapeWithTextAccessors(
const kind = checkedShapeType(node, value)
prepareFollowedFieldEdit(graph(this, internals), node, 'shapeType', 'shape-type')
// The new kind lays the label out in its own area, as FigJam's shape switcher does.
- updateNode(this, internals, {
- shapeWithTextType: kind,
- ...shapeWithTextFrameFields(kind, node.width, node.height)
- })
+ updateNode(this, internals, shapeWithTextKindFields(node, kind))
}
}
})
diff --git a/packages/core/tests/canvas/labels/color.test.ts b/packages/core/tests/canvas/labels/color.test.ts
new file mode 100644
index 000000000..ffdb6a9f1
--- /dev/null
+++ b/packages/core/tests/canvas/labels/color.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, test } from 'bun:test'
+
+import { expectDefined } from '#core-tests/helpers/assert'
+
+import { outlineColorInMode } from '@open-pencil/core/canvas'
+import { SceneGraph } from '@open-pencil/scene-graph'
+import { figJamColor, figJamSectionColorFields, figJamSectionTitleColors } from '@open-pencil/scene-graph/figjam'
+
+describe('section title colours', () => {
+ // A board titles a section in the outline it draws, which a variable sets per mode.
+ test("take the outline's colour in the section's active mode", () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const collection = graph.createCollection('Theme')
+ const light = collection.defaultModeId
+ const dark = expectDefined(graph.createMode(collection.id, 'Dark'), 'dark mode')
+ const outline = graph.createVariable('Outline', 'COLOR', collection.id, figJamColor('red'))
+ outline.valuesByMode[dark] = figJamColor('yellow')
+ const section = graph.createNode('SECTION', page.id, {
+ ...figJamSectionColorFields({ strokes: [] }, 'red'),
+ boundVariables: { 'strokes/0/color': outline.id }
+ })
+ const titleIn = (modeId: string) => {
+ graph.updateNode(section.id, { variableModes: { [collection.id]: modeId } })
+ const node = expectDefined(graph.getNode(section.id), 'section')
+ return figJamSectionTitleColors(node, (stroke, index) =>
+ outlineColorInMode(graph, node, stroke, index)
+ )?.lightText
+ }
+ // Red takes white text, yellow black, though the stored outline stays red.
+ expect(titleIn(light)).toBe(true)
+ expect(titleIn(dark)).toBe(false)
+ })
+})
diff --git a/packages/core/tests/editor/shapes/with-text.test.ts b/packages/core/tests/editor/shapes/with-text.test.ts
index 4d052325f..27c6d2dae 100644
--- a/packages/core/tests/editor/shapes/with-text.test.ts
+++ b/packages/core/tests/editor/shapes/with-text.test.ts
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { expectDefined } from '#core-tests/helpers/assert'
import { createEditor } from '@open-pencil/core'
-import { setTextMeasurer } from '@open-pencil/core/layout'
+import { computeAllLayouts, setTextMeasurer } from '@open-pencil/core/layout'
import { colorToHex } from '@open-pencil/scene-graph/color'
import { shapeWithTextLabel } from '@open-pencil/scene-graph/generated'
import { lineHeightPixels } from '@open-pencil/scene-graph/text'
@@ -50,6 +50,19 @@ describe('shapes with text in the editor', () => {
expect(editor.graph.getNode(id)?.shapeWithTextType).toBe('SQUARE')
})
+ // Measured in FigJam: 70.4 on a 176 square, 80 at 400×200, and 6 again once it is a square.
+ test('a rounded rectangle keeps its corners at 0.4 of its shorter side, as FigJam does', () => {
+ const { editor, pageId } = setup()
+ const id = editor.createShape('SHAPE_WITH_TEXT', 0, 0, 176, 176, pageId)
+ editor.changeShapesWithTextKind([id], 'ROUNDED_RECTANGLE')
+ expect(editor.graph.getNode(id)?.cornerRadius).toBeCloseTo(70.4)
+ editor.graph.updateNode(id, { width: 400, height: 200, shapeUserHeight: 200 })
+ computeAllLayouts(editor.graph, id)
+ expect(editor.graph.getNode(id)?.cornerRadius).toBeCloseTo(80)
+ editor.changeShapesWithTextKind([id], 'SQUARE')
+ expect(editor.graph.getNode(id)?.cornerRadius).toBe(6)
+ })
+
test("typing in a shape's label renames a shape that was never renamed", () => {
const { editor, pageId } = setup()
const id = editor.createShape('SHAPE_WITH_TEXT', 0, 0, 176, 176, pageId)
diff --git a/packages/core/tests/editor/views/profile.test.ts b/packages/core/tests/editor/views/profile.test.ts
index 9db8dac3d..4f581676b 100644
--- a/packages/core/tests/editor/views/profile.test.ts
+++ b/packages/core/tests/editor/views/profile.test.ts
@@ -27,6 +27,7 @@ describe('canvas view profiles', () => {
dotGrid: null,
pixelGrid: true,
framesOpen: true,
+ sectionTitles: 'fill',
lint: true,
rulers: true,
panelsOpen: true
@@ -50,6 +51,7 @@ describe('canvas view profiles', () => {
dotGrid: null,
pixelGrid: true,
framesOpen: true,
+ sectionTitles: 'fill',
lint: true,
rulers: true,
panelsOpen: true
@@ -143,6 +145,8 @@ describe('document kinds', () => {
false,
false
])
+ // FigJam colours a section's title from its outline.
+ expect(profile.sectionTitles).toBe('outline')
})
test('becoming a board drops a design tool, and refuses picking one', () => {
diff --git a/packages/design-jsx/src/reference/authoring.md b/packages/design-jsx/src/reference/authoring.md
index 2e1473541..bf05c27f7 100644
--- a/packages/design-jsx/src/reference/authoring.md
+++ b/packages/design-jsx/src/reference/authoring.md
@@ -29,7 +29,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
- `Icon` uses an Iconify name, size, and color. Prefer icons to emoji when reliable vector output is needed. Image fills belong on appropriate leaf shapes, not containers whose children must remain visible.
- Pick artwork by where it comes from. A known icon is an `Icon` by its Iconify name. A shape you draw yourself is a `Vector` with its path in SVG syntax, `<Vector d="M11 2L4 11h5l-1 7 7-9h-5z" bg={designVar(…)} />`: one layer, styled with the same props as any other, so its fills can use variables, stack, and take `strokeAlign` and `effects`. `fillRule="evenodd"` cuts holes. An open path with a `stroke` stays open, a line; with only a `bg` it fills as if closed. Given `w` and `h`, the path is drawn in that box as written, as an icon is in its view box; without them the layer fits the path. `paths` takes several paths as export writes them, each `{ windingRule, data }` with `NONZERO`, `EVENODD`, or `NONE` for an open path: `paths={[{ windingRule: "EVENODD", data: "M0 0L10 0L10 10Z" }]}`. Inline `<svg>` is for artwork copied from elsewhere: it draws the SVG's shapes, a frame with a layer per shape in the SVG's own colours, and leaves out its text, so do not use it for a shape you will style or bind, nor for text.
- `Sticky` makes a FigJam sticky note whose text is its body: `<Sticky color="yellow" author="Ada">Ship it</Sticky>`. `color` takes a FigJam palette name (`white`, `gray`, `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `violet`, `pink`) or any colour, `author` signs it (`authorVisible={false}` hides the line), `wide` makes it 416 wide instead of 240, `textSize` is `small`, `medium`, `large`, `extraLarge`, or `huge`, `typeface` is `simple`, `bookish`, `technical`, or `scribbled`, and `scale` scales the whole note. A note sizes itself; it takes no `w`, `h`, fills, or layout props.
-- `Shape` makes a FigJam shape with text whose text is its label: `<Shape kind="diamond" bg="#FFC943">Valid?</Shape>`. `kind` is `square` (default), `ellipse`, `diamond`, `rounded_rectangle`, `parallelogram_right`, `eng_database`, or another FigJam shape, written in lowercase. `w` and `h` are the size it is given (176 by default); it grows taller to fit its label, never wider, so a word too long for it breaks. `truncate` keeps its height and cuts the label off with "…" instead, as FigJam does once a shape's height is set. `bg`, `stroke`, `strokeWidth`, `rounded` (6 by default), and `rotate` style it as any layer; the label is centred in the shape and takes the same `textSize` and `typeface` as `Sticky`.
+- `Shape` makes a FigJam shape with text whose text is its label: `<Shape kind="diamond" bg="#FFC943">Valid?</Shape>`. `kind` is `square` (default), `ellipse`, `diamond`, `rounded_rectangle`, `parallelogram_right`, `eng_database`, or another FigJam shape, written in lowercase. `w` and `h` are the size it is given (176 by default); it grows taller to fit its label, never wider, so a word too long for it breaks. `truncate` keeps its height and cuts the label off with "…" instead, as FigJam does once a shape's height is set. `bg`, `stroke`, `strokeWidth`, `rounded` (6 by default; a `rounded_rectangle` keeps its corners at 0.4 of its shorter side, as FigJam does), and `rotate` style it as any layer; the label is centred in the shape and takes the same `textSize` and `typeface` as `Sticky`.
- `Connector` draws a FigJam connector that follows the layers it joins: `<Connector from="Start" to="Done">Next</Connector>`. `from` and `to` name layers of the same JSX, take a `ref()` a layer is marked with (`const card = ref()`, then `<Frame ref={card}>` and `from={card}`), or give `{ x, y }` points in the connector's parent; `fromSide`/`toSide` (`top`, `right`, `bottom`, `left`) fix the side it attaches by, `line` is `elbowed` (default), `straight`, or `curved`, and `startCap`/`endCap` take the same caps as `strokeCap`: `none`, `round`, `square`, `arrow_lines`, `arrow_equilateral`, `triangle_filled`, `diamond_filled`, or `circle_filled` (it starts with none and ends with `arrow_lines`). Its text is the label; `stroke`, `strokeWidth`, and `dashPattern` style the line. When layers share a name, join them by `ref()`; a name more than one layer has joins the first, with a warning.
- Design JSX props are the portable authoring interface. Some CSS-style aliases are supported, but this is not a browser CSS engine; do not assume arbitrary HTML, classes, or styles work.
diff --git a/packages/docs/reference/design-authoring.md b/packages/docs/reference/design-authoring.md
index a6a91e823..1e54beac4 100644
--- a/packages/docs/reference/design-authoring.md
+++ b/packages/docs/reference/design-authoring.md
@@ -31,7 +31,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
- `Icon` uses an Iconify name, size, and color. Prefer icons to emoji when reliable vector output is needed. Image fills belong on appropriate leaf shapes, not containers whose children must remain visible.
- Pick artwork by where it comes from. A known icon is an `Icon` by its Iconify name. A shape you draw yourself is a `Vector` with its path in SVG syntax, `<Vector d="M11 2L4 11h5l-1 7 7-9h-5z" bg={designVar(…)} />`: one layer, styled with the same props as any other, so its fills can use variables, stack, and take `strokeAlign` and `effects`. `fillRule="evenodd"` cuts holes. An open path with a `stroke` stays open, a line; with only a `bg` it fills as if closed. Given `w` and `h`, the path is drawn in that box as written, as an icon is in its view box; without them the layer fits the path. `paths` takes several paths as export writes them, each `{ windingRule, data }` with `NONZERO`, `EVENODD`, or `NONE` for an open path: `paths={[{ windingRule: "EVENODD", data: "M0 0L10 0L10 10Z" }]}`. Inline `<svg>` is for artwork copied from elsewhere: it draws the SVG's shapes, a frame with a layer per shape in the SVG's own colours, and leaves out its text, so do not use it for a shape you will style or bind, nor for text.
- `Sticky` makes a FigJam sticky note whose text is its body: `<Sticky color="yellow" author="Ada">Ship it</Sticky>`. `color` takes a FigJam palette name (`white`, `gray`, `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `violet`, `pink`) or any colour, `author` signs it (`authorVisible={false}` hides the line), `wide` makes it 416 wide instead of 240, `textSize` is `small`, `medium`, `large`, `extraLarge`, or `huge`, `typeface` is `simple`, `bookish`, `technical`, or `scribbled`, and `scale` scales the whole note. A note sizes itself; it takes no `w`, `h`, fills, or layout props.
-- `Shape` makes a FigJam shape with text whose text is its label: `<Shape kind="diamond" bg="#FFC943">Valid?</Shape>`. `kind` is `square` (default), `ellipse`, `diamond`, `rounded_rectangle`, `parallelogram_right`, `eng_database`, or another FigJam shape, written in lowercase. `w` and `h` are the size it is given (176 by default); it grows taller to fit its label, never wider, so a word too long for it breaks. `truncate` keeps its height and cuts the label off with "…" instead, as FigJam does once a shape's height is set. `bg`, `stroke`, `strokeWidth`, `rounded` (6 by default), and `rotate` style it as any layer; the label is centred in the shape and takes the same `textSize` and `typeface` as `Sticky`.
+- `Shape` makes a FigJam shape with text whose text is its label: `<Shape kind="diamond" bg="#FFC943">Valid?</Shape>`. `kind` is `square` (default), `ellipse`, `diamond`, `rounded_rectangle`, `parallelogram_right`, `eng_database`, or another FigJam shape, written in lowercase. `w` and `h` are the size it is given (176 by default); it grows taller to fit its label, never wider, so a word too long for it breaks. `truncate` keeps its height and cuts the label off with "…" instead, as FigJam does once a shape's height is set. `bg`, `stroke`, `strokeWidth`, `rounded` (6 by default; a `rounded_rectangle` keeps its corners at 0.4 of its shorter side, as FigJam does), and `rotate` style it as any layer; the label is centred in the shape and takes the same `textSize` and `typeface` as `Sticky`.
- `Connector` draws a FigJam connector that follows the layers it joins: `<Connector from="Start" to="Done">Next</Connector>`. `from` and `to` name layers of the same JSX, take a `ref()` a layer is marked with (`const card = ref()`, then `<Frame ref={card}>` and `from={card}`), or give `{ x, y }` points in the connector's parent; `fromSide`/`toSide` (`top`, `right`, `bottom`, `left`) fix the side it attaches by, `line` is `elbowed` (default), `straight`, or `curved`, and `startCap`/`endCap` take the same caps as `strokeCap`: `none`, `round`, `square`, `arrow_lines`, `arrow_equilateral`, `triangle_filled`, `diamond_filled`, or `circle_filled` (it starts with none and ends with `arrow_lines`). Its text is the label; `stroke`, `strokeWidth`, and `dashPattern` style the line. When layers share a name, join them by `ref()`; a name more than one layer has joins the first, with a warning.
- Design JSX props are the portable authoring interface. Some CSS-style aliases are supported, but this is not a browser CSS engine; do not assume arbitrary HTML, classes, or styles work.
diff --git a/packages/scene-graph/src/figjam/palette.ts b/packages/scene-graph/src/figjam/palette.ts
index eaba6d0fd..2d1254f65 100644
--- a/packages/scene-graph/src/figjam/palette.ts
+++ b/packages/scene-graph/src/figjam/palette.ts
@@ -244,6 +244,27 @@ export function figJamOutlineColorName(color: Color | undefined): FigJamColorNam
)
}
+/**
+ * How FigJam draws a section's title on a board, measured in FigJam: a pill in the colour of the
+ * outline drawn on top, with white text on the palette's darkest colours and black on the rest;
+ * null for a section without an outline from the palette. `colorOf` reads an outline's colour as
+ * drawn, such as a variable's value in the active mode.
+ */
+export function figJamSectionTitleColors(
+ section: Pick<SceneNode, 'strokes'>,
+ colorOf: (stroke: Stroke, index: number) => Color = (stroke) => stroke.color
+): { strokeIndex: number; background: Color; lightText: boolean } | null {
+ const strokeIndex = section.strokes.findLastIndex(
+ (stroke) => stroke.visible && stroke.opacity > 0
+ )
+ const outline = section.strokes.at(strokeIndex)
+ if (strokeIndex === -1 || !outline) return null
+ const color = colorOf(outline, strokeIndex)
+ const name = figJamOutlineColorName(color)
+ if (!name) return null
+ return { strokeIndex, background: { ...color, a: 1 }, lightText: PALETTE[name].lightLabel }
+}
+
/** The fill mode a shape's first fill shows. */
export function figJamFillMode(fill: Fill | undefined): FigJamFillMode {
if (!fill || !fill.visible || fill.opacity === 0) return 'none'
@@ -380,9 +401,16 @@ export function figJamSectionColorFields(
name: FigJamColorName
): Partial<SceneNode> {
const { sectionFill, sectionStroke } = PALETTE[name]
+ // The outline shows in full at FigJam's 2, as its menu draws it, unless the line menu hid it.
+ const strokes = strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT).map(
+ (stroke): Stroke =>
+ stroke.opacity === 0
+ ? stroke
+ : { ...stroke, opacity: 1, visible: true, weight: SECTION_STROKE_WEIGHT }
+ )
return {
fills: [{ type: 'SOLID', color: parseColor(sectionFill), opacity: 1, visible: true }],
- strokes: strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT),
+ strokes,
strokeWeight: SECTION_STROKE_WEIGHT
}
}
diff --git a/packages/scene-graph/src/generated/shape-with-text.ts b/packages/scene-graph/src/generated/shape-with-text.ts
index b63998db5..1540dbfbd 100644
--- a/packages/scene-graph/src/generated/shape-with-text.ts
+++ b/packages/scene-graph/src/generated/shape-with-text.ts
@@ -9,9 +9,15 @@ import type { GeneratedFrameKind } from './kind'
/** FigJam makes a shape with text 176 px square, from its tool and from its plugin API. */
export const SHAPE_WITH_TEXT_SIZE = 176
-/** FigJam rounds every corner of a shape by 6 px. */
+/** FigJam rounds every corner of a shape by 6 px, but a rounded rectangle's. */
export const SHAPE_WITH_TEXT_CORNER_RADIUS = 6
+/**
+ * FigJam keeps a rounded rectangle's corner radius at 0.4 of its shorter side, through resizes,
+ * measured in FigJam; Figma draws the shape from that radius.
+ */
+const ROUNDED_RECTANGLE_RADIUS = 0.4
+
const LABEL_FILL: Fill = { type: 'SOLID', color: { ...BLACK }, opacity: 0.8, visible: true }
/** How the label sits in its shape: as wide as the label box, as tall as its text. */
@@ -42,7 +48,27 @@ export function shapeWithTextFrameFields(
paddingRight: Math.max(0, width - box.x - box.width),
paddingBottom: Math.max(0, height - box.y - box.height),
paddingLeft: box.x,
- clipsContent: false
+ clipsContent: false,
+ ...(kind === 'ROUNDED_RECTANGLE'
+ ? { cornerRadius: ROUNDED_RECTANGLE_RADIUS * Math.min(width, height) }
+ : {})
+ }
+}
+
+/**
+ * What turning a shape into `kind` changes, keeping its size, as FigJam's shape switcher does: the
+ * label area, and the corners a rounded rectangle leaves go back to FigJam's 6.
+ */
+export function shapeWithTextKindFields(
+ shape: Pick<SceneNode, 'shapeWithTextType' | 'width' | 'height'>,
+ kind: ShapeWithTextType
+): Partial<SceneNode> {
+ const leavesRounded =
+ shape.shapeWithTextType === 'ROUNDED_RECTANGLE' && kind !== shape.shapeWithTextType
+ return {
+ shapeWithTextType: kind,
+ ...(leavesRounded ? { cornerRadius: SHAPE_WITH_TEXT_CORNER_RADIUS } : {}),
+ ...shapeWithTextFrameFields(kind, shape.width, shape.height)
}
}
diff --git a/packages/scene-graph/tests/figjam/palette.test.ts b/packages/scene-graph/tests/figjam/palette.test.ts
index b49ddedb7..771c13846 100644
--- a/packages/scene-graph/tests/figjam/palette.test.ts
+++ b/packages/scene-graph/tests/figjam/palette.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'bun:test'
import { createShapeWithText, shapeWithTextLabel } from '@open-pencil/scene-graph/generated'
-import { figJamColor, figJamColorName, figJamLayerColorFields, figJamLayerColorName, figJamLineColorFields, figJamLineStyle, figJamLineStyleFields, figJamOutlineColorName, figJamSectionColorFields, figJamSectionColorName, figJamShapePaint } from '@open-pencil/scene-graph/figjam'
+import { figJamColor, figJamColorName, figJamLayerColorFields, figJamLayerColorName, figJamLineColorFields, figJamLineStyle, figJamLineStyleFields, figJamOutlineColorName, figJamSectionColorFields, figJamSectionTitleColors, figJamSectionColorName, figJamShapePaint } from '@open-pencil/scene-graph/figjam'
import { SceneGraph } from '@open-pencil/scene-graph'
import { colorToHex } from '@open-pencil/scene-graph/color'
@@ -90,10 +90,29 @@ describe("FigJam's shape colours", () => {
expect(hexes(figJamSectionColorFields({ strokes: [] }, 'black').strokes)).toEqual(['#2c2c2c'])
expect(hexes(figJamSectionColorFields({ strokes: [] }, 'lightBlue').fills)).toEqual(['#f5fbff'])
expect(figJamSectionColorName({ fills: red.fills ?? [] })).toBe('red')
+ // A new section's faint outline shows in full once coloured; one the line menu hid stays hidden.
+ const faint = { type: 'SOLID' as const, color: figJamColor('white'), opacity: 0.1, visible: true, weight: 1, align: 'INSIDE' as const }
+ expect(figJamSectionColorFields({ strokes: [faint] }, 'red').strokes?.[0]?.opacity).toBe(1)
+ expect(figJamSectionColorFields({ strokes: [{ ...faint, opacity: 0 }] }, 'red').strokes?.[0]?.opacity).toBe(0)
const hidden = { type: 'SOLID' as const, color: figJamColor('blue'), opacity: 1, visible: false }
expect(figJamSectionColorName({ fills: [hidden, ...(red.fills ?? [])] })).toBe('red')
})
+ // Measured in FigJam: the title pill takes the outline colour, white text on the darkest colours.
+ test("a section's title takes its outline colour, with light text on the darkest", () => {
+ const title = (name: 'red' | 'yellow' | 'black' | 'lightBlue') => {
+ const colors = figJamSectionTitleColors({ strokes: figJamSectionColorFields({ strokes: [] }, name).strokes ?? [] })
+ return colors && [colorToHex(colors.background).toLowerCase(), colors.lightText]
+ }
+ expect([title('red'), title('yellow'), title('black'), title('lightBlue')]).toEqual([
+ ['#f24822', true],
+ ['#ffc943', false],
+ ['#2c2c2c', true],
+ ['#c2e5ff', false]
+ ])
+ expect(figJamSectionTitleColors({ strokes: [] })).toBeNull()
+ })
+
test("a section's outline reads as its colour, darker for black and lighter for white", () => {
for (const name of ['black', 'white', 'red'] as const) {
const outline = figJamSectionColorFields({ strokes: [] }, name).strokes?.[0]?.color
diff --git a/skills/open-pencil/references/design-authoring.md b/skills/open-pencil/references/design-authoring.md
index a6a91e823..1e54beac4 100644
--- a/skills/open-pencil/references/design-authoring.md
+++ b/skills/open-pencil/references/design-authoring.md
@@ -31,7 +31,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
- `Icon` uses an Iconify name, size, and color. Prefer icons to emoji when reliable vector output is needed. Image fills belong on appropriate leaf shapes, not containers whose children must remain visible.
- Pick artwork by where it comes from. A known icon is an `Icon` by its Iconify name. A shape you draw yourself is a `Vector` with its path in SVG syntax, `<Vector d="M11 2L4 11h5l-1 7 7-9h-5z" bg={designVar(…)} />`: one layer, styled with the same props as any other, so its fills can use variables, stack, and take `strokeAlign` and `effects`. `fillRule="evenodd"` cuts holes. An open path with a `stroke` stays open, a line; with only a `bg` it fills as if closed. Given `w` and `h`, the path is drawn in that box as written, as an icon is in its view box; without them the layer fits the path. `paths` takes several paths as export writes them, each `{ windingRule, data }` with `NONZERO`, `EVENODD`, or `NONE` for an open path: `paths={[{ windingRule: "EVENODD", data: "M0 0L10 0L10 10Z" }]}`. Inline `<svg>` is for artwork copied from elsewhere: it draws the SVG's shapes, a frame with a layer per shape in the SVG's own colours, and leaves out its text, so do not use it for a shape you will style or bind, nor for text.
- `Sticky` makes a FigJam sticky note whose text is its body: `<Sticky color="yellow" author="Ada">Ship it</Sticky>`. `color` takes a FigJam palette name (`white`, `gray`, `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `violet`, `pink`) or any colour, `author` signs it (`authorVisible={false}` hides the line), `wide` makes it 416 wide instead of 240, `textSize` is `small`, `medium`, `large`, `extraLarge`, or `huge`, `typeface` is `simple`, `bookish`, `technical`, or `scribbled`, and `scale` scales the whole note. A note sizes itself; it takes no `w`, `h`, fills, or layout props.
-- `Shape` makes a FigJam shape with text whose text is its label: `<Shape kind="diamond" bg="#FFC943">Valid?</Shape>`. `kind` is `square` (default), `ellipse`, `diamond`, `rounded_rectangle`, `parallelogram_right`, `eng_database`, or another FigJam shape, written in lowercase. `w` and `h` are the size it is given (176 by default); it grows taller to fit its label, never wider, so a word too long for it breaks. `truncate` keeps its height and cuts the label off with "…" instead, as FigJam does once a shape's height is set. `bg`, `stroke`, `strokeWidth`, `rounded` (6 by default), and `rotate` style it as any layer; the label is centred in the shape and takes the same `textSize` and `typeface` as `Sticky`.
+- `Shape` makes a FigJam shape with text whose text is its label: `<Shape kind="diamond" bg="#FFC943">Valid?</Shape>`. `kind` is `square` (default), `ellipse`, `diamond`, `rounded_rectangle`, `parallelogram_right`, `eng_database`, or another FigJam shape, written in lowercase. `w` and `h` are the size it is given (176 by default); it grows taller to fit its label, never wider, so a word too long for it breaks. `truncate` keeps its height and cuts the label off with "…" instead, as FigJam does once a shape's height is set. `bg`, `stroke`, `strokeWidth`, `rounded` (6 by default; a `rounded_rectangle` keeps its corners at 0.4 of its shorter side, as FigJam does), and `rotate` style it as any layer; the label is centred in the shape and takes the same `textSize` and `typeface` as `Sticky`.
- `Connector` draws a FigJam connector that follows the layers it joins: `<Connector from="Start" to="Done">Next</Connector>`. `from` and `to` name layers of the same JSX, take a `ref()` a layer is marked with (`const card = ref()`, then `<Frame ref={card}>` and `from={card}`), or give `{ x, y }` points in the connector's parent; `fromSide`/`toSide` (`top`, `right`, `bottom`, `left`) fix the side it attaches by, `line` is `elbowed` (default), `straight`, or `curved`, and `startCap`/`endCap` take the same caps as `strokeCap`: `none`, `round`, `square`, `arrow_lines`, `arrow_equilateral`, `triangle_filled`, `diamond_filled`, or `circle_filled` (it starts with none and ends with `arrow_lines`). Its text is the label; `stroke`, `strokeWidth`, and `dashPattern` style the line. When layers share a name, join them by `ref()`; a name more than one layer has joins the first, with a warning.
- Design JSX props are the portable authoring interface. Some CSS-style aliases are supported, but this is not a browser CSS engine; do not assume arbitrary HTML, classes, or styles work.
diff --git a/src/components/canvas/labels/presentation.ts b/src/components/canvas/labels/presentation.ts
index dea84d521..e086bc54c 100644
--- a/src/components/canvas/labels/presentation.ts
+++ b/src/components/canvas/labels/presentation.ts
@@ -1,6 +1,7 @@
-import type { Editor } from '@open-pencil/core'
-import { canvasLabelForeground } from '@open-pencil/core/canvas'
+import { resolveEditorProfile, type Editor } from '@open-pencil/core'
+import { canvasLabelForeground, outlineColorInMode } from '@open-pencil/core/canvas'
import type { SceneNode } from '@open-pencil/scene-graph'
+import { figJamSectionTitleColors } from '@open-pencil/scene-graph/figjam'
import type { Color } from '@open-pencil/scene-graph/primitives'
import type { CanvasLabelKind } from '@open-pencil/vue/canvas/labels'
@@ -23,6 +24,23 @@ export function canvasLabelPresentation(
kind: CanvasLabelKind | undefined
): CanvasLabelPresentation {
if (kind && kind !== 'section-title') return NAME_FIELD
+ // A board draws the title on the section's outline colour, as FigJam does.
+ const figJam =
+ node && resolveEditorProfile(editor.state).sectionTitles === 'outline'
+ ? figJamSectionTitleColors(node, (stroke, index) =>
+ outlineColorInMode(editor.graph, node, stroke, index)
+ )
+ : null
+ if (figJam) {
+ const outline = node?.strokes[figJam.strokeIndex]
+ const background =
+ (node &&
+ outline &&
+ editor.renderer?.resolveStrokeColor(outline, figJam.strokeIndex, node, editor.graph)) ||
+ figJam.background
+ // Opaque, as the pill it edits is.
+ return { background: { ...background, a: 1 }, foreground: figJam.lightText ? 'light' : 'dark' }
+ }
const fill = node?.fills[0]
const background =
node && fill?.visible
diff --git a/tests/e2e/canvas/board-chrome.spec.ts b/tests/e2e/canvas/board/chrome.spec.ts
similarity index 100%
rename from tests/e2e/canvas/board-chrome.spec.ts
rename to tests/e2e/canvas/board/chrome.spec.ts
diff --git a/tests/e2e/canvas/board-dots-visual.spec.ts b/tests/e2e/canvas/board/dots-visual.spec.ts
similarity index 100%
rename from tests/e2e/canvas/board-dots-visual.spec.ts
rename to tests/e2e/canvas/board/dots-visual.spec.ts
diff --git a/tests/e2e/canvas/board-dots-visual.spec.ts-snapshots/board-dots-100-openpencil-darwin.png b/tests/e2e/canvas/board/dots-visual.spec.ts-snapshots/board-dots-100-openpencil-darwin.png
similarity index 100%
rename from tests/e2e/canvas/board-dots-visual.spec.ts-snapshots/board-dots-100-openpencil-darwin.png
rename to tests/e2e/canvas/board/dots-visual.spec.ts-snapshots/board-dots-100-openpencil-darwin.png
diff --git a/tests/e2e/canvas/board-dots-visual.spec.ts-snapshots/board-dots-25-openpencil-darwin.png b/tests/e2e/canvas/board/dots-visual.spec.ts-snapshots/board-dots-25-openpencil-darwin.png
similarity index 100%
rename from tests/e2e/canvas/board-dots-visual.spec.ts-snapshots/board-dots-25-openpencil-darwin.png
rename to tests/e2e/canvas/board/dots-visual.spec.ts-snapshots/board-dots-25-openpencil-darwin.png
diff --git a/tests/e2e/canvas/board/sections-visual.spec.ts b/tests/e2e/canvas/board/sections-visual.spec.ts
new file mode 100644
index 000000000..71006aed4
--- /dev/null
+++ b/tests/e2e/canvas/board/sections-visual.spec.ts
@@ -0,0 +1,35 @@
+import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
+import { boardDriver } from '#tests/helpers/canvas/board-driver'
+
+const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
+const board = boardDriver(() => editor.page)
+
+test.afterEach(() => board.close())
+
+// Measured in FigJam: a section's title is a pill in its outline colour, with white text on the
+// darkest palette colours and black on the rest.
+test('a board titles its sections in their outline colours, as FigJam does', async () => {
+ await board.open()
+ await editor.page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ const sections = [
+ { name: 'Red', color: 'red', x: 32 },
+ { name: 'Yellow', color: 'yellow', x: 224 },
+ { name: 'Light blue', color: 'lightBlue', x: 416 }
+ ] as const
+ for (const { name, color, x } of sections) {
+ const id = store.createShape('SECTION', x, 48, 160, 64)
+ store.graph.updateNode(id, { name })
+ store.setFigJamColor([id], color)
+ }
+ store.select([])
+ store.requestRender()
+ })
+ await board.showAt(1)
+ await editor.canvas.waitForRender()
+ editor.canvas.assertNoErrors()
+ expect(await editor.canvas.screenshotCanvasRegion(600, 140)).toMatchSnapshot(
+ 'board-section-titles.png'
+ )
+})
diff --git a/tests/e2e/canvas/board/sections-visual.spec.ts-snapshots/board-section-titles-openpencil-darwin.png b/tests/e2e/canvas/board/sections-visual.spec.ts-snapshots/board-section-titles-openpencil-darwin.png
new file mode 100644
index 000000000..42001b80f
Binary files /dev/null and b/tests/e2e/canvas/board/sections-visual.spec.ts-snapshots/board-section-titles-openpencil-darwin.png differ