Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/main/screenshots/__tests__/capture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,57 @@ describe("captureScreenshots — failed frames", () => {
expect(result).toEqual({ delivered: 1, failed: 0 });
});
});

describe("captureScreenshots — export scale", () => {
function setup(frameWidth: number, frameHeight: number) {
const page = makePage("page-1");
const frame = makeFrame("f-1", page);
frame.absoluteBoundingBox = { x: 0, y: 0, width: frameWidth, height: frameHeight };
const text = makeTextNode("t-1", frame);
text.absoluteBoundingBox = { x: 6000, y: 3000, width: 1200, height: 600 };
installFigma(async (id) => (id === "t-1" ? text : null));
return frame;
}

it("exports frames up to 3 million pixels at 100% with positions untouched", async () => {
const frame = setup(1500, 2000);

const delivered: FrameScreenshot[] = [];
await captureScreenshots(["t-1"], (s) => delivered.push(s));

expect(frame.exportAsync).toHaveBeenCalledWith({
format: "PNG",
constraint: { type: "SCALE", value: 1 },
});
expect(delivered[0]?.keys[0]).toMatchObject({ x: 6000, y: 3000, width: 1200, height: 600 });
});

it("shrinks bigger frames to 3 million pixels and scales key positions to match", async () => {
const frame = setup(12000, 12000);

const delivered: FrameScreenshot[] = [];
await captureScreenshots(["t-1"], (s) => delivered.push(s));

const scale = Math.sqrt(3_000_000 / 144_000_000);
expect(frame.exportAsync).toHaveBeenCalledWith({
format: "PNG",
constraint: { type: "SCALE", value: expect.closeTo(scale, 6) },
});
expect(delivered[0]?.info).toMatchObject({ width: 1732, height: 1732 });
expect(delivered[0]?.keys[0]).toMatchObject({ x: 866, y: 433, width: 173, height: 87 });
});

it("exports at 100% when the frame has no bounding box or no area", async () => {
for (const box of [null, { x: 0, y: 0, width: 0, height: 0 }]) {
const frame = setup(1, 1);
frame.absoluteBoundingBox = box as never;

await captureScreenshots(["t-1"], () => {});

expect(frame.exportAsync).toHaveBeenCalledWith({
format: "PNG",
constraint: { type: "SCALE", value: 1 },
});
}
});
});
31 changes: 18 additions & 13 deletions src/main/screenshots/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import type { FrameInfo, FrameScreenshot, NodeInfo } from "$shared/types";
import { groupNodesByFrame } from "./groupByFrame";
import { getNodeRelativePosition } from "./position";

// Tolgee renders position overlays against the exported image at 1x. The node
// x/y/width/height we send come from `absoluteBoundingBox` (in design-space
// pixels) so the screenshot must match — exporting at 2x while keeping
// positions at 1x makes the overlay boxes appear half-size and offset. The
// legacy plugin exported at 1x for the same reason.
const EXPORT_SCALE = 1;
// Tolgee stores screenshots at no more than this many pixels
// (`ImageProcessor.MAX_STORED_PIXELS` on the server). Key positions must be
// scaled by the same factor as the export: Tolgee overlays them on the image
// it received.
const MAX_EXPORT_PIXELS = 3_000_000;

function exportScale(box: Rect | null): number {
if (!box) return 1;
return Math.min(1, Math.sqrt(MAX_EXPORT_PIXELS / (box.width * box.height)));
}

/**
* How many frames we may export back-to-back before yielding to the event
Expand Down Expand Up @@ -56,15 +60,15 @@ async function ensurePageLoaded(node: BaseNode): Promise<void> {
}

/**
* Export a single frame to a PNG byte buffer at `EXPORT_SCALE`. The caller is
* Export a single frame to a PNG byte buffer at `scale`. The caller is
* responsible for guaranteeing the frame's page has been loaded — we still
* defensively load it here so the helper is safe to use in isolation.
*/
export async function exportFrame(frame: FrameNode): Promise<Uint8Array> {
export async function exportFrame(frame: FrameNode, scale = 1): Promise<Uint8Array> {
await ensurePageLoaded(frame);
return frame.exportAsync({
format: "PNG",
constraint: { type: "SCALE", value: EXPORT_SCALE },
constraint: { type: "SCALE", value: scale },
});
}

Expand Down Expand Up @@ -133,17 +137,18 @@ export async function captureScreenshots(
await ensurePageLoaded(frame);

const frameBox = frame.absoluteBoundingBox;
const image = await exportFrame(frame);
const scale = exportScale(frameBox);
const image = await exportFrame(frame, scale);

const info: FrameInfo = {
id: frame.id,
name: frame.name,
width: frameBox?.width ?? 0,
height: frameBox?.height ?? 0,
width: Math.round((frameBox?.width ?? 0) * scale),
height: Math.round((frameBox?.height ?? 0) * scale),
};

const keys: FrameScreenshot["keys"] = nodes.map((node) => {
const position = getNodeRelativePosition(node, frame);
const position = getNodeRelativePosition(node, frame, scale);
const nodeInfo: NodeInfo = getNodeInfo(node);
return {
...nodeInfo,
Expand Down
15 changes: 8 additions & 7 deletions src/main/screenshots/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
*
* Both nodes expose `absoluteBoundingBox` in document space; subtracting the
* frame origin yields coordinates suitable for overlaying the key marker on
* the exported PNG. If either bounding box is unavailable (rare — e.g. nodes
* that have just been detached) we fall back to a zero-position so callers
* never have to special-case null.
* the exported PNG; `scale` is the scale the PNG was exported at. If either
* bounding box is unavailable (rare — e.g. nodes that have just been detached)
* we fall back to a zero-position so callers never have to special-case null.
*/
export function getNodeRelativePosition(
node: SceneNode,
frame: FrameNode,
scale = 1,
): { x: number; y: number; width: number; height: number } {
const nodeBox = node.absoluteBoundingBox;
const frameBox = frame.absoluteBoundingBox;
if (!nodeBox || !frameBox) {
return { x: 0, y: 0, width: 0, height: 0 };
}
return {
x: nodeBox.x - frameBox.x,
y: nodeBox.y - frameBox.y,
width: nodeBox.width,
height: nodeBox.height,
x: Math.round((nodeBox.x - frameBox.x) * scale),
y: Math.round((nodeBox.y - frameBox.y) * scale),
width: Math.round(nodeBox.width * scale),
height: Math.round(nodeBox.height * scale),
};
}
Loading