Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

perf(fig): lift dragged layers, render wide blurs coarsely (#7519)

Jacob Beckerman1ecf4ce481c6
diff --git a/apps/web/src/features/block-fig/browser-test/README.md b/apps/web/src/features/block-fig/browser-test/README.md
index 10454d76af..59b5310fb3 100644
--- a/apps/web/src/features/block-fig/browser-test/README.md
+++ b/apps/web/src/features/block-fig/browser-test/README.md
@@ -44,7 +44,7 @@ bunx playwright test --config src/features/block-fig/browser-test/playwright.con
They cover rendering, the layers and pages lists, Figma's selection rules
(top-level frames select their child, double-click into instances), Escape,
frame and page navigation, zoom shortcuts, export, the shortcuts dialog,
-and editing: drawing, moving, and saving shapes, lines and arrows, fills
+and editing: drawing, moving (lifted off the page while dragged, one edit at the drop, also when let go before the drag starts), and saving shapes, lines and arrows, fills
and shadows from the design panel with undo and redo, typing and styling
text, auto layout (adding it, gap and padding, sizing, drag to reorder),
constraints, resizing several layers and rotating one, components
@@ -104,7 +104,7 @@ service's transport implements), which also holds the stored file;
status, and peers, and `storeOutside()`, `reopen(name)`, and
`setReachable(bool)` replace the stored file, reopen a person, and take the
server down. `collaboration.browser.e2e.ts` covers edits and undo reaching
-the other person, remote pointers, selections and avatars, one person
+the other person, a dragged layer reaching them while it moves, remote pointers, selections and avatars, one person
storing the merged file, following someone's view, a file stored outside
the session, and an unreachable sync service.
diff --git a/apps/web/src/features/block-fig/browser-test/collaboration.browser.e2e.ts b/apps/web/src/features/block-fig/browser-test/collaboration.browser.e2e.ts
index d5b669555e..36900365b8 100644
--- a/apps/web/src/features/block-fig/browser-test/collaboration.browser.e2e.ts
+++ b/apps/web/src/features/block-fig/browser-test/collaboration.browser.e2e.ts
@@ -119,6 +119,44 @@ test('edits, selections, and pointers reach the other person', async ({
.toEqual(expect.arrayContaining([true, false]));
});
+test('the other person sees a layer move while it is dragged', async ({
+ page,
+}) => {
+ await open(page);
+ const alice = person(page, 'Alice').getByTestId('fig-canvas');
+ await drawRectangle(page, alice, [0.1, 0.72], [0.4, 0.8]);
+ await expect.poll(() => layersNamed(page, 1, 'Rectangle 1')).toBe(1);
+ /** Where a person's engine has the rectangle. */
+ const left = (who: number) =>
+ page.evaluate(async (index) => {
+ const engine = window.figFixture.collab?.people()[index]?.engine();
+ if (!engine) return undefined;
+ const row = (await engine.layers(0)).find(
+ (r) => r.name === 'Rectangle 1'
+ );
+ const [g] = row ? await engine.geometry(0, [row.id]) : [];
+ return g?.bounds.x;
+ }, who);
+ const start = (await left(1)) ?? 0;
+ // Alice drags it right (her canvas draws the move itself) and holds it.
+ const box = await alice.boundingBox();
+ if (!box) throw new Error('no canvas');
+ const y = box.y + box.height * 0.76;
+ await page.mouse.move(box.x + box.width * 0.25, y);
+ await page.mouse.down();
+ await page.mouse.move(box.x + box.width * 0.25 + 120, y, { steps: 8 });
+ // Bob sees it move before she lets go …
+ await expect.poll(() => left(1)).toBeGreaterThan(start + 10);
+ await page.mouse.up();
+ // … and they agree where it landed.
+ await expect
+ .poll(async () => {
+ const [a, b] = [await left(0), await left(1)];
+ return a !== undefined && a === b && a > start + 10;
+ })
+ .toBe(true);
+});
+
test('both people’s layers appear on both sides, and following shows the other view', async ({
page,
}) => {
diff --git a/apps/web/src/features/block-fig/browser-test/fig-viewer.browser.e2e.ts b/apps/web/src/features/block-fig/browser-test/fig-viewer.browser.e2e.ts
index c48d95d42c..646c4114cc 100644
--- a/apps/web/src/features/block-fig/browser-test/fig-viewer.browser.e2e.ts
+++ b/apps/web/src/features/block-fig/browser-test/fig-viewer.browser.e2e.ts
@@ -236,6 +236,139 @@ test('draws, moves, and saves shapes', async ({ page }) => {
.toEqual([]);
});
+/** The color of the tile canvas at a canvas point (CSS pixels). */
+function colorAt(page: Page, x: number, y: number) {
+ return page
+ .getByTestId('fig-canvas')
+ .locator('canvas')
+ .first()
+ .evaluate(
+ (node, [px, py]) => {
+ const c = node as HTMLCanvasElement;
+ const k = c.width / c.getBoundingClientRect().width;
+ const [r, g, b] = c
+ .getContext('2d')
+ ?.getImageData(Math.round(px * k), Math.round(py * k), 1, 1).data ?? [
+ 0, 0, 0,
+ ];
+ if (r > 200 && g < 60 && b < 60) return 'red';
+ if (b > 200 && r < 60 && g < 60) return 'blue';
+ return 'other';
+ },
+ [x, y]
+ );
+}
+
+/** Sets the selection's first fill. */
+async function fillSelection(page: Page, hex: string) {
+ await page.getByTestId('fig-fill-0-hex').fill(hex);
+ await page.getByTestId('fig-fill-0-hex').press('Enter');
+}
+
+test('moves layers lifted off the page, one edit at the drop', async ({
+ page,
+}) => {
+ await openNew(page);
+ const canvas = page.getByTestId('fig-canvas');
+ const fill = (hex: string) => fillSelection(page, hex);
+ // A red layer, then a blue one above it.
+ await canvas.focus();
+ await page.keyboard.press('r');
+ await dragOnCanvas(page, [100, 100], [200, 200]);
+ await fill('FF0000');
+ await canvas.focus();
+ await page.keyboard.press('r');
+ await dragOnCanvas(page, [300, 100], [400, 200]);
+ await fill('0000FF');
+ // The tile canvas at a canvas point (100% shows page units).
+ const color = (x: number, y: number) => colorAt(page, x, y);
+ const redX = async () => {
+ const engine = await page.evaluateHandle(() => window.figFixture.engine());
+ return engine.evaluate(async (e) => {
+ if (!e) return undefined;
+ const rows = await e.layers(0);
+ const red = rows.find((r) => r.name === 'Rectangle 1');
+ const [g] = red ? await e.geometry(0, [red.id]) : [];
+ return g?.bounds.x;
+ });
+ };
+
+ // Drag the red layer halfway under the blue one, still holding it.
+ const box = await canvas.boundingBox();
+ if (!box) throw new Error('The canvas is not visible.');
+ await page.mouse.move(box.x + 150, box.y + 150);
+ await page.mouse.down();
+ await page.mouse.move(box.x + 300, box.y + 150, { steps: 10 });
+ // The canvas shows it where it is now, under the blue layer, and nothing
+ // where it was …
+ await expect.poll(() => color(270, 150)).toBe('red');
+ await expect.poll(() => color(320, 150)).toBe('blue');
+ await expect.poll(() => color(120, 150)).toBe('other');
+ // … while the document has not moved it: nothing renders per step. The
+ // design panel shows where it will land.
+ expect(await redX()).toBe(100);
+ await expect(page.getByTestId('fig-field-x')).toHaveValue('250');
+ await page.mouse.up();
+ await expect(page.getByTestId('fig-field-x')).toHaveValue('250');
+ await expect.poll(redX).toBe(250);
+ await expect.poll(() => color(270, 150)).toBe('red');
+ await expect.poll(() => color(120, 150)).toBe('other');
+ // The move is one step.
+ await canvas.focus();
+ await page.keyboard.press('Control+z');
+ await expect(page.getByTestId('fig-field-x')).toHaveValue('100');
+ await expect.poll(() => color(120, 150)).toBe('red');
+ await expect
+ .poll(() => page.evaluate(() => window.figFixture.errors()))
+ .toEqual([]);
+});
+
+test('lands a drag let go before it started', async ({ page }) => {
+ await openNew(page);
+ const canvas = page.getByTestId('fig-canvas');
+ await canvas.focus();
+ await page.keyboard.press('r');
+ await dragOnCanvas(page, [100, 100], [200, 200]);
+ await fillSelection(page, 'FF0000');
+ const xs = async () => {
+ const engine = await page.evaluateHandle(() => window.figFixture.engine());
+ return engine.evaluate(async (e) => {
+ if (!e) return [];
+ const ids = (await e.layers(0)).map((r) => r.id);
+ const geometry = await e.geometry(0, ids);
+ return geometry.map((g) => g.bounds.x).sort((a, b) => a - b);
+ });
+ };
+ // ⌥-drag a copy and let go at once, all before the engine answers: the
+ // press is decided, and the copy made, after the pointer is up. The copy
+ // still lands, once.
+ await canvas.evaluate((host) => {
+ const r = host.getBoundingClientRect();
+ const at = (x: number, buttons: number) => ({
+ pointerId: 1,
+ pointerType: 'mouse',
+ isPrimary: true,
+ bubbles: true,
+ cancelable: true,
+ altKey: true,
+ clientX: r.left + x,
+ clientY: r.top + 150,
+ button: 0,
+ buttons,
+ });
+ host.dispatchEvent(new PointerEvent('pointerdown', at(150, 1)));
+ host.dispatchEvent(new PointerEvent('pointermove', at(300, 1)));
+ host.dispatchEvent(new PointerEvent('pointerup', at(300, 0)));
+ });
+ await expect.poll(xs).toEqual([100, 250]);
+ await expect(page.getByTestId('fig-field-x')).toHaveValue('250');
+ await expect.poll(() => colorAt(page, 270, 150)).toBe('red');
+ await expect.poll(() => colorAt(page, 120, 150)).toBe('red');
+ await expect
+ .poll(() => page.evaluate(() => window.figFixture.errors()))
+ .toEqual([]);
+});
+
test('undo and redo bring back the selection', async ({ page }) => {
await openNew(page);
const canvas = page.getByTestId('fig-canvas');
diff --git a/apps/web/src/features/block-fig/core/code-snippets.test.ts b/apps/web/src/features/block-fig/core/code-snippets.test.ts
index 9a2c8be830..18fb44a59d 100644
--- a/apps/web/src/features/block-fig/core/code-snippets.test.ts
+++ b/apps/web/src/features/block-fig/core/code-snippets.test.ts
@@ -9,6 +9,7 @@ const base: NodeInfo = {
typeLabel: 'Frame',
x: 0,
y: 0,
+ panelMove: [1, 0, 0, 1],
width: 320,
height: 200,
rotation: 0,
diff --git a/apps/web/src/features/block-fig/core/css.test.ts b/apps/web/src/features/block-fig/core/css.test.ts
index 461e1f0679..b83a254e43 100644
--- a/apps/web/src/features/block-fig/core/css.test.ts
+++ b/apps/web/src/features/block-fig/core/css.test.ts
@@ -9,6 +9,7 @@ const base: NodeInfo = {
typeLabel: 'Frame',
x: 0,
y: 0,
+ panelMove: [1, 0, 0, 1],
width: 320,
height: 200,
rotation: 0,
diff --git a/apps/web/src/features/block-fig/core/lift.test.ts b/apps/web/src/features/block-fig/core/lift.test.ts
new file mode 100644
index 0000000000..1bd182350d
--- /dev/null
+++ b/apps/web/src/features/block-fig/core/lift.test.ts
@@ -0,0 +1,113 @@
+import type { LiftPlan } from '@core/fig-engine/types';
+import { describe, expect, it } from 'vitest';
+import { liftParts, liftTiles, offsetRect, runTiles, tilePatch } from './lift';
+
+// At scale 1 a tile covers 512 page units; the view is 4×2 tiles.
+const camera = { x: 0, y: 0, zoom: 1 };
+const viewport = { w: 2048, h: 1024 };
+const ids = (tiles: { key: { ix: number; iy: number } }[]) =>
+ tiles.map((t) => `${t.key.ix},${t.key.iy}`).sort();
+
+const plan = (above: { x: number; y: number; w: number; h: number }[] = []) =>
+ ({
+ runs: [
+ {
+ ids: ['1:2'],
+ bounds: { x: 100, y: 100, w: 200, h: 200 },
+ origin: { x: 100, y: 100 },
+ clips: [],
+ above,
+ },
+ ],
+ }) satisfies LiftPlan;
+
+describe('lift', () => {
+ it('names the parts to render', () => {
+ const run = (ids: string[], x: number) => ({
+ ids,
+ bounds: { x, y: 0, w: 10, h: 10 },
+ origin: { x, y: 0 },
+ clips: [],
+ above: [],
+ });
+ const two: LiftPlan = { runs: [run(['1:1', '1:2'], 0), run(['1:5'], 40)] };
+ expect(liftParts(two)).toEqual({
+ page: { skip: ['1:1', '1:2', '1:5'] },
+ below: { before: '1:1' },
+ runs: [
+ { only: ['1:1', '1:2'], anchor: [0, 0] },
+ { only: ['1:5'], anchor: [40, 0] },
+ ],
+ above: [
+ { after: '1:2', before: '1:5' },
+ { after: '1:5', before: null },
+ ],
+ });
+ });
+
+ it('draws the tiles a run covers where it started and where it is', () => {
+ // Moved from tile 0,0 into tile 1,0.
+ const tiles = liftTiles(plan(), { x: 400, y: 0 }, camera, viewport, 1);
+ expect(ids(tiles)).toEqual(['0,0', '1,0']);
+ const home = tiles.find((t) => t.key.ix === 0);
+ const now = tiles.find((t) => t.key.ix === 1);
+ // Where it started its pixels must go; where it is the page shows below.
+ expect(home?.below).toBe(true);
+ expect(now?.below).toBe(false);
+ expect(now?.above).toEqual([]);
+ });
+
+ it('takes what paints above a run out of the page tiles under it', () => {
+ const above = [{ x: 600, y: 50, w: 100, h: 100 }];
+ const tiles = liftTiles(plan(above), { x: 400, y: 0 }, camera, viewport, 1);
+ const now = tiles.find((t) => t.key.ix === 1);
+ expect(now?.below).toBe(true);
+ expect(now?.above).toEqual([0]);
+ // Away from the run, what paints above it needs nothing drawn apart.
+ const still = liftTiles(plan(above), { x: 0, y: 0 }, camera, viewport, 1);
+ expect(ids(still)).toEqual(['0,0']);
+ });
+
+ it('renders the tiles of a run that show at its offset', () => {
+ const bounds = { x: 100, y: 100, w: 1200, h: 200 };
+ // Moved left by 900: only its right part (from 900 on) is in view.
+ expect(
+ runTiles(bounds, { x: -900, y: 0 }, camera, viewport, 1).map(
+ (k) => `${k.ix},${k.iy}`
+ )
+ ).toEqual(expect.arrayContaining(['1,0', '2,0']));
+ expect(
+ runTiles(bounds, { x: -900, y: 0 }, camera, viewport, 1).some(
+ (k) => k.ix === 0
+ )
+ ).toBe(false);
+ });
+
+ it('renders only the part of a tile that rectangles cover', () => {
+ // Scale 2: tile 0,0 covers page 0..256.
+ const key = { scale: 2, ix: 0, iy: 0 };
+ expect(tilePatch(key, [{ x: 10, y: 20, w: 30, h: 5 }])).toEqual({
+ x: 19,
+ y: 39,
+ w: 62,
+ h: 12,
+ });
+ // Two rectangles: what covers both; clamped to the tile.
+ expect(
+ tilePatch(key, [
+ { x: 10, y: 20, w: 30, h: 5 },
+ { x: 200, y: -50, w: 100, h: 100 },
+ ])
+ ).toEqual({ x: 19, y: 0, w: 493, h: 101 });
+ expect(tilePatch(key, [{ x: 300, y: 0, w: 10, h: 10 }])).toBeUndefined();
+ });
+
+ it('offsets rectangles', () => {
+ expect(offsetRect({ x: 1, y: 2, w: 3, h: 4 }, { x: 10, y: -2 })).toEqual({
+ x: 11,
+ y: 0,
+ w: 3,
+ h: 4,
+ });
+ });
+});
diff --git a/apps/web/src/features/block-fig/core/lift.ts b/apps/web/src/features/block-fig/core/lift.ts
new file mode 100644
index 0000000000..f724ade44c
--- /dev/null
+++ b/apps/web/src/features/block-fig/core/lift.ts
@@ -0,0 +1,151 @@
+/**
+ * Moving layers by drawing them apart from the rest of the page.
+ *
+ * A move drag does not render the page again at every pointer move. The
+ * engine's lift plan (`FigEngine.liftPlan`) splits the page into parts:
+ * what paints below the moving layers, each run of them, and what paints
+ * above each run. The parts render once, in tiles like the page, and every
+ * frame composites them with the runs shifted by the drag's offset; the move
+ * reaches the document once, at the drop.
+ *
+ * Only tiles a run covers, where it started or where it is now, are drawn
+ * from the parts; elsewhere the page's own tiles are right. Of those, a tile
+ * needs a render of the below part of its own only where a run started (its
+ * pixels must go) or where something paints above a run (that must come out
+ * from under the run); elsewhere the page's tile is the below part.
+ */
+
+import type { LayersSpec, LiftPlan, Rect } from '@core/fig-engine/types';
+import type { Camera, Point, Size } from './camera';
+import { TILE, type TileKey, tileRect, tilesFor, tileTouches } from './tiles';
+
+export function offsetRect(r: Rect, d: Point): Rect {
+ return { x: r.x + d.x, y: r.y + d.y, w: r.w, h: r.h };
+}
+
+function touchesAny(key: TileKey, rects: Rect[]): boolean {
+ return rects.some((r) => tileTouches(key, r));
+}
+
+/** The renders of a lift's parts. */
+export interface LiftParts {
+ /**
+ * The page without the moving layers, for page tiles rendered while they
+ * are dragged (the document may move them meanwhile).
+ */
+ page: LayersSpec;
+ /** What paints below the first run, on the page color. */
+ below: LayersSpec;
+ /** Each run by itself. */
+ runs: LayersSpec[];
+ /** What paints after each run and before the next one. */
+ above: LayersSpec[];
+}
+
+export function liftParts(plan: LiftPlan): LiftParts {
+ const runs = plan.runs;
+ return {
+ page: { skip: runs.flatMap((r) => r.ids) },
+ below: { before: runs[0].ids[0] },
+ // Anchored: the document may move the layers while they are dragged.
+ runs: runs.map((r) => ({ only: r.ids, anchor: [r.origin.x, r.origin.y] })),
+ above: runs.map((r, k) => ({
+ after: r.ids[r.ids.length - 1],
+ before: runs[k + 1]?.ids[0] ?? null,
+ })),
+ };
+}
+
+/** A tile of the view drawn from a lift's parts. */
+export interface LiftTile {
+ key: TileKey;
+ /** Drawn from a render of the below part (else the page's tile). */
+ below: boolean;
+ /** The runs whose above part it draws. */
+ above: number[];
+}
+
+/**
+ * The view's tiles at `scale` drawn from a lift's parts: those a run covers
+ * where it started or where it is now, moved by `offset`.
+ */
+export function liftTiles(
+ plan: LiftPlan,
+ offset: Point,
+ camera: Camera,
+ viewport: Size,
+ scale: number
+): LiftTile[] {
+ const started = plan.runs.map((r) => r.bounds);
+ const now = started.map((b) => offsetRect(b, offset));
+ const tiles: LiftTile[] = [];
+ for (const key of tilesFor(camera, viewport, scale, 0)) {
+ const home = touchesAny(key, started);
+ if (!home && !touchesAny(key, now)) continue;
+ const above = plan.runs.flatMap((r, k) =>
+ touchesAny(key, r.above) ? [k] : []
+ );
+ tiles.push({ key, below: home || above.length > 0, above });
+ }
+ return tiles;
+}
+
+/**
+ * The tiles of a run's own render, at the place it started, that show in the
+ * view while it is moved by `offset`.
+ */
+export function runTiles(
+ bounds: Rect,
+ offset: Point,
+ camera: Camera,
+ viewport: Size,
+ scale: number
+): TileKey[] {
+ // Moved back by the offset, the view covers what of the run shows.
+ const back = { ...camera, x: camera.x - offset.x, y: camera.y - offset.y };
+ return tilesFor(back, viewport, scale, 0).filter((key) =>
+ tileTouches(key, bounds)
+ );
+}
+
+/** Device pixels of a tile, from its top-left corner. */
+export interface Patch {
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+}
+
+/**
+ * The part of a tile that page rectangles cover, in its device pixels, with
+ * a pixel to spare for anti-aliasing; `undefined` when they miss it. A
+ * lift's parts differ from the page only there, so only that much renders.
+ */
+export function tilePatch(key: TileKey, rects: Rect[]): Patch | undefined {
+ const t = tileRect(key);
+ let x0 = Number.POSITIVE_INFINITY;
+ let y0 = Number.POSITIVE_INFINITY;
+ let x1 = Number.NEGATIVE_INFINITY;
+ let y1 = Number.NEGATIVE_INFINITY;
+ for (const r of rects) {
+ const ax = Math.max(r.x, t.x);
+ const ay = Math.max(r.y, t.y);
+ const bx = Math.min(r.x + r.w, t.x + t.w);
+ const by = Math.min(r.y + r.h, t.y + t.h);
+ if (bx <= ax || by <= ay) continue;
+ x0 = Math.min(x0, ax);
+ y0 = Math.min(y0, ay);
+ x1 = Math.max(x1, bx);
+ y1 = Math.max(y1, by);
+ }
+ if (x0 > x1) return undefined;
+ // Outward to whole device pixels, then one more, within the tile.
+ const start = (v: number) => Math.max(0, Math.floor(v * key.scale) - 1);
+ const end = (v: number) => Math.min(TILE, Math.ceil(v * key.scale) + 1);
+ const left = start(x0 - t.x);
+ const top = start(y0 - t.y);
+ const right = end(x1 - t.x);
+ const bottom = end(y1 - t.y);
+ if (right <= left || bottom <= top) return undefined;
+ return { x: left, y: top, w: right - left, h: bottom - top };
+}
diff --git a/apps/web/src/features/block-fig/primitives/create-fig-editor.ts b/apps/web/src/features/block-fig/primitives/create-fig-editor.ts
index 52da9f6800..703546e8a3 100644
--- a/apps/web/src/features/block-fig/primitives/create-fig-editor.ts
+++ b/apps/web/src/features/block-fig/primitives/create-fig-editor.ts
@@ -398,13 +398,15 @@ export function createFigEditor(options: FigEditorOptions) {
viewer.select(wanted.filter((s) => alive.has(s.id)));
};
- const settle = async (result: EditResult) => {
+ /** After a step: undo state, saving, and (unless `quiet`) the view. */
+ const settle = async (result: EditResult, quiet = false) => {
setCanUndo(result.canUndo);
setCanRedo(result.canRedo);
undoStep = result.undoStep;
redoStep = result.redoStep;
- if (result.dirty) options.onDirty(result.dirty);
scheduleSave();
+ if (quiet) return;
+ if (result.dirty) options.onDirty(result.dirty);
await viewer.afterEdit();
};
@@ -457,8 +459,10 @@ export function createFigEditor(options: FigEditorOptions) {
/**
* Applies operations as one undo step, in order with other edits.
* Resolves to the engine's result (undefined when not allowed or failed).
+ * `quiet` steps re-render and reload nothing (a lifted move's steps,
+ * which the canvas draws itself).
*/
- const apply = (ops: Op[], coalesce?: string) => {
+ const apply = (ops: Op[], coalesce?: string, quiet = false) => {
if (!enabled() || ops.length === 0) return Promise.resolve(undefined);
const run = queue.then(async () => {
try {
@@ -478,7 +482,7 @@ export function createFigEditor(options: FigEditorOptions) {
}
if (coalesce) pushSoon();
else await pushNow();
- await settle(result);
+ await settle(result, quiet);
return result;
} catch (e) {
options.notifyError(e instanceof Error ? e.message : String(e));
@@ -1031,6 +1035,49 @@ export function createFigEditor(options: FigEditorOptions) {
};
};
+ /**
+ * A move the canvas draws itself while the layers are dragged (a lifted
+ * move). In a shared design the document follows at gesture pace,
+ * quietly, so other people see the layers move; the drop applies the
+ * rest, all one undo step. `end` resolves to how far the document moved
+ * them.
+ */
+ const startLiftedMove = (targets: string[]) => {
+ const key = `drag-${++dragKey}`;
+ let wanted = { x: 0, y: 0 };
+ let applied = { x: 0, y: 0 };
+ let moved = { x: 0, y: 0 };
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const step = async (last: boolean) => {
+ const dx = wanted.x - applied.x;
+ const dy = wanted.y - applied.y;
+ applied = { ...wanted };
+ const ops: Op[] =
+ dx !== 0 || dy !== 0 ? [{ op: 'translate', ids: targets, dx, dy }] : [];
+ // Settle what was dragged into its auto layout slot.
+ if (last && (applied.x !== 0 || applied.y !== 0))
+ ops.push({ op: 'reflow', ids: targets });
+ const result = await apply(ops, key, !last);
+ if (result) moved = { x: moved.x + dx, y: moved.y + dy };
+ };
+ return {
+ to(dx: number, dy: number) {
+ wanted = { x: dx, y: dy };
+ if (!sharing || timer) return;
+ timer = setTimeout(() => {
+ timer = undefined;
+ void step(false);
+ }, GESTURE_PUSH_MS);
+ },
+ async end() {
+ clearTimeout(timer);
+ timer = undefined;
+ await step(true);
+ return moved;
+ },
+ };
+ };
+
/** A resize drag on one layer: new page bounds → position and size. */
/**
* A resize drag on the selection: each layer scales with the selection's
@@ -1292,6 +1339,7 @@ export function createFigEditor(options: FigEditorOptions) {
importLibrary,
viewCenter,
startMove,
+ startLiftedMove,
startResize,
startRotate,
create,
diff --git a/apps/web/src/features/block-fig/primitives/create-fig-libraries.ts b/apps/web/src/features/block-fig/primitives/create-fig-libraries.ts
index 8e0b918639..31530b2aab 100644
--- a/apps/web/src/features/block-fig/primitives/create-fig-libraries.ts
+++ b/apps/web/src/features/block-fig/primitives/create-fig-libraries.ts
@@ -96,7 +96,8 @@ export function createFigLibraries(options: {
// A file that fails to answer keeps the last state.
}
};
- createEffect(on(viewer.editVersion, () => void loadState()));
+ // File-wide, so once per pause in a stream of edits (a drag's steps).
+ createEffect(on(viewer.editsSettled, () => void loadState()));
/** Starts (or stops) keeping the publishing state current. */
const watchStatus = (on: boolean) => {
diff --git a/apps/web/src/features/block-fig/primitives/create-tile-compositor.ts b/apps/web/src/features/block-fig/primitives/create-tile-compositor.ts
index ee6eb0f431..8bc85db4cc 100644
--- a/apps/web/src/features/block-fig/primitives/create-tile-compositor.ts
+++ b/apps/web/src/features/block-fig/primitives/create-tile-compositor.ts
@@ -20,6 +20,11 @@
* are re-rendered at once; the margin's and the overview's wait until
* edits pause (and then behind the view's), so a drag does not re-render
* the whole page at every step.
+ * - Layers being moved are lifted (`core/lift`): what paints below them,
+ * the layers, and what paints above them render once, in tiles, and each
+ * frame composites them with the layers shifted, so a move drag renders
+ * nothing per step. After the drop the lift stays up until the page's
+ * tiles have caught up with the move.
*/
import type {
@@ -27,8 +32,16 @@ import type {
PendingTile,
TileResult,
} from '@core/fig-engine/client';
-import type { Rect } from '@core/fig-engine/types';
-import type { Camera, Size } from '../core/camera';
+import type { LiftPlan, Rect } from '@core/fig-engine/types';
+import type { Camera, Point, Size } from '../core/camera';
+import {
+ liftParts,
+ liftTiles,
+ offsetRect,
+ type Patch,
+ runTiles,
+ tilePatch,
+} from '../core/lift';
import {
quantizeScale,
TILE,
@@ -48,6 +61,10 @@ interface Entry {
stale?: boolean;
lastUsed: number;
pinned: boolean;
+ /** For a lift's part: the part of the tile it renders (else all). */
+ patch?: Patch;
+ /** The edits (see `edits`) its bitmap shows. */
+ version?: number;
}
export interface TileCompositorOptions {
@@ -77,11 +94,46 @@ export interface PageState {
/** Longest side of the overview, in device pixels. */
const OVERVIEW_SIDE = 2048;
/** Quiet time after which the view counts as settled. */
-const SETTLE_MS = 140;
+const SETTLE_MS = 100;
/** Quiet time after an edit before tiles out of view are re-rendered. */
const EDIT_SETTLE_MS = 300;
/** Priority of overview tiles changed by an edit: after every view tile. */
const AFTER_VIEW = Number.MAX_SAFE_INTEGER;
+/**
+ * Priorities of a lift's tiles, ahead of the view's: the moving layers
+ * first, then what goes where they started, then the rest.
+ */
+const LIFT_RUN = -3_000_000;
+const LIFT_HOME = -2_000_000;
+const LIFT_OTHER = -1_000_000;
+/** Longest a landed lift waits for the page's tiles before dropping. */
+const LAND_TIMEOUT_MS = 2000;
+
+/** A part of a lift (see `core/lift`): its renders and their tiles. */
+interface LiftPart {
+ /** `LayersSpec` JSON. */
+ layers: string;
+ tiles: Map<string, Entry>;
+}
+
+interface Lift {
+ plan: LiftPlan;
+ /** Page tiles rendered while it is dragged leave the layers out. */
+ page: string;
+ /** Scale (device pixels per page unit) of every part's tiles. */
+ scale: number;
+ below: LiftPart;
+ runs: { part: LiftPart; clips: Path2D[] }[];
+ above: LiftPart[];
+ /** Page-space offset of the moving layers. */
+ offset: Point;
+ /** Rendering ahead of a drag that has not started: not drawn yet. */
+ hidden: boolean;
+ /** Dropped: the parts stay as they are until the lift comes down. */
+ frozen: boolean;
+ /** The move reached the document (the area it changed). */
+ landing?: { dirty: Rect; since: number };
+}
export function createTileCompositor(options: TileCompositorOptions) {
const { engine } = options;
@@ -96,6 +148,13 @@ export function createTileCompositor(options: TileCompositorOptions) {
let lastZoom = 0;
let zoomChangedAt = 0;
let disposed = false;
+ /** Edits seen (invalidations), to order renders of one tile. */
+ let edits = 0;
+ /** The layers being dragged (or about to be). */
+ let lift: Lift | undefined;
+ /** Dropped lifts, oldest first, drawn until the page shows their moves. */
+ const landing: Lift[] = [];
+ let landTimer: ReturnType<typeof setTimeout> | undefined;
/** Render timings, for diagnostics. */
const stats = { rendered: 0, millis: 0 };
@@ -109,56 +168,75 @@ export function createTileCompositor(options: TileCompositorOptions) {
}
};
- const render = (entry: Entry, priority: number) => {
+ /**
+ * What page tiles leave out: the layers of the lift being dragged, which
+ * the document may move before the drop (it shares the drag with other
+ * people as it goes) while the lift draws them.
+ */
+ const pageLayers = () =>
+ lift && !lift.hidden && !lift.frozen ? lift.page : undefined;
+
+ /** Renders a tile of the page, or of a lift's part into its tiles. */
+ const render = (entry: Entry, priority: number, part?: LiftPart) => {
if (!page) return;
- const rect = tileRect(entry.key);
+ const home = part?.tiles ?? cache;
+ const { key } = entry;
+ const patch = entry.patch ?? { x: 0, y: 0, w: TILE, h: TILE };
const pending = engine.render({
page: page.page,
- x: rect.x,
- y: rect.y,
- scale: entry.key.scale,
- width: TILE,
- height: TILE,
+ x: (key.ix * TILE + patch.x) / key.scale,
+ y: (key.iy * TILE + patch.y) / key.scale,
+ scale: key.scale,
+ width: patch.w,
+ height: patch.h,
outline: page.outline,
priority,
+ layers: part?.layers ?? pageLayers(),
});
entry.pending = pending;
const id = tileId(entry.key);
const forGeneration = generation;
+ const version = edits;
+ /** A render that brought nothing: the entry goes if it holds nothing. */
+ const settleEmpty = () => {
+ if (entry.pending?.id === pending.id) entry.pending = undefined;
+ if (!entry.bitmap && !entry.pending && home.get(id) === entry)
+ home.delete(id);
+ };
pending.promise
.then((result: TileResult | null) => {
- // Superseded (an edit re-requested it) or cancelled.
+ // Cancelled, or (when not current) superseded by an edit.
const current = entry.pending?.id === pending.id;
if (!result) {
- if (current) {
- entry.pending = undefined;
- if (!entry.bitmap && cache.get(id) === entry) cache.delete(id);
- }
+ settleEmpty();
return;
}
if (
disposed ||
- !current ||
forGeneration !== generation ||
- cache.get(id) !== entry
+ home.get(id) !== entry ||
+ version < (entry.version ?? -1)
) {
result.bitmap.close();
return;
}
+ // A render an edit superseded mid-way is still newer than what
+ // shows: it shows until the current one lands, so a stream of
+ // edits (a drag the canvas cannot lift) updates as fast as tiles
+ // render instead of waiting for the edits to pause.
entry.bitmap?.close();
entry.bitmap = result.bitmap;
- entry.pending = undefined;
- entry.stale = false;
- stats.rendered++;
- stats.millis += result.millis;
- evict();
+ entry.version = version;
+ if (current) {
+ entry.pending = undefined;
+ entry.stale = false;
+ stats.rendered++;
+ stats.millis += result.millis;
+ }
+ if (!part) evict();
options.onTile();
})
- .catch(() => {
- if (entry.pending?.id !== pending.id) return;
- entry.pending = undefined;
- if (!entry.bitmap && cache.get(id) === entry) cache.delete(id);
- });
+ .catch(settleEmpty);
};
const request = (key: TileKey, priority: number, pinned = false) => {
@@ -255,11 +333,231 @@ export function createTileCompositor(options: TileCompositorOptions) {
}, EDIT_SETTLE_MS);
};
+ // ---- lifting --------------------------------------------------------
+
+ /** Requests a tile of a lift's part unless it has a current one. */
+ const requestPart = (
+ part: LiftPart,
+ key: TileKey,
+ priority: number,
+ patch: Patch | undefined
+ ) => {
+ if (!patch) return;
+ const id = tileId(key);
+ const existing = part.tiles.get(id);
+ if (existing) {
+ if (existing.stale && !existing.pending) render(existing, priority, part);
+ return;
+ }
+ const entry: Entry = { key, lastUsed: ++clock, pinned: false, patch };
+ part.tiles.set(id, entry);
+ render(entry, priority, part);
+ };
+
+ /** Requests what the lift needs to draw the view at its offset. */
+ const requestLift = () => {
+ const view = lastView;
+ if (!lift || lift.frozen || !view) return;
+ const { camera, viewport } = view;
+ const { plan, scale, offset, below, runs, above } = lift;
+ // The below part differs from the page where the runs started and
+ // under what paints above them.
+ const homes = plan.runs.map((r) => r.bounds);
+ const changed = [...homes, ...plan.runs.flatMap((r) => r.above)];
+ let n = 0;
+ plan.runs.forEach((run, k) => {
+ for (const key of runTiles(run.bounds, offset, camera, viewport, scale))
+ requestPart(
+ runs[k].part,
+ key,
+ LIFT_RUN + n++,
+ tilePatch(key, [run.bounds])
+ );
+ });
+ for (const t of liftTiles(plan, offset, camera, viewport, scale)) {
+ const home = homes.some((b) => tileTouches(t.key, b));
+ const priority = (home ? LIFT_HOME : LIFT_OTHER) + n++;
+ if (t.below)
+ requestPart(below, t.key, priority, tilePatch(t.key, changed));
+ for (const k of t.above)
+ requestPart(
+ above[k],
+ t.key,
+ priority,
+ tilePatch(t.key, plan.runs[k].above)
+ );
+ }
+ };
+
+ const partsOf = (l: Lift) => [
+ l.below,
+ ...l.runs.map((r) => r.part),
+ ...l.above,
+ ];
+
+ /** Frees a lift's tiles. */
+ const discard = (l: Lift) => {
+ const cancel: number[] = [];
+ for (const part of partsOf(l)) {
+ for (const e of part.tiles.values()) {
+ e.bitmap?.close();
+ if (e.pending) cancel.push(e.pending.id);
+ }
+ part.tiles.clear();
+ }
+ engine.cancel(cancel);
+ };
+
+ /** Takes down the lift being dragged (dropped ones land on their own). */
+ const unlift = () => {
+ if (lift) discard(lift);
+ lift = undefined;
+ };
+
+ /**
+ * A lift starts being drawn: page renders queued before it would draw the
+ * layers wherever the document has them when they run, so they are
+ * queued again, leaving the layers out (`pageLayers`).
+ */
+ const holdPage = () => {
+ const cancel: number[] = [];
+ for (const [id, e] of cache) {
+ if (!e.pending) continue;
+ cancel.push(e.pending.id);
+ e.pending = undefined;
+ if (!e.bitmap) cache.delete(id);
+ }
+ engine.cancel(cancel);
+ if (lastView) schedule(lastView, false);
+ };
+
+ /**
+ * Whether the page's tiles in view show a dropped lift's move: those it
+ * changed are rendered again (or the wait ran out).
+ */
+ const landed = (l: Lift, view: ViewState): boolean => {
+ const done = l.landing;
+ if (!done) return false;
+ if (performance.now() - done.since > LAND_TIMEOUT_MS) return true;
+ const target = quantizeScale(view.camera.zoom * view.dpr);
+ return tilesFor(view.camera, view.viewport, target, 0).every((key) => {
+ if (!tileTouches(key, done.dirty)) return true;
+ if (page?.content && !tileTouches(key, page.content)) return true;
+ const e = cache.get(tileId(key));
+ return !!e?.bitmap && !e.stale && !e.pending;
+ });
+ };
+
+ /** Draws a lift over the page's tiles (see `core/lift`). */
+ const drawLift = (
+ ctx: CanvasRenderingContext2D,
+ view: ViewState,
+ l: Lift
+ ) => {
+ if (l.hidden) return;
+ const { camera, dpr } = view;
+ const { plan, scale, offset } = l;
+ const unit = camera.zoom * dpr;
+ // At the view's own scale, tiles land on whole device pixels.
+ const exact = scale === quantizeScale(unit);
+ const k = exact ? scale : unit;
+ const ox = exact ? Math.round(camera.x * scale) : camera.x * unit;
+ const oy = exact ? Math.round(camera.y * scale) : camera.y * unit;
+ // Device pixels of a tile's own pixel at the view's scale.
+ const px = exact ? 1 : unit / scale;
+ const blit = (e: Entry | undefined, dx = 0, dy = 0) => {
+ if (!e?.bitmap) return;
+ const patch = e.patch ?? { x: 0, y: 0, w: TILE, h: TILE };
+ const x = e.key.ix * TILE * px - ox + patch.x * px + dx;
+ const y = e.key.iy * TILE * px - oy + patch.y * px + dy;
+ ctx.drawImage(e.bitmap, x, y, patch.w * px, patch.h * px);
+ };
+ const tile = (part: LiftPart, key: TileKey) => part.tiles.get(tileId(key));
+ ctx.imageSmoothingEnabled = !exact;
+ ctx.imageSmoothingQuality = 'low';
+ // A tile is drawn from the parts once every part it needs is in; until
+ // then the page's tile stays, with the layers drawn over it.
+ const tiles = liftTiles(plan, offset, camera, view.viewport, scale).filter(
+ (t) =>
+ (!t.below || !!tile(l.below, t.key)?.bitmap) &&
+ t.above.every((a) => !!tile(l.above[a], t.key)?.bitmap)
+ );
+ for (const t of tiles) if (t.below) blit(tile(l.below, t.key));
+ const dx = exact ? Math.round(offset.x * scale) : offset.x * unit;
+ const dy = exact ? Math.round(offset.y * scale) : offset.y * unit;
+ l.runs.forEach((run, index) => {
+ ctx.save();
+ if (run.clips.length > 0) {
+ // The clipping ancestors stay where they are.
+ ctx.setTransform(k, 0, 0, k, -ox, -oy);
+ for (const clip of run.clips) ctx.clip(clip);
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ }
+ for (const e of run.part.tiles.values()) blit(e, dx, dy);
+ ctx.restore();
+ for (const t of tiles) {
+ if (t.above.includes(index)) blit(tile(l.above[index], t.key));
+ }
+ });
+ };
+
+ /**
+ * Marks tiles touching a page rectangle as changed: visible ones are
+ * re-rendered (the old pixels stay up meanwhile), others dropped.
+ */
+ const invalidate = (rect: Rect) => {
+ edits++;
+ const cancel: number[] = [];
+ for (const [id, e] of cache) {
+ if (!tileTouches(e.key, rect)) continue;
+ if (!e.bitmap && !e.pending) {
+ cache.delete(id);
+ continue;
+ }
+ // Renders already under way still land (see `render`); the rest
+ // are dropped, and the tile renders again.
+ if (e.pending) {
+ cancel.push(e.pending.id);
+ e.pending = undefined;
+ }
+ e.stale = true;
+ }
+ engine.cancel(cancel);
+ if (page && page.content) {
+ // Edits can grow the page's content beyond its old bounds.
+ page = { ...page, content: unionContent(page.content, rect) };
+ }
+ // Other people's edits during a drag reach the lift's parts (its runs
+ // render anchored where the lift started, the rest leaves them out). A
+ // dropped lift keeps its parts: the move itself is what changed.
+ if (lift && !lift.frozen) {
+ const cancelParts: number[] = [];
+ for (const part of partsOf(lift)) {
+ for (const [id, e] of part.tiles) {
+ if (!tileTouches(e.key, rect)) continue;
+ // As for the page's tiles: a render under way still lands, but
+ // the part renders again.
+ if (e.pending) {
+ cancelParts.push(e.pending.id);
+ e.pending = undefined;
+ }
+ if (e.bitmap) e.stale = true;
+ else part.tiles.delete(id);
+ }
+ }
+ engine.cancel(cancelParts);
+ requestLift();
+ }
+ afterEdit();
+ };
+
return {
stats,
/** Starts over for a page (or after switching outline view). */
setPage(next: PageState) {
+ unlift();
+ for (const l of landing.splice(0)) discard(l);
for (const e of cache.values()) {
e.bitmap?.close();
if (e.pending) engine.cancel([e.pending.id]);
@@ -271,28 +569,7 @@ export function createTileCompositor(options: TileCompositorOptions) {
if (lastView) schedule(lastView, true);
},
- /**
- * Marks tiles touching a page rectangle as changed: visible ones are
- * re-rendered (the old pixels stay up meanwhile), others dropped.
- */
- invalidate(rect: Rect) {
- const cancel: number[] = [];
- for (const [id, e] of cache) {
- if (!tileTouches(e.key, rect)) continue;
- if (e.pending) {
- cancel.push(e.pending.id);
- e.pending = undefined;
- }
- if (e.bitmap) e.stale = true;
- else cache.delete(id);
- }
- engine.cancel(cancel);
- if (page && page.content) {
- // Edits can grow the page's content beyond its old bounds.
- page = { ...page, content: unionContent(page.content, rect) };
- }
- afterEdit();
- },
+ invalidate,
/** New content bounds for the same page (after edits). */
setContent(content: Rect | undefined) {
@@ -303,6 +580,7 @@ export function createTileCompositor(options: TileCompositorOptions) {
/** Call on every view change; fetches what the view needs. */
update(view: ViewState) {
lastView = view;
+ requestLift();
schedule(view, false);
clearTimeout(settleTimer);
settleTimer = setTimeout(() => {
@@ -312,6 +590,11 @@ export function createTileCompositor(options: TileCompositorOptions) {
/** Draws the view from the tiles available now. */
draw(ctx: CanvasRenderingContext2D, view: ViewState) {
+ // In order: a newer drop may sit on top of an older one's parts.
+ while (landing.length > 0 && landed(landing[0], view)) {
+ const done = landing.shift();
+ if (done) discard(done);
+ }
const { camera, dpr } = view;
const width = ctx.canvas.width;
const height = ctx.canvas.height;
@@ -398,8 +681,111 @@ export function createTileCompositor(options: TileCompositorOptions) {
ctx.imageSmoothingQuality = d.size > TILE ? 'low' : 'high';
ctx.drawImage(d.bitmap, d.x, d.y, d.size, d.size);
}
+ for (const l of landing) drawLift(ctx, view, l);
+ if (lift) drawLift(ctx, view, lift);
},
+ /**
+ * Lifts layers to move them (see `core/lift`) at the view's scale;
+ * `hidden` renders the parts ahead of a drag without drawing them yet.
+ * Returns whether the lift is up (its clips must parse).
+ */
+ lift(plan: LiftPlan, hidden = false): boolean {
+ unlift();
+ const view = lastView;
+ if (!page || !view || plan.refused || plan.runs.length === 0)
+ return false;
+ let clips: Path2D[][];
+ try {
+ clips = plan.runs.map((r) => r.clips.map((d) => new Path2D(d)));
+ } catch {
+ return false;
+ }
+ const parts = liftParts(plan);
+ const part = (layers: unknown): LiftPart => ({
+ layers: JSON.stringify(layers),
+ tiles: new Map(),
+ });
+ lift = {
+ plan,
+ page: JSON.stringify(parts.page),
+ scale: quantizeScale(view.camera.zoom * view.dpr),
+ below: part(parts.below),
+ runs: parts.runs.map((r, k) => ({ part: part(r), clips: clips[k] })),
+ above: parts.above.map(part),
+ offset: { x: 0, y: 0 },
+ hidden,
+ frozen: false,
+ };
+ requestLift();
+ if (!hidden) holdPage();
+ return true;
+ },
+
+ /** Draws a hidden lift from now on; whether there was one. */
+ showLift(): boolean {
+ if (!lift) return false;
+ if (lift.hidden) {
+ lift.hidden = false;
+ holdPage();
+ }
+ return true;
+ },
+
+ /** Moves the lifted layers to a page-space offset from where they were. */
+ moveLift(offset: Point) {
+ if (!lift || lift.frozen) return;
+ lift.offset = offset;
+ requestLift();
+ },
+
+ /** The drop: the lift's parts stay as they are from now on. */
+ freezeLift() {
+ if (lift) lift.frozen = true;
+ },
+
+ /**
+ * The drop reached the document, which moved the layers by `moved`
+ * (`null`: not at all). Page tiles drawn meanwhile left the layers out,
+ * so the page renders again where they were and where they are, and
+ * the lift comes down once it shows them.
+ */
+ landLift(moved: Point | null) {
+ const dropped = lift;
+ lift = undefined;
+ if (!dropped) return;
+ if (dropped.hidden) {
+ // Never drawn, so the page tiles are as they were.
+ discard(dropped);
+ return;
+ }
+ dropped.frozen = true;
+ const home = dropped.plan.runs
+ .map((r) => r.bounds)
+ .reduce((a, b) => unionContent(a, b));
+ const there =
+ moved && (moved.x !== 0 || moved.y !== 0)
+ ? offsetRect(home, moved)
+ : undefined;
+ dropped.landing = {
+ dirty: there ? unionContent(home, there) : home,
+ since: performance.now(),
+ };
+ landing.push(dropped);
+ invalidate(home);
+ if (there) invalidate(there);
+ clearTimeout(landTimer);
+ landTimer = setTimeout(() => {
+ if (!disposed) options.onTile();
+ }, LAND_TIMEOUT_MS + 50);
+ },
+
+ /** Takes down the lift being dragged. */
+ unlift,
+
+ /** Whether the lift being dragged (or prepared) is `plan`'s. */
+ isLifted: (plan: LiftPlan) => lift?.plan === plan,
+
/** Whether every tile the settled view needs is drawn sharp. */
isSharp(view: ViewState): boolean {
const target = quantizeScale(view.camera.zoom * view.dpr);
@@ -411,6 +797,9 @@ export function createTileCompositor(options: TileCompositorOptions) {
dispose() {
disposed = true;
+ unlift();
+ for (const l of landing.splice(0)) discard(l);
+ clearTimeout(landTimer);
clearTimeout(settleTimer);
clearTimeout(editTimer);
for (const e of cache.values()) {
diff --git a/apps/web/src/features/block-fig/views/assets-panel.tsx b/apps/web/src/features/block-fig/views/assets-panel.tsx
index b89216ed4b..a02411e732 100644
--- a/apps/web/src/features/block-fig/views/assets-panel.tsx
+++ b/apps/web/src/features/block-fig/views/assets-panel.tsx
@@ -24,8 +24,9 @@ export function AssetsPanel(props: {
fileName?: string;
}) {
const [query, setQuery] = createSignal('');
+ // File-wide, so once per pause in a stream of edits (a drag's steps).
const [components] = createResource(
- () => props.viewer.editVersion(),
+ () => props.viewer.editsSettled(),
() => props.engine.components(),
{ initialValue: [] }
);
diff --git a/apps/web/src/features/block-fig/views/fig-viewer.tsx b/apps/web/src/features/block-fig/views/fig-viewer.tsx
index 8423ccb839..40c918a8a2 100644
--- a/apps/web/src/features/block-fig/views/fig-viewer.tsx
+++ b/apps/web/src/features/block-fig/views/fig-viewer.tsx
@@ -248,9 +248,40 @@ export function FigViewer() {
})();
};
+ /**
+ * A lifted move being dragged (the canvas draws it) and the layer's values
+ * from before it, which the panel shows moved along until the values
+ * after the drop arrive (`until`: the values shown at the drop).
+ */
+ const [moving, setMoving] = createSignal<{
+ offset: Point;
+ from: NodeInfo | undefined;
+ until?: NodeInfo;
+ }>();
+ const onMove = (offset: Point | undefined) => {
+ const m = moving();
+ if (offset) setMoving({ offset, from: m && !m.until ? m.from : info() });
+ else if (m) setMoving({ ...m, until: info() });
+ };
+ const movedInfo = () => {
+ const i = info();
+ const m = moving();
+ const from = m?.from;
+ if (!i || !m || !from || viewer.selected().length !== 1) return i;
+ if (i.id !== from.id || (m.until && i !== m.until)) return i;
+ const [a, b, c, d] = from.panelMove;
+ const { x: dx, y: dy } = m.offset;
+ return {
+ ...i,
+ x: from.x + a * dx + b * dy,
+ y: from.y + c * dx + d * dy,
+ bounds: { ...from.bounds, x: from.bounds.x + dx, y: from.bounds.y + dy },
+ };
+ };
+
/** The design panel's layer, showing the text editor's selection. */
const panel = () => {
- const i = info();
+ const i = movedInfo();
const range = editor.textSelection();
if (!i?.text || !range || range.id !== i.id || range.start === range.end)
return { info: i, mixed: undefined };
@@ -939,6 +970,7 @@ export function FigViewer() {
}}
peers={peerOverlays}
onPointer={collab ? setPointer : undefined}
+ onMove={onMove}
aids={aids}
devMode={devMode}
drop={
diff --git a/apps/web/src/features/block-fig/views/viewer-canvas.tsx b/apps/web/src/features/block-fig/views/viewer-canvas.tsx
index 027096d95e..996369baea 100644
--- a/apps/web/src/features/block-fig/views/viewer-canvas.tsx
+++ b/apps/web/src/features/block-fig/views/viewer-canvas.tsx
@@ -2,7 +2,9 @@
* The canvas: rendered tiles underneath, the canvas UI on top, and Figma's
* pointer gestures (scroll to pan, ⌘/Ctrl+scroll or pinch to zoom, Space or
* middle-drag to pan, click and marquee to select). When the file is
- * editable: drag to move (⌥ to copy, ⇧ to constrain), the selection's
+ * editable: drag to move (⌥ to copy, ⇧ to constrain; the moving layers are
+ * lifted off the page and shifted each frame when the engine allows, see
+ * `core/lift`), the selection's
* handles to resize, the shape tools to draw, the pen to draw paths (click
* for corners, drag for curves, click the first point to close), and,
* while a layer's points are edited, drag its points and handles (⌥ breaks
@@ -11,7 +13,13 @@
import { IS_MAC } from '@core/constant/isMac';
import type { FigEngine } from '@core/fig-engine/client';
-import type { LayerRow, NodeInfo, Rect } from '@core/fig-engine/types';
+import type {
+ LayerRow,
+ LiftPlan,
+ NodeGeometry,
+ NodeInfo,
+ Rect,
+} from '@core/fig-engine/types';
import {
createEffect,
createMemo,
@@ -29,6 +37,7 @@ import {
import { PeerCursors } from '../components/peer-presence';
import { type Point, screenToPage } from '../core/camera';
import { type SnapLines, snapResize } from '../core/layout-grid';
+import { offsetRect } from '../core/lift';
import { measure } from '../core/measure';
import type { PeerOverlay } from '../core/presence';
import { isAxisAligned, rotationFor } from '../core/rotation';
@@ -101,6 +110,23 @@ const ROTATE_CURSOR = `url("data:image/svg+xml,${encodeURIComponent(
type Pressed = { row: LayerRow; wasSelected: boolean } | undefined;
+/** A move drag: offsets from where the layers started, then the drop. */
+interface Mover {
+ to(dx: number, dy: number): void;
+ end(): Promise<void>;
+}
+
+/** Layers lifted when pressed, before a drag starts (see `startMove`). */
+interface PreparedLift {
+ ids: string[];
+ plan: Promise<LiftPlan | undefined>;
+ /** The press ended without a drag. */
+ dropped: boolean;
+}
+
+const sameIds = (a: string[], b: string[]) =>
+ a.length === b.length && a.every((id, i) => id === b[i]);
+
type Drag =
| { kind: 'pan'; last: Point }
| {
@@ -111,7 +137,9 @@ type Drag =
/** The pressed layer, once the hit test answers. */
pressed: Promise<Pressed>;
resolved?: Pressed;
- mover?: ReturnType<FigEditor['startMove']>;
+ /** The pressed layers, lifted in case they are dragged. */
+ prepared?: PreparedLift;
+ mover?: Mover;
/** The selection's bounds at the start, and what it snaps to. */
snap?: { start: Rect; targets: Rect[]; lines?: SnapLines };
marquee: boolean;
@@ -231,6 +259,12 @@ export function ViewerCanvas(props: {
peers?: () => PeerOverlay[];
/** The pointer in page coordinates (`null` when it leaves the canvas). */
onPointer?: (page: Point | null) => void;
+ /**
+ * Lifted layers being dragged: their page-space offset from where they
+ * were (`undefined` once the drop's edit lands); the document moves them
+ * only at the drop.
+ */
+ onMove?: (offset: Point | undefined) => void;
/** Layout grids and ruler guides. */
aids?: LayoutAidsController;
/** Dev Mode: hovering measures from the selection without ⌥. */
@@ -347,6 +381,33 @@ export function ViewerCanvas(props: {
let marquee: Rect | undefined;
let linePreview: [Point, Point] | undefined;
let guides: Guide[] = [];
+ /**
+ * Lifted layers being moved: their offset (page units) and the
+ * selection's geometry when they were lifted. The selection box is drawn
+ * from it until the drop's edit lands (the document may move the layers
+ * at its own pace meanwhile, to share the drag).
+ */
+ let lifted:
+ | { offset: Point; geometry: NodeGeometry[]; bounds: Rect | undefined }
+ | undefined;
+
+ const shownGeometry = (): NodeGeometry[] => {
+ if (!lifted) return viewer.selectionGeometry();
+ const d = lifted.offset;
+ return lifted.geometry.map((g) => ({
+ ...g,
+ corners: g.corners.map((c) => ({
+ x: c.x + d.x,
+ y: c.y + d.y,
+ })) as NodeGeometry['corners'],
+ bounds: offsetRect(g.bounds, d),
+ }));
+ };
+
+ const shownBounds = (): Rect | undefined => {
+ if (!lifted) return viewer.selectionBounds();
+ return lifted.bounds && offsetRect(lifted.bounds, lifted.offset);
+ };
/** Where the pointer is (page coordinates) while the pen draws. */
let penCursor: Point | undefined;
@@ -365,7 +426,7 @@ export function ViewerCanvas(props: {
return geometry.map((g) => g.bounds);
};
const measurements = () => {
- const a = viewer.selectionBounds();
+ const a = shownBounds();
const b = viewer.hoverBounds();
if ((!props.altHeld() && !props.devMode?.()) || !a || !b) return [];
if (viewer.selected().some((s) => s.id === viewer.hover()?.id)) return [];
@@ -416,8 +477,8 @@ export function ViewerCanvas(props: {
dpr: dpr(),
frames: viewer.layout()?.frames ?? [],
selectedIds: selectionIds,
- selection: editingPoints ? [] : viewer.selectionGeometry(),
- selectionBounds: editingPoints ? undefined : viewer.selectionBounds(),
+ selection: editingPoints ? [] : shownGeometry(),
+ selectionBounds: editingPoints ? undefined : shownBounds(),
componentSelection: false,
hoverPath: hoverPath(),
hoverComponent:
@@ -732,9 +793,133 @@ export function ViewerCanvas(props: {
drag = press;
void pressed.then((r) => {
press.resolved = r;
+ // What a drag from here would move, lifted ahead of it.
+ if (r && editing() && !r.row.locked && !additive && props.editor) {
+ const ids = props.editor.editableIds();
+ if (ids.length > 0 && drag === press && !press.active)
+ press.prepared = prepareMove(ids);
+ }
});
};
+ /**
+ * The last lifted drop's edit. A new lift waits for it, so its parts are
+ * rendered with the dropped layers where they landed.
+ */
+ let drop: Promise<unknown> = Promise.resolve();
+
+ const planLift = async (ids: string[]): Promise<LiftPlan | undefined> => {
+ const page = viewer.page();
+ await drop;
+ try {
+ return await props.engine.liftPlan(page, ids);
+ } catch {
+ return undefined;
+ }
+ };
+
+ /**
+ * Lifts pressed layers, hidden, so their parts render while the press is
+ * still deciding whether it is a drag.
+ */
+ const prepareMove = (ids: string[]): PreparedLift => {
+ const prepared: PreparedLift = { ids, plan: planLift(ids), dropped: false };
+ void (async () => {
+ const plan = await prepared.plan;
+ if (plan && !prepared.dropped) compositor.lift(plan, true);
+ })();
+ return prepared;
+ };
+
+ /** A press that did not drag: its prepared lift comes down. */
+ const dropPrepared = (prepared: PreparedLift | undefined) => {
+ if (!prepared) return;
+ prepared.dropped = true;
+ void (async () => {
+ const plan = await prepared.plan;
+ if (plan && compositor.isLifted(plan)) compositor.unlift();
+ })();
+ };
+
+ /**
+ * Moves layers. When the engine says it is exact, they are lifted: drawn
+ * apart from the page and shifted every frame without rendering, with one
+ * edit at the drop. Otherwise the editor moves them step by step.
+ */
+ const startMove = (ids: string[], prepared?: PreparedLift): Mover => {
+ const editor = props.editor;
+ if (!editor) return { to: () => {}, end: async () => {} };
+ let offset: Point = { x: 0, y: 0 };
+ let stepwise: ReturnType<FigEditor['startMove']> | undefined;
+ let follower: ReturnType<FigEditor['startLiftedMove']> | undefined;
+ const reuse = !!prepared && sameIds(prepared.ids, ids);
+ if (prepared && !reuse) dropPrepared(prepared);
+ const plan = reuse && prepared ? prepared.plan : planLift(ids);
+ // A prepared lift's plan resolves to its own handlers first (they were
+ // registered first), so it is up, hidden, by the time this runs.
+ const decided = (async () => {
+ const p = await plan;
+ const shown =
+ !!p &&
+ (compositor.isLifted(p) ? compositor.showLift() : compositor.lift(p));
+ if (shown) {
+ follower = editor.startLiftedMove(ids);
+ lifted = {
+ offset,
+ geometry: viewer.selectionGeometry(),
+ bounds: viewer.selectionBounds(),
+ };
+ follower.to(offset.x, offset.y);
+ compositor.moveLift(offset);
+ props.onMove?.(offset);
+ requestDraw();
+ return;
+ }
+ stepwise = editor.startMove(ids);
+ if (offset.x !== 0 || offset.y !== 0) stepwise.to(offset.x, offset.y);
+ })();
+ return {
+ to(dx, dy) {
+ offset = { x: dx, y: dy };
+ if (stepwise) stepwise.to(dx, dy);
+ else if (follower && lifted) {
+ lifted = { ...lifted, offset };
+ follower.to(dx, dy);
+ compositor.moveLift(offset);
+ props.onMove?.(offset);
+ requestDraw();
+ }
+ },
+ end() {
+ const done = (async () => {
+ await decided;
+ if (stepwise) return stepwise.end();
+ if (!follower) return;
+ compositor.freezeLift();
+ let moved: Point | null = null;
+ try {
+ moved = await follower.end();
+ } finally {
+ // Down once the page shows the move (at once if it did not
+ // apply); the selection box lands with the edit.
+ compositor.landLift(moved);
+ lifted = undefined;
+ props.onMove?.(undefined);
+ requestDraw();
+ }
+ })();
+ drop = (async () => {
+ try {
+ await done;
+ } catch {
+ // The editor reports what failed; the next lift goes ahead.
+ }
+ })();
+ return done;
+ },
+ };
+ };
+
/** Starts moving the selection (⌥: a copy of it). */
const beginMove = async (
press: Extract<Drag, { kind: 'press' }>,
@@ -746,8 +931,14 @@ export function ViewerCanvas(props: {
const ids = editor.editableIds();
if (ids.length === 0) return;
const start = viewer.selectionBounds();
- press.mover = editor.startMove(ids);
+ press.mover = startMove(ids, press.prepared);
movePress(press, press.current, false);
+ // The pointer came up while this was starting (a copy being made, or
+ // the press still resolving): the move ends where it got to.
+ if (drag !== press) {
+ void press.mover.end();
+ return;
+ }
if (start) {
const targets = await snapTargets(ids).catch(() => []);
press.snap = { start, targets, lines: props.aids?.linesNear(start) };
@@ -980,6 +1171,7 @@ export function ViewerCanvas(props: {
} else if (ended.kind === 'rotate') {
void ended.rotator.end();
} else if (ended.kind === 'press') {
+ if (!ended.mover) dropPrepared(ended.prepared);
if (ended.mover) {
guides = [];
requestDraw();
diff --git a/apps/web/src/lib/core/fig-engine/client.ts b/apps/web/src/lib/core/fig-engine/client.ts
index a714660fec..b8dacc41af 100644
--- a/apps/web/src/lib/core/fig-engine/client.ts
+++ b/apps/web/src/lib/core/fig-engine/client.ts
@@ -31,6 +31,7 @@ import type {
FileSummary,
FontUse,
LayerRow,
+ LiftPlan,
NodeGeometry,
NodeInfo,
PageLayout,
@@ -61,6 +62,8 @@ export interface TileRequest {
outline: boolean;
/** Lower renders first. */
priority: number;
+ /** Only some layers (`LayersSpec` JSON); all when absent. */
+ layers?: string;
}
/** What an edit changed (`EditResult` in the engine). */
@@ -358,6 +361,14 @@ export class FigEngine {
return this.query('search', page, query, limit);
}
+ /**
+ * How to draw layers apart from the rest of the page while they move
+ * (or why that would not be exact).
+ */
+ liftPlan(page: number, ids: string[]): Promise<LiftPlan> {
+ return this.query('liftPlan', page, JSON.stringify(ids));
+ }
+
/** The page's guides and its frames' layout grids and guides. */
layoutAids(page: number): Promise<LayoutAids> {
return this.query('layoutAids', page);
diff --git a/apps/web/src/lib/core/fig-engine/fig.worker.ts b/apps/web/src/lib/core/fig-engine/fig.worker.ts
index 34c1e9bc2b..e7478755c5 100644
--- a/apps/web/src/lib/core/fig-engine/fig.worker.ts
+++ b/apps/web/src/lib/core/fig-engine/fig.worker.ts
@@ -46,6 +46,7 @@ const PAGE_QUERIES = new Set<QueryMethod>([
'outline',
'inRect',
'layoutAids',
+ 'liftPlan',
]);
const pageOnly = (r: FigRequest) =>
@@ -141,10 +142,11 @@ async function serve(request: FigRequest) {
request.scale,
request.width,
request.height,
- request.outline
+ request.outline,
+ request.layers
);
- // Tiles are opaque (drawn on the page color), so premultiplied and
- // straight alpha agree.
+ // Straight alpha, as ImageData holds it: tiles drawn on the page
+ // color are opaque, and the engine unpremultiplies the others.
const image = new ImageData(
new Uint8ClampedArray(
pixels.buffer as ArrayBuffer,
@@ -254,6 +256,9 @@ async function serve(request: FigRequest) {
case 'exportables':
json = f.exportables(a as number, b as string);
break;
+ case 'liftPlan':
+ json = f.liftPlan(a as number, b as string);
+ break;
}
post({ id: request.id, ok: true, kind: 'query', json });
return;
diff --git a/apps/web/src/lib/core/fig-engine/protocol.ts b/apps/web/src/lib/core/fig-engine/protocol.ts
index a66d898ba4..0e9bdf8be2 100644
--- a/apps/web/src/lib/core/fig-engine/protocol.ts
+++ b/apps/web/src/lib/core/fig-engine/protocol.ts
@@ -35,7 +35,8 @@ export type QueryMethod =
| 'textGeometry'
| 'libraryStatus'
| 'libraryAssets'
- | 'libraryUses';
+ | 'libraryUses'
+ | 'liftPlan';
export type FigRequest =
| { id: number; kind: 'open'; bytes: ArrayBuffer }
@@ -52,6 +53,8 @@ export type FigRequest =
outline: boolean;
/** Lower renders first. */
priority: number;
+ /** Only some layers (`LayersSpec` JSON); all when absent. */
+ layers?: string;
}
| { id: number; kind: 'cancel'; ids: number[] }
| {
diff --git a/apps/web/src/lib/core/fig-engine/types.ts b/apps/web/src/lib/core/fig-engine/types.ts
index e9e8d4b15b..0fccf02569 100644
--- a/apps/web/src/lib/core/fig-engine/types.ts
+++ b/apps/web/src/lib/core/fig-engine/types.ts
@@ -251,6 +251,11 @@ export interface NodeInfo {
/** Relative to the containing frame (or the page). */
x: number;
y: number;
+ /**
+ * How moving the layer on the page changes `x` and `y`:
+ * `[m00, m01, m10, m11]` (identity unless its frame is turned).
+ */
+ panelMove: [number, number, number, number];
width: number;
height: number;
rotation: number;
@@ -302,6 +307,44 @@ export interface NodeGeometry {
bounds: Rect;
}
+/**
+ * How to move layers by drawing them apart from the rest of the page while
+ * they are dragged. Mirrors `fig_engine::inspect::LiftPlan`.
+ */
+export interface LiftPlan {
+ /** Why the layers cannot be drawn apart; absent when they can. */
+ refused?: string | null;
+ /** The moving layers in runs of adjacent siblings, bottom first. */
+ runs: LiftRun[];
+}
+
+export interface LiftRun {
+ /** The run's layers, bottom first. */
+ ids: string[];
+ /** Page bounds of everything they draw. */
+ bounds: Rect;
+ /**
+ * Where the first of them is (its origin on the page). Renders anchored
+ * here draw the run where the lift started, however the document moved it.
+ */
+ origin: Vec2;
+ /** SVG outlines (page coordinates) of the ancestors that clip the run. */
+ clips: string[];
+ /** Page bounds of what paints after the run and before the next one. */
+ above: Rect[];
+}
+
+/**
+ * Which layers a render draws (`LayersSpec` in the engine): only some
+ * layers (their first one's origin drawn at `anchor`), all but some, or what
+ * paints after `after` and before `before`. Only some layers, and a window
+ * that does not start at the bottom of the page, render on transparency.
+ */
+export type LayersSpec =
+ | { only: string[]; anchor?: [number, number] }
+ | { skip: string[] }
+ | { after?: string | null; before?: string | null };
+
/** A component, for the assets list. */
export interface ComponentInfo {
id: string;
diff --git a/apps/web/src/lib/core/fig-engine/wasm-module.ts b/apps/web/src/lib/core/fig-engine/wasm-module.ts
index f8e1f19093..e5712f542b 100644
--- a/apps/web/src/lib/core/fig-engine/wasm-module.ts
+++ b/apps/web/src/lib/core/fig-engine/wasm-module.ts
@@ -25,7 +25,10 @@ export interface WasmFigFile {
designInfo: (page: number, id: string) => string;
/** `PageLayout` JSON. */
openPage: (page: number) => string;
- /** Premultiplied RGBA, `width × height × 4` bytes. */
+ /**
+ * RGBA, `width × height × 4` bytes: opaque, or straight alpha when
+ * `layers` (`LayersSpec` JSON) draws on transparency.
+ */
render: (
page: number,
x: number,
@@ -33,8 +36,11 @@ export interface WasmFigFile {
scale: number,
width: number,
height: number,
- outline: boolean
+ outline: boolean,
+ layers?: string | null
) => Uint8Array;
+ /** `LiftPlan` JSON for a JSON array of ids. */
+ liftPlan: (page: number, ids: string) => string;
/** `LayerRow[]` JSON. */
layers: (page: number, parent?: string | null) => string;
/** `LayerRow[]` JSON for a JSON array of ids. */
diff --git a/crates/fig_engine/README.md b/crates/fig_engine/README.md
index e9eb63658c..28a5d7c90a 100644
--- a/crates/fig_engine/README.md
+++ b/crates/fig_engine/README.md
@@ -60,9 +60,12 @@ document nodes, the tree, pages, blobs (geometry parsed lazily, once)
scene one page with instances expanded: overrides, component
properties, swaps, shared styles, world transforms, bounds
render tiles: fills, strokes (inside/outside via clipping), masks,
- blend modes, isolation, effects (shadows, blurs), images, text
+ blend modes, isolation, effects (shadows, blurs), images, text;
+ some layers only (a paint-order window, a set, all but a set);
+ wide blurs at a fraction of the resolution (render/coarse)
inspect layer rows, frames, hit tests, marquee, node info, search, SVG
- outlines; a page's prototype (inspect/prototype)
+ outlines; a page's prototype (inspect/prototype); how to draw
+ moving layers apart from the page (inspect/lift)
describe a design summarized for AI agents and search: pages, frames,
their text and components, the design system; text by page
edit edit operations, undo/redo, fractional-index positions;
@@ -350,6 +353,46 @@ CPU rasterizer over the scene; the viewer asks for 512 px tiles at the
current scale and composites them on a canvas, so a file renders
progressively and only what is visible is drawn.
+## Blurs and shadows
+
+Figma's blur radius is twice a Gaussian's standard deviation, and the
+renderer approximates the Gaussian with three box blurs, which cost the same
+at any radius but grow with the area they cover, as zooming in grows it. A
+wide blur is smooth, so a layer blur, drop shadow, inner shadow, or
+background blur at least 16 device pixels wide (its deviation) renders at a
+power-of-two fraction of the resolution that keeps it at least 8 coarse
+pixels wide, and scales back up bilinearly (`render/coarse`); fractional
+offsets and spreads stay exact. A layer with drop shadows and no layer blur
+keeps its own content at full resolution and casts the shadows from a
+coarse copy. Coarse pixels are aligned to the page's origin, so tiles agree
+where they meet. Inner shadows are cast from the node's own geometry, so
+unlike background blurs they need no margin around the tile. Tests compare
+coarse blurs with true Gaussians and tiles with whole renders.
+
+## Moving layers
+
+A rasterizer cannot redraw the page at every pointer move of a drag, so
+the editor lifts the layers it moves: `inspect::lift_plan` splits the page
+into parts that `render::render_layers` draws on their own, once each, and
+the canvas composites them every frame with the layers shifted, applying
+the move to the document at the drop (and, in a shared design, at gesture
+pace meanwhile, quietly). The parts are what paints below the moving
+layers (a paint-order window ending at them, `Layers::Window`), each run of
+adjacent moving siblings by itself (`Layers::Only`, anchored where the lift
+started, clipped by the canvas to its clipping ancestors' outlines), and
+what paints above each run (the window after it), with bounds so only tiles
+holding something render; page tiles drawn meanwhile leave the layers out
+(`Layers::Skip`). A node paints its fills when the window's walk enters it
+and the strokes it draws over its children when it leaves
+(`Scene::paint_times`), so a window ending at a layer keeps its parent's
+fills and leaves out the parent's border. The composite is exact unless
+moving changes other pixels or a part reads what is behind it, and the plan
+refuses those layers: in components, booleans, or instances; placed by an
+auto layout parent; masks or masked; under an ancestor that composites its
+children as one (opacity, blend mode, drop shadow, layer blur); or with a
+blend mode or background blur in them or above them. Tests composite the
+parts and compare them with the page rendered after the move.
+
## Use
```sh
diff --git a/crates/fig_engine/src/inspect.rs b/crates/fig_engine/src/inspect.rs
index 32ff78bf42..aea1135fcb 100644
--- a/crates/fig_engine/src/inspect.rs
+++ b/crates/fig_engine/src/inspect.rs
@@ -12,6 +12,7 @@ use serde::Serialize;
mod colors;
mod design;
mod handoff;
+mod lift;
pub mod prototype;
mod variables;
pub use colors::page_colors;
@@ -19,6 +20,7 @@ pub use design::{DesignInfo, StyleInfo, design_info, local_styles};
pub use handoff::{
Exportable, FrameAids, GridInfo, GuideInfo, LayoutAids, exportables, grid_info, layout_aids,
};
+pub use lift::{LiftPlan, LiftRun, lift_plan};
pub use variables::{CollectionInfo, variables};
/// One row of the layers panel.
@@ -156,6 +158,11 @@ pub struct NodeInfo {
/// Relative to the containing frame (or page), as Figma shows them.
pub x: f64,
pub y: f64,
+ /// How moving the layer on the page changes `x` and `y`: `[m00, m01,
+ /// m10, m11]`, the linear part of the containing frame's inverse
+ /// transform (identity on the page), so the editor can show where a
+ /// layer it is dragging will land.
+ pub panel_move: [f64; 4],
pub width: f64,
pub height: f64,
pub rotation: f64,
@@ -299,14 +306,11 @@ pub fn node_info(doc: &Document, scene: &Scene, i: SceneIdx) -> NodeInfo {
let node = scene.node(i);
let size = props.size();
let origin = node.world.panel_origin(size);
- let (x, y) = match coordinate_parent(scene, doc, i) {
- Some(p) => {
- let inv = scene.node(p).world.invert().unwrap_or_default();
- let local = inv.apply(origin);
- (local.x, local.y)
- }
- None => (origin.x, origin.y),
- };
+ let space = coordinate_parent(scene, doc, i)
+ .map(|p| scene.node(p).world.invert().unwrap_or_default())
+ .unwrap_or_default();
+ let local = space.apply(origin);
+ let (x, y) = (local.x, local.y);
let node_type = props.node_type();
let main_component = (node_type == NodeType::Instance)
.then(|| {
@@ -401,6 +405,7 @@ pub fn node_info(doc: &Document, scene: &Scene, i: SceneIdx) -> NodeInfo {
type_label: node_type.label(),
x,
y,
+ panel_move: [space.m00, space.m01, space.m10, space.m11],
width: size.x,
height: size.y,
rotation: node.world.panel_rotation_degrees(),
diff --git a/crates/fig_engine/src/inspect/lift.rs b/crates/fig_engine/src/inspect/lift.rs
new file mode 100644
index 0000000000..3f4a015ec2
--- /dev/null
+++ b/crates/fig_engine/src/inspect/lift.rs
@@ -0,0 +1,357 @@
+//! Moving layers by compositing them apart from the rest of the page.
+//!
+//! A CPU rasterizer cannot redraw a page at every pointer move while layers
+//! are dragged over it. Instead the editor lifts the layers it moves: it
+//! renders what paints below them, the layers themselves, and what paints
+//! above them (only where something does) once each, slides the layers'
+//! pixels over the rest at every pointer move, and applies the move to the
+//! document once, at the drop. [`lift_plan`] decides whether that composite
+//! is exact for a set of layers and describes its parts:
+//!
+//! - runs: the moving layers grouped into runs of adjacent siblings, in
+//! paint order. A run renders by itself ([`Layers::Only`]) and moves; it
+//! shows only inside the outlines of the ancestors that clip it, which
+//! stay where they are.
+//! - below: what paints before the first run ([`Layers::Window`] ending at
+//! it), on the page color.
+//! - above each run: what paints after it and before the next run (or the
+//! end of the page), on transparency, with the bounds of what it holds so
+//! that only tiles holding something are rendered.
+//!
+//! The composite is exact when moving the layers changes no other pixels
+//! and each part composites with plain source-over. Layers are refused
+//! when:
+//! - they are inside a component (its instances show the move), a boolean
+//! operation (its shape follows its operands), or an instance (their moves
+//! are overrides);
+//! - their auto layout parent places its other children around them;
+//! - they are a mask, or a mask applies to them;
+//! - an ancestor composites its children as one (opacity, a blend mode, a
+//! drop shadow, or a layer blur), so the parts would not add up to it;
+//! - they, or what paints above them, blend with or blur what is behind
+//! them (a blend mode or a background blur), which a part drawn on its
+//! own cannot know.
+//!
+//! [`Layers::Only`]: crate::render::Layers::Only
+//! [`Layers::Window`]: crate::render::Layers::Window
+
+use super::{Document, EffectKind, NodeType, Rect, Scene, SceneIdx, Serialize, Vec2, geometry};
+use crate::model::Props;
+use std::collections::HashSet;
+
+/// Runs a plan may have: each is a part to render and composite.
+const MAX_RUNS: usize = 16;
+/// Bounds listed for what paints above a run; past this they are merged.
+const MAX_ABOVE_RECTS: usize = 256;
+
+/// How to draw moving layers apart from the rest of the page.
+#[derive(Serialize, Debug, Default)]
+#[serde(rename_all = "camelCase")]
+pub struct LiftPlan {
+ /// Why the layers cannot be lifted (`None` when they can).
+ pub refused: Option<&'static str>,
+ /// The moving layers in runs, bottom first.
+ pub runs: Vec<LiftRun>,
+}
+
+/// Adjacent sibling layers that move together.
+#[derive(Serialize, Debug)]
+#[serde(rename_all = "camelCase")]
+pub struct LiftRun {
+ /// The run's layers, bottom first.
+ pub ids: Vec<String>,
+ /// Page bounds of everything they draw.
+ pub bounds: Rect,
+ /// Where the first of them is (its origin on the page): renders of the
+ /// run anchored here draw it where the lift started, even after the
+ /// document moved it (see `Layers::Only`).
+ pub origin: Vec2,
+ /// Outlines (SVG path data, page coordinates) of the ancestors that
+ /// clip the run: it shows only inside all of them.
+ pub clips: Vec<String>,
+ /// Page bounds of what paints after this run and before the next one
+ /// (or the end of the page).
+ pub above: Vec<Rect>,
+}
+
+fn refuse(reason: &'static str) -> LiftPlan {
+ LiftPlan {
+ refused: Some(reason),
+ runs: Vec::new(),
+ }
+}
+
+/// Whether a node composites what it holds as one, so its children's
+/// pixels cannot be drawn apart from each other.
+fn isolates(props: &Props) -> bool {
+ props.opacity() < 1.0
+ || !props.blend_mode().is_normal()
+ || props.effects().iter().any(|e| {
+ e.is_visible()
+ && match e.kind {
+ EffectKind::DropShadow => true,
+ EffectKind::LayerBlur => e.radius > 0.0,
+ _ => false,
+ }
+ })
+}
+
+fn has_background_blur(props: &Props) -> bool {
+ props
+ .effects()
+ .iter()
+ .any(|e| e.is_visible() && e.kind == EffectKind::BackgroundBlur && e.radius > 0.0)
+}
+
+/// What in the visible subtree of `i` reads the pixels behind it.
+fn reads_backdrop(doc: &Document, scene: &Scene, i: SceneIdx) -> Option<&'static str> {
+ let mut stack = vec![i];
+ while let Some(n) = stack.pop() {
+ let props = scene.props(doc, n);
+ if !props.visible() {
+ continue;
+ }
+ if !props.blend_mode().is_normal() {
+ return Some("blend mode");
+ }
+ if has_background_blur(props) {
+ return Some("background blur");
+ }
+ stack.extend(scene.node(n).below());
+ }
+ None
+}
+
+fn is_stack(props: &Props) -> bool {
+ props.node_type().is_frame_like()
+ && props.node_type() != NodeType::Instance
+ && props.auto_layout.as_ref().is_some_and(|a| a.is_stack())
+}
+
+/// Why node `n` (a layer to move) cannot be lifted, if it cannot.
+fn check(doc: &Document, scene: &Scene, n: SceneIdx) -> Option<&'static str> {
+ let node = scene.node(n);
+ let props = scene.props(doc, n);
+ if node.path.is_some() {
+ return Some("in an instance");
+ }
+ if props.is_mask() {
+ return Some("mask");
+ }
+ if let Some(reason) = reads_backdrop(doc, scene, n) {
+ return Some(reason);
+ }
+ if let Some(p) = node.parent
+ && p != scene.root()
+ {
+ let parent = scene.props(doc, p);
+ if is_stack(parent) && !props.layout_child.as_ref().is_some_and(|l| l.is_absolute()) {
+ return Some("auto layout");
+ }
+ }
+ // A mask masks the siblings after it.
+ let mut at = n;
+ while let Some(p) = scene.node(at).parent {
+ let siblings = &scene.node(p).children;
+ let index = siblings.iter().position(|&c| c == at).unwrap_or(0);
+ if siblings[..index].iter().any(|&c| {
+ let s = scene.props(doc, c);
+ s.is_mask() && s.visible()
+ }) {
+ return Some("masked");
+ }
+ if p == scene.root() {
+ break;
+ }
+ let ancestor = scene.props(doc, p);
+ match ancestor.node_type() {
+ NodeType::Symbol => return Some("in a component"),
+ NodeType::BooleanOperation => return Some("in a boolean"),
+ NodeType::Instance => return Some("in an instance"),
+ _ => {}
+ }
+ if isolates(ancestor) {
+ return Some("ancestor composites");
+ }
+ at = p;
+ }
+ None
+}
+
+/// Bounds of what paints in the exclusive [`Scene::paint_times`] window
+/// `(lo, hi)`, or why it cannot be drawn on its own.
+fn window_content(
+ doc: &Document,
+ scene: &Scene,
+ (lo, hi): (u32, u32),
+) -> Result<Vec<Rect>, &'static str> {
+ let times = scene.paint_times();
+ let mut rects = Vec::new();
+ let mut stack = vec![scene.root()];
+ while let Some(i) = stack.pop() {
+ let props = scene.props(doc, i);
+ if i != scene.root() && !props.visible() {
+ continue;
+ }
+ let (enter, exit) = times[i as usize];
+ if exit <= lo || enter >= hi {
+ continue;
+ }
+ let node = scene.node(i);
+ if enter > lo && exit < hi {
+ if let Some(reason) = reads_backdrop(doc, scene, i) {
+ return Err(reason);
+ }
+ if !node.bounds.is_empty() {
+ rects.push(node.bounds);
+ }
+ continue;
+ }
+ // The node holds an end of the window: its own paint is in it at
+ // whichever of its times are.
+ if i != scene.root() {
+ let entered = enter > lo && enter < hi;
+ let left = exit > lo && exit < hi;
+ let frame_like = props.node_type().is_frame_like();
+ let strokes = props.has_visible_strokes();
+ let container_stroke = frame_like && !props.clips_content();
+ let paints_entering = props.has_visible_fills()
+ || props.vector_styles.is_some()
+ || (container_stroke && strokes)
+ || props
+ .effects()
+ .iter()
+ .any(|e| e.is_visible() && e.kind == EffectKind::InnerShadow);
+ if entered && has_background_blur(props) {
+ return Err("background blur");
+ }
+ if (entered && paints_entering) || (left && strokes && !container_stroke) {
+ let own = scene.own_bounds(doc, i);
+ if !own.is_empty() {
+ rects.push(own);
+ }
+ }
+ }
+ stack.extend(node.below());
+ }
+ if rects.len() > MAX_ABOVE_RECTS {
+ let all = rects.iter().fold(Rect::EMPTY, |acc, r| acc.union(r));
+ rects = vec![all];
+ }
+ Ok(rects)
+}
+
+/// SVG path data (page coordinates) of what node `i` clips its children to.
+fn clip_outline(doc: &Document, scene: &Scene, i: SceneIdx) -> String {
+ let props = scene.props(doc, i);
+ let world = scene.node(i).world;
+ let mut out = String::new();
+ for shape in crate::render::fill_shapes(doc, props) {
+ geometry::to_svg(shape.path(), &world, &mut out);
+ }
+ out
+}
+
+/// How to move layers `ids` by compositing them apart from the rest of the
+/// page (see the module documentation), or why that would not be exact.
+pub fn lift_plan(doc: &Document, scene: &Scene, ids: &[String]) -> LiftPlan {
+ let mut nodes = Vec::new();
+ for id in ids {
+ match scene.find(doc, id) {
+ Some(n) if n != scene.root() => nodes.push(n),
+ _ => return refuse("missing"),
+ }
+ }
+ // Moving a layer moves what it holds: selected children of selected
+ // layers move with them.
+ let set: HashSet<SceneIdx> = nodes.iter().copied().collect();
+ nodes.retain(|&n| {
+ let mut at = scene.node(n).parent;
+ while let Some(p) = at {
+ if set.contains(&p) {
+ return false;
+ }
+ at = scene.node(p).parent;
+ }
+ true
+ });
+ let times = scene.paint_times();
+ nodes.sort_by_key(|&n| times[n as usize].0);
+ nodes.dedup();
+ if nodes.is_empty() {
+ return refuse("missing");
+ }
+ for &n in &nodes {
+ if let Some(reason) = check(doc, scene, n) {
+ return refuse(reason);
+ }
+ }
+ // Runs: siblings with nothing between them.
+ let mut runs: Vec<Vec<SceneIdx>> = Vec::new();
+ for &n in &nodes {
+ let adjacent = runs.last().and_then(|run| run.last()).is_some_and(|&last| {
+ let parent = scene.node(n).parent;
+ parent.is_some()
+ && parent == scene.node(last).parent
+ && parent.is_some_and(|p| {
+ let siblings = &scene.node(p).children;
+ let a = siblings.iter().position(|&c| c == last);
+ let b = siblings.iter().position(|&c| c == n);
+ matches!((a, b), (Some(a), Some(b)) if b == a + 1)
+ })
+ });
+ match runs.last_mut() {
+ Some(run) if adjacent => run.push(n),
+ _ => runs.push(vec![n]),
+ }
+ }
+ if runs.len() > MAX_RUNS {
+ return refuse("too many runs");
+ }
+ let mut out = Vec::with_capacity(runs.len());
+ for (k, run) in runs.iter().enumerate() {
+ let first = run[0];
+ let last = *run.last().expect("runs are not empty");
+ let lo = times[last as usize].1;
+ let hi = runs
+ .get(k + 1)
+ .map_or(u32::MAX, |next| times[next[0] as usize].0);
+ let above = match window_content(doc, scene, (lo, hi)) {
+ Ok(rects) => rects,
+ Err(reason) => return refuse(reason),
+ };
+ let mut clips = Vec::new();
+ let mut at = scene.node(first).parent;
+ while let Some(p) = at {
+ if p == scene.root() {
+ break;
+ }
+ if scene.props(doc, p).clips_content() {
+ clips.push(clip_outline(doc, scene, p));
+ }
+ at = scene.node(p).parent;
+ }
+ let bounds = run
+ .iter()
+ .fold(Rect::EMPTY, |acc, &n| acc.union(&scene.node(n).bounds));
+ if bounds.is_empty() {
+ // Nothing to draw apart (hidden layers move as they are).
+ return refuse("nothing drawn");
+ }
+ let world = scene.node(first).world;
+ out.push(LiftRun {
+ ids: run.iter().map(|&n| scene.id(doc, n)).collect(),
+ bounds,
+ origin: Vec2::new(world.m02, world.m12),
+ clips,
+ above,
+ });
+ }
+ LiftPlan {
+ refused: None,
+ runs: out,
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/fig_engine/src/inspect/lift/test.rs b/crates/fig_engine/src/inspect/lift/test.rs
new file mode 100644
index 0000000000..edbf346c79
--- /dev/null
+++ b/crates/fig_engine/src/inspect/lift/test.rs
@@ -0,0 +1,522 @@
+use super::*;
+use crate::edit::{History, Op};
+use crate::images::ImageStore;
+use crate::model::{Guid, Vec2};
+use crate::render::{Layers, RenderOptions, Viewport, render, render_layers};
+use tiny_skia::{FillRule, Mask, Pixmap, PixmapPaint, Transform};
+
+/// A blank design edited with `ops` (JSON); returns it and the created ids.
+fn design(ops: &str) -> (Document, Vec<String>) {
+ let original = crate::save::blank("Lift");
+ let mut doc = Document::open(&original).unwrap();
+ let ops: Vec<Op> = serde_json::from_str(ops).unwrap();
+ let created = History::default()
+ .apply(&mut doc, &ops, None)
+ .unwrap()
+ .created;
+ (doc, created)
+}
+
+fn rect(parent: &str, name: &str, x: f64, y: f64, w: f64, h: f64, props: &str) -> String {
+ format!(
+ r#"{{"op":"create","parent":"{parent}","node":{{"type":"RECTANGLE","name":"{name}","x":{x},"y":{y},"width":{w},"height":{h},"props":{props}}}}}"#
+ )
+}
+
+fn frame(parent: &str, name: &str, x: f64, y: f64, w: f64, h: f64, props: &str) -> String {
+ format!(
+ r#"{{"op":"create","parent":"{parent}","node":{{"type":"FRAME","name":"{name}","x":{x},"y":{y},"width":{w},"height":{h},"props":{props}}}}}"#
+ )
+}
+
+fn scene_of(doc: &Document) -> Scene {
+ Scene::build(doc, doc.pages[0])
+}
+
+fn plan(doc: &Document, ids: &[&str]) -> LiftPlan {
+ let ids: Vec<String> = ids.iter().map(|s| s.to_string()).collect();
+ lift_plan(doc, &scene_of(doc), &ids)
+}
+
+const VP: Viewport = Viewport {
+ x: -10.0,
+ y: -10.0,
+ scale: 2.0,
+ width: 900,
+ height: 700,
+};
+
+fn opts(background: Option<crate::model::Color>) -> RenderOptions {
+ RenderOptions {
+ outline: false,
+ background,
+ }
+}
+
+/// The clip a run shows through: its clipping ancestors' shapes, where they
+/// are on the page.
+fn clip_mask(doc: &Document, scene: &Scene, first: SceneIdx, vp: &Viewport) -> Option<Mask> {
+ let base = crate::model::Affine::scale(vp.scale, vp.scale)
+ .mul(&crate::model::Affine::translate(-vp.x, -vp.y));
+ let mut mask: Option<Mask> = None;
+ let mut at = scene.node(first).parent;
+ while let Some(p) = at {
+ if p == scene.root() {
+ break;
+ }
+ let props = scene.props(doc, p);
+ if props.clips_content() {
+ let ts = base.mul(&scene.node(p).world).to_skia();
+ for shape in crate::render::fill_shapes(doc, props) {
+ match &mut mask {
+ None => {
+ let mut m = Mask::new(vp.width, vp.height).unwrap();
+ m.fill_path(shape.path(), FillRule::Winding, true, ts);
+ mask = Some(m);
+ }
+ Some(m) => m.intersect_path(shape.path(), FillRule::Winding, true, ts),
+ }
+ }
+ }
+ at = scene.node(p).parent;
+ }
+ mask
+}
+
+/// What the editor shows while the plan's layers are moved by `(dx, dy)`:
+/// below, then each run (moved, clipped where it was) and what paints
+/// above it.
+fn composite(doc: &Document, scene: &Scene, plan: &LiftPlan, (dx, dy): (f64, f64)) -> Pixmap {
+ let mut images = ImageStore::default();
+ let node = |id: &String| scene.find(doc, id).unwrap();
+ let first = node(&plan.runs[0].ids[0]);
+ let background = Some(doc.page_background(scene.page));
+ let below = Layers::Window {
+ after: None,
+ before: Some(first),
+ };
+ let mut out = render_layers(doc, scene, &mut images, &VP, opts(background), &below).unwrap();
+ for (k, run) in plan.runs.iter().enumerate() {
+ let nodes: Vec<SceneIdx> = run.ids.iter().map(node).collect();
+ let sprite = render_layers(
+ doc,
+ scene,
+ &mut images,
+ &VP,
+ opts(None),
+ &Layers::Only {
+ nodes: nodes.clone(),
+ shift: Vec2::new(0.0, 0.0),
+ },
+ )
+ .unwrap();
+ let clip = clip_mask(doc, scene, nodes[0], &VP);
+ out.draw_pixmap(
+ (dx * VP.scale) as i32,
+ (dy * VP.scale) as i32,
+ sprite.as_ref(),
+ &PixmapPaint::default(),
+ Transform::identity(),
+ clip.as_ref(),
+ );
+ let above = Layers::Window {
+ after: Some(*nodes.last().unwrap()),
+ before: plan.runs.get(k + 1).map(|r| node(&r.ids[0])),
+ };
+ let above = render_layers(doc, scene, &mut images, &VP, opts(None), &above).unwrap();
+ out.draw_pixmap(
+ 0,
+ 0,
+ above.as_ref(),
+ &PixmapPaint::default(),
+ Transform::identity(),
+ None,
+ );
+ }
+ out
+}
+
+/// The page rendered after moving `ids` by `(dx, dy)`.
+fn moved(doc: &mut Document, ids: &[String], (dx, dy): (f64, f64)) -> Pixmap {
+ let op: Op = serde_json::from_str(&format!(
+ r#"{{"op":"translate","ids":{},"dx":{dx},"dy":{dy}}}"#,
+ serde_json::to_string(ids).unwrap()
+ ))
+ .unwrap();
+ History::default().apply(doc, &[op], None).unwrap();
+ let scene = scene_of(doc);
+ let background = Some(doc.page_background(scene.page));
+ render(
+ doc,
+ &scene,
+ &mut ImageStore::default(),
+ &VP,
+ opts(background),
+ )
+ .unwrap()
+}
+
+/// The largest difference in any channel between two renders, and where.
+fn difference(a: &Pixmap, b: &Pixmap) -> (u8, u32, u32) {
+ let mut worst = (0, 0, 0);
+ for y in 0..a.height() {
+ for x in 0..a.width() {
+ let (p, q) = (a.pixel(x, y).unwrap(), b.pixel(x, y).unwrap());
+ let d = [
+ p.red().abs_diff(q.red()),
+ p.green().abs_diff(q.green()),
+ p.blue().abs_diff(q.blue()),
+ p.alpha().abs_diff(q.alpha()),
+ ]
+ .into_iter()
+ .max()
+ .unwrap();
+ if d > worst.0 {
+ worst = (d, x, y);
+ }
+ }
+ }
+ worst
+}
+
+/// Lifts `ids`, composites them moved by `offset`, and checks the result
+/// against the page rendered after the move.
+fn assert_lift_matches(mut doc: Document, ids: &[String], offset: (f64, f64)) -> LiftPlan {
+ let scene = scene_of(&doc);
+ let plan = lift_plan(&doc, &scene, ids);
+ assert_eq!(plan.refused, None, "{plan:?}");
+ let shown = composite(&doc, &scene, &plan, offset);
+ let expected = moved(&mut doc, ids, offset);
+ let (d, x, y) = difference(&shown, &expected);
+ // Anti-aliased edges round once more when composited.
+ assert!(d <= 3, "differs by {d} at ({x}, {y})");
+ plan
+}
+
+/// A design with a picture-like background, a clipping frame with a
+/// stroke holding layers below and above the one that moves, a
+/// half-transparent layer over it, and a layer above the frame.
+fn layered() -> (Document, Vec<String>) {
+ let (mut doc, mut ids) = design(&format!(
+ "[{},{}]",
+ rect(
+ "0:1",
+ "Backdrop",
+ 0.0,
+ 0.0,
+ 420.0,
+ 320.0,
+ r#"{"fills":[{"color":"3366AA"}]}"#
+ ),
+ frame(
+ "0:1",
+ "Frame",
+ 20.0,
+ 20.0,
+ 300.0,
+ 220.0,
+ r#"{"fills":[{"color":"FFFFFF"}],"strokes":[{"color":"000000"}],"strokeWeight":3,"cornerRadius":12}"#
+ ),
+ ));
+ let f = ids[1].clone();
+ let inner = {
+ let ops: Vec<Op> = serde_json::from_str(&format!(
+ "[{},{},{},{},{}]",
+ rect(&f, "Below", 40.0, 40.0, 90.0, 60.0, r#"{"fills":[{"color":"2255FF"}]}"#),
+ rect(
+ &f,
+ "Moving",
+ 110.0,
+ 70.0,
+ 60.0,
+ 50.0,
+ r#"{"fills":[{"color":"FF0000"}],"strokes":[{"color":"00AA00"}],"strokeWeight":4,"effects":[{"type":"DROP_SHADOW","color":"00000080","x":3,"y":4,"radius":6}]}"#
+ ),
+ rect(&f, "Neighbor", 180.0, 70.0, 40.0, 40.0, r#"{"fills":[{"color":"FF9900"}]}"#),
+ rect(
+ &f,
+ "Glass",
+ 150.0,
+ 95.0,
+ 70.0,
+ 60.0,
+ r#"{"fills":[{"color":"FFFF00"}],"opacity":0.5}"#
+ ),
+ rect(
+ "0:1",
+ "Top",
+ 240.0,
+ 150.0,
+ 90.0,
+ 90.0,
+ r#"{"fills":[{"color":"8800CC"}],"cornerRadius":45}"#
+ ),
+ ))
+ .unwrap();
+ History::default()
+ .apply(&mut doc, &ops, None)
+ .unwrap()
+ .created
+ };
+ ids.extend(inner);
+ (doc, ids)
+}
+
+#[test]
+fn window_of_everything_is_the_page() {
+ let (doc, _) = layered();
+ let scene = scene_of(&doc);
+ let background = Some(doc.page_background(scene.page));
+ let mut images = ImageStore::default();
+ let all = render(&doc, &scene, &mut images, &VP, opts(background)).unwrap();
+ let window = Layers::Window {
+ after: None,
+ before: None,
+ };
+ let windowed =
+ render_layers(&doc, &scene, &mut images, &VP, opts(background), &window).unwrap();
+ assert_eq!(difference(&all, &windowed).0, 0);
+}
+
+#[test]
+fn lifted_layer_composites_like_the_moved_page() {
+ // In place; over its neighbor and under the glass and the top layer;
+ // partly out of the frame (clipped) in either direction.
+ for offset in [(0.0, 0.0), (40.0, 20.0), (-60.0, 90.0), (150.0, -30.0)] {
+ let (doc, ids) = layered();
+ let moving = ids[3].clone();
+ let plan = assert_lift_matches(doc, std::slice::from_ref(&moving), offset);
+ assert_eq!(plan.runs.len(), 1);
+ assert_eq!(plan.runs[0].ids, vec![moving]);
+ assert_eq!(plan.runs[0].clips.len(), 1, "the frame clips it");
+ }
+}
+
+#[test]
+fn lists_what_paints_above() {
+ let (doc, ids) = layered();
+ let p = plan(&doc, &[ids[3].as_str()]);
+ let above = &p.runs[0].above;
+ let scene = scene_of(&doc);
+ let bounds = |id: &str| scene.node(scene.find(&doc, id).unwrap()).bounds;
+ // The neighbor, the glass, the frame's stroke (drawn over its
+ // children), and the top layer; not the backdrop or the layer below.
+ for id in [&ids[4], &ids[5], &ids[6]] {
+ assert!(above.contains(&bounds(id)), "{id} in {above:?}");
+ }
+ assert!(!above.contains(&bounds(&ids[0])));
+ assert!(!above.contains(&bounds(&ids[2])));
+ let f = scene.own_bounds(&doc, scene.find(&doc, &ids[1]).unwrap());
+ assert!(above.contains(&f), "the frame's stroke");
+ // The clip is the frame's rounded outline on the page.
+ assert!(p.runs[0].clips[0].starts_with('M'), "{:?}", p.runs[0].clips);
+}
+
+#[test]
+fn layers_with_one_between_lift_as_two_runs() {
+ let (doc, ids) = layered();
+ // Below and Neighbor, with Moving between them.
+ let lifted = [ids[2].clone(), ids[4].clone()];
+ let plan = assert_lift_matches(doc, &lifted, (30.0, 40.0));
+ assert_eq!(plan.runs.len(), 2);
+ assert_eq!(plan.runs[0].ids, vec![ids[2].clone()]);
+ assert_eq!(plan.runs[1].ids, vec![ids[4].clone()]);
+}
+
+#[test]
+fn adjacent_layers_lift_as_one_run() {
+ let (doc, ids) = layered();
+ let lifted = [ids[3].clone(), ids[4].clone()];
+ let plan = assert_lift_matches(doc, &lifted, (-25.0, 35.0));
+ assert_eq!(plan.runs.len(), 1);
+ assert_eq!(plan.runs[0].ids, lifted.to_vec());
+}
+
+#[test]
+fn a_frame_lifts_with_what_it_holds() {
+ let (doc, ids) = layered();
+ // The frame and one of its children: the child moves with the frame.
+ let lifted = [ids[1].clone(), ids[3].clone()];
+ let plan = assert_lift_matches(doc, &lifted, (35.0, -15.0));
+ assert_eq!(plan.runs.len(), 1);
+ assert_eq!(plan.runs[0].ids, vec![ids[1].clone()]);
+ assert!(plan.runs[0].clips.is_empty());
+}
+
+#[test]
+fn lifts_top_level_layers_under_others() {
+ let (doc, ids) = layered();
+ let plan = assert_lift_matches(doc, std::slice::from_ref(&ids[0]), (70.0, 50.0));
+ assert!(!plan.runs[0].above.is_empty());
+}
+
+fn refused(doc: &Document, id: &str) -> Option<&'static str> {
+ plan(doc, &[id]).refused
+}
+
+#[test]
+fn refuses_layers_auto_layout_places() {
+ let (mut doc, ids) = layered();
+ let f = ids[1].clone();
+ let op: Vec<Op> = serde_json::from_str(&format!(
+ r#"[{{"op":"set","ids":["{f}"],"props":{{"layoutMode":"HORIZONTAL"}}}}]"#
+ ))
+ .unwrap();
+ History::default().apply(&mut doc, &op, None).unwrap();
+ assert_eq!(refused(&doc, &ids[3]), Some("auto layout"));
+ // A layer placed freely in it can move on its own.
+ let op: Vec<Op> = serde_json::from_str(&format!(
+ r#"[{{"op":"set","ids":["{}"],"props":{{"layoutPositioning":"ABSOLUTE"}}}}]"#,
+ ids[3]
+ ))
+ .unwrap();
+ History::default().apply(&mut doc, &op, None).unwrap();
+ assert_eq!(refused(&doc, &ids[3]), None);
+}
+
+#[test]
+fn refuses_layers_in_components() {
+ let (mut doc, ids) = layered();
+ let op: Vec<Op> = serde_json::from_str(&format!(
+ r#"[{{"op":"createComponent","ids":["{}"]}}]"#,
+ ids[1]
+ ))
+ .unwrap();
+ History::default().apply(&mut doc, &op, None).unwrap();
+ assert_eq!(refused(&doc, &ids[3]), Some("in a component"));
+ // The component itself moves like any layer.
+ assert_eq!(refused(&doc, &ids[1]), None);
+}
+
+#[test]
+fn refuses_what_composites_with_its_backdrop() {
+ let set = |doc: &mut Document, id: &str, props: &str| {
+ let op: Vec<Op> = serde_json::from_str(&format!(
+ r#"[{{"op":"set","ids":["{id}"],"props":{props}}}]"#
+ ))
+ .unwrap();
+ History::default().apply(doc, &op, None).unwrap();
+ };
+ let (mut doc, ids) = layered();
+ set(&mut doc, &ids[3], r#"{"blendMode":"MULTIPLY"}"#);
+ assert_eq!(refused(&doc, &ids[3]), Some("blend mode"));
+
+ let (mut doc, ids) = layered();
+ set(
+ &mut doc,
+ &ids[3],
+ r#"{"effects":[{"type":"BACKGROUND_BLUR","radius":8}]}"#,
+ );
+ assert_eq!(refused(&doc, &ids[3]), Some("background blur"));
+
+ // Above it: the glass multiplies what is under it.
+ let (mut doc, ids) = layered();
+ set(&mut doc, &ids[5], r#"{"blendMode":"MULTIPLY"}"#);
+ assert_eq!(refused(&doc, &ids[3]), Some("blend mode"));
+ // Below it nothing reads the moving layer.
+ assert_eq!(refused(&doc, &ids[6]), None);
+
+ // The frame fades its children together.
+ let (mut doc, ids) = layered();
+ set(&mut doc, &ids[1], r#"{"opacity":0.5}"#);
+ assert_eq!(refused(&doc, &ids[3]), Some("ancestor composites"));
+}
+
+#[test]
+fn refuses_masks_and_what_they_mask() {
+ let (mut doc, ids) = layered();
+ let below = doc.find(Guid::parse(&ids[2]).unwrap()).unwrap();
+ doc.nodes[below as usize].props.mask = Some(true);
+ assert_eq!(refused(&doc, &ids[2]), Some("mask"));
+ assert_eq!(refused(&doc, &ids[3]), Some("masked"));
+ // Layers before the mask are not masked.
+ assert_eq!(refused(&doc, &ids[1]), None);
+}
+
+#[test]
+fn refuses_unknown_and_hidden_layers() {
+ let (mut doc, ids) = layered();
+ assert_eq!(refused(&doc, "9:999"), Some("missing"));
+ let op: Vec<Op> = serde_json::from_str(&format!(
+ r#"[{{"op":"set","ids":["{}"],"props":{{"visible":false}}}}]"#,
+ ids[3]
+ ))
+ .unwrap();
+ History::default().apply(&mut doc, &op, None).unwrap();
+ assert_eq!(refused(&doc, &ids[3]), Some("nothing drawn"));
+}
+
+#[test]
+fn straight_alpha_unpremultiplies() {
+ let mut px = [64, 32, 0, 128, 10, 20, 30, 255, 0, 0, 0, 0];
+ crate::render::straight_alpha(&mut px);
+ assert_eq!(px, [128, 64, 0, 128, 10, 20, 30, 255, 0, 0, 0, 0]);
+}
+
+#[test]
+fn anchored_runs_render_where_the_lift_started() {
+ let (mut doc, ids) = layered();
+ let moving = ids[3].clone();
+ let scene = scene_of(&doc);
+ let plan = lift_plan(&doc, &scene, std::slice::from_ref(&moving));
+ let run = &plan.runs[0];
+ let only = |doc: &Document, scene: &Scene, shift: Vec2| {
+ let n = scene.find(doc, &moving).unwrap();
+ let layers = Layers::Only {
+ nodes: vec![n],
+ shift,
+ };
+ render_layers(
+ doc,
+ scene,
+ &mut ImageStore::default(),
+ &VP,
+ opts(None),
+ &layers,
+ )
+ .unwrap()
+ };
+ let before = only(&doc, &scene, Vec2::new(0.0, 0.0));
+ // The document moves the layer while it is dragged …
+ moved(&mut doc, std::slice::from_ref(&moving), (37.0, -12.0));
+ let scene = scene_of(&doc);
+ let n = scene.find(&doc, &moving).unwrap();
+ let world = scene.node(n).world;
+ // … and drawn from the plan's origin, it renders as it did.
+ let shift = Vec2::new(run.origin.x - world.m02, run.origin.y - world.m12);
+ assert_eq!((shift.x, shift.y), (-37.0, 12.0));
+ assert_eq!(difference(&before, &only(&doc, &scene, shift)).0, 0);
+}
+
+#[test]
+fn skipping_lifted_layers_draws_the_rest_of_the_page() {
+ let (mut doc, ids) = layered();
+ let moving = ids[3].clone();
+ let scene = scene_of(&doc);
+ let n = scene.find(&doc, &moving).unwrap();
+ let background = Some(doc.page_background(scene.page));
+ let skipped = render_layers(
+ &doc,
+ &scene,
+ &mut ImageStore::default(),
+ &VP,
+ opts(background),
+ &Layers::Skip(vec![n]),
+ )
+ .unwrap();
+ let op: Vec<Op> = serde_json::from_str(&format!(
+ r#"[{{"op":"set","ids":["{moving}"],"props":{{"visible":false}}}}]"#
+ ))
+ .unwrap();
+ History::default().apply(&mut doc, &op, None).unwrap();
+ let scene = scene_of(&doc);
+ let hidden = render(
+ &doc,
+ &scene,
+ &mut ImageStore::default(),
+ &VP,
+ opts(background),
+ )
+ .unwrap();
+ assert_eq!(difference(&skipped, &hidden).0, 0);
+}
diff --git a/crates/fig_engine/src/inspect/test.rs b/crates/fig_engine/src/inspect/test.rs
index 45b53e0153..7513c3048a 100644
--- a/crates/fig_engine/src/inspect/test.rs
+++ b/crates/fig_engine/src/inspect/test.rs
@@ -51,6 +51,28 @@ fn describes_a_node() {
);
assert_eq!(info.fills.len(), 1);
assert_eq!(info.fills[0].color.as_deref(), Some("FF0000"));
+ // The frame is not turned: a move on the page moves x and y as much.
+ assert_eq!(info.panel_move, [1.0, 0.0, 0.0, 1.0]);
+}
+
+#[test]
+fn maps_page_moves_into_a_turned_frame() {
+ let (mut doc, _) = open();
+ let ops: Vec<crate::edit::Op> =
+ serde_json::from_str(r#"[{"op":"set","ids":["1:2"],"props":{"rotation":90}}]"#).unwrap();
+ crate::edit::History::default()
+ .apply(&mut doc, &ops, None)
+ .unwrap();
+ let scene = Scene::build(&doc, doc.pages[0]);
+ let info = node_info(&doc, &scene, scene.find(&doc, "1:3").unwrap());
+ let [a, b, c, d] = info.panel_move;
+ // Moving the layer right on the page moves it along the frame's turned
+ // axes; the panel's x and y follow the frame.
+ let (dx, dy) = (a * 10.0 + b * 0.0, c * 10.0 + d * 0.0);
+ assert!(
+ (dx.abs() + dy.abs() - 10.0).abs() < 1e-9 && dx.abs() < 1e-9,
+ "{dx}, {dy}"
+ );
}
#[test]
diff --git a/crates/fig_engine/src/render.rs b/crates/fig_engine/src/render.rs
index 06d968827a..8d38b76c24 100644
--- a/crates/fig_engine/src/render.rs
+++ b/crates/fig_engine/src/render.rs
@@ -8,8 +8,11 @@
//! following siblings until the next mask or the end of its parent.
//!
//! Layers are sized to the node's bounds within the region being rendered,
-//! so isolated nodes cost what they cover, not a full surface.
+//! so isolated nodes cost what they cover, not a full surface; wide blurs
+//! and shadows render at a fraction of the resolution (see `coarse`).
+mod blend;
+mod coarse;
mod effects;
pub(crate) mod paint;
pub(crate) mod pattern;
@@ -19,11 +22,12 @@ use crate::document::Document;
use crate::geometry::{self, ParsedPath};
use crate::images::ImageStore;
use crate::model::{
- Affine, Color, EffectKind, MaskType, NodeType, Props, Rect, StrokeAlign, WindingRule,
+ Affine, Color, EffectKind, MaskType, NodeType, Props, Rect, StrokeAlign, Vec2, WindingRule,
};
use crate::scene::{Scene, SceneIdx};
+use blend::composite;
use std::sync::Arc;
-use tiny_skia::{FillRule, Mask, Path, PathBuilder, Pixmap, PixmapPaint, Transform};
+use tiny_skia::{FillRule, Mask, Path, PathBuilder, Pixmap, Transform};
/// What to draw: a page rectangle starting at `(x, y)` (page units) at
/// `scale` device pixels per unit, `width × height` pixels.
@@ -44,6 +48,36 @@ pub struct RenderOptions {
pub background: Option<Color>,
}
+/// Which of a page's layers a render draws. The editor moves layers by
+/// drawing them apart from the rest (see `inspect::lift_plan`): what paints
+/// below them, the layers themselves, and what paints above them, each
+/// rendered once and composited as they move.
+#[derive(Clone, Debug, Default)]
+pub enum Layers {
+ /// Everything.
+ #[default]
+ All,
+ /// What paints after the node `after` and what it holds (from the start
+ /// of the page when `None`) and before the node `before` (to the end
+ /// when `None`), in [`Scene::paint_times`] order. A node draws its
+ /// fills before its children and the strokes it puts over them after,
+ /// so a window ending at a layer keeps its parent's fills and leaves out
+ /// the parent's strokes.
+ Window {
+ after: Option<SceneIdx>,
+ before: Option<SceneIdx>,
+ },
+ /// Only these nodes and what they hold, in this order, as if nothing
+ /// else were on the page: their ancestors' clips do not apply either
+ /// (whoever composites them clips them where they are drawn). They are
+ /// drawn `shift` page units from where they are: layers moved while
+ /// lifted still render where the lift started.
+ Only { nodes: Vec<SceneIdx>, shift: Vec2 },
+ /// Everything but these nodes and what they hold: the page while they
+ /// are lifted (whatever the document has done with them meanwhile).
+ Skip(Vec<SceneIdx>),
+}
+
/// A pixmap whose pixel (0, 0) is device pixel `(ox, oy)`.
pub(crate) struct Surface {
pub pixmap: Pixmap,
@@ -95,6 +129,12 @@ pub(crate) struct Painter<'a> {
masks: Vec<Mask>,
/// Sources currently being rendered, to stop cyclic patterns.
active_patterns: Vec<crate::model::Guid>,
+ /// The paint-order window being drawn (exclusive [`Scene::paint_times`]
+ /// bounds), while drawing nodes that straddle its ends; `None` draws
+ /// everything (and inside the window, once a node lies wholly in it).
+ window: Option<(u32, u32)>,
+ /// Nodes not drawn, sorted ([`Layers::Skip`]).
+ skip: Vec<SceneIdx>,
}
/// Renders a viewport of a scene into a premultiplied RGBA pixmap.
@@ -105,7 +145,22 @@ pub fn render(
vp: &Viewport,
opts: RenderOptions,
) -> Option<Pixmap> {
- let base = Affine::scale(vp.scale, vp.scale).mul(&Affine::translate(-vp.x, -vp.y));
+ render_layers(doc, scene, images, vp, opts, &Layers::All)
+}
+
+/// [`render`] drawing only some of the page's layers.
+pub fn render_layers(
+ doc: &Document,
+ scene: &Scene,
+ images: &mut ImageStore,
+ vp: &Viewport,
+ opts: RenderOptions,
+ layers: &Layers,
+) -> Option<Pixmap> {
+ let mut base = Affine::scale(vp.scale, vp.scale).mul(&Affine::translate(-vp.x, -vp.y));
+ if let Layers::Only { shift, .. } = layers {
+ base = base.mul(&Affine::translate(shift.x, shift.y));
+ }
let region = Rect::new(0.0, 0.0, f64::from(vp.width), f64::from(vp.height));
let mut painter = Painter {
doc,
@@ -119,10 +174,23 @@ pub fn render(
reach: 0.0,
masks: Vec::new(),
active_patterns: Vec::new(),
+ window: None,
+ skip: Vec::new(),
+ };
+ let only = match layers {
+ Layers::Only { nodes, .. } => Some(nodes.as_slice()),
+ _ => None,
};
// Effects sample beyond what they cover: render with a margin so blurs
// and shadows from just outside the region are complete inside it.
- let margin = painter.effect_margin(scene.root()).ceil().min(MAX_MARGIN) as i32;
+ let margin = match only {
+ Some(nodes) => nodes
+ .iter()
+ .map(|&n| painter.effect_margin(n))
+ .fold(0.0, f64::max),
+ None => painter.effect_margin(scene.root()),
+ };
+ let margin = margin.ceil().min(MAX_MARGIN) as i32;
let (w, h) = (vp.width as i32 + 2 * margin, vp.height as i32 + 2 * margin);
let mut surface = Surface::new(-margin, -margin, w as u32, h as u32)?;
painter.region = surface.device_rect();
@@ -130,8 +198,33 @@ pub fn render(
if let Some(bg) = opts.background {
surface.pixmap.fill(bg.to_skia());
}
- let children = scene.node(scene.root()).children.clone();
- painter.draw_children(&children, &mut surface, None);
+ match layers {
+ Layers::All => {
+ let children = scene.node(scene.root()).children.clone();
+ painter.draw_children(&children, &mut surface, None);
+ }
+ Layers::Window { after, before } => {
+ let times = scene.paint_times();
+ let lo = after.map_or(0, |a| times[a as usize].1);
+ let hi = before.map_or(u32::MAX, |b| times[b as usize].0);
+ if lo < hi {
+ painter.window = Some((lo, hi));
+ let children = scene.node(scene.root()).children.clone();
+ painter.draw_children(&children, &mut surface, None);
+ }
+ }
+ Layers::Only { nodes, .. } => {
+ for &n in nodes {
+ painter.draw_node(n, &mut surface, None);
+ }
+ }
+ Layers::Skip(nodes) => {
+ painter.skip = nodes.clone();
+ painter.skip.sort_unstable();
+ let children = scene.node(scene.root()).children.clone();
+ painter.draw_children(&children, &mut surface, None);
+ }
+ }
if margin == 0 {
return Some(surface.pixmap);
}
@@ -140,6 +233,20 @@ pub fn render(
)?)
}
+/// Premultiplied RGBA bytes (what renders produce) as straight alpha, in
+/// place: what a browser's `ImageData` holds.
+pub fn straight_alpha(rgba: &mut [u8]) {
+ for px in rgba.chunks_exact_mut(4) {
+ let a = u16::from(px[3]);
+ if a == 0 || a == 255 {
+ continue;
+ }
+ for c in &mut px[..3] {
+ *c = ((u16::from(*c) * 255 + a / 2) / a).min(255) as u8;
+ }
+ }
+}
+
/// Renders one node by itself (its own bounds, transparent background) at
/// `scale` device pixels per unit, as Figma's export does.
pub fn render_node(
@@ -174,6 +281,8 @@ pub fn render_node(
reach: 0.0,
masks: Vec::new(),
active_patterns: Vec::new(),
+ window: None,
+ skip: Vec::new(),
};
let mut surface = Surface::new(0, 0, vp.width, vp.height)?;
if let Some(bg) = opts.background {
@@ -183,6 +292,41 @@ pub fn render_node(
Some(surface.pixmap)
}
+/// The shapes a node's fills cover, in its own coordinates: its fill
+/// geometry, or for frames and rectangles without stored geometry, its
+/// (rounded) box. A frame that clips its content clips it to these.
+pub(crate) fn fill_shapes(doc: &Document, props: &Props) -> Vec<Shape> {
+ let mut shapes: Vec<Shape> = props
+ .fill_geometry()
+ .iter()
+ .filter_map(|g| {
+ doc.blobs
+ .path(g.blob)
+ .map(|p| Shape::Blob(p, fill_rule(g.winding)))
+ })
+ .collect();
+ if shapes.is_empty() {
+ let size = props.size();
+ let boxy = props.node_type().is_frame_like() || is_box(props.node_type());
+ if props.node_type() == NodeType::Ellipse
+ && let Some(rect) = tiny_skia::Rect::from_xywh(0.0, 0.0, size.x as f32, size.y as f32)
+ && let Some(p) = PathBuilder::from_oval(rect)
+ {
+ shapes.push(Shape::Owned(p, FillRule::Winding));
+ } else if boxy
+ && let Some(p) = geometry::rounded_rect(
+ size.x as f32,
+ size.y as f32,
+ props.radii(),
+ props.corner_smoothing.unwrap_or(0.0),
+ )
+ {
+ shapes.push(Shape::Owned(p, FillRule::Winding));
+ }
+ }
+ shapes
+}
+
pub(crate) fn fill_rule(w: WindingRule) -> FillRule {
match w {
WindingRule::NonZero => FillRule::Winding,
@@ -229,36 +373,41 @@ impl<'a> Painter<'a> {
}
/// How far (device pixels) the node's layer blurs and drop shadows
- /// sample beyond a pixel, with a few pixels to spare for rounding.
+ /// sample beyond a pixel, with a few pixels to spare for rounding. Layers
+ /// at full resolution reach no further than [`MAX_MARGIN`].
fn layer_reach(&self, i: SceneIdx) -> f64 {
- let mut reach: f64 = 0.0;
+ let (mut blur, mut shadow): (f64, f64) = (0.0, 0.0);
for e in self.props(i).effects().iter().filter(|e| e.is_visible()) {
- let r = match e.kind {
+ match e.kind {
EffectKind::DropShadow => {
- f64::from(e.radius) * 1.5
- + if self.props(i).supports_shadow_spread() {
- f64::from(e.spread.abs())
- } else {
- 0.0
- }
- + e.offset.x.abs().max(e.offset.y.abs())
+ shadow = shadow.max(
+ f64::from(e.radius) * 1.5
+ + if self.props(i).supports_shadow_spread() {
+ f64::from(e.spread.abs())
+ } else {
+ 0.0
+ }
+ + e.offset.x.abs().max(e.offset.y.abs()),
+ );
}
- EffectKind::LayerBlur if e.radius > 0.0 => f64::from(e.radius) * 1.5,
- _ => continue,
- };
- reach = reach.max(r);
+ EffectKind::LayerBlur if e.radius > 0.0 => blur += f64::from(e.radius) * 1.5,
+ _ => {}
+ }
}
+ // Blurs apply one after another, and drop shadows are cast from the
+ // blurred layer: their reaches add up.
+ let reach = blur + shadow;
if reach > 0.0 {
- (reach * self.scene.node(i).world.scale_factor() * self.scale + 3.0).min(MAX_MARGIN)
+ reach * self.scene.node(i).world.scale_factor() * self.scale + 3.0
} else {
0.0
}
}
- /// How far (device pixels) inner shadows and background blurs in the
- /// subtree sample beyond their node's geometry, for nodes that intersect
- /// the region. (They read the surface they draw on; layer blurs and drop
- /// shadows extend their own layers instead.)
+ /// How far (device pixels) background blurs in the subtree sample beyond
+ /// their node's geometry, for nodes that intersect the region. (They
+ /// read the surface they draw on; inner shadows, layer blurs, and drop
+ /// shadows cast from their node's own geometry or layer instead.)
fn effect_margin(&self, i: SceneIdx) -> f64 {
let node = self.scene.node(i);
let mut margin: f64 = 0.0;
@@ -268,20 +417,13 @@ impl<'a> Painter<'a> {
return 0.0;
}
for e in props.effects().iter().filter(|e| e.is_visible()) {
- let reach = match e.kind {
- EffectKind::InnerShadow => {
- f64::from(e.radius) * 1.5
- + if props.supports_shadow_spread() {
- f64::from(e.spread.abs())
- } else {
- 0.0
- }
- + e.offset.x.abs().max(e.offset.y.abs())
- }
- EffectKind::BackgroundBlur => f64::from(e.radius) * 1.5,
- _ => 0.0,
- };
- margin = margin.max(reach * node.world.scale_factor() * self.scale);
+ if e.kind == EffectKind::BackgroundBlur && e.radius > 0.0 {
+ // As far as the blur reaches, at the resolution it
+ // renders at (see `background_blur`).
+ let sigma = f64::from(e.radius) / 2.0 * node.world.scale_factor() * self.scale;
+ let k = f64::from(coarse::coarseness_for(sigma));
+ margin = margin.max(3.0 * sigma + 3.0 + coarse::PAD * k);
+ }
}
}
for c in node.below() {
@@ -305,7 +447,10 @@ impl<'a> Painter<'a> {
while end < children.len() && !self.props(children[end]).is_mask() {
end += 1;
}
- self.draw_masked(c, &children[k + 1..end], surface, clip);
+ // A window that misses the whole group draws none of it.
+ if !self.window_misses(c, children[end - 1]) {
+ self.draw_masked(c, &children[k + 1..end], surface, clip);
+ }
k = end;
} else {
self.draw_node(c, surface, clip);
@@ -351,7 +496,11 @@ impl<'a> Painter<'a> {
}
}
MaskType::Alpha | MaskType::Luminance => {
+ // The mask applies whole to what the window keeps of its
+ // content.
+ let window = self.window.take();
self.draw_node_content(mask_node, &mut mask_layer, None, true);
+ self.window = window;
}
}
let mask = Mask::from_pixmap(
@@ -381,11 +530,47 @@ impl<'a> Painter<'a> {
Surface::new(x0, y0, w, h)
}
+ /// Whether the paint-order window being drawn misses every node from
+ /// `first` to `last` (siblings, in order) and what they hold.
+ fn window_misses(&self, first: SceneIdx, last: SceneIdx) -> bool {
+ let Some((lo, hi)) = self.window else {
+ return false;
+ };
+ let times = self.scene.paint_times();
+ times[last as usize].1 <= lo || times[first as usize].0 >= hi
+ }
+
+ /// Whether the window being drawn keeps what node `i` paints when it is
+ /// entered (fills, inner shadows, a container's strokes, a background
+ /// blur) and when it is left (strokes over its children).
+ fn window_phases(&self, i: SceneIdx) -> (bool, bool) {
+ let Some((lo, hi)) = self.window else {
+ return (true, true);
+ };
+ let (enter, exit) = self.scene.paint_times()[i as usize];
+ (enter > lo && enter < hi, exit > lo && exit < hi)
+ }
+
pub(crate) fn draw_node(&mut self, i: SceneIdx, surface: &mut Surface, clip: Option<&Mask>) {
let props = self.props(i);
- if !props.visible() {
+ if !props.visible() || self.skip.binary_search(&i).is_ok() {
return;
}
+ if let Some((lo, hi)) = self.window {
+ let (enter, exit) = self.scene.paint_times()[i as usize];
+ if exit <= lo || enter >= hi {
+ return;
+ }
+ if enter > lo && exit < hi {
+ // Wholly in the window: drawn as usual.
+ self.window = None;
+ self.draw_node(i, surface, clip);
+ self.window = Some((lo, hi));
+ return;
+ }
+ // Otherwise the node holds an end of the window: its children
+ // decide for themselves, and `window_phases` for its own paint.
+ }
let node = self.scene.node(i);
let device = self.to_device(&node.bounds);
if !device.intersects(&surface.device_rect())
@@ -420,7 +605,7 @@ impl<'a> Painter<'a> {
.iter()
.any(|e| e.is_visible() && e.kind == EffectKind::BackgroundBlur && e.radius > 0.0);
- if has_background_blur {
+ if has_background_blur && self.window_phases(i).0 {
self.background_blur(i, surface, clip);
}
@@ -435,8 +620,16 @@ impl<'a> Painter<'a> {
// Blurs and shadows sample around each pixel: their layer reaches
// past the surface by as far as they do, so it is complete within
// (and no further than any layer around it reaches past the region).
+ let reach = self.layer_reach(i);
+ if reach > 0.0 {
+ let k = self.coarseness(i);
+ if k > 1 {
+ self.draw_coarse(i, surface, clip, k);
+ return;
+ }
+ }
let outer = self.reach;
- let layer = match self.layer_reach(i) {
+ let layer = match reach.min(MAX_MARGIN) {
reach if reach > 0.0 => {
self.reach = outer.max(reach - self.margin);
let bound = surface
@@ -483,12 +676,11 @@ impl<'a> Painter<'a> {
let offset = self.device_vector(i, e.offset);
effects::drop_shadow(&mut with_shadows.pixmap, &layer.pixmap, &e, offset, scale);
}
- with_shadows.pixmap.draw_pixmap(
- 0,
- 0,
+ blend::draw_over(
+ &mut with_shadows.pixmap,
layer.pixmap.as_ref(),
- &PixmapPaint::default(),
- Transform::identity(),
+ (0, 0),
+ 1.0,
None,
);
layer = with_shadows;
@@ -557,16 +749,19 @@ impl<'a> Painter<'a> {
let node_type = props.node_type();
let ts = self.node_transform(i, surface);
let size = props.size();
+ let (entered, left) = self.window_phases(i);
- if node_type.is_text() {
- self.draw_text(i, props, &ts, surface, clip, opacity);
- } else if props.vector_styles.is_some() {
- self.fill_regions(props, &ts, surface, clip, opacity);
- } else if props.has_visible_fills() {
- let shapes = self.fill_shapes(i);
- for paint in props.fills().iter().filter(|p| p.is_visible()) {
- for shape in &shapes {
- self.fill_shape(surface, shape, &ts, paint, size, opacity, clip);
+ if entered {
+ if node_type.is_text() {
+ self.draw_text(i, props, &ts, surface, clip, opacity);
+ } else if props.vector_styles.is_some() {
+ self.fill_regions(props, &ts, surface, clip, opacity);
+ } else if props.has_visible_fills() {
+ let shapes = self.fill_shapes(i);
+ for paint in props.fills().iter().filter(|p| p.is_visible()) {
+ for shape in &shapes {
+ self.fill_shape(surface, shape, &ts, paint, size, opacity, clip);
+ }
}
}
}
@@ -577,14 +772,14 @@ impl<'a> Painter<'a> {
.filter(|e| e.is_visible() && e.kind == EffectKind::InnerShadow)
.cloned()
.collect();
- if !inner.is_empty() {
+ if !inner.is_empty() && entered {
self.inner_shadows(i, &inner, surface, clip);
}
// A nonclipping container's border belongs below its contents: a tab can
// cover a parent's border with its own underline or background.
let container_stroke = node_type.is_frame_like() && !props.clips_content();
- if container_stroke && props.has_visible_strokes() {
+ if container_stroke && props.has_visible_strokes() && entered {
self.draw_strokes(i, props, &ts, surface, clip, opacity);
}
@@ -616,46 +811,14 @@ impl<'a> Painter<'a> {
}
// Text draws its own strokes, clipped to its glyphs.
- if props.has_visible_strokes() && !node_type.is_text() && !container_stroke {
+ if props.has_visible_strokes() && !node_type.is_text() && !container_stroke && left {
self.draw_strokes(i, props, &ts, surface, clip, opacity);
}
}
- /// The shapes a node's fills cover: its fill geometry, or for frames and
- /// rectangles without stored geometry, its (rounded) box.
+ /// The shapes a node's fills cover (see [`fill_shapes`]).
pub(crate) fn fill_shapes(&self, i: SceneIdx) -> Vec<Shape> {
- let props = self.props(i);
- let mut shapes: Vec<Shape> = props
- .fill_geometry()
- .iter()
- .filter_map(|g| {
- self.doc
- .blobs
- .path(g.blob)
- .map(|p| Shape::Blob(p, fill_rule(g.winding)))
- })
- .collect();
- if shapes.is_empty() {
- let size = props.size();
- let boxy = props.node_type().is_frame_like() || is_box(props.node_type());
- if props.node_type() == NodeType::Ellipse
- && let Some(rect) =
- tiny_skia::Rect::from_xywh(0.0, 0.0, size.x as f32, size.y as f32)
- && let Some(p) = PathBuilder::from_oval(rect)
- {
- shapes.push(Shape::Owned(p, FillRule::Winding));
- } else if boxy
- && let Some(p) = geometry::rounded_rect(
- size.x as f32,
- size.y as f32,
- props.radii(),
- props.corner_smoothing.unwrap_or(0.0),
- )
- {
- shapes.push(Shape::Owned(p, FillRule::Winding));
- }
- }
- shapes
+ fill_shapes(self.doc, self.props(i))
}
/// Every shape the node draws (fills, else strokes), for masks.
@@ -804,7 +967,9 @@ impl<'a> Painter<'a> {
..Default::default()
};
let node_type = props.node_type();
- if node_type.is_text() {
+ // The hairline is the node's own paint, drawn before its children.
+ let entered = self.window_phases(i).0;
+ if entered && node_type.is_text() {
if let Some(layout) = &props.text_layout {
for g in layout.glyphs.iter() {
let Some(path) = g.blob.and_then(|b| self.doc.blobs.path(b)) else {
@@ -816,7 +981,7 @@ impl<'a> Painter<'a> {
.fill_path(&path.path, &paint, FillRule::Winding, gts, clip);
}
}
- } else {
+ } else if entered {
let mut shapes = self.fill_shapes(i);
if shapes.is_empty() {
shapes = self.shapes(i);
@@ -1097,27 +1262,5 @@ pub(crate) fn solid_paint(c: Color) -> tiny_skia::Paint<'static> {
p
}
-/// Draws `layer` onto `surface` at its device position.
-fn composite(
- surface: &mut Surface,
- layer: &Surface,
- opacity: f32,
- blend: tiny_skia::BlendMode,
- clip: Option<&Mask>,
-) {
- surface.pixmap.draw_pixmap(
- layer.ox - surface.ox,
- layer.oy - surface.oy,
- layer.pixmap.as_ref(),
- &PixmapPaint {
- opacity,
- blend_mode: blend,
- quality: tiny_skia::FilterQuality::Nearest,
- },
- Transform::identity(),
- clip,
- );
-}
-
#[cfg(test)]
mod test;
diff --git a/crates/fig_engine/src/render/blend.rs b/crates/fig_engine/src/render/blend.rs
new file mode 100644
index 0000000000..748714c6ca
--- /dev/null
+++ b/crates/fig_engine/src/render/blend.rs
@@ -0,0 +1,127 @@
+//! Compositing layers.
+//!
+//! tiny-skia draws one pixmap onto another through its floating-point
+//! pipeline, sampling the source like any image, which costs several times
+//! what blending does. Layers drawn with normal blending (nearly all of
+//! them) blend here instead, with the same results; other blend modes go to
+//! tiny-skia.
+
+use super::Surface;
+use tiny_skia::{BlendMode, Mask, Pixmap, PixmapPaint, PixmapRef, Transform};
+
+/// Draws `layer` onto `surface` at its device position.
+pub(super) fn composite(
+ surface: &mut Surface,
+ layer: &Surface,
+ opacity: f32,
+ blend: BlendMode,
+ clip: Option<&Mask>,
+) {
+ let at = (layer.ox - surface.ox, layer.oy - surface.oy);
+ if blend == BlendMode::SourceOver {
+ draw_over(
+ &mut surface.pixmap,
+ layer.pixmap.as_ref(),
+ at,
+ opacity,
+ clip,
+ );
+ return;
+ }
+ surface.pixmap.draw_pixmap(
+ at.0,
+ at.1,
+ layer.pixmap.as_ref(),
+ &PixmapPaint {
+ opacity,
+ blend_mode: blend,
+ quality: tiny_skia::FilterQuality::Nearest,
+ },
+ Transform::identity(),
+ clip,
+ );
+}
+
+/// Draws premultiplied `src` over `dst` with normal blending, its top-left
+/// corner at `(x, y)` in `dst`, scaled by `opacity` and by `mask` (a
+/// coverage per pixel of `dst`, as tiny-skia's clip masks) where given.
+pub(super) fn draw_over(
+ dst: &mut Pixmap,
+ src: PixmapRef,
+ (x, y): (i32, i32),
+ opacity: f32,
+ mask: Option<&Mask>,
+) {
+ let (dw, dh) = (dst.width() as i32, dst.height() as i32);
+ let (x0, y0) = (x.max(0), y.max(0));
+ let x1 = (x + src.width() as i32).min(dw);
+ let y1 = (y + src.height() as i32).min(dh);
+ if x0 >= x1 || y0 >= y1 || opacity <= 0.0 {
+ return;
+ }
+ // As with tiny-skia, a mask of another size than the destination's
+ // draws nothing.
+ if mask.is_some_and(|m| m.width() as i32 != dw || m.height() as i32 != dh) {
+ return;
+ }
+ let n = (x1 - x0) as usize;
+ let (dst_line, src_line) = (dw as usize * 4, src.width() as usize * 4);
+ let (src, dst) = (src.data(), dst.data_mut());
+ for row in y0..y1 {
+ let d = &mut dst[row as usize * dst_line + x0 as usize * 4..][..n * 4];
+ let s = &src[(row - y) as usize * src_line + (x0 - x) as usize * 4..][..n * 4];
+ match mask {
+ None if opacity >= 1.0 => over(d, s),
+ _ => {
+ let m = mask.map(|m| &m.data()[row as usize * dw as usize + x0 as usize..][..n]);
+ over_scaled(d, s, opacity, m);
+ }
+ }
+ }
+}
+
+/// `x / 255` rounded down, for `x < 65535`.
+#[inline]
+pub(super) fn div255(x: u32) -> u32 {
+ (x + 1 + (x >> 8)) >> 8
+}
+
+/// Premultiplied pixel `s` over `d`: `s + d × (1 - alpha(s))`, rounded.
+#[inline]
+pub(super) fn over_pixel(d: &mut [u8], s: [u8; 4]) {
+ let inv = 255 - u32::from(s[3]);
+ for (d, s) in d.iter_mut().zip(s) {
+ *d = (u32::from(s) + div255(u32::from(*d) * inv + 127)).min(255) as u8;
+ }
+}
+
+/// A row of premultiplied pixels `s` over `d`.
+fn over(d: &mut [u8], s: &[u8]) {
+ for (d, s) in d.chunks_exact_mut(4).zip(s.chunks_exact(4)) {
+ match s[3] {
+ 0 => {}
+ 255 => d.copy_from_slice(s),
+ _ => over_pixel(d, [s[0], s[1], s[2], s[3]]),
+ }
+ }
+}
+
+/// [`over`], the source scaled by `opacity` and per pixel by `mask`.
+fn over_scaled(d: &mut [u8], s: &[u8], opacity: f32, mask: Option<&[u8]>) {
+ for (k, (d, s)) in d.chunks_exact_mut(4).zip(s.chunks_exact(4)).enumerate() {
+ let scale = match mask {
+ Some(m) => opacity * f32::from(m[k]) / 255.0,
+ None => opacity,
+ };
+ if scale <= 0.0 || s[3] == 0 {
+ continue;
+ }
+ let inv = 1.0 - f32::from(s[3]) * scale / 255.0;
+ for (d, &s) in d.iter_mut().zip(s) {
+ *d = (f32::from(s) * scale + f32::from(*d) * inv + 0.5).min(255.0) as u8;
+ }
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/fig_engine/src/render/blend/test.rs b/crates/fig_engine/src/render/blend/test.rs
new file mode 100644
index 0000000000..5cbfa9cd1a
--- /dev/null
+++ b/crates/fig_engine/src/render/blend/test.rs
@@ -0,0 +1,105 @@
+use super::*;
+
+/// Premultiplied pixels of every sort: clear, opaque, and in between.
+fn pixels(w: u32, h: u32, seed: u32) -> Pixmap {
+ let mut p = Pixmap::new(w, h).unwrap();
+ let mut x = seed;
+ for px in p.data_mut().chunks_exact_mut(4) {
+ x ^= x << 13;
+ x ^= x >> 17;
+ x ^= x << 5;
+ let a = match x % 5 {
+ 0 => 0,
+ 1 => 255,
+ _ => (x >> 8) as u8,
+ };
+ px[3] = a;
+ for (k, c) in px[..3].iter_mut().enumerate() {
+ *c = ((u32::from(a) * ((x >> (10 + 5 * k)) & 0xff)) / 255) as u8;
+ }
+ }
+ p
+}
+
+fn mask(w: u32, h: u32) -> Mask {
+ let mut m = Mask::new(w, h).unwrap();
+ for (k, v) in m.data_mut().iter_mut().enumerate() {
+ *v = [0, 255, 7, 128, 200][k % 5];
+ }
+ m
+}
+
+/// The largest channel difference between `draw_over` and tiny-skia's
+/// `draw_pixmap`.
+fn worst(opacity: f32, masked: bool, at: (i32, i32)) -> u8 {
+ let src = pixels(37, 23, 7);
+ let base = pixels(41, 29, 11);
+ let m = mask(41, 29);
+ let m = masked.then_some(&m);
+ let mut ours = base.clone();
+ draw_over(&mut ours, src.as_ref(), at, opacity, m);
+ // tiny-skia draws a row past a source placed above the destination's
+ // top (repeating its last), so it gets the part that shows.
+ let (cx, cy) = ((-at.0).max(0), (-at.1).max(0));
+ let shown = src
+ .clone_rect(
+ tiny_skia::IntRect::from_xywh(
+ cx,
+ cy,
+ src.width() - cx as u32,
+ src.height() - cy as u32,
+ )
+ .unwrap(),
+ )
+ .unwrap();
+ let mut theirs = base;
+ theirs.draw_pixmap(
+ at.0.max(0),
+ at.1.max(0),
+ shown.as_ref(),
+ &PixmapPaint {
+ opacity,
+ blend_mode: BlendMode::SourceOver,
+ quality: tiny_skia::FilterQuality::Nearest,
+ },
+ Transform::identity(),
+ m,
+ );
+ ours.data()
+ .iter()
+ .zip(theirs.data())
+ .map(|(a, b)| a.abs_diff(*b))
+ .max()
+ .unwrap()
+}
+
+#[test]
+fn divides_by_255() {
+ for x in 0..65535 {
+ assert_eq!(div255(x), x / 255, "{x}");
+ }
+}
+
+#[test]
+fn blends_as_tiny_skia_does() {
+ for at in [(0, 0), (3, -2), (-5, 9), (20, 15)] {
+ assert_eq!(worst(1.0, false, at), 0, "opaque at {at:?}");
+ for (opacity, masked) in [(0.37, false), (1.0, true), (0.6, true)] {
+ let d = worst(opacity, masked, at);
+ assert!(
+ d <= 1,
+ "opacity {opacity}, masked {masked}, at {at:?}: off by {d}"
+ );
+ }
+ }
+}
+
+#[test]
+fn keeps_pixels_premultiplied() {
+ let src = pixels(16, 16, 3);
+ let mut dst = pixels(16, 16, 5);
+ draw_over(&mut dst, src.as_ref(), (0, 0), 0.8, Some(&mask(16, 16)));
+ for px in dst.data().chunks_exact(4) {
+ assert!(px[..3].iter().all(|&c| c <= px[3]), "{px:?}");
+ }
+}
diff --git a/crates/fig_engine/src/render/coarse.rs b/crates/fig_engine/src/render/coarse.rs
new file mode 100644
index 0000000000..b0d08ed62b
--- /dev/null
+++ b/crates/fig_engine/src/render/coarse.rs
@@ -0,0 +1,373 @@
+//! Layer blurs and drop shadows rendered coarsely.
+//!
+//! A blur many device pixels wide is smooth, so it loses nothing rendered `k`
+//! times coarser than the device and scaled back up bilinearly where it is
+//! drawn, and it costs about `1/k²` as much. At high zoom, where blurs are
+//! widest, they are otherwise most of a tile's work.
+//!
+//! A node with a layer blur renders its whole layer coarsely, drop shadows
+//! included (they are shadows of the blurred layer). One with drop shadows
+//! and no layer blur renders its content at full resolution where it shows,
+//! and a coarse copy, out to where its shadows reach, to cast them from.
+//! Coarse pixels start at every `k`th device pixel from the page's origin, so
+//! the tiles of a view agree where they meet.
+
+use super::blend::{composite, draw_over};
+use super::effects::{alpha_plane, blur_alpha, blur_pixmap, paint_shadow, resample, spread_by};
+use super::{MAX_MARGIN, Painter, Surface};
+use crate::model::{Affine, Effect, EffectKind, Rect};
+use crate::scene::SceneIdx;
+use tiny_skia::Mask;
+
+/// Blurs render coarsely while their standard deviation stays at least this
+/// many coarse pixels, where three box filters still approximate a Gaussian
+/// closely.
+const MIN_SIGMA: f64 = 8.0;
+
+/// The coarsest a blur renders, in device pixels per coarse pixel.
+const MAX_COARSENESS: u32 = 256;
+
+/// Coarse pixels a coarse plane extends past what its blurs reach: for the
+/// rounding of its box filters, the bilinear filter, fractional spreads and
+/// offsets, and the partly covered pixels at its edges.
+pub(super) const PAD: f64 = 6.0;
+
+/// The coarseness (device pixels per pixel, a power of two) at which a blur
+/// of `sigma` device pixels renders.
+pub(super) fn coarseness_for(sigma: f64) -> u32 {
+ let mut k = 1;
+ while k < MAX_COARSENESS && sigma / f64::from(2 * k) >= MIN_SIGMA {
+ k *= 2;
+ }
+ k
+}
+
+/// The blur at coarseness `k` that matches one of `sigma` device pixels once
+/// scaled up: rendering coarsely averages `k × k` device pixels (a variance of
+/// `k²/12`), and scaling up bilinearly spreads a pixel over `2k` (`k²/6`).
+pub(super) fn coarse_sigma(sigma: f64, k: f64) -> f32 {
+ ((sigma * sigma - k * k / 4.0).max(0.0).sqrt() / k) as f32
+}
+
+/// The standard deviation, in device pixels, of a blur `radius` wide at
+/// `scale` device pixels per unit.
+fn sigma(radius: f32, scale: f64) -> f64 {
+ f64::from(radius) / 2.0 * scale
+}
+
+/// The pixels of an image (`line` bytes to a row, `px` bytes to a pixel)
+/// holding anything but zeros: columns `x0..x1` and rows `y0..y1`.
+fn nonzero_bounds(data: &[u8], line: usize, px: usize) -> Option<[usize; 4]> {
+ let mut bounds: Option<[usize; 4]> = None;
+ for (y, row) in data.chunks_exact(line).enumerate() {
+ let Some(first) = row.iter().position(|&v| v != 0) else {
+ continue;
+ };
+ let last = row.iter().rposition(|&v| v != 0).unwrap_or(first);
+ let (x0, x1) = (first / px, last / px + 1);
+ bounds = Some(match bounds {
+ Some([a, b, c, _]) => [a.min(x0), b, c.max(x1), y + 1],
+ None => [x0, y, x1, y + 1],
+ });
+ }
+ bounds
+}
+
+/// What [`Painter::coarsen`] changes, to put back.
+struct Fine {
+ base: Affine,
+ scale: f64,
+ region: Rect,
+ margin: f64,
+ reach: f64,
+}
+
+impl Painter<'_> {
+ /// How many device pixels to a pixel node `i`'s layer blur, or else its
+ /// drop shadows, render at: 1 for full resolution.
+ pub(super) fn coarseness(&self, i: SceneIdx) -> u32 {
+ let scale = self.scene.node(i).world.scale_factor() * self.scale;
+ let effects = self.props(i).effects();
+ let visible = || effects.iter().filter(|e| e.is_visible());
+ // Blurs compose by adding variances.
+ let blur = visible()
+ .filter(|e| e.kind == EffectKind::LayerBlur && e.radius > 0.0)
+ .map(|e| sigma(e.radius, scale).powi(2))
+ .sum::<f64>()
+ .sqrt();
+ if blur > 0.0 {
+ return coarseness_for(blur);
+ }
+ visible()
+ .filter(|e| e.kind == EffectKind::DropShadow)
+ .map(|e| coarseness_for(sigma(e.radius, scale)))
+ .min()
+ .unwrap_or(1)
+ }
+
+ /// Draws isolated node `i`, whose layer blur or drop shadows render `k`
+ /// times coarser than the device (see [`Painter::coarseness`]).
+ pub(super) fn draw_coarse(
+ &mut self,
+ i: SceneIdx,
+ surface: &mut Surface,
+ clip: Option<&Mask>,
+ k: u32,
+ ) {
+ let node = self.scene.node(i);
+ let props = self.props(i);
+ let scale = node.world.scale_factor() * self.scale;
+ let kf = f64::from(k);
+ let device = self.to_device(&node.bounds);
+ let visible = || props.effects().iter().filter(|e| e.is_visible());
+ let blur = visible()
+ .filter(|e| e.kind == EffectKind::LayerBlur && e.radius > 0.0)
+ .map(|e| sigma(e.radius, scale).powi(2))
+ .sum::<f64>()
+ .sqrt();
+ let shadows: Vec<&Effect> = visible()
+ .filter(|e| e.kind == EffectKind::DropShadow)
+ .collect();
+ let spread = |e: &Effect| {
+ if props.supports_shadow_spread() {
+ f64::from(e.spread) * scale / kf
+ } else {
+ 0.0
+ }
+ };
+ let grid = self.grid();
+ let to_coarse = Affine::scale(1.0 / kf, 1.0 / kf).mul(&Affine::translate(-grid.0, -grid.1));
+ // An unblurred layer's content, at full resolution where it shows.
+ let crisp = if blur > 0.0 {
+ None
+ } else {
+ let Some(mut layer) = self.layer(
+ &device.intersect(&surface.device_rect()),
+ &self.region.outset(self.reach),
+ ) else {
+ return;
+ };
+ self.draw_node_content(i, &mut layer, None, false);
+ Some(layer)
+ };
+ // The coarse layer reaches past the surface as far as the blurs and
+ // shadows do (and no further than any layer around it reaches past
+ // the region).
+ let reach = self.layer_reach(i).min(MAX_MARGIN * kf) / kf + PAD;
+ let bound = to_coarse.map_rect(&surface.device_rect()).outset(reach);
+ let fine = self.coarsen(&to_coarse, kf);
+ self.reach = self.reach.max(reach - self.margin);
+ let mut coarse = self.layer(
+ &self.to_device(&node.bounds),
+ &bound.intersect(&self.region.outset(self.reach)),
+ );
+ if let Some(layer) = coarse.as_mut() {
+ self.draw_node_content(i, layer, None, false);
+ }
+ self.refine(fine);
+ let Some(mut coarse) = coarse else {
+ return;
+ };
+ let (cw, ch) = (
+ coarse.pixmap.width() as usize,
+ coarse.pixmap.height() as usize,
+ );
+ let (cx, cy) = (f64::from(coarse.ox), f64::from(coarse.oy));
+ // The coarse layer's position (coarse pixels from its top-left
+ // corner) of device position `(x, y)`, less `offset`.
+ let coarse_at = |x: i32, y: i32, offset: (f32, f32)| {
+ (
+ (f64::from(x) - f64::from(offset.0) - grid.0) / kf - cx,
+ (f64::from(y) - f64::from(offset.1) - grid.1) / kf - cy,
+ )
+ };
+ let (opacity, blend) = (props.opacity(), props.blend_mode().to_skia());
+
+ if let Some(layer) = crisp {
+ // Shadows cast from the coarse copy, scaled up beneath the crisp
+ // content, over the part of the layer either shows in.
+ let (w, h) = (
+ layer.pixmap.width() as usize,
+ layer.pixmap.height() as usize,
+ );
+ let mut shown = nonzero_bounds(layer.pixmap.data(), w * 4, 4);
+ let source = alpha_plane(&coarse.pixmap);
+ let mut casts = Vec::new();
+ for e in shadows {
+ let mut plane = source.clone();
+ spread_by(&mut plane, cw, ch, spread(e) as f32);
+ blur_alpha(&mut plane, cw, ch, coarse_sigma(sigma(e.radius, scale), kf));
+ let Some([x0, y0, x1, y1]) = nonzero_bounds(&plane, cw, 1) else {
+ continue;
+ };
+ // In layer pixels, with a coarse pixel to spare for the filter.
+ let offset = self.device_vector(i, e.offset);
+ let to_layer = |c: f64, o: f64, d: f32, layer_o: i32, n: usize| {
+ (o + c * kf + f64::from(d) - f64::from(layer_o)).clamp(0.0, n as f64)
+ };
+ let cast = [
+ to_layer(cx + x0 as f64 - 1.0, grid.0, offset.0, layer.ox, w).floor() as usize,
+ to_layer(cy + y0 as f64 - 1.0, grid.1, offset.1, layer.oy, h).floor() as usize,
+ to_layer(cx + x1 as f64 + 1.0, grid.0, offset.0, layer.ox, w).ceil() as usize,
+ to_layer(cy + y1 as f64 + 1.0, grid.1, offset.1, layer.oy, h).ceil() as usize,
+ ];
+ if cast[0] >= cast[2] || cast[1] >= cast[3] {
+ continue;
+ }
+ shown = Some(match shown {
+ Some(b) => [
+ b[0].min(cast[0]),
+ b[1].min(cast[1]),
+ b[2].max(cast[2]),
+ b[3].max(cast[3]),
+ ],
+ None => cast,
+ });
+ casts.push((e, plane, offset));
+ }
+ let Some([x0, y0, x1, y1]) = shown else {
+ return;
+ };
+ let (ox, oy) = (layer.ox + x0 as i32, layer.oy + y0 as i32);
+ let Some(mut with_shadows) = Surface::new(ox, oy, (x1 - x0) as u32, (y1 - y0) as u32)
+ else {
+ return;
+ };
+ let content: Vec<u8> = layer
+ .pixmap
+ .data()
+ .chunks_exact(w * 4)
+ .take(y1)
+ .skip(y0)
+ .flat_map(|row| row[x0 * 4..x1 * 4].iter().skip(3).step_by(4).copied())
+ .collect();
+ for (e, plane, offset) in casts {
+ let mut shadow = vec![0u8; content.len()];
+ resample::<1>(
+ &plane,
+ (cw, ch),
+ &mut shadow,
+ x1 - x0,
+ kf,
+ coarse_at(ox, oy, offset),
+ );
+ paint_shadow(&mut with_shadows.pixmap, &mut shadow, &content, e);
+ }
+ draw_over(
+ &mut with_shadows.pixmap,
+ layer.pixmap.as_ref(),
+ (-(x0 as i32), -(y0 as i32)),
+ 1.0,
+ None,
+ );
+ composite(surface, &with_shadows, opacity, blend, clip);
+ return;
+ }
+
+ blur_pixmap(&mut coarse.pixmap, coarse_sigma(blur, kf));
+ if !shadows.is_empty() {
+ let source = alpha_plane(&coarse.pixmap);
+ let Some(mut with_shadows) = Surface::new(coarse.ox, coarse.oy, cw as u32, ch as u32)
+ else {
+ return;
+ };
+ for e in shadows {
+ let mut plane = source.clone();
+ spread_by(&mut plane, cw, ch, spread(e) as f32);
+ // Moved by the offset, fractions of a coarse pixel included.
+ let (dx, dy) = self.device_vector(i, e.offset);
+ let mut shadow = vec![0u8; plane.len()];
+ resample::<1>(
+ &plane,
+ (cw, ch),
+ &mut shadow,
+ cw,
+ 1.0,
+ (-f64::from(dx) / kf, -f64::from(dy) / kf),
+ );
+ blur_alpha(&mut shadow, cw, ch, (sigma(e.radius, scale) / kf) as f32);
+ paint_shadow(&mut with_shadows.pixmap, &mut shadow, &source, e);
+ }
+ draw_over(
+ &mut with_shadows.pixmap,
+ coarse.pixmap.as_ref(),
+ (0, 0),
+ 1.0,
+ None,
+ );
+ coarse = with_shadows;
+ }
+ // Scaled up where it shows on the surface (with a coarse pixel to
+ // spare for the filter).
+ let Some([x0, y0, x1, y1]) = nonzero_bounds(coarse.pixmap.data(), cw * 4, 4) else {
+ return;
+ };
+ let shown = Rect::new(
+ grid.0 + (cx + x0 as f64 - 1.0) * kf,
+ grid.1 + (cy + y0 as f64 - 1.0) * kf,
+ (x1 - x0 + 2) as f64 * kf,
+ (y1 - y0 + 2) as f64 * kf,
+ )
+ .intersect(&surface.device_rect());
+ if shown.is_empty() {
+ return;
+ }
+ let (x, y) = (shown.x.round() as i32, shown.y.round() as i32);
+ let (w, h) = (shown.w.round() as u32, shown.h.round() as u32);
+ let Some(mut up) = Surface::new(x, y, w, h) else {
+ return;
+ };
+ resample::<4>(
+ coarse.pixmap.data(),
+ (cw, ch),
+ up.pixmap.data_mut(),
+ w as usize,
+ kf,
+ coarse_at(x, y, (0.0, 0.0)),
+ );
+ composite(surface, &up, opacity, blend, clip);
+ }
+
+ /// Where coarse pixels start, every `k`th device pixel from: the device
+ /// pixel of the page's origin.
+ pub(super) fn grid(&self) -> (f64, f64) {
+ (self.base.m02.round(), self.base.m12.round())
+ }
+
+ /// Switches to drawing in pixels `k` times coarser than the device's,
+ /// mapped from device pixels by `to_coarse`.
+ fn coarsen(&mut self, to_coarse: &Affine, k: f64) -> Fine {
+ let fine = Fine {
+ base: self.base,
+ scale: self.scale,
+ region: self.region,
+ margin: self.margin,
+ reach: self.reach,
+ };
+ self.base = to_coarse.mul(&self.base);
+ self.scale /= k;
+ self.region = to_coarse.map_rect(&self.region);
+ self.margin /= k;
+ self.reach /= k;
+ fine
+ }
+
+ /// Switches back to device pixels after [`Painter::coarsen`].
+ fn refine(&mut self, fine: Fine) {
+ let Fine {
+ base,
+ scale,
+ region,
+ margin,
+ reach,
+ } = fine;
+ self.base = base;
+ self.scale = scale;
+ self.region = region;
+ self.margin = margin;
+ self.reach = reach;
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/fig_engine/src/render/coarse/test.rs b/crates/fig_engine/src/render/coarse/test.rs
new file mode 100644
index 0000000000..87037a7808
--- /dev/null
+++ b/crates/fig_engine/src/render/coarse/test.rs
@@ -0,0 +1,431 @@
+use super::*;
+use crate::document::Document;
+use crate::images::ImageStore;
+use crate::render::{RenderOptions, Viewport, render};
+use crate::scene::Scene;
+use crate::testing::{V, color, fig_file, node, size, solid, translate};
+use tiny_skia::Pixmap;
+
+/// The standard normal distribution function (Abramowitz and Stegun 7.1.26,
+/// within 1.5e-7).
+fn phi(x: f64) -> f64 {
+ let z = x.abs() / std::f64::consts::SQRT_2;
+ let t = 1.0 / (1.0 + 0.327_591_1 * z);
+ let poly = ((((1.061_405_429 * t - 1.453_152_027) * t + 1.421_413_741) * t - 0.284_496_736)
+ * t
+ + 0.254_829_592)
+ * t;
+ let erf = 1.0 - poly * (-z * z).exp();
+ if x >= 0.0 {
+ 0.5 * (1.0 + erf)
+ } else {
+ 0.5 * (1.0 - erf)
+ }
+}
+
+/// How much of the box `[x0, x1] × [y0, y1]`, blurred by a Gaussian of
+/// `sigma`, covers point `(x, y)`.
+fn blurred_box(x: f64, y: f64, [x0, y0, x1, y1]: [f64; 4], sigma: f64) -> f64 {
+ (phi((x - x0) / sigma) - phi((x - x1) / sigma))
+ * (phi((y - y0) / sigma) - phi((y - y1) / sigma))
+}
+
+fn effect(kind: &'static str, radius: f32, offset: (f32, f32), spread: f32) -> V {
+ V::Msg(vec![
+ ("type", V::Enum(kind)),
+ ("color", color(0.0, 0.0, 0.0, 0.5)),
+ ("offset", size(offset.0, offset.1)),
+ ("radius", V::Float(radius)),
+ ("spread", V::Float(spread)),
+ ("visible", V::Bool(true)),
+ ])
+}
+
+/// The box the Gaussian tests draw: (100, 80), 60 × 40.
+const BOX: [f64; 4] = [100.0, 80.0, 160.0, 120.0];
+
+fn file(boxes: Vec<(f32, f32, f32, f32, V, Vec<V>)>) -> Vec<u8> {
+ let mut nodes = vec![
+ node(0, None, "DOCUMENT", "Document", vec![]),
+ node(
+ 1,
+ Some((0, "a")),
+ "CANVAS",
+ "Page",
+ vec![("backgroundColor", color(1.0, 1.0, 1.0, 1.0))],
+ ),
+ ];
+ for (k, (x, y, w, h, fill, effects)) in boxes.into_iter().enumerate() {
+ nodes.push(node(
+ 2 + k as u32,
+ Some((1, "a")),
+ "RECTANGLE",
+ "Box",
+ vec![
+ ("size", size(w, h)),
+ ("transform", translate(x, y)),
+ ("fillPaints", V::List(vec![fill])),
+ ("effects", V::List(effects)),
+ ],
+ ));
+ }
+ fig_file(nodes, vec![])
+}
+
+fn draw(bytes: &[u8], vp: Viewport) -> Pixmap {
+ let doc = Document::open(bytes).unwrap();
+ let scene = Scene::build(&doc, doc.pages[0]);
+ let opts = RenderOptions {
+ outline: false,
+ background: Some(doc.page_background(doc.pages[0])),
+ };
+ render(&doc, &scene, &mut ImageStore::default(), &vp, opts).unwrap()
+}
+
+/// A `side`-pixel square view at `scale` centered on page point `(cx, cy)`.
+fn around(cx: f64, cy: f64, scale: f64, side: u32) -> Viewport {
+ let half = f64::from(side) / 2.0 / scale;
+ Viewport {
+ x: cx - half,
+ y: cy - half,
+ scale,
+ width: side,
+ height: side,
+ }
+}
+
+/// The largest difference between a render and `expected` (page point →
+/// RGB), over the pixels `keep` (page point) chooses.
+fn worst(
+ p: &Pixmap,
+ vp: &Viewport,
+ expected: impl Fn(f64, f64) -> [f64; 3],
+ keep: impl Fn(f64, f64) -> bool,
+) -> f64 {
+ let mut worst: f64 = 0.0;
+ for py in 0..p.height() {
+ for px in 0..p.width() {
+ let x = vp.x + (f64::from(px) + 0.5) / vp.scale;
+ let y = vp.y + (f64::from(py) + 0.5) / vp.scale;
+ if !keep(x, y) {
+ continue;
+ }
+ let c = p.pixel(px, py).unwrap().demultiply();
+ let got = [c.red(), c.green(), c.blue()].map(f64::from);
+ for (g, e) in got.iter().zip(expected(x, y)) {
+ worst = worst.max((g - e).abs());
+ }
+ }
+ }
+ worst
+}
+
+/// The pages' canvas color: white.
+const CANVAS: f64 = 255.0;
+
+#[test]
+fn picks_coarseness_by_blur_width() {
+ assert_eq!(coarseness_for(0.0), 1);
+ assert_eq!(coarseness_for(15.9), 1);
+ assert_eq!(coarseness_for(16.0), 2);
+ assert_eq!(coarseness_for(48.0), 4);
+ assert_eq!(coarseness_for(128.0), 16);
+ assert_eq!(coarseness_for(1e9), MAX_COARSENESS);
+}
+
+#[test]
+fn coarse_layer_blurs_match_a_gaussian() {
+ // A black box blurred by 12 (a Gaussian of 6) on the canvas.
+ let bytes = file(vec![(
+ 100.0,
+ 80.0,
+ 60.0,
+ 40.0,
+ solid(0.0, 0.0, 0.0),
+ vec![effect("FOREGROUND_BLUR", 12.0, (0.0, 0.0), 0.0)],
+ )]);
+ // Full resolution, then 2, 4, and 16 times coarser.
+ for (scale, k) in [(2.0, 1), (4.0, 2), (8.0, 4), (32.0, 16)] {
+ assert_eq!(coarseness_for(6.0 * scale), k);
+ // Across the box's left edge, and around its corner.
+ for (cx, cy) in [(100.0, 100.0), (160.0, 120.0)] {
+ let vp = around(cx, cy, scale, 256);
+ let p = draw(&bytes, vp);
+ let d = worst(
+ &p,
+ &vp,
+ |x, y| [CANVAS * (1.0 - blurred_box(x, y, BOX, 6.0)); 3],
+ |_, _| true,
+ );
+ assert!(
+ d <= 4.0,
+ "{scale}× ({k} coarser) at ({cx}, {cy}): off by {d}"
+ );
+ }
+ }
+}
+
+#[test]
+fn coarse_drop_shadows_match_a_gaussian() {
+ // A blue box with a shadow of half-opaque black, blurred by 12, moved
+ // (3.3, 5.7) and spread 2.3: fractions of a pixel at every scale.
+ let (dx, dy, spread) = (3.3, 5.7, 2.3);
+ let bytes = file(vec![(
+ 100.0,
+ 80.0,
+ 60.0,
+ 40.0,
+ solid(0.2, 0.5, 1.0),
+ vec![effect(
+ "DROP_SHADOW",
+ 12.0,
+ (dx as f32, dy as f32),
+ spread as f32,
+ )],
+ )]);
+ let shadow = [
+ BOX[0] - spread + dx,
+ BOX[1] - spread + dy,
+ BOX[2] + spread + dx,
+ BOX[3] + spread + dy,
+ ];
+ for (scale, k) in [(4.0, 2), (8.0, 4), (32.0, 16)] {
+ assert_eq!(coarseness_for(6.0 * scale), k);
+ for (cx, cy) in [(160.0, 100.0), (165.0, 125.0), (130.0, 125.0)] {
+ let vp = around(cx, cy, scale, 256);
+ let p = draw(&bytes, vp);
+ // Outside the box (its anti-aliased edge aside), the canvas
+ // under the shadow; inside, the box alone.
+ let margin = 1.0 / scale;
+ let inside = |x: f64, y: f64| {
+ x > BOX[0] + margin
+ && x < BOX[2] - margin
+ && y > BOX[1] + margin
+ && y < BOX[3] - margin
+ };
+ let outside = |x: f64, y: f64| {
+ x < BOX[0] - margin
+ || x > BOX[2] + margin
+ || y < BOX[1] - margin
+ || y > BOX[3] + margin
+ };
+ let d = worst(
+ &p,
+ &vp,
+ |x, y| [CANVAS * (1.0 - 0.5 * blurred_box(x, y, shadow, 6.0)); 3],
+ outside,
+ );
+ assert!(
+ d <= 4.0,
+ "shadow at {scale}× ({k} coarser) around ({cx}, {cy}): off by {d}"
+ );
+ let d = worst(&p, &vp, |_, _| [51.0, 127.5, 255.0], inside);
+ assert!(d <= 1.0, "box at {scale}× around ({cx}, {cy}): off by {d}");
+ }
+ }
+}
+
+#[test]
+fn inner_shadows_match_a_gaussian() {
+ // A white box with an inner shadow of half-opaque black, blurred by 12,
+ // moved (3.3, 5.7) and spread 2.3, on a white canvas.
+ let (dx, dy, spread) = (3.3, 5.7, 2.3);
+ let bytes = file(vec![(
+ 100.0,
+ 80.0,
+ 60.0,
+ 40.0,
+ solid(1.0, 1.0, 1.0),
+ vec![effect(
+ "INNER_SHADOW",
+ 12.0,
+ (dx as f32, dy as f32),
+ spread as f32,
+ )],
+ )]);
+ // The shadow covers what of the box the box, moved and shrunk by the
+ // spread, blurred, leaves.
+ let hole = [
+ BOX[0] + spread + dx,
+ BOX[1] + spread + dy,
+ BOX[2] - spread + dx,
+ BOX[3] - spread + dy,
+ ];
+ for (scale, k) in [(1.0, 1), (2.0, 1), (4.0, 2), (8.0, 4), (32.0, 16)] {
+ assert_eq!(coarseness_for(6.0 * scale), k);
+ for (cx, cy) in [(100.0, 100.0), (130.0, 80.0), (160.0, 120.0)] {
+ let vp = around(cx, cy, scale, 256);
+ let p = draw(&bytes, vp);
+ let margin = 1.0 / scale;
+ let inside = |x: f64, y: f64| {
+ x > BOX[0] + margin
+ && x < BOX[2] - margin
+ && y > BOX[1] + margin
+ && y < BOX[3] - margin
+ };
+ let d = worst(
+ &p,
+ &vp,
+ |x, y| [CANVAS * (1.0 - 0.5 * (1.0 - blurred_box(x, y, hole, 6.0))); 3],
+ inside,
+ );
+ assert!(
+ d <= 4.0,
+ "{scale}× ({k} coarser) around ({cx}, {cy}): off by {d}"
+ );
+ }
+ }
+}
+
+#[test]
+fn background_blurs_match_a_gaussian() {
+ // A black box under a clear one with a background blur of 12, which
+ // shows the black box blurred inside it.
+ let glass = [90.0, 70.0, 150.0, 110.0];
+ let bytes = file(vec![
+ (100.0, 80.0, 60.0, 40.0, solid(0.0, 0.0, 0.0), vec![]),
+ (
+ 90.0,
+ 70.0,
+ 60.0,
+ 40.0,
+ V::Msg(vec![
+ ("type", V::Enum("SOLID")),
+ ("color", color(1.0, 1.0, 1.0, 1.0)),
+ ("opacity", V::Float(0.0)),
+ ("visible", V::Bool(true)),
+ ]),
+ vec![effect("BACKGROUND_BLUR", 12.0, (0.0, 0.0), 0.0)],
+ ),
+ ]);
+ for (scale, k) in [(2.0, 1), (4.0, 2), (8.0, 4), (32.0, 16)] {
+ assert_eq!(coarseness_for(6.0 * scale), k);
+ for (cx, cy) in [(100.0, 80.0), (130.0, 95.0), (150.0, 110.0)] {
+ let vp = around(cx, cy, scale, 256);
+ let p = draw(&bytes, vp);
+ let margin = 1.0 / scale;
+ let inside = |x: f64, y: f64| {
+ x > glass[0] + margin
+ && x < glass[2] - margin
+ && y > glass[1] + margin
+ && y < glass[3] - margin
+ };
+ let d = worst(
+ &p,
+ &vp,
+ |x, y| [CANVAS * (1.0 - blurred_box(x, y, BOX, 6.0)); 3],
+ inside,
+ );
+ assert!(
+ d <= 4.0,
+ "{scale}× ({k} coarser) around ({cx}, {cy}): off by {d}"
+ );
+ }
+ }
+}
+
+#[test]
+fn coarse_tiles_match_the_whole_render() {
+ // Shadows and blurs 2 to 16 times coarser than the device, crossing
+ // tile edges; one box has both.
+ let bytes = file(vec![
+ (
+ 10.0,
+ 12.0,
+ 30.0,
+ 20.0,
+ solid(0.2, 0.5, 1.0),
+ vec![effect("DROP_SHADOW", 16.0, (6.0, 9.0), 2.0)],
+ ),
+ (
+ 52.0,
+ 44.0,
+ 24.0,
+ 18.0,
+ solid(0.9, 0.3, 0.1),
+ vec![effect("FOREGROUND_BLUR", 20.0, (0.0, 0.0), 0.0)],
+ ),
+ (
+ 30.0,
+ 60.0,
+ 20.0,
+ 14.0,
+ solid(0.1, 0.7, 0.3),
+ vec![effect("FOREGROUND_BLUR", 64.0, (0.0, 0.0), 0.0)],
+ ),
+ (
+ 62.0,
+ 8.0,
+ 16.0,
+ 16.0,
+ solid(0.5, 0.2, 0.8),
+ vec![
+ effect("FOREGROUND_BLUR", 10.0, (0.0, 0.0), 0.0),
+ effect("DROP_SHADOW", 24.0, (-3.5, 4.25), 1.5),
+ ],
+ ),
+ (
+ 8.0,
+ 40.0,
+ 36.0,
+ 30.0,
+ solid(0.95, 0.95, 0.9),
+ vec![
+ effect("INNER_SHADOW", 16.0, (2.5, -3.0), 1.5),
+ effect("INNER_SHADOW", 4.0, (0.0, 1.0), 0.0),
+ ],
+ ),
+ (
+ 40.0,
+ 30.0,
+ 40.0,
+ 36.0,
+ solid(1.0, 1.0, 1.0),
+ vec![effect("BACKGROUND_BLUR", 18.0, (0.0, 0.0), 0.0)],
+ ),
+ ]);
+ let doc = Document::open(&bytes).unwrap();
+ let scene = Scene::build(&doc, doc.pages[0]);
+ let opts = RenderOptions {
+ outline: false,
+ background: Some(doc.page_background(doc.pages[0])),
+ };
+ let mut images = ImageStore::default();
+ let (scale, side, tiles) = (4.0, 48u32, 8);
+ let whole = render(
+ &doc,
+ &scene,
+ &mut images,
+ &Viewport {
+ x: 0.0,
+ y: 0.0,
+ scale,
+ width: side * tiles,
+ height: side * tiles,
+ },
+ opts,
+ )
+ .unwrap();
+ for ty in 0..tiles {
+ for tx in 0..tiles {
+ let (x, y) = (tx * side, ty * side);
+ let vp = Viewport {
+ x: f64::from(x) / scale,
+ y: f64::from(y) / scale,
+ scale,
+ width: side,
+ height: side,
+ };
+ let tile = render(&doc, &scene, &mut images, &vp, opts).unwrap();
+ for py in 0..side {
+ for px in 0..side {
+ assert_eq!(
+ tile.pixel(px, py),
+ whole.pixel(x + px, y + py),
+ "tile {tx},{ty} pixel {px},{py}"
+ );
+ }
+ }
+ }
+ }
+}
diff --git a/crates/fig_engine/src/render/effects.rs b/crates/fig_engine/src/render/effects.rs
index f6d0ae608b..6ec2ebe0fe 100644
--- a/crates/fig_engine/src/render/effects.rs
+++ b/crates/fig_engine/src/render/effects.rs
@@ -3,8 +3,10 @@
//! Figma's blur radius is twice the Gaussian standard deviation (as in CSS
//! `box-shadow`); Gaussians are approximated by three box blurs.
+use super::blend::{div255, over_pixel};
+use super::coarse::{PAD, coarse_sigma, coarseness_for};
use super::{Painter, Surface};
-use crate::model::{Color, Effect, Rect};
+use crate::model::{Affine, Color, Effect, Rect};
use crate::scene::SceneIdx;
use tiny_skia::{Mask, Pixmap, PixmapPaint, Transform};
@@ -145,12 +147,12 @@ fn blur_channels<const C: usize>(data: &mut [u8], w: usize, h: usize, sigma: f32
}
/// Blurs premultiplied RGBA in place.
-pub(crate) fn blur_pixmap(pixmap: &mut Pixmap, sigma: f32) {
+pub(super) fn blur_pixmap(pixmap: &mut Pixmap, sigma: f32) {
let (w, h) = (pixmap.width() as usize, pixmap.height() as usize);
blur_channels::<4>(pixmap.data_mut(), w, h, sigma);
}
-fn blur_alpha(alpha: &mut [u8], w: usize, h: usize, sigma: f32) {
+pub(super) fn blur_alpha(alpha: &mut [u8], w: usize, h: usize, sigma: f32) {
blur_channels::<1>(alpha, w, h, sigma);
}
@@ -206,7 +208,115 @@ fn max_pass(
}
}
-fn alpha_plane(pixmap: &Pixmap) -> Vec<u8> {
+/// [`spread`] by a fraction of a pixel too: between the planes spread by the
+/// whole pixels either side of `r`, weighted by how near each is. Blurred
+/// afterwards, that is as good as moving the edges by the fraction.
+pub(super) fn spread_by(alpha: &mut [u8], w: usize, h: usize, r: f32) {
+ let below = r.floor();
+ let t = ((r - below) * 256.0).round() as u16;
+ let below = below as i32;
+ if t == 0 || t == 256 {
+ spread(alpha, w, h, below + i32::from(t == 256));
+ return;
+ }
+ // Spread as far as the nearer of the two to zero, then a pixel further
+ // for the other (square filters compose by adding their radii).
+ let near = if below >= 0 { below } else { below + 1 };
+ spread(alpha, w, h, near);
+ let mut far = alpha.to_vec();
+ spread(&mut far, w, h, if below >= 0 { 1 } else { -1 });
+ let (lo, hi): (&[u8], &[u8]) = if below >= 0 {
+ (alpha, &far)
+ } else {
+ (&far, alpha)
+ };
+ let mixed: Vec<u8> = lo
+ .iter()
+ .zip(hi)
+ .map(|(&a, &b)| ((u16::from(a) * (256 - t) + u16::from(b) * t + 128) >> 8) as u8)
+ .collect();
+ alpha.copy_from_slice(&mixed);
+}
+
+/// Bilinear resampling of `C`-channel pixels, with zero outside the source:
+/// `dst` (`dw` pixels wide) is `src` (`sw × sh`) scaled up `k` times, with
+/// its first pixel's top-left corner at source position `origin` (source
+/// pixels from the source's top-left corner).
+pub(super) fn resample<const C: usize>(
+ src: &[u8],
+ (sw, sh): (usize, usize),
+ dst: &mut [u8],
+ dw: usize,
+ k: f64,
+ origin: (f64, f64),
+) {
+ // Each output column (row) reads the source column (row) at or left of
+ // its center and the next one, the next weighted by `t` of 256.
+ let taps = |n: usize, o: f64| -> Vec<(isize, u16)> {
+ (0..n)
+ .map(|x| {
+ let u = o + (x as f64 + 0.5) / k - 0.5;
+ let i = u.floor();
+ let t = ((u - i) * 256.0).round() as u16;
+ if t >= 256 {
+ (i as isize + 1, 0)
+ } else {
+ (i as isize, t)
+ }
+ })
+ .collect()
+ };
+ let line = dw * C;
+ if line == 0 {
+ return;
+ }
+ let cols = taps(dw, origin.0);
+ let rows = taps(dst.len() / line, origin.1);
+ let (Some(first), Some(last)) = (rows.first(), rows.last()) else {
+ return;
+ };
+ // Horizontal pass, over the source rows the output reads: weighted sums
+ // of 256 (at most 255 × 256, a u16).
+ let r0 = first.0.clamp(0, sh as isize) as usize;
+ let r1 = (last.0 + 2).clamp(0, sh as isize) as usize;
+ let mut tmp = vec![0u16; (r1.max(r0) - r0) * line];
+ let px = |row: &[u8], i: isize, c: usize| -> u16 {
+ if i >= 0 && (i as usize) < sw {
+ u16::from(row[i as usize * C + c])
+ } else {
+ 0
+ }
+ };
+ for (y, out) in (r0..r1).zip(tmp.chunks_exact_mut(line)) {
+ let row = &src[y * sw * C..(y + 1) * sw * C];
+ for (o, &(i, t)) in out.chunks_exact_mut(C).zip(&cols) {
+ for (c, o) in o.iter_mut().enumerate() {
+ *o = px(row, i, c) * (256 - t) + px(row, i + 1, c) * t;
+ }
+ }
+ }
+ // Vertical pass.
+ let sums = |j: isize| -> Option<&[u16]> {
+ (j >= r0 as isize && j < r1 as isize)
+ .then(|| &tmp[(j as usize - r0) * line..(j as usize - r0 + 1) * line])
+ };
+ for (out, &(j, t)) in dst.chunks_exact_mut(line).zip(&rows) {
+ let (wa, wb) = (u32::from(256 - t), u32::from(t));
+ let mix = |a: u16, b: u16| ((u32::from(a) * wa + u32::from(b) * wb + 32768) >> 16) as u8;
+ match (sums(j), sums(j + 1)) {
+ (Some(a), Some(b)) => {
+ for ((o, &a), &b) in out.iter_mut().zip(a).zip(b) {
+ *o = mix(a, b);
+ }
+ }
+ (Some(a), None) => out.iter_mut().zip(a).for_each(|(o, &a)| *o = mix(a, 0)),
+ (None, Some(b)) => out.iter_mut().zip(b).for_each(|(o, &b)| *o = mix(0, b)),
+ (None, None) => out.fill(0),
+ }
+ }
+}
+
+pub(super) fn alpha_plane(pixmap: &Pixmap) -> Vec<u8> {
pixmap.data().chunks_exact(4).map(|p| p[3]).collect()
}
@@ -229,21 +339,65 @@ fn shifted(plane: &[u8], w: usize, h: usize, dx: i32, dy: i32, fill: u8) -> Vec<
out
}
-fn colorize(alpha: &[u8], color: Color, w: u32, h: u32) -> Option<Pixmap> {
- let mut out = Pixmap::new(w, h)?;
+/// The premultiplied pixel of `color` at each coverage.
+fn shades(color: Color) -> [[u8; 4]; 256] {
let a = color.a.clamp(0.0, 1.0);
let (r, g, b) = (color.r * a, color.g * a, color.b * a);
- for (px, &m) in out.data_mut().chunks_exact_mut(4).zip(alpha) {
- if m == 0 {
- continue;
+ std::array::from_fn(|m| {
+ let k = m as f32 / 255.0;
+ [r, g, b, a].map(|c| (c * k * 255.0 + 0.5) as u8)
+ })
+}
+
+/// Draws coverage `plane` (`w` pixels wide) in `color` onto `dst`, its
+/// top-left corner at `(x, y)`.
+fn paint_coverage(
+ dst: &mut Pixmap,
+ (x, y): (i32, i32),
+ plane: &[u8],
+ w: u32,
+ color: Color,
+ blend: tiny_skia::BlendMode,
+) {
+ let h = plane.len() as u32 / w.max(1);
+ let shades = shades(color);
+ if blend != tiny_skia::BlendMode::SourceOver {
+ let Some(mut out) = Pixmap::new(w, h) else {
+ return;
+ };
+ for (px, &m) in out.data_mut().chunks_exact_mut(4).zip(plane) {
+ px.copy_from_slice(&shades[usize::from(m)]);
+ }
+ dst.draw_pixmap(
+ x,
+ y,
+ out.as_ref(),
+ &PixmapPaint {
+ opacity: 1.0,
+ blend_mode: blend,
+ quality: tiny_skia::FilterQuality::Nearest,
+ },
+ Transform::identity(),
+ None,
+ );
+ return;
+ }
+ let (dw, dh) = (dst.width() as i32, dst.height() as i32);
+ let (x0, x1) = (x.max(0), (x + w as i32).min(dw));
+ if x0 >= x1 {
+ return;
+ }
+ let line = dw as usize * 4;
+ let data = dst.data_mut();
+ for row in y.max(0)..(y + h as i32).min(dh) {
+ let src = &plane[(row - y) as usize * w as usize..][(x0 - x) as usize..(x1 - x) as usize];
+ let out = &mut data[row as usize * line..][x0 as usize * 4..x1 as usize * 4];
+ for (d, &m) in out.chunks_exact_mut(4).zip(src) {
+ if m != 0 {
+ over_pixel(d, shades[usize::from(m)]);
+ }
}
- let k = f32::from(m) / 255.0;
- px[0] = (r * k * 255.0 + 0.5) as u8;
- px[1] = (g * k * 255.0 + 0.5) as u8;
- px[2] = (b * k * 255.0 + 0.5) as u8;
- px[3] = (a * k * 255.0 + 0.5) as u8;
}
- Some(out)
}
/// Draws a drop shadow of `content` into `dst` (same size and position).
@@ -272,38 +426,29 @@ pub(crate) fn drop_shadow(
0,
);
blur_alpha(&mut plane, w, h, (f64::from(e.radius) / 2.0 * scale) as f32);
+ paint_shadow(dst, &mut plane, &source, e);
+}
+
+/// Draws a drop shadow whose alpha is `plane` into `dst` (the same size),
+/// cut out under its node's content (alpha `content`) unless it shows behind
+/// it.
+pub(super) fn paint_shadow(dst: &mut Pixmap, plane: &mut [u8], content: &[u8], e: &Effect) {
if !e.show_behind_node {
- for (s, &a) in plane.iter_mut().zip(&source) {
+ for (s, &a) in plane.iter_mut().zip(content) {
*s = ((u16::from(*s) * u16::from(255 - a) + 127) / 255) as u8;
}
}
- let Some(shadow) = colorize(&plane, e.color, content.width(), content.height()) else {
- return;
- };
- dst.draw_pixmap(
- 0,
- 0,
- shadow.as_ref(),
- &PixmapPaint {
- opacity: 1.0,
- blend_mode: e.blend_mode.to_skia(),
- quality: tiny_skia::FilterQuality::Nearest,
- },
- Transform::identity(),
- None,
- );
+ let w = dst.width();
+ paint_coverage(dst, (0, 0), plane, w, e.color, e.blend_mode.to_skia());
}
impl Painter<'_> {
- /// The node's own shape coverage as an alpha plane over `area` (device
- /// pixels, relative to `surface`).
- fn shape_plane(&self, i: SceneIdx, surface: &Surface, area: (i32, i32, u32, u32)) -> Vec<u8> {
- let (x, y, w, h) = area;
+ /// The node's own shape coverage as a `w × h` alpha plane, `ts` mapping
+ /// the node's coordinates to the plane's.
+ fn shape_plane(&self, i: SceneIdx, ts: Transform, (w, h): (u32, u32)) -> Vec<u8> {
let Some(mut mask) = Mask::new(w, h) else {
return Vec::new();
};
- let ts = Transform::from_translate(-x as f32, -y as f32)
- .pre_concat(self.node_transform(i, surface).to_skia());
let props = self.props(i);
if props.node_type().is_text() {
if let Some(layout) = &props.text_layout {
@@ -356,6 +501,22 @@ impl Painter<'_> {
Some((x0, y0, w, h))
}
+ /// The pixels `k` times coarser than the device's (see `coarse`) that
+ /// cover device rectangle `r`: the first one's position in coarse pixels
+ /// from the grid, and how many across and down.
+ fn coarse_cover(&self, r: &Rect, k: f64) -> ((f64, f64), (usize, usize)) {
+ let grid = self.grid();
+ let (x0, y0) = (((r.x - grid.0) / k).floor(), ((r.y - grid.1) / k).floor());
+ let x1 = ((r.right() - grid.0) / k).ceil();
+ let y1 = ((r.bottom() - grid.1) / k).ceil();
+ (
+ (x0, y0),
+ ((x1 - x0).max(1.0) as usize, (y1 - y0).max(1.0) as usize),
+ )
+ }
+
+ /// Inner shadows: cast inside the node's shape from outside it, offset,
+ /// spread, and blurred, at a fraction of the resolution when wide.
pub(crate) fn inner_shadows(
&mut self,
i: SceneIdx,
@@ -363,70 +524,92 @@ impl Painter<'_> {
surface: &mut Surface,
clip: Option<&Mask>,
) {
- let Some(area) = self.surface_area(i, surface, 1.0) else {
+ // They show inside the node's own shape, where it is on the surface.
+ let Some(shown) = self.surface_area(i, surface, 1.0) else {
return;
};
- let (x, y, w, h) = area;
- let shape = self.shape_plane(i, surface, area);
+ let (x, y, w, h) = shown;
+ let to_surface = self.node_transform(i, surface).to_skia();
+ let shape = self.shape_plane(
+ i,
+ Transform::from_translate(-x as f32, -y as f32).pre_concat(to_surface),
+ (w, h),
+ );
if shape.is_empty() {
return;
}
- let scale = self.scene.node(i).world.scale_factor() * self.scale;
+ let clip = clip.map(|c| sub_mask(c, shown));
+ let world = self.scene.node(i).world;
+ let scale = world.scale_factor() * self.scale;
+ let spreads = self.props(i).supports_shadow_spread();
+ let grid = self.grid();
+ let (ox, oy) = (f64::from(surface.ox + x), f64::from(surface.oy + y));
for e in effects {
- let inverse: Vec<u8> = shape.iter().map(|a| 255 - a).collect();
- let mut plane = inverse;
- spread(
- &mut plane,
- w as usize,
- h as usize,
- if self.props(i).supports_shadow_spread() {
- (f64::from(e.spread) * scale).round() as i32
- } else {
- 0
- },
- );
+ let sigma = f64::from(e.radius) / 2.0 * scale;
+ let k = f64::from(coarseness_for(sigma));
+ let spread = if spreads {
+ f64::from(e.spread) * scale
+ } else {
+ 0.0
+ };
let (dx, dy) = self.device_vector(i, e.offset);
- let mut plane = shifted(
- &plane,
- w as usize,
- h as usize,
- dx.round() as i32,
- dy.round() as i32,
- 255,
- );
- blur_alpha(
+ // Cast from outside the shape as far around what shows as the
+ // shadow reaches, `k` times coarser than the device.
+ let reach =
+ 3.0 * sigma + spread.abs() + f64::from(dx.abs().max(dy.abs())) + 3.0 + PAD * k;
+ let around = Rect::new(ox, oy, f64::from(w), f64::from(h)).outset(reach);
+ let ((cx, cy), (cw, ch)) = self.coarse_cover(&around, k);
+ let to_coarse = Affine::translate(-cx, -cy)
+ .mul(&Affine::scale(1.0 / k, 1.0 / k))
+ .mul(&Affine::translate(-grid.0, -grid.1))
+ .mul(&self.base)
+ .mul(&world);
+ let mut outside = self.shape_plane(i, to_coarse.to_skia(), (cw as u32, ch as u32));
+ if outside.is_empty() {
+ continue;
+ }
+ outside.iter_mut().for_each(|a| *a = 255 - *a);
+ spread_by(&mut outside, cw, ch, (spread / k) as f32);
+ let blur = if k > 1.0 {
+ coarse_sigma(sigma, k)
+ } else {
+ sigma as f32
+ };
+ blur_alpha(&mut outside, cw, ch, blur);
+ // Scaled up over what shows, moved by the offset.
+ let mut plane = vec![0u8; shape.len()];
+ resample::<1>(
+ &outside,
+ (cw, ch),
&mut plane,
w as usize,
- h as usize,
- (f64::from(e.radius) / 2.0 * scale) as f32,
+ k,
+ (
+ (ox - f64::from(dx) - grid.0) / k - cx,
+ (oy - f64::from(dy) - grid.1) / k - cy,
+ ),
);
for (s, &a) in plane.iter_mut().zip(&shape) {
*s = ((u16::from(*s) * u16::from(a) + 127) / 255) as u8;
}
- if let Some(c) = clip {
- let part = sub_mask(c, area);
- for (s, &m) in plane.iter_mut().zip(part.data()) {
+ if let Some(c) = &clip {
+ for (s, &m) in plane.iter_mut().zip(c.data()) {
*s = ((u16::from(*s) * u16::from(m) + 127) / 255) as u8;
}
}
- let Some(shadow) = colorize(&plane, e.color, w, h) else {
- continue;
- };
- surface.pixmap.draw_pixmap(
- x,
- y,
- shadow.as_ref(),
- &PixmapPaint {
- opacity: 1.0,
- blend_mode: e.blend_mode.to_skia(),
- quality: tiny_skia::FilterQuality::Nearest,
- },
- Transform::identity(),
- None,
+ paint_coverage(
+ &mut surface.pixmap,
+ (x, y),
+ &plane,
+ w,
+ e.color,
+ e.blend_mode.to_skia(),
);
}
}
+ /// A background blur: what is under the node's shape, blurred, at a
+ /// fraction of the resolution when wide.
pub(crate) fn background_blur(
&mut self,
i: SceneIdx,
@@ -443,42 +626,121 @@ impl Painter<'_> {
};
let scale = self.scene.node(i).world.scale_factor() * self.scale;
let sigma = f64::from(e.radius) / 2.0 * scale;
- let Some(area) = self.surface_area(i, surface, sigma * 3.0) else {
+ let k = coarseness_for(sigma);
+ let kf = f64::from(k);
+ // The shape, where it is on the surface (and in the clip).
+ let Some(shown) = self.surface_area(i, surface, 1.0) else {
return;
};
- let (x, y, w, h) = area;
- let Some(rect) = tiny_skia::IntRect::from_xywh(x, y, w, h) else {
+ let (x, y, w, h) = shown;
+ let ts = Transform::from_translate(-x as f32, -y as f32)
+ .pre_concat(self.node_transform(i, surface).to_skia());
+ let mut cover = Mask::new(w, h);
+ let Some(cover) = cover.as_mut() else {
return;
};
- let Some(mut backdrop) = surface.pixmap.clone_rect(rect) else {
+ for s in self.fill_shapes(i) {
+ cover.fill_path(s.path(), s.rule(), true, ts);
+ }
+ if let Some(c) = clip {
+ let part = sub_mask(c, shown);
+ for (a, &m) in cover.data_mut().iter_mut().zip(part.data()) {
+ *a = ((u16::from(*a) * u16::from(m) + 127) / 255) as u8;
+ }
+ }
+ // The backdrop, as far around the shape as the blur reaches (the
+ // render's margin holds it), averaged `k` times coarser.
+ let around = Rect::new(
+ f64::from(surface.ox + x),
+ f64::from(surface.oy + y),
+ f64::from(w),
+ f64::from(h),
+ )
+ .outset(3.0 * sigma + 3.0 + PAD * kf);
+ let ((cx, cy), (cw, ch)) = self.coarse_cover(&around, kf);
+ let grid = self.grid();
+ let at = (
+ grid.0 + cx * kf - f64::from(surface.ox),
+ grid.1 + cy * kf - f64::from(surface.oy),
+ );
+ let Some(mut backdrop) = shrink(&surface.pixmap, at, k, (cw, ch)) else {
return;
};
- blur_pixmap(&mut backdrop, sigma as f32);
- let ts = self.node_transform(i, surface);
- let pattern = tiny_skia::Pattern::new(
- backdrop.as_ref(),
- tiny_skia::SpreadMode::Pad,
- tiny_skia::FilterQuality::Nearest,
- 1.0,
- ts.invert()
- .unwrap_or_default()
- .mul(&crate::model::Affine::translate(f64::from(x), f64::from(y)))
- .to_skia(),
+ blur_pixmap(
+ &mut backdrop,
+ if k > 1 {
+ coarse_sigma(sigma, kf)
+ } else {
+ sigma as f32
+ },
);
- let paint = tiny_skia::Paint {
- shader: pattern,
- blend_mode: tiny_skia::BlendMode::Source,
- anti_alias: true,
- force_hq_pipeline: false,
- };
- for s in &self.fill_shapes(i) {
- surface
- .pixmap
- .fill_path(s.path(), &paint, s.rule(), ts.to_skia(), clip);
+ // Scaled up under the shape.
+ let mut blurred = vec![0u8; w as usize * h as usize * 4];
+ resample::<4>(
+ backdrop.data(),
+ (cw, ch),
+ &mut blurred,
+ w as usize,
+ kf,
+ ((f64::from(x) - at.0) / kf, (f64::from(y) - at.1) / kf),
+ );
+ let line = surface.pixmap.width() as usize * 4;
+ let data = surface.pixmap.data_mut();
+ let rows = blurred
+ .chunks_exact(w as usize * 4)
+ .zip(cover.data().chunks_exact(w as usize));
+ for (row, (src, cover)) in rows.enumerate() {
+ let at = (y as usize + row) * line + x as usize * 4;
+ let dst = &mut data[at..at + w as usize * 4];
+ for ((d, s), &m) in dst.chunks_exact_mut(4).zip(src.chunks_exact(4)).zip(cover) {
+ // The backdrop replaces what is under the shape.
+ match m {
+ 0 => {}
+ 255 => d.copy_from_slice(s),
+ _ => {
+ let (m, n) = (u32::from(m), 255 - u32::from(m));
+ for (d, &s) in d.iter_mut().zip(s) {
+ *d = div255(u32::from(s) * m + u32::from(*d) * n + 127) as u8;
+ }
+ }
+ }
+ }
}
}
}
+/// `src` averaged over `k × k` blocks: `w × h` of them, the first one's
+/// top-left corner at `at` (pixels of `src`, whole numbers); outside `src`
+/// counts as transparent.
+fn shrink(src: &Pixmap, at: (f64, f64), k: u32, (w, h): (usize, usize)) -> Option<Pixmap> {
+ let mut out = Pixmap::new(w as u32, h as u32)?;
+ let (sw, sh) = (src.width() as i64, src.height() as i64);
+ let (k, ax, ay) = (i64::from(k), at.0.round() as i64, at.1.round() as i64);
+ let area = (k * k) as u32;
+ let data = src.data();
+ let mut sums = vec![0u32; w * 4];
+ for (q, row) in out.data_mut().chunks_exact_mut(w * 4).enumerate() {
+ sums.fill(0);
+ let top = ay + q as i64 * k;
+ for sy in top.max(0)..(top + k).min(sh) {
+ let line = &data[(sy * sw * 4) as usize..((sy + 1) * sw * 4) as usize];
+ for (p, sum) in sums.chunks_exact_mut(4).enumerate() {
+ let left = ax + p as i64 * k;
+ for sx in left.max(0)..(left + k).min(sw) {
+ let px = &line[(sx * 4) as usize..(sx * 4 + 4) as usize];
+ for (s, &v) in sum.iter_mut().zip(px) {
+ *s += u32::from(v);
+ }
+ }
+ }
+ }
+ for (o, &s) in row.iter_mut().zip(&sums) {
+ *o = ((s + area / 2) / area) as u8;
+ }
+ }
+ Some(out)
+}
+
/// The part of a mask under `area`, as its own mask.
fn sub_mask(mask: &Mask, area: (i32, i32, u32, u32)) -> Mask {
let (x, y, w, h) = area;
diff --git a/crates/fig_engine/src/render/effects/test.rs b/crates/fig_engine/src/render/effects/test.rs
index 3400d02ddf..031e8701c7 100644
--- a/crates/fig_engine/src/render/effects/test.rs
+++ b/crates/fig_engine/src/render/effects/test.rs
@@ -74,3 +74,104 @@ fn divisors_are_exact_below_the_bound() {
}
}
}
+
+/// Bilinear resampling by definition, in floating point.
+fn reference_resample(
+ src: &[u8],
+ (sw, sh): (usize, usize),
+ (dw, dh): (usize, usize),
+ k: f64,
+ (ox, oy): (f64, f64),
+) -> Vec<f64> {
+ let at = |i: f64, j: f64| -> f64 {
+ if i < 0.0 || j < 0.0 || i >= sw as f64 || j >= sh as f64 {
+ 0.0
+ } else {
+ f64::from(src[j as usize * sw + i as usize])
+ }
+ };
+ let mut out = Vec::with_capacity(dw * dh);
+ for y in 0..dh {
+ for x in 0..dw {
+ let u = ox + (x as f64 + 0.5) / k - 0.5;
+ let v = oy + (y as f64 + 0.5) / k - 0.5;
+ let (i, j) = (u.floor(), v.floor());
+ let (s, t) = (u - i, v - j);
+ out.push(
+ (1.0 - t) * ((1.0 - s) * at(i, j) + s * at(i + 1.0, j))
+ + t * ((1.0 - s) * at(i, j + 1.0) + s * at(i + 1.0, j + 1.0)),
+ );
+ }
+ }
+ out
+}
+
+#[test]
+fn resampling_matches_bilinear_filtering() {
+ let (sw, sh) = (13, 9);
+ let src = noise(sw * sh);
+ for (k, origin, (dw, dh)) in [
+ (1.0, (0.0, 0.0), (13, 9)),
+ (1.0, (-0.37, 0.81), (15, 11)),
+ (4.0, (0.0, 0.0), (52, 36)),
+ (4.0, (2.6, -1.3), (40, 30)),
+ (16.0, (-0.75, 3.25), (100, 64)),
+ ] {
+ let mut dst = vec![0u8; dw * dh];
+ resample::<1>(&src, (sw, sh), &mut dst, dw, k, origin);
+ let expected = reference_resample(&src, (sw, sh), (dw, dh), k, origin);
+ for (n, (&got, want)) in dst.iter().zip(&expected).enumerate() {
+ assert!(
+ (f64::from(got) - want).abs() <= 1.0,
+ "{k}× from {origin:?}: pixel {} of {dw}: {got} for {want:.2}",
+ n
+ );
+ }
+ }
+ // Channels resample alike.
+ let rgba: Vec<u8> = src.iter().flat_map(|&v| [v, v / 2, v / 3, v]).collect();
+ let mut one = vec![0u8; 40 * 30];
+ let mut four = vec![0u8; 40 * 30 * 4];
+ resample::<1>(&src, (sw, sh), &mut one, 40, 4.0, (2.6, -1.3));
+ resample::<4>(&rgba, (sw, sh), &mut four, 40, 4.0, (2.6, -1.3));
+ for (a, px) in one.iter().zip(four.chunks_exact(4)) {
+ assert_eq!((*a, px[3]), (px[0], *a));
+ assert!(px[1] <= px[3] && px[2] <= px[3], "stays premultiplied");
+ }
+}
+
+#[test]
+fn fractional_spreads_lie_between_whole_ones() {
+ let (w, h) = (24, 18);
+ let mut base = vec![0u8; w * h];
+ for y in 6..12 {
+ for x in 8..15 {
+ base[y * w + x] = 255;
+ }
+ }
+ base[4 * w + 3] = 128;
+ let spread_to = |r: f32| {
+ let mut a = base.clone();
+ spread_by(&mut a, w, h, r);
+ a
+ };
+ let whole = |r: i32| {
+ let mut a = base.clone();
+ spread(&mut a, w, h, r);
+ a
+ };
+ for r in [-2, -1, 0, 1, 3] {
+ assert_eq!(spread_to(r as f32), whole(r), "spread {r}");
+ }
+ for (r, lo, hi) in [(1.25, 1, 2), (-1.5, -2, -1), (-0.25, -1, 0), (2.75, 2, 3)] {
+ let (a, b, got) = (whole(lo), whole(hi), spread_to(r));
+ for ((&a, &b), &g) in a.iter().zip(&b).zip(&got) {
+ assert!(
+ a.min(b) <= g && g <= a.max(b),
+ "spread {r}: {g} not within {a}..{b}"
+ );
+ }
+ assert_ne!(got, a, "spread {r} moves past {lo}");
+ assert_ne!(got, b, "spread {r} stops short of {hi}");
+ }
+}
diff --git a/crates/fig_engine/src/render/pattern.rs b/crates/fig_engine/src/render/pattern.rs
index 2c99cc2b97..7ddd704133 100644
--- a/crates/fig_engine/src/render/pattern.rs
+++ b/crates/fig_engine/src/render/pattern.rs
@@ -99,6 +99,8 @@ pub(crate) fn render_tile(
reach: 0.0,
masks: Vec::new(),
active_patterns,
+ window: None,
+ skip: Vec::new(),
};
// Neighboring copies contribute when spacing is negative. Rendering
// them into one period makes the shader repeat seamlessly.
diff --git a/crates/fig_engine/src/scene.rs b/crates/fig_engine/src/scene.rs
index 966ed64a64..1796a2304f 100644
--- a/crates/fig_engine/src/scene.rs
+++ b/crates/fig_engine/src/scene.rs
@@ -74,6 +74,10 @@ pub struct Scene {
pub page: NodeIdx,
pub nodes: Vec<SceneNode>,
by_guid: HashMap<Guid, SceneIdx>,
+ /// See [`Scene::paint_times`]; computed on first use. Edits that change
+ /// the tree rebuild the scene, so the times stay valid across
+ /// [`Scene::refresh`].
+ paint_times: std::sync::OnceLock<Box<[(u32, u32)]>>,
}
/// Deepest instance nesting expanded (guards against cyclic components).
@@ -115,6 +119,7 @@ impl Scene {
page,
nodes: b.nodes,
by_guid: b.by_guid,
+ paint_times: std::sync::OnceLock::new(),
};
variables::resolve(doc, &mut scene);
scene.compute_world(doc);
@@ -138,6 +143,7 @@ impl Scene {
page,
nodes: b.nodes,
by_guid: b.by_guid,
+ paint_times: std::sync::OnceLock::new(),
};
variables::resolve(doc, &mut scene);
scene.compute_world(doc);
@@ -230,6 +236,35 @@ impl Scene {
chain
}
+ /// When each node paints, as a walk of the tree in paint order meets it:
+ /// `(enter, exit)` per scene node, all distinct. A node's fills (and
+ /// generated layers) paint when it is entered, the strokes it draws over
+ /// its children when it is left, and everything it holds in between, so
+ /// one node painted before another has the smaller time. Renders of a
+ /// paint-order window ([`crate::render::Layers::Window`]) compare these.
+ pub fn paint_times(&self) -> &[(u32, u32)] {
+ self.paint_times.get_or_init(|| {
+ let mut times = vec![(0u32, 0u32); self.nodes.len()];
+ let mut clock = 0u32;
+ // (node, whether its subtree was walked)
+ let mut stack = vec![(self.root(), false)];
+ while let Some((i, walked)) = stack.pop() {
+ if walked {
+ times[i as usize].1 = clock;
+ clock += 1;
+ continue;
+ }
+ times[i as usize].0 = clock;
+ clock += 1;
+ stack.push((i, true));
+ // Pushed in reverse so the first below is walked first.
+ let below: Vec<SceneIdx> = self.nodes[i as usize].below().collect();
+ stack.extend(below.into_iter().rev().map(|c| (c, false)));
+ }
+ times.into_boxed_slice()
+ })
+ }
+
fn compute_world(&mut self, doc: &Document) {
// Parents precede children in `nodes`, so one forward pass suffices.
for i in 0..self.nodes.len() {
@@ -565,23 +600,27 @@ fn text_bounds(props: &Props, size: Vec2) -> Rect {
/// How far effects reach beyond the node's geometry, in page units.
fn effect_outset(props: &Props, world: &Affine) -> f64 {
- let mut outset: f64 = 0.0;
+ let (mut blur, mut shadow): (f64, f64) = (0.0, 0.0);
for e in props.effects().iter().filter(|e| e.is_visible()) {
- let reach = match e.kind {
+ match e.kind {
EffectKind::DropShadow => {
- f64::from(e.radius) * 1.5
- + if props.supports_shadow_spread() {
- f64::from(e.spread.max(0.0))
- } else {
- 0.0
- }
- + e.offset.x.abs().max(e.offset.y.abs())
+ shadow = shadow.max(
+ f64::from(e.radius) * 1.5
+ + if props.supports_shadow_spread() {
+ f64::from(e.spread.max(0.0))
+ } else {
+ 0.0
+ }
+ + e.offset.x.abs().max(e.offset.y.abs()),
+ );
}
- EffectKind::LayerBlur => f64::from(e.radius) * 1.5,
- _ => 0.0,
- };
- outset = outset.max(reach);
+ EffectKind::LayerBlur => blur += f64::from(e.radius.max(0.0)) * 1.5,
+ _ => {}
+ }
}
+ // Blurs apply one after another, and drop shadows are cast from the
+ // blurred layer: their reaches add up.
+ let outset = blur + shadow;
if outset > 0.0 {
outset * world.scale_factor().max(1e-6)
} else {
diff --git a/crates/fig_engine/src/wasm.rs b/crates/fig_engine/src/wasm.rs
index 401b8c0a28..9032182955 100644
--- a/crates/fig_engine/src/wasm.rs
+++ b/crates/fig_engine/src/wasm.rs
@@ -16,9 +16,9 @@ use crate::edit::{History, Op};
use crate::images::{ImageStore, encode_png};
use crate::inspect;
use crate::model::{Rect, Vec2};
-use crate::render::{self, RenderOptions, Viewport};
+use crate::render::{self, Layers, RenderOptions, Viewport};
use crate::scene::{Scene, SceneIdx};
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
fn js_err(e: impl std::fmt::Display) -> JsError {
@@ -68,6 +68,27 @@ struct NodeGeometry {
bounds: Rect,
}
+/// Which layers a render draws (see [`render::Layers`]); everything when
+/// absent. A spec mixing these, or with any other field, does not parse.
+#[derive(Deserialize)]
+#[serde(untagged, deny_unknown_fields)]
+enum LayersSpec {
+ /// Only these layers (transparent, unclipped by their ancestors), the
+ /// first one's origin drawn at `anchor` (page coordinates) wherever the
+ /// document has it now.
+ Only {
+ only: Vec<String>,
+ anchor: Option<[f64; 2]>,
+ },
+ /// Everything but these layers (opaque, as the whole page).
+ Skip { skip: Vec<String> },
+ /// What paints after `after` (transparent) and before `before`.
+ Window {
+ after: Option<String>,
+ before: Option<String>,
+ },
+}
+
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EditResult {
@@ -405,8 +426,13 @@ impl FigFile {
}
/// Renders `width × height` device pixels showing the page from
- /// `(x, y)` at `scale` pixels per unit, on the page color. Premultiplied
- /// RGBA.
+ /// `(x, y)` at `scale` pixels per unit, on the page color: RGBA, opaque.
+ ///
+ /// `layers` (`LayersSpec` JSON) draws only some layers: what paints in
+ /// a paint-order window, only some layers (the parts `liftPlan`
+ /// describes), or all but some. Only some layers, and a window that
+ /// does not start at the bottom of the page, are drawn on transparency,
+ /// as straight-alpha RGBA.
#[allow(clippy::too_many_arguments)]
pub fn render(
&mut self,
@@ -417,11 +443,49 @@ impl FigFile {
width: u32,
height: u32,
outline: bool,
+ layers: Option<String>,
) -> Result<Vec<u8>, JsError> {
self.page_scene(page)?;
let (_, scene) = self.scene.as_ref().expect("scene built above");
- let background = Some(self.doc.page_background(scene.page));
- let pixmap = render::render(
+ let spec: Option<LayersSpec> = layers
+ .as_deref()
+ .map(serde_json::from_str)
+ .transpose()
+ .map_err(js_err)?;
+ let find = |id: &str| {
+ scene
+ .find(&self.doc, id)
+ .ok_or_else(|| js_err(crate::FigError::NoSuchNode(id.to_owned())))
+ };
+ let layers = match &spec {
+ None => Layers::All,
+ Some(LayersSpec::Only { only: ids, anchor }) => {
+ let nodes: Vec<SceneIdx> =
+ ids.iter().map(|id| find(id)).collect::<Result<_, _>>()?;
+ let shift = match (anchor, nodes.first()) {
+ (Some([x, y]), Some(&first)) => {
+ let world = scene.node(first).world;
+ Vec2::new(x - world.m02, y - world.m12)
+ }
+ _ => Vec2::new(0.0, 0.0),
+ };
+ Layers::Only { nodes, shift }
+ }
+ Some(LayersSpec::Skip { skip: ids }) => {
+ Layers::Skip(ids.iter().map(|id| find(id)).collect::<Result<_, _>>()?)
+ }
+ Some(LayersSpec::Window { after, before }) => Layers::Window {
+ after: after.as_deref().map(find).transpose()?,
+ before: before.as_deref().map(find).transpose()?,
+ },
+ };
+ let opaque = match &layers {
+ Layers::All | Layers::Skip(_) => true,
+ Layers::Window { after, .. } => after.is_none(),
+ Layers::Only { .. } => false,
+ };
+ let background = opaque.then(|| self.doc.page_background(scene.page));
+ let pixmap = render::render_layers(
&self.doc,
scene,
&mut self.images,
@@ -436,9 +500,25 @@ impl FigFile {
outline,
background,
},
+ &layers,
)
.ok_or_else(|| js_err("render failed"))?;
- Ok(pixmap.take())
+ let mut rgba = pixmap.take();
+ if !opaque {
+ render::straight_alpha(&mut rgba);
+ }
+ Ok(rgba)
+ }
+
+ /// How to move layers (`string[]` JSON of ids) by drawing them apart
+ /// from the rest of the page while they are dragged (`LiftPlan` JSON;
+ /// see `inspect::lift_plan`).
+ #[wasm_bindgen(js_name = liftPlan)]
+ pub fn lift_plan(&mut self, page: usize, ids: &str) -> Result<String, JsError> {
+ let ids: Vec<String> = serde_json::from_str(ids).map_err(js_err)?;
+ self.page_scene(page)?;
+ let (_, scene) = self.scene.as_ref().expect("scene built above");
+ to_json(&inspect::lift_plan(&self.doc, scene, &ids))
}
/// Children of a layer (the page when `parent` is absent) for the
diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.md
index cc15636076..cf83ce61cc 100644
--- a/docs/AGENT_GUIDE/documents.md
+++ b/docs/AGENT_GUIDE/documents.md
@@ -1089,7 +1089,10 @@ Layout and test hooks:
zooms, Space-drag or middle-drag pans. When editing: drag a layer to move it
(⌥ drags a copy, ⇧ constrains, edges and centers snap to siblings and the
parent frame with red guides; in an auto layout frame the dragged layer
- takes the slot it is dropped over), drag the selection's corners or edges to
+ takes the slot it is dropped over; the canvas draws the moving layers
+ itself while they are dragged, the design panel's X and Y follow them, and
+ the move is one undo step, which the others in a live design see as it
+ goes), drag the selection's corners or edges to
resize (⇧ keeps proportions; several selected layers scale together), drag
just beyond a corner of one layer to rotate it (⇧ snaps to 15°), draw with the frame, rectangle, ellipse, and
text tools (a click places a default size; new layers go into the frame