UNPKG

pptx-automizer

Version:

A template based pptx generator

53 lines 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeSrcRectForNewImage = exports.inferContainerAr = void 0; const K = 100000; // Convert the source rectangle to fractions of the image width and height for easier calculation function toFractions(src) { var _a, _b, _c, _d; return { l: ((_a = src.l) !== null && _a !== void 0 ? _a : 0) / K, t: ((_b = src.t) !== null && _b !== void 0 ? _b : 0) / K, r: ((_c = src.r) !== null && _c !== void 0 ? _c : 0) / K, b: ((_d = src.b) !== null && _d !== void 0 ? _d : 0) / K, }; } // Infer the container aspect ratio from the original image dimensions and the current source rectangle function inferContainerAr(oldImageWidth, oldImageHeight, currentSrcRect) { const { l, t, r, b } = toFractions(currentSrcRect); const wf = 1 - (l + r); const hf = 1 - (t + b); const oldAr = oldImageWidth / oldImageHeight; return oldAr * (wf / hf || 1); // guard vs hf=0 } exports.inferContainerAr = inferContainerAr; // Compute the new source rectangle for a new image based on the container aspect ratio and the new image dimensions function computeSrcRectForNewImage(containerAr, newImageWidth, newImageHeight) { const newAr = newImageWidth / newImageHeight; if (!isFinite(containerAr) || !isFinite(newAr) || containerAr <= 0 || newAr <= 0) { return { l: 0, t: 0, r: 0, b: 0 }; } if (newAr > containerAr) { // new image is wider than the container -> crop width const visibleW = containerAr / newAr; const crop = Math.max(0, Math.min(0.5, (1 - visibleW) / 2)); const cropHorizontal = Math.round(crop * K); return { l: cropHorizontal, r: cropHorizontal, t: 0, b: 0 }; } else if (newAr < containerAr) { // new image is taller than the container -> crop height const visibleH = newAr / containerAr; const crop = Math.max(0, Math.min(0.5, (1 - visibleH) / 2)); const cropVertical = Math.round(crop * K); return { l: 0, r: 0, t: cropVertical, b: cropVertical }; } else { // equal AR -> no crop needed return { l: 0, t: 0, r: 0, b: 0 }; } } exports.computeSrcRectForNewImage = computeSrcRectForNewImage; //# sourceMappingURL=compute-src-rect.js.map