UNPKG

printmaker

Version:

Generate PDF documents and from JavaScript objects

42 lines 1.73 kB
import { ZERO_EDGES } from './box.js'; import { createAnchorObject } from './layout.js'; export function layoutImage(block, box, doc) { var _a, _b; const padding = (_a = block.padding) !== null && _a !== void 0 ? _a : ZERO_EDGES; const paddingWidth = padding.left + padding.right; const paddingHeight = padding.top + padding.bottom; const fixedWidth = block.width; const fixedHeight = block.height; const image = (_b = doc.images.find((image) => image.name === block.image)) === null || _b === void 0 ? void 0 : _b.pdfImage; if (!image) throw new Error(`Unknown image: ${block.image}`); const xScale = ((fixedWidth !== null && fixedWidth !== void 0 ? fixedWidth : box.width) - paddingWidth) / image.width; const yScale = (fixedHeight - paddingHeight) / image.height; const hasFixedWidth = fixedWidth != null; const hasFixedHeight = fixedHeight != null; const scale = hasFixedWidth ? hasFixedHeight ? Math.min(xScale, yScale) : xScale : hasFixedHeight ? yScale : Math.min(xScale, 1); const imageObj = { type: 'image', x: padding.left, y: padding.top, width: image.width * scale, height: image.height * scale, image, }; const objects = [imageObj, ...(block.id ? [createAnchorObject(block.id)] : [])]; return { type: 'paragraph', x: box.x, y: box.y, width: fixedWidth !== null && fixedWidth !== void 0 ? fixedWidth : box.width, height: fixedHeight !== null && fixedHeight !== void 0 ? fixedHeight : image.height * scale + paddingHeight, objects, }; } //# sourceMappingURL=layout-image.js.map