@embedpdf/models 2.5.0 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/geometry.d.ts +73 -0
- package/dist/helpers/font.d.ts +13 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +191 -11
- package/dist/index.js.map +1 -1
- package/dist/pdf.d.ts +97 -2
- package/dist/task-sequence.d.ts +69 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -242,6 +242,66 @@ function boundingRect(rects) {
|
|
|
242
242
|
function boundingRectOrEmpty(rects) {
|
|
243
243
|
return boundingRect(rects) ?? EMPTY_RECT;
|
|
244
244
|
}
|
|
245
|
+
function normalizeAngle(degrees) {
|
|
246
|
+
const normalized = degrees % 360;
|
|
247
|
+
return normalized < 0 ? normalized + 360 : normalized;
|
|
248
|
+
}
|
|
249
|
+
function getRectCenter(rect) {
|
|
250
|
+
return {
|
|
251
|
+
x: rect.origin.x + rect.size.width / 2,
|
|
252
|
+
y: rect.origin.y + rect.size.height / 2
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function rotatePointAround(point, center, angleDegrees) {
|
|
256
|
+
const angleRad = angleDegrees * Math.PI / 180;
|
|
257
|
+
const cos = Math.cos(angleRad);
|
|
258
|
+
const sin = Math.sin(angleRad);
|
|
259
|
+
const dx = point.x - center.x;
|
|
260
|
+
const dy = point.y - center.y;
|
|
261
|
+
return {
|
|
262
|
+
x: center.x + dx * cos - dy * sin,
|
|
263
|
+
y: center.y + dx * sin + dy * cos
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function rotateVertices(points, center, angleDegrees) {
|
|
267
|
+
return points.map((v) => rotatePointAround(v, center, angleDegrees));
|
|
268
|
+
}
|
|
269
|
+
function calculateRotatedRectAABBAroundPoint(rect, angleDegrees, center) {
|
|
270
|
+
const corners = [
|
|
271
|
+
{ x: rect.origin.x, y: rect.origin.y },
|
|
272
|
+
{ x: rect.origin.x + rect.size.width, y: rect.origin.y },
|
|
273
|
+
{ x: rect.origin.x + rect.size.width, y: rect.origin.y + rect.size.height },
|
|
274
|
+
{ x: rect.origin.x, y: rect.origin.y + rect.size.height }
|
|
275
|
+
];
|
|
276
|
+
const rotated = rotateVertices(corners, center, angleDegrees);
|
|
277
|
+
return rectFromPoints(rotated);
|
|
278
|
+
}
|
|
279
|
+
function calculateRotatedRectAABB(unrotatedRect, angleDegrees) {
|
|
280
|
+
return calculateRotatedRectAABBAroundPoint(
|
|
281
|
+
unrotatedRect,
|
|
282
|
+
angleDegrees,
|
|
283
|
+
getRectCenter(unrotatedRect)
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
function inferRotationCenterFromRects(unrotatedRect, rotatedAabb, angleDegrees) {
|
|
287
|
+
const angleRad = angleDegrees * Math.PI / 180;
|
|
288
|
+
const cos = Math.cos(angleRad);
|
|
289
|
+
const sin = Math.sin(angleRad);
|
|
290
|
+
const m00 = 1 - cos;
|
|
291
|
+
const m01 = sin;
|
|
292
|
+
const m10 = -sin;
|
|
293
|
+
const m11 = 1 - cos;
|
|
294
|
+
const det = m00 * m11 - m01 * m10;
|
|
295
|
+
const unrotatedCenter = getRectCenter(unrotatedRect);
|
|
296
|
+
if (Math.abs(det) < 1e-10) return unrotatedCenter;
|
|
297
|
+
const rotatedCenter = getRectCenter(rotatedAabb);
|
|
298
|
+
const rhsX = rotatedCenter.x - (cos * unrotatedCenter.x - sin * unrotatedCenter.y);
|
|
299
|
+
const rhsY = rotatedCenter.y - (sin * unrotatedCenter.x + cos * unrotatedCenter.y);
|
|
300
|
+
return {
|
|
301
|
+
x: (m11 * rhsX - m01 * rhsY) / det,
|
|
302
|
+
y: (-m10 * rhsX + m00 * rhsY) / det
|
|
303
|
+
};
|
|
304
|
+
}
|
|
245
305
|
function buildUserToDeviceMatrix(rect, rotation, outW, outH) {
|
|
246
306
|
const L = rect.origin.x;
|
|
247
307
|
const B = rect.origin.y;
|
|
@@ -1538,7 +1598,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1538
1598
|
bold: true,
|
|
1539
1599
|
italic: false,
|
|
1540
1600
|
label: "Courier Bold",
|
|
1541
|
-
css:
|
|
1601
|
+
css: "Courier, monospace"
|
|
1542
1602
|
},
|
|
1543
1603
|
{
|
|
1544
1604
|
id: PdfStandardFont.Courier_BoldOblique,
|
|
@@ -1546,7 +1606,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1546
1606
|
bold: true,
|
|
1547
1607
|
italic: true,
|
|
1548
1608
|
label: "Courier Bold Oblique",
|
|
1549
|
-
css:
|
|
1609
|
+
css: "Courier, monospace"
|
|
1550
1610
|
},
|
|
1551
1611
|
{
|
|
1552
1612
|
id: PdfStandardFont.Courier_Oblique,
|
|
@@ -1554,7 +1614,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1554
1614
|
bold: false,
|
|
1555
1615
|
italic: true,
|
|
1556
1616
|
label: "Courier Oblique",
|
|
1557
|
-
css:
|
|
1617
|
+
css: "Courier, monospace"
|
|
1558
1618
|
},
|
|
1559
1619
|
HELVETICA_DESC,
|
|
1560
1620
|
{
|
|
@@ -1563,7 +1623,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1563
1623
|
bold: true,
|
|
1564
1624
|
italic: false,
|
|
1565
1625
|
label: "Helvetica Bold",
|
|
1566
|
-
css:
|
|
1626
|
+
css: "Helvetica, Arial, sans-serif"
|
|
1567
1627
|
},
|
|
1568
1628
|
{
|
|
1569
1629
|
id: PdfStandardFont.Helvetica_BoldOblique,
|
|
@@ -1571,7 +1631,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1571
1631
|
bold: true,
|
|
1572
1632
|
italic: true,
|
|
1573
1633
|
label: "Helvetica Bold Oblique",
|
|
1574
|
-
css:
|
|
1634
|
+
css: "Helvetica, Arial, sans-serif"
|
|
1575
1635
|
},
|
|
1576
1636
|
{
|
|
1577
1637
|
id: PdfStandardFont.Helvetica_Oblique,
|
|
@@ -1579,7 +1639,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1579
1639
|
bold: false,
|
|
1580
1640
|
italic: true,
|
|
1581
1641
|
label: "Helvetica Oblique",
|
|
1582
|
-
css:
|
|
1642
|
+
css: "Helvetica, Arial, sans-serif"
|
|
1583
1643
|
},
|
|
1584
1644
|
{
|
|
1585
1645
|
id: PdfStandardFont.Times_Roman,
|
|
@@ -1595,7 +1655,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1595
1655
|
bold: true,
|
|
1596
1656
|
italic: false,
|
|
1597
1657
|
label: "Times Bold",
|
|
1598
|
-
css: '"Times New Roman
|
|
1658
|
+
css: '"Times New Roman", Times, serif'
|
|
1599
1659
|
},
|
|
1600
1660
|
{
|
|
1601
1661
|
id: PdfStandardFont.Times_BoldItalic,
|
|
@@ -1603,7 +1663,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1603
1663
|
bold: true,
|
|
1604
1664
|
italic: true,
|
|
1605
1665
|
label: "Times Bold Italic",
|
|
1606
|
-
css: '"Times New Roman
|
|
1666
|
+
css: '"Times New Roman", Times, serif'
|
|
1607
1667
|
},
|
|
1608
1668
|
{
|
|
1609
1669
|
id: PdfStandardFont.Times_Italic,
|
|
@@ -1611,7 +1671,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1611
1671
|
bold: false,
|
|
1612
1672
|
italic: true,
|
|
1613
1673
|
label: "Times Italic",
|
|
1614
|
-
css: '"Times New Roman
|
|
1674
|
+
css: '"Times New Roman", Times, serif'
|
|
1615
1675
|
},
|
|
1616
1676
|
{
|
|
1617
1677
|
id: PdfStandardFont.Symbol,
|
|
@@ -1619,7 +1679,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1619
1679
|
bold: false,
|
|
1620
1680
|
italic: false,
|
|
1621
1681
|
label: "Symbol",
|
|
1622
|
-
css: "Symbol"
|
|
1682
|
+
css: "Symbol, serif"
|
|
1623
1683
|
},
|
|
1624
1684
|
{
|
|
1625
1685
|
id: PdfStandardFont.ZapfDingbats,
|
|
@@ -1627,7 +1687,7 @@ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
|
|
|
1627
1687
|
bold: false,
|
|
1628
1688
|
italic: false,
|
|
1629
1689
|
label: "Zapf Dingbats",
|
|
1630
|
-
css: "ZapfDingbats"
|
|
1690
|
+
css: "ZapfDingbats, serif"
|
|
1631
1691
|
}
|
|
1632
1692
|
]);
|
|
1633
1693
|
const idToDescriptor = STANDARD_FONT_DESCRIPTORS.reduce((m, d) => (m[d.id] = d, m), {});
|
|
@@ -1659,6 +1719,14 @@ function standardFontLabel(font) {
|
|
|
1659
1719
|
function standardFontCss(font) {
|
|
1660
1720
|
return getStandardFontDescriptor(font).css;
|
|
1661
1721
|
}
|
|
1722
|
+
function standardFontCssProperties(font) {
|
|
1723
|
+
const desc = getStandardFontDescriptor(font);
|
|
1724
|
+
return {
|
|
1725
|
+
fontFamily: desc.css,
|
|
1726
|
+
fontWeight: desc.bold ? "bold" : "normal",
|
|
1727
|
+
fontStyle: desc.italic ? "italic" : "normal"
|
|
1728
|
+
};
|
|
1729
|
+
}
|
|
1662
1730
|
const standardFontFamilySelectOptions = Object.values(PdfStandardFontFamily).filter(
|
|
1663
1731
|
(f) => f !== "Unknown"
|
|
1664
1732
|
/* Unknown */
|
|
@@ -2015,6 +2083,109 @@ class CompoundTask extends Task {
|
|
|
2015
2083
|
return compound;
|
|
2016
2084
|
}
|
|
2017
2085
|
}
|
|
2086
|
+
class TaskSequence {
|
|
2087
|
+
constructor(parentTask) {
|
|
2088
|
+
this.parentTask = parentTask;
|
|
2089
|
+
this.activeChild = null;
|
|
2090
|
+
this.disposed = false;
|
|
2091
|
+
const origAbort = parentTask.abort.bind(parentTask);
|
|
2092
|
+
parentTask.abort = (reason) => {
|
|
2093
|
+
var _a;
|
|
2094
|
+
this.disposed = true;
|
|
2095
|
+
(_a = this.activeChild) == null ? void 0 : _a.abort(reason);
|
|
2096
|
+
origAbort(reason);
|
|
2097
|
+
};
|
|
2098
|
+
}
|
|
2099
|
+
/**
|
|
2100
|
+
* Execute a child Task and return its result as a Promise.
|
|
2101
|
+
*
|
|
2102
|
+
* If the parent task has been aborted, throws `TaskAbortedError` immediately.
|
|
2103
|
+
* If the parent task is aborted while the child is running, the child is aborted too.
|
|
2104
|
+
*/
|
|
2105
|
+
run(factory) {
|
|
2106
|
+
return new Promise((resolve, reject) => {
|
|
2107
|
+
if (this.disposed || this.parentTask.state.stage !== TaskStage.Pending) {
|
|
2108
|
+
reject(new TaskAbortedError("Sequence aborted"));
|
|
2109
|
+
return;
|
|
2110
|
+
}
|
|
2111
|
+
const child = factory();
|
|
2112
|
+
this.activeChild = child;
|
|
2113
|
+
child.wait(
|
|
2114
|
+
(result) => {
|
|
2115
|
+
this.activeChild = null;
|
|
2116
|
+
resolve(result);
|
|
2117
|
+
},
|
|
2118
|
+
(error) => {
|
|
2119
|
+
this.activeChild = null;
|
|
2120
|
+
if (error.type === "abort") {
|
|
2121
|
+
reject(new TaskAbortedError(error.reason));
|
|
2122
|
+
} else {
|
|
2123
|
+
reject(new TaskRejectedError(error.reason));
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
);
|
|
2127
|
+
});
|
|
2128
|
+
}
|
|
2129
|
+
/**
|
|
2130
|
+
* Execute a child Task and return its result as a Promise,
|
|
2131
|
+
* forwarding the child's progress events to the parent task
|
|
2132
|
+
* through the provided mapper function.
|
|
2133
|
+
*
|
|
2134
|
+
* If the parent task has been aborted, throws `TaskAbortedError` immediately.
|
|
2135
|
+
* If the parent task is aborted while the child is running, the child is aborted too.
|
|
2136
|
+
*/
|
|
2137
|
+
runWithProgress(factory, mapProgress) {
|
|
2138
|
+
return new Promise((resolve, reject) => {
|
|
2139
|
+
if (this.disposed || this.parentTask.state.stage !== TaskStage.Pending) {
|
|
2140
|
+
reject(new TaskAbortedError("Sequence aborted"));
|
|
2141
|
+
return;
|
|
2142
|
+
}
|
|
2143
|
+
const child = factory();
|
|
2144
|
+
this.activeChild = child;
|
|
2145
|
+
child.onProgress((p) => {
|
|
2146
|
+
this.parentTask.progress(mapProgress(p));
|
|
2147
|
+
});
|
|
2148
|
+
child.wait(
|
|
2149
|
+
(result) => {
|
|
2150
|
+
this.activeChild = null;
|
|
2151
|
+
resolve(result);
|
|
2152
|
+
},
|
|
2153
|
+
(error) => {
|
|
2154
|
+
this.activeChild = null;
|
|
2155
|
+
if (error.type === "abort") {
|
|
2156
|
+
reject(new TaskAbortedError(error.reason));
|
|
2157
|
+
} else {
|
|
2158
|
+
reject(new TaskRejectedError(error.reason));
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
);
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
/**
|
|
2165
|
+
* Execute an async function body that uses `run()` / `runWithProgress()`,
|
|
2166
|
+
* automatically handling abort and error routing to the parent task.
|
|
2167
|
+
*
|
|
2168
|
+
* - If the body throws `TaskAbortedError`, it is silently ignored
|
|
2169
|
+
* (the parent task was already aborted via the abort override).
|
|
2170
|
+
* - If the body throws `TaskRejectedError` (from a child task rejection
|
|
2171
|
+
* via `run()` / `runWithProgress()`), its `.reason` is forwarded directly
|
|
2172
|
+
* to the parent task, bypassing `mapError`.
|
|
2173
|
+
* - Any other thrown error is mapped through `mapError` and used to
|
|
2174
|
+
* reject the parent task. This handles unexpected runtime exceptions
|
|
2175
|
+
* in the async body itself.
|
|
2176
|
+
* - On success, the body is responsible for calling `parentTask.resolve()`.
|
|
2177
|
+
*/
|
|
2178
|
+
execute(fn, mapError) {
|
|
2179
|
+
fn().catch((err) => {
|
|
2180
|
+
if (err instanceof TaskAbortedError) return;
|
|
2181
|
+
if (err instanceof TaskRejectedError) {
|
|
2182
|
+
this.parentTask.reject(err.reason);
|
|
2183
|
+
return;
|
|
2184
|
+
}
|
|
2185
|
+
this.parentTask.reject(mapError(err));
|
|
2186
|
+
});
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2018
2189
|
function ignore() {
|
|
2019
2190
|
}
|
|
2020
2191
|
export {
|
|
@@ -2077,6 +2248,7 @@ export {
|
|
|
2077
2248
|
Task,
|
|
2078
2249
|
TaskAbortedError,
|
|
2079
2250
|
TaskRejectedError,
|
|
2251
|
+
TaskSequence,
|
|
2080
2252
|
TaskStage,
|
|
2081
2253
|
blendModeLabel,
|
|
2082
2254
|
blendModeSelectOptions,
|
|
@@ -2088,6 +2260,8 @@ export {
|
|
|
2088
2260
|
buildUserToDeviceMatrix,
|
|
2089
2261
|
calculateAngle,
|
|
2090
2262
|
calculateDegree,
|
|
2263
|
+
calculateRotatedRectAABB,
|
|
2264
|
+
calculateRotatedRectAABBAroundPoint,
|
|
2091
2265
|
combinePdfColorWithAlpha,
|
|
2092
2266
|
combineWebColorWithOpacity,
|
|
2093
2267
|
compareSearchTarget,
|
|
@@ -2100,12 +2274,15 @@ export {
|
|
|
2100
2274
|
extractWebOpacity,
|
|
2101
2275
|
flagsToNames,
|
|
2102
2276
|
getBlendModeInfo,
|
|
2277
|
+
getRectCenter,
|
|
2103
2278
|
getStandardFontDescriptor,
|
|
2104
2279
|
getTextAlignmentInfo,
|
|
2105
2280
|
ignore,
|
|
2281
|
+
inferRotationCenterFromRects,
|
|
2106
2282
|
isUuidV4,
|
|
2107
2283
|
makeStandardFont,
|
|
2108
2284
|
namesToFlags,
|
|
2285
|
+
normalizeAngle,
|
|
2109
2286
|
pdfAlphaColorToWebAlphaColor,
|
|
2110
2287
|
pdfAlphaToWebOpacity,
|
|
2111
2288
|
pdfColorToWebColor,
|
|
@@ -2121,12 +2298,15 @@ export {
|
|
|
2121
2298
|
restorePosition,
|
|
2122
2299
|
restoreRect,
|
|
2123
2300
|
rotateAndTranslatePoint,
|
|
2301
|
+
rotatePointAround,
|
|
2124
2302
|
rotatePosition,
|
|
2125
2303
|
rotateRect,
|
|
2304
|
+
rotateVertices,
|
|
2126
2305
|
scalePosition,
|
|
2127
2306
|
scaleRect,
|
|
2128
2307
|
serializeLogger,
|
|
2129
2308
|
standardFontCss,
|
|
2309
|
+
standardFontCssProperties,
|
|
2130
2310
|
standardFontFamily,
|
|
2131
2311
|
standardFontFamilyLabel,
|
|
2132
2312
|
standardFontFamilySelectOptions,
|