printmaker
Version:
Generate PDF documents and from JavaScript objects
33 lines • 2.18 kB
JavaScript
import { ZERO_EDGES } from './box.js';
import { createAnchorObject, layoutBlock } from './layout.js';
export function layoutColumns(block, box, doc) {
const fixedWidth = block.width;
const fixedHeight = block.height;
const maxWidth = fixedWidth !== null && fixedWidth !== void 0 ? fixedWidth : box.width;
const maxHeight = fixedHeight !== null && fixedHeight !== void 0 ? fixedHeight : box.height;
const colWidths = block.columns.map((column) => {
var _a, _b, _c, _d;
return column.width == null
? undefined
: column.width + ((_b = (_a = column.margin) === null || _a === void 0 ? void 0 : _a.left) !== null && _b !== void 0 ? _b : 0) + ((_d = (_c = column.margin) === null || _c === void 0 ? void 0 : _c.right) !== null && _d !== void 0 ? _d : 0);
});
const reservedWidth = colWidths.reduce((p, c) => p + (c !== null && c !== void 0 ? c : 0), 0);
const flexColCount = colWidths.reduce((p, c) => p + (c == null ? 1 : 0), 0);
const flexColWidth = flexColCount ? Math.max(0, maxWidth - reservedWidth) / flexColCount : 0;
const children = [];
let colX = 0;
let maxColHeight = 0;
block.columns.forEach((column) => {
var _a, _b, _c;
const margin = (_a = column.margin) !== null && _a !== void 0 ? _a : ZERO_EDGES;
colX += margin.left;
const colWidth = (_b = column.width) !== null && _b !== void 0 ? _b : flexColWidth - margin.left - margin.right;
const colBox = { x: colX, y: margin.top, width: colWidth, height: (_c = column.height) !== null && _c !== void 0 ? _c : maxHeight };
colX += colWidth + margin.right;
const block = layoutBlock(column, colBox, doc);
children.push(block);
maxColHeight = Math.max(maxColHeight, block.height + margin.top + margin.bottom);
});
return Object.assign({ type: 'columns', x: box.x, y: box.y, width: fixedWidth !== null && fixedWidth !== void 0 ? fixedWidth : box.width, height: fixedHeight !== null && fixedHeight !== void 0 ? fixedHeight : maxColHeight, children }, (block.id && { objects: [createAnchorObject(block.id)] }));
}
//# sourceMappingURL=layout-columns.js.map