printmaker
Version:
Generate PDF documents and from JavaScript objects
100 lines • 3.9 kB
JavaScript
import { parseColor } from './colors.js';
import { asArray, asBoolean, asNonNegNumber, asNumber, asObject, check, getFrom, optional, pickDefined, required, typeError, } from './types.js';
export function parseGraphics(input) {
var _a;
return (_a = asArray(input)) === null || _a === void 0 ? void 0 : _a.map((el, idx) => check(el, `[${idx}]`, parseGraphicsObject));
}
/**
* Parses a given input as a graphics shape object. Throws if the input cannot be parsed.
*
* @param input The input value to parse.
* @returns A graphics shape object.
*/
export function parseGraphicsObject(input) {
const shape = asObject(input);
const type = getFrom(shape, 'type', required(asGraphicsType));
switch (type) {
case 'rect':
return parseRect(shape);
case 'line':
return parseLine(shape);
case 'polyline':
return parsePolyline(shape);
}
}
function asGraphicsType(input) {
if (input === 'rect' || input === 'line' || input === 'polyline')
return input;
throw typeError("'rect', 'line', or 'polyline'", input);
}
function parseRect(input) {
return pickDefined({
type: 'rect',
x: getFrom(input, 'x', required(asNumber)),
y: getFrom(input, 'y', required(asNumber)),
width: getFrom(input, 'width', required(asNumber)),
height: getFrom(input, 'height', required(asNumber)),
strokeWidth: getFrom(input, 'strokeWidth', optional(asNonNegNumber)),
strokeColor: getFrom(input, 'strokeColor', optional(parseColor)),
fillColor: getFrom(input, 'fillColor', optional(parseColor)),
});
}
function parseLine(input) {
return pickDefined({
type: 'line',
x1: getFrom(input, 'x1', required(asNumber)),
x2: getFrom(input, 'x2', required(asNumber)),
y1: getFrom(input, 'y1', required(asNumber)),
y2: getFrom(input, 'y2', required(asNumber)),
strokeWidth: getFrom(input, 'strokeWidth', optional(asNonNegNumber)),
strokeColor: getFrom(input, 'strokeColor', optional(parseColor)),
});
}
function parsePolyline(input) {
return pickDefined({
type: 'polyline',
points: getFrom(input, 'points', required(asPoints)),
closePath: getFrom(input, 'closePath', optional(asBoolean)),
strokeWidth: getFrom(input, 'strokeWidth', optional(asNonNegNumber)),
strokeColor: getFrom(input, 'strokeColor', optional(parseColor)),
fillColor: getFrom(input, 'fillColor', optional(parseColor)),
});
}
/**
* Shifts the a graphics object to a given position by adding the position's `x` and `y` values
* to the coordinates of the graphics object.
*
* @param rect The input graphics object to shift
* @param pos The position to shift to
* @returns The new graphics object
*/
export function shiftGraphicsObject(shape, pos) {
switch (shape.type) {
case 'rect':
return shiftRect(shape, pos);
case 'line':
return shiftLine(shape, pos);
case 'polyline':
return shiftPolyline(shape, pos);
}
}
function shiftRect(rect, pos) {
return Object.assign(Object.assign({}, rect), { x: rect.x + pos.x, y: rect.y + pos.y });
}
function shiftLine(line, pos) {
return Object.assign(Object.assign({}, line), { x1: line.x1 + pos.x, x2: line.x2 + pos.x, y1: line.y1 + pos.y, y2: line.y2 + pos.y });
}
function shiftPolyline(polyline, pos) {
return Object.assign(Object.assign({}, polyline), { points: polyline.points.map((p) => ({ x: p.x + pos.x, y: p.y + pos.y })) });
}
function asPoints(input) {
return asArray(input).map((point, idx) => check(point, `[${idx}]`, asPoint));
}
function asPoint(input) {
const obj = asObject(input);
return {
x: getFrom(obj, 'x', required(asNumber)),
y: getFrom(obj, 'y', required(asNumber)),
};
}
//# sourceMappingURL=graphics.js.map