printmaker
Version:
Generate PDF documents and from JavaScript objects
81 lines • 3.58 kB
JavaScript
import { isObject, typeError } from './types.js';
export const ZERO_EDGES = Object.freeze(parseEdges(0));
/**
* Computes an inner box by subtracting the given space (e.g. a padding) from the given box.
*
* @param box The outer box.
* @param edges The space to subtract.
* @returns The resulting inner box.
*/
export function subtractEdges(box, edges) {
var _a, _b, _c, _d, _e, _f;
return {
x: box.x + ((_a = edges === null || edges === void 0 ? void 0 : edges.left) !== null && _a !== void 0 ? _a : 0),
y: box.y + ((_b = edges === null || edges === void 0 ? void 0 : edges.top) !== null && _b !== void 0 ? _b : 0),
width: Math.max(0, box.width - ((_c = edges === null || edges === void 0 ? void 0 : edges.left) !== null && _c !== void 0 ? _c : 0) - ((_d = edges === null || edges === void 0 ? void 0 : edges.right) !== null && _d !== void 0 ? _d : 0)),
height: Math.max(0, box.height - ((_e = edges === null || edges === void 0 ? void 0 : edges.top) !== null && _e !== void 0 ? _e : 0) - ((_f = edges === null || edges === void 0 ? void 0 : edges.bottom) !== null && _f !== void 0 ? _f : 0)),
};
}
/**
* Parses a given value as a box lengths definition (`BoxLengths | Length`) to the lengths of the
* edges.
* Throws if the given input is invalid.
*
* @param input the input value
* @returns an object with the four edges in `pt`, or `undefined` if the input is missing or `null`
*/
export function parseEdges(input) {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (typeof input === 'number' || typeof input === 'string') {
const value = parseLength(input);
return { right: value, left: value, top: value, bottom: value };
}
if (isObject(input)) {
const obj = input;
return {
right: parseLength((_b = (_a = obj.right) !== null && _a !== void 0 ? _a : obj.x) !== null && _b !== void 0 ? _b : 0),
left: parseLength((_d = (_c = obj.left) !== null && _c !== void 0 ? _c : obj.x) !== null && _d !== void 0 ? _d : 0),
top: parseLength((_f = (_e = obj.top) !== null && _e !== void 0 ? _e : obj.y) !== null && _f !== void 0 ? _f : 0),
bottom: parseLength((_h = (_g = obj.bottom) !== null && _g !== void 0 ? _g : obj.y) !== null && _h !== void 0 ? _h : 0),
};
}
throw typeError('number, length string, or object', input);
}
/**
* Converts a length definition into the corresponding value in points (`pt`).
* Throws if the given value is not a valid length.
*
* @param input the input value
* @returns the length in `pt`, or `undefined` if the input is missing or `null`
*/
export function parseLength(input) {
if (typeof input === 'number' && Number.isFinite(input)) {
return input;
}
if (typeof input === 'string') {
const unit = input.slice(-2);
if (unit === 'pt' || unit === 'in' || unit === 'mm' || unit === 'cm') {
const value = parseFloat(input.slice(0, -2));
if (Number.isFinite(value)) {
return convertToPt(value, unit);
}
}
}
throw typeError('number or length string', input);
}
function convertToPt(value, fromUnit) {
// 1in = 72pt = 25.4mm
switch (fromUnit) {
case 'pt':
return value * 1;
case 'in':
return value * 72;
case 'mm':
return (value * 72) / 25.4;
case 'cm':
return (value * 72) / 2.54;
default:
throw new TypeError(`Invalid unit: '${fromUnit}'`);
}
}
//# sourceMappingURL=box.js.map