Skip to content
Merged
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
82 changes: 50 additions & 32 deletions packages/layout-engine/layout-bridge/src/incrementalLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@

const MIN_FOOTNOTE_BODY_HEIGHT = 1;
const DEFAULT_FOOTNOTE_SEPARATOR_SPACING_BEFORE = 12;
const MAX_FOOTNOTE_LAYOUT_PASSES = 4;

const computeMaxFootnoteReserve = (layoutForPages: Layout, pageIndex: number, baseReserve = 0): number => {
const page = layoutForPages.pages?.[pageIndex];
Expand Down Expand Up @@ -739,7 +740,7 @@
// Dirty region computation
const dirtyStart = performance.now();
const dirty = computeDirtyRegions(previousBlocks, nextBlocks);
const dirtyTime = performance.now() - dirtyStart;

Check warning on line 743 in packages/layout-engine/layout-bridge/src/incrementalLayout.ts

View workflow job for this annotation

GitHub Actions / validate

'dirtyTime' is assigned a value but never used. Allowed unused vars must match /^_/u

if (dirty.deletedBlockIds.length > 0) {
measureCache.invalidate(dirty.deletedBlockIds);
Expand Down Expand Up @@ -1662,38 +1663,62 @@
return { columns, idsByColumn };
};

// Pass 1: assign + reserve from current layout.
let { columns: pageColumns, idsByColumn } = resolveFootnoteAssignments(layout);
let { measuresById } = await measureFootnoteBlocks(collectFootnoteIdsByColumn(idsByColumn));
let plan = computeFootnoteLayoutPlan(layout, idsByColumn, measuresById, [], pageColumns);
let reserves = plan.reserves;

// If any reserves, relayout once, then re-assign and inject.
if (reserves.some((h) => h > 0)) {
layout = layoutDocument(currentBlocks, currentMeasures, {
const relayout = (footnoteReservedByPageIndex: number[]) =>
layoutDocument(currentBlocks, currentMeasures, {
...options,
footnoteReservedByPageIndex: reserves,
footnoteReservedByPageIndex,
headerContentHeights,
footerContentHeights,
remeasureParagraph: (block: FlowBlock, maxWidth: number, firstLineIndent?: number) =>
remeasureParagraph(block as ParagraphBlock, maxWidth, firstLineIndent),
});

// Pass 2: recompute assignment and reserves for the updated pagination.
({ columns: pageColumns, idsByColumn } = resolveFootnoteAssignments(layout));
({ measuresById } = await measureFootnoteBlocks(collectFootnoteIdsByColumn(idsByColumn)));
plan = computeFootnoteLayoutPlan(layout, idsByColumn, measuresById, reserves, pageColumns);
reserves = plan.reserves;
// Pass 1: assign + reserve from current layout.
let { columns: pageColumns, idsByColumn } = resolveFootnoteAssignments(layout);
let { measuresById } = await measureFootnoteBlocks(collectFootnoteIdsByColumn(idsByColumn));
let plan = computeFootnoteLayoutPlan(layout, idsByColumn, measuresById, [], pageColumns);
let reserves = plan.reserves;

// Relayout with footnote reserves and iterate until reserves and page count stabilize,
// so each page gets the correct reserve (avoids "too much" on one page and "not enough" on another).
if (reserves.some((h) => h > 0)) {
let reservesStabilized = false;
const seenReserveKeys = new Set<string>([reserves.join(',')]);
for (let pass = 0; pass < MAX_FOOTNOTE_LAYOUT_PASSES; pass += 1) {
layout = relayout(reserves);
({ columns: pageColumns, idsByColumn } = resolveFootnoteAssignments(layout));
({ measuresById } = await measureFootnoteBlocks(collectFootnoteIdsByColumn(idsByColumn)));
plan = computeFootnoteLayoutPlan(layout, idsByColumn, measuresById, reserves, pageColumns);
const nextReserves = plan.reserves;
const reservesStable =
Comment thread
harbournick marked this conversation as resolved.
nextReserves.length === reserves.length &&
nextReserves.every((h, i) => (reserves[i] ?? 0) === h) &&
reserves.every((h, i) => (nextReserves[i] ?? 0) === h);
if (reservesStable) {
reserves = nextReserves;
reservesStabilized = true;
break;
}
// Detect oscillation: if we've produced a reserve vector we already tried,
// the loop will never converge. Break early to avoid wasted relayout passes.
const nextKey = nextReserves.join(',');
if (seenReserveKeys.has(nextKey)) {
break;
}
seenReserveKeys.add(nextKey);
// Only update reserves when we will do another layout pass; otherwise layout
// would be built with the previous reserves while reserves would be nextReserves,
// and the plan/injection phase could place footnotes in the wrong band.
if (pass < MAX_FOOTNOTE_LAYOUT_PASSES - 1) {
reserves = nextReserves;
}
}
if (!reservesStabilized) {
console.warn(
`[incrementalLayout] Footnote reserve loop did not converge (max ${MAX_FOOTNOTE_LAYOUT_PASSES} passes); layout may have suboptimal footnote placement.`,
);
}

// Apply final reserves (best-effort second relayout) then inject fragments.
layout = layoutDocument(currentBlocks, currentMeasures, {
...options,
footnoteReservedByPageIndex: reserves,
headerContentHeights,
footerContentHeights,
remeasureParagraph: (block: FlowBlock, maxWidth: number, firstLineIndent?: number) =>
remeasureParagraph(block as ParagraphBlock, maxWidth, firstLineIndent),
});
let { columns: finalPageColumns, idsByColumn: finalIdsByColumn } = resolveFootnoteAssignments(layout);
let { blocks: finalBlocks, measuresById: finalMeasuresById } = await measureFootnoteBlocks(
collectFootnoteIdsByColumn(finalIdsByColumn),
Expand All @@ -1712,14 +1737,7 @@
finalReserves.some((h, i) => (reserves[i] ?? 0) !== h) ||
reserves.some((h, i) => (finalReserves[i] ?? 0) !== h);
if (reservesDiffer) {
layout = layoutDocument(currentBlocks, currentMeasures, {
...options,
footnoteReservedByPageIndex: finalReserves,
headerContentHeights,
footerContentHeights,
remeasureParagraph: (block: FlowBlock, maxWidth: number, firstLineIndent?: number) =>
remeasureParagraph(block as ParagraphBlock, maxWidth, firstLineIndent),
});
layout = relayout(finalReserves);
reservesAppliedToLayout = finalReserves;
({ columns: finalPageColumns, idsByColumn: finalIdsByColumn } = resolveFootnoteAssignments(layout));
({ blocks: finalBlocks, measuresById: finalMeasuresById } = await measureFootnoteBlocks(
Expand Down
6 changes: 3 additions & 3 deletions packages/layout-engine/layout-bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,9 @@ export const hitTestTableFragment = (
const blockEndY = blockStartY + blockHeight;

// Calculate position within the cell (accounting for cell padding)
const padding = cell.attrs?.padding ?? { top: 2, left: 4, right: 4, bottom: 2 };
const padding = cell.attrs?.padding ?? { top: 0, left: 4, right: 4, bottom: 0 };
const cellLocalX = localX - colX - (padding.left ?? 4);
const cellLocalY = localY - rowY - (padding.top ?? 2);
const cellLocalY = localY - rowY - (padding.top ?? 0);
const paragraphBlock = cellBlock as ParagraphBlock;
const paragraphMeasure = cellBlockMeasure as ParagraphMeasure;

Expand Down Expand Up @@ -1339,7 +1339,7 @@ type TableRowBlock = TableBlock['rows'][number];
type TableCellBlock = TableRowBlock['cells'][number];
type TableCellMeasure = TableMeasure['rows'][number]['cells'][number];

const DEFAULT_CELL_PADDING = { top: 2, bottom: 2, left: 4, right: 4 };
const DEFAULT_CELL_PADDING = { top: 0, bottom: 0, left: 4, right: 4 };

const getCellPaddingFromRow = (cellIdx: number, row?: TableRowBlock) => {
const padding = row?.cells?.[cellIdx]?.attrs?.padding ?? {};
Expand Down
210 changes: 210 additions & 0 deletions packages/layout-engine/layout-bridge/test/footnoteMultiPass.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import { describe, it, expect, vi } from 'vitest';
import type { FlowBlock, Measure } from '@superdoc/contracts';
import * as layoutEngine from '@superdoc/layout-engine';
import { incrementalLayout } from '../src/incrementalLayout';

/**
* Builds a paragraph with pmStart/pmEnd so footnote ref position can be resolved to a page.
*/
const makeParagraph = (id: string, text: string, pmStart: number): FlowBlock => ({
kind: 'paragraph',
id,
runs: [{ text, fontFamily: 'Arial', fontSize: 12, pmStart, pmEnd: pmStart + text.length }],
});

const makeMeasure = (lineHeight: number, textLength: number): Measure => ({
kind: 'paragraph',
lines: [
{
fromRun: 0,
fromChar: 0,
toRun: 0,
toChar: textLength,
width: 200,
ascent: lineHeight * 0.8,
descent: lineHeight * 0.2,
lineHeight,
},
],
totalHeight: lineHeight,
});

/** Multi-line paragraph measure so footnote reserve height is large enough to shift content. */
const makeMultiLineMeasure = (lineHeight: number, lineCount: number): Measure => {
const lines = Array.from({ length: lineCount }, (_, i) => ({
fromRun: 0,
fromChar: i,
toRun: 0,
toChar: i + 1,
width: 200,
ascent: lineHeight * 0.8,
descent: lineHeight * 0.2,
lineHeight,
}));
return {
kind: 'paragraph',
lines,
totalHeight: lineCount * lineHeight,
};
};

/**
* Scenario that forces the footnote reserve loop to run multiple passes:
* - Content height is small (240px). Body has 12 one-line blocks (20px each) so they
* exactly fill page 1 with no reserve.
* - Footnote ref is in the last body block (ref on page 1 in pass 1).
* - Footnote is tall (5 lines) so its reserve is ~80px. When we relayout with that
* reserve on page 1, content height becomes 160px → only 8 lines fit → the ref
* moves to page 2. So pass 2 assigns the footnote to page 2 and reserves there.
* This catches regressions where layout and reserves get out of sync (e.g. wrong
* reserve vector used for layout when max passes hit).
*/
describe('Footnote multi-pass reserve loop', () => {
it('runs multiple layout passes when footnotes shift pages and stabilizes correctly', async () => {
const BODY_LINE_HEIGHT = 20;
const FOOTNOTE_LINE_HEIGHT = 12;
const LINES_ON_PAGE_1_WITHOUT_RESERVE = 12;
const FOOTNOTE_LINES = 5;

let pos = 0;
const bodyBlocks: FlowBlock[] = [];
for (let i = 0; i < LINES_ON_PAGE_1_WITHOUT_RESERVE; i += 1) {
const text = `Line ${i + 1}.`;
bodyBlocks.push(makeParagraph(`body-${i}`, text, pos));
pos += text.length + 1; // +1 for implied break
}
// Ref in last body block (so on page 1 when no reserve, then moves to page 2 when we reserve)
const refPos = pos - 2; // inside last paragraph
const footnoteBlock = makeParagraph(
'footnote-1-0-paragraph',
'Footnote content that spans multiple lines here.',
0,
);

const measureBlock = vi.fn(async (block: FlowBlock) => {
if (block.id.startsWith('footnote-')) {
return makeMultiLineMeasure(FOOTNOTE_LINE_HEIGHT, FOOTNOTE_LINES);
}
const textLength = block.kind === 'paragraph' ? (block.runs?.[0]?.text?.length ?? 1) : 1;
return makeMeasure(BODY_LINE_HEIGHT, textLength);
});

// Content height 240px: 12 * 20 = 240. With ~80px reserve → 160px → 8 lines on page 1.
const contentHeight = 240;
const margins = { top: 72, right: 72, bottom: 72, left: 72 };
const pageHeight = contentHeight + margins.top + margins.bottom;

const layoutDocSpy = vi.spyOn(layoutEngine, 'layoutDocument');

const result = await incrementalLayout(
[],
null,
bodyBlocks,
{
pageSize: { w: 612, h: pageHeight },
margins,
footnotes: {
refs: [{ id: '1', pos: refPos }],
blocksById: new Map([['1', [footnoteBlock]]]),
topPadding: 4,
dividerHeight: 2,
},
},
measureBlock,
);

const footnoteReserveCalls = layoutDocSpy.mock.calls.filter((call) =>
(call[2] as { footnoteReservedByPageIndex?: number[] })?.footnoteReservedByPageIndex?.some((h) => h > 0),
);
layoutDocSpy.mockRestore();

expect(footnoteReserveCalls.length).toBeGreaterThanOrEqual(2);

const { layout } = result;
expect(layout.pages.length).toBeGreaterThanOrEqual(2);

const page2 = layout.pages[1];
expect(page2.footnoteReserved).toBeGreaterThan(0);

const footnoteFragment = layout.pages.flatMap((p) => p.fragments).find((f) => f.blockId === footnoteBlock.id);
expect(footnoteFragment).toBeTruthy();
const pageOfFootnote = layout.pages.find((p) => p.fragments.some((f) => f.blockId === footnoteBlock.id));
expect(pageOfFootnote).toBe(page2);

// Sanity: footnote band does not overlap body (reserve is at bottom; body content ends above it)
const bodyFragmentsOnPage2 = page2.fragments.filter(
(f) => f.blockId !== footnoteBlock.id && !String(f.blockId).startsWith('footnote-separator'),
);
const footnoteBandTop =
(page2.size?.h ?? pageHeight) - (page2.margins?.bottom ?? margins.bottom) - (page2.footnoteReserved ?? 0);
for (const f of bodyFragmentsOnPage2) {
const fragBottom =
'y' in f && typeof f.y === 'number' && 'height' in f
? f.y + (f.height as number)
: ((f as { y?: number }).y ?? 0);
expect(fragBottom).toBeLessThanOrEqual(footnoteBandTop + 1);
}
});

it('does not exhaust max reserve passes when reserves oscillate between pages', async () => {
const BODY_LINE_HEIGHT = 20;
const FOOTNOTE_LINE_HEIGHT = 12;
const LINES_ON_PAGE_1_WITHOUT_RESERVE = 12;
const FOOTNOTE_LINES = 5;

let pos = 0;
const bodyBlocks: FlowBlock[] = [];
for (let i = 0; i < LINES_ON_PAGE_1_WITHOUT_RESERVE; i += 1) {
const text = `Line ${i + 1}.`;
bodyBlocks.push(makeParagraph(`body-${i}`, text, pos));
pos += text.length + 1;
}

const refPos = pos - 2;
const footnoteBlock = makeParagraph(
'footnote-1-0-paragraph',
'Footnote content that spans multiple lines here.',
0,
);

const measureBlock = vi.fn(async (block: FlowBlock) => {
if (block.id.startsWith('footnote-')) {
return makeMultiLineMeasure(FOOTNOTE_LINE_HEIGHT, FOOTNOTE_LINES);
}
const textLength = block.kind === 'paragraph' ? (block.runs?.[0]?.text?.length ?? 1) : 1;
return makeMeasure(BODY_LINE_HEIGHT, textLength);
});

const contentHeight = 240;
const margins = { top: 72, right: 72, bottom: 72, left: 72 };
const pageHeight = contentHeight + margins.top + margins.bottom;

const layoutDocSpy = vi.spyOn(layoutEngine, 'layoutDocument');

await incrementalLayout(
[],
null,
bodyBlocks,
{
pageSize: { w: 612, h: pageHeight },
margins,
footnotes: {
refs: [{ id: '1', pos: refPos }],
blocksById: new Map([['1', [footnoteBlock]]]),
topPadding: 4,
dividerHeight: 2,
},
},
measureBlock,
);

const footnoteReserveCalls = layoutDocSpy.mock.calls.filter((call) =>
(call[2] as { footnoteReservedByPageIndex?: number[] })?.footnoteReservedByPageIndex?.some((h) => h > 0),
);
layoutDocSpy.mockRestore();

// Current regression: this scenario oscillates A -> B -> A and runs all passes (+ final relayout).
// Desired behavior: detect oscillation and stop early.
expect(footnoteReserveCalls.length).toBeLessThanOrEqual(3);
});
});
Loading
Loading