printmaker
Version:
Generate PDF documents and from JavaScript objects
38 lines (36 loc) • 1.35 kB
text/typescript
import { Box, ZERO_EDGES } from './box.js';
import { Document } from './document.js';
import { createAnchorObject, Frame, layoutBlock } from './layout.js';
import { Rows } from './text.js';
export function layoutRows(block: Rows, box: Box, doc: Document): Frame {
const fixedWidth = block.width;
const fixedHeight = block.height;
const maxWidth = fixedWidth ?? box.width;
const maxHeight = fixedHeight ?? box.height;
const children = [];
let rowY = 0;
let lastMargin = 0;
let aggregatedHeight = 0;
let remainingHeight = maxHeight;
block.rows.forEach((row) => {
const margin = row.margin ?? ZERO_EDGES;
const topMargin = Math.max(lastMargin, margin.top);
lastMargin = margin.bottom;
const nextPos = { x: margin.left, y: rowY + topMargin };
const maxSize = { width: maxWidth - margin.left - margin.right, height: remainingHeight };
const frame = layoutBlock(row, { ...nextPos, ...maxSize }, doc);
children.push(frame);
rowY += topMargin + frame.height;
remainingHeight -= topMargin + frame.height;
aggregatedHeight += topMargin + frame.height;
});
return {
type: 'rows',
x: box.x,
y: box.y,
width: fixedWidth ?? box.width,
height: fixedHeight ?? aggregatedHeight + lastMargin,
children,
...(block.id && { objects: [createAnchorObject(block.id)] }),
};
}