@jasy/pdf 1.0.0-alpha.2 → 1.0.0-alpha.3
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/README.md +3 -3
- package/dist/api/args.d.ts +1 -1
- package/dist/api/args.js +2 -5
- package/dist/api/color.d.ts +4 -4
- package/dist/api/color.js +11 -17
- package/dist/api/content.d.ts +8 -8
- package/dist/api/content.js +23 -24
- package/dist/api/descriptor.d.ts +2 -2
- package/dist/api/descriptor.js +75 -31
- package/dist/api/index.d.ts +8 -8
- package/dist/api/index.js +8 -24
- package/dist/api/insets.js +4 -8
- package/dist/api/layout.d.ts +18 -16
- package/dist/api/layout.js +41 -52
- package/dist/api/structure.d.ts +60 -13
- package/dist/api/structure.js +132 -88
- package/dist/api/table.d.ts +5 -5
- package/dist/api/table.js +28 -24
- package/dist/api/text.d.ts +27 -2
- package/dist/api/text.js +45 -27
- package/dist/assets/font-data.d.ts +2 -0
- package/dist/assets/font-data.js +6 -0
- package/dist/assets/font-data.ts +7 -0
- package/dist/common/color.js +1 -5
- package/dist/constants/page-sizes.js +3 -6
- package/dist/constants/pdf-parts.js +1 -4
- package/dist/elements/container-element.d.ts +4 -4
- package/dist/elements/container-element.js +9 -13
- package/dist/elements/image-element.d.ts +18 -2
- package/dist/elements/image-element.js +81 -105
- package/dist/elements/index.d.ts +12 -11
- package/dist/elements/index.js +12 -29
- package/dist/elements/layout/default-text-style-element.d.ts +30 -0
- package/dist/elements/layout/default-text-style-element.js +47 -0
- package/dist/elements/layout/deferred-element.d.ts +3 -3
- package/dist/elements/layout/deferred-element.js +4 -8
- package/dist/elements/layout/expanded-element.d.ts +3 -3
- package/dist/elements/layout/expanded-element.js +10 -14
- package/dist/elements/layout/padding-element.d.ts +3 -3
- package/dist/elements/layout/padding-element.js +9 -14
- package/dist/elements/layout/positioned-element.d.ts +17 -4
- package/dist/elements/layout/positioned-element.js +29 -25
- package/dist/elements/layout/repeating-header-element.d.ts +3 -3
- package/dist/elements/layout/repeating-header-element.js +8 -12
- package/dist/elements/layout/sized-container-element.d.ts +2 -2
- package/dist/elements/layout/sized-container-element.js +6 -11
- package/dist/elements/line-element.d.ts +3 -3
- package/dist/elements/line-element.js +5 -10
- package/dist/elements/page-element.d.ts +8 -6
- package/dist/elements/page-element.js +20 -23
- package/dist/elements/pdf-document-element.d.ts +10 -4
- package/dist/elements/pdf-document-element.js +11 -10
- package/dist/elements/pdf-element.d.ts +12 -3
- package/dist/elements/pdf-element.js +10 -19
- package/dist/elements/rectangle-element.d.ts +5 -5
- package/dist/elements/rectangle-element.js +19 -25
- package/dist/elements/row-element.d.ts +3 -3
- package/dist/elements/row-element.js +7 -11
- package/dist/elements/text-element.d.ts +37 -11
- package/dist/elements/text-element.js +64 -39
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -19
- package/dist/ir/display-list.d.ts +4 -2
- package/dist/ir/display-list.js +1 -2
- package/dist/layout/box-constraints.js +2 -6
- package/dist/layout/fragmentation.d.ts +8 -1
- package/dist/layout/fragmentation.js +22 -10
- package/dist/platform/browser-fs.d.ts +2 -0
- package/dist/platform/browser-fs.js +9 -0
- package/dist/platform/browser-image.d.ts +5 -0
- package/dist/platform/browser-image.js +13 -0
- package/dist/platform/node-fs.d.ts +2 -0
- package/dist/platform/node-fs.js +10 -0
- package/dist/platform/node-image.d.ts +5 -0
- package/dist/platform/node-image.js +9 -0
- package/dist/renderer/container-renderer.d.ts +3 -3
- package/dist/renderer/container-renderer.js +12 -27
- package/dist/renderer/default-text-style-renderer.d.ts +6 -0
- package/dist/renderer/default-text-style-renderer.js +10 -0
- package/dist/renderer/deferred-renderer.d.ts +3 -3
- package/dist/renderer/deferred-renderer.js +8 -23
- package/dist/renderer/expanded-renderer.d.ts +3 -3
- package/dist/renderer/expanded-renderer.js +6 -21
- package/dist/renderer/image-renderer.d.ts +3 -3
- package/dist/renderer/image-renderer.js +77 -75
- package/dist/renderer/index.d.ts +10 -10
- package/dist/renderer/index.js +10 -26
- package/dist/renderer/line-renderer.d.ts +3 -3
- package/dist/renderer/line-renderer.js +13 -28
- package/dist/renderer/padding-renderer.d.ts +3 -3
- package/dist/renderer/padding-renderer.js +6 -21
- package/dist/renderer/page-renderer.d.ts +2 -2
- package/dist/renderer/page-renderer.js +61 -77
- package/dist/renderer/pdf-backend.d.ts +2 -2
- package/dist/renderer/pdf-backend.js +21 -19
- package/dist/renderer/pdf-config.js +4 -7
- package/dist/renderer/pdf-document-class.d.ts +5 -5
- package/dist/renderer/pdf-document-class.js +24 -41
- package/dist/renderer/pdf-document-renderer.d.ts +3 -3
- package/dist/renderer/pdf-document-renderer.js +71 -85
- package/dist/renderer/pdf-renderer.d.ts +2 -2
- package/dist/renderer/pdf-renderer.js +83 -93
- package/dist/renderer/positioned-renderer.d.ts +3 -3
- package/dist/renderer/positioned-renderer.js +8 -23
- package/dist/renderer/rectangle-renderer.d.ts +3 -3
- package/dist/renderer/rectangle-renderer.js +45 -52
- package/dist/renderer/repeating-header-renderer.d.ts +3 -3
- package/dist/renderer/repeating-header-renderer.js +11 -26
- package/dist/renderer/row-renderer.d.ts +3 -3
- package/dist/renderer/row-renderer.js +12 -27
- package/dist/renderer/text-renderer.d.ts +6 -5
- package/dist/renderer/text-renderer.js +33 -42
- package/dist/text/line-breaker.d.ts +8 -5
- package/dist/text/line-breaker.js +67 -16
- package/dist/text/text-style.d.ts +25 -0
- package/dist/text/text-style.js +29 -0
- package/dist/utils/afm-parser.js +3 -13
- package/dist/utils/bytes.d.ts +24 -0
- package/dist/utils/bytes.js +76 -0
- package/dist/utils/flex-layout.d.ts +2 -2
- package/dist/utils/flex-layout.js +15 -20
- package/dist/utils/font-metrics.d.ts +1 -1
- package/dist/utils/font-metrics.js +1 -2
- package/dist/utils/font-path.js +3 -6
- package/dist/utils/image-helper.d.ts +6 -5
- package/dist/utils/image-helper.js +101 -111
- package/dist/utils/md5.d.ts +4 -0
- package/dist/utils/md5.js +80 -0
- package/dist/utils/pdf-object-manager.d.ts +10 -6
- package/dist/utils/pdf-object-manager.js +89 -94
- package/dist/utils/renderer-registry.js +1 -5
- package/dist/utils/ttf-parser.d.ts +2 -2
- package/dist/utils/ttf-parser.js +32 -36
- package/dist/utils/ttf-subsetter.d.ts +1 -1
- package/dist/utils/ttf-subsetter.js +40 -42
- package/dist/utils/utf8-to-windows1252-encoder.js +1 -4
- package/dist/validators/element-validator.d.ts +2 -2
- package/dist/validators/element-validator.js +9 -13
- package/package.json +14 -2
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const pdf_element_1 = require("../elements/pdf-element");
|
|
5
|
-
const box_constraints_1 = require("../layout/box-constraints");
|
|
6
|
-
exports.VERTICAL_AXIS = {
|
|
1
|
+
import { FlexiblePDFElement } from "../elements/pdf-element.js";
|
|
2
|
+
import { BoxConstraints } from "../layout/box-constraints.js";
|
|
3
|
+
export const VERTICAL_AXIS = {
|
|
7
4
|
mainOf: (s) => s.height,
|
|
8
5
|
crossOf: (s) => s.width,
|
|
9
|
-
measureConstraints: (crossAvail) =>
|
|
10
|
-
flexConstraints: (mainExtent, crossAvail) =>
|
|
6
|
+
measureConstraints: (crossAvail) => BoxConstraints.loose(crossAvail, Infinity),
|
|
7
|
+
flexConstraints: (mainExtent, crossAvail) => BoxConstraints.loose(crossAvail, mainExtent),
|
|
11
8
|
offsetAt: (mainPos, crossPos) => ({ x: crossPos, y: mainPos }),
|
|
12
9
|
};
|
|
13
|
-
|
|
10
|
+
export const HORIZONTAL_AXIS = {
|
|
14
11
|
mainOf: (s) => s.width,
|
|
15
12
|
crossOf: (s) => s.height,
|
|
16
|
-
measureConstraints: (crossAvail) =>
|
|
17
|
-
flexConstraints: (mainExtent, crossAvail) =>
|
|
13
|
+
measureConstraints: (crossAvail) => BoxConstraints.loose(Infinity, crossAvail),
|
|
14
|
+
flexConstraints: (mainExtent, crossAvail) => BoxConstraints.loose(mainExtent, crossAvail),
|
|
18
15
|
offsetAt: (mainPos, crossPos) => ({ x: mainPos, y: crossPos }),
|
|
19
16
|
};
|
|
20
17
|
/** Cross-axis offset of a child of size `childCross` within `crossExtent`. */
|
|
@@ -25,7 +22,7 @@ function crossOffset(align, crossExtent, childCross) {
|
|
|
25
22
|
return Math.max(0, crossExtent - childCross);
|
|
26
23
|
return 0; // start, stretch (stretch fills, so no offset)
|
|
27
24
|
}
|
|
28
|
-
class FlexLayoutHelper {
|
|
25
|
+
export class FlexLayoutHelper {
|
|
29
26
|
/**
|
|
30
27
|
* Lays out a flex line along `axis`, IN SOURCE ORDER, and places every child.
|
|
31
28
|
* Fixed children take their natural main extent; flex (`ExpandedElement`) children
|
|
@@ -36,10 +33,9 @@ class FlexLayoutHelper {
|
|
|
36
33
|
* previous Column layout exactly.
|
|
37
34
|
*/
|
|
38
35
|
static layout(children, axis, mainAvail, crossAvail, mainStart, crossOrigin, options, ctx) {
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const cross = (_c = options.cross) !== null && _c !== void 0 ? _c : "stretch";
|
|
36
|
+
const gap = options.gap ?? 0;
|
|
37
|
+
const main = options.main ?? "start";
|
|
38
|
+
const cross = options.cross ?? "stretch";
|
|
43
39
|
const count = children.length;
|
|
44
40
|
// Pass 1: measure the fixed children (main extent + cross size) and total the flex.
|
|
45
41
|
let fixedMain = 0;
|
|
@@ -47,7 +43,7 @@ class FlexLayoutHelper {
|
|
|
47
43
|
let crossUsed = 0;
|
|
48
44
|
const fixedSize = new Map();
|
|
49
45
|
for (const child of children) {
|
|
50
|
-
if (child instanceof
|
|
46
|
+
if (child instanceof FlexiblePDFElement) {
|
|
51
47
|
totalFlex += child.getFlex();
|
|
52
48
|
}
|
|
53
49
|
else {
|
|
@@ -80,7 +76,7 @@ class FlexLayoutHelper {
|
|
|
80
76
|
// toward the line's cross extent - they're placed in pass 2, but crossExtent needs them now.
|
|
81
77
|
if (totalFlex > 0) {
|
|
82
78
|
for (const child of children) {
|
|
83
|
-
if (child instanceof
|
|
79
|
+
if (child instanceof FlexiblePDFElement) {
|
|
84
80
|
const mainExtent = (child.getFlex() / totalFlex) * remaining;
|
|
85
81
|
const size = child.calculateLayout(axis.flexConstraints(mainExtent, crossAvail), axis.offsetAt(mainStart, crossOrigin), ctx);
|
|
86
82
|
crossUsed = Math.max(crossUsed, axis.crossOf(size));
|
|
@@ -98,7 +94,7 @@ class FlexLayoutHelper {
|
|
|
98
94
|
let placedCross = 0;
|
|
99
95
|
children.forEach((child, index) => {
|
|
100
96
|
let mainExtent;
|
|
101
|
-
if (child instanceof
|
|
97
|
+
if (child instanceof FlexiblePDFElement) {
|
|
102
98
|
mainExtent = (child.getFlex() / totalFlex) * remaining;
|
|
103
99
|
const size = child.calculateLayout(axis.flexConstraints(mainExtent, stretch ? crossExtent : crossAvail), axis.offsetAt(mainPos, crossOrigin), ctx);
|
|
104
100
|
placedCross = Math.max(placedCross, axis.crossOf(size));
|
|
@@ -116,4 +112,3 @@ class FlexLayoutHelper {
|
|
|
116
112
|
return { mainUsed: mainPos - mainStart, crossUsed: placedCross };
|
|
117
113
|
}
|
|
118
114
|
}
|
|
119
|
-
exports.FlexLayoutHelper = FlexLayoutHelper;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FontStyle } from "./pdf-object-manager";
|
|
1
|
+
import type { FontStyle } from "./pdf-object-manager.js";
|
|
2
2
|
/**
|
|
3
3
|
* Read-only font measurement - the slice of the object manager that the layout pass
|
|
4
4
|
* needs. Keeping layout (and, later, fragmentation) behind this interface means those
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/utils/font-path.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const os = require("os");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
function getFontPath(fontName) {
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function getFontPath(fontName) {
|
|
7
4
|
const platform = os.platform();
|
|
8
5
|
let fontPath = "";
|
|
9
6
|
if (platform === "win32") {
|
|
@@ -7,7 +7,7 @@ interface ImageDimensions {
|
|
|
7
7
|
width: number;
|
|
8
8
|
height: number;
|
|
9
9
|
}
|
|
10
|
-
export declare function getImageDimensions(buffer:
|
|
10
|
+
export declare function getImageDimensions(buffer: Uint8Array): Promise<ImageDimensions>;
|
|
11
11
|
/**
|
|
12
12
|
* Converts the given image to grayscale and returns its binary data.
|
|
13
13
|
* @param imagePath Path to the input image file.
|
|
@@ -16,15 +16,16 @@ export declare function getImageDimensions(buffer: Buffer): Promise<ImageDimensi
|
|
|
16
16
|
/**
|
|
17
17
|
* Decodes a PNG into raw DeviceRGB samples, Flate-compressed, ready to embed as a PDF
|
|
18
18
|
* image XObject. A PNG file is NOT a valid `/FlateDecode` stream (it's a signature plus
|
|
19
|
-
* chunks of filtered, zlib-compressed scanlines), so it must be decoded first.
|
|
20
|
-
* no alpha
|
|
19
|
+
* chunks of filtered, zlib-compressed scanlines), so it must be decoded first. DeviceRGB has
|
|
20
|
+
* no alpha itself, so any transparency rides alongside as a DeviceGray `/SMask` (the return's `smask`).
|
|
21
21
|
*/
|
|
22
|
-
export declare function decodePngToRgbFlate(pngBuffer:
|
|
22
|
+
export declare function decodePngToRgbFlate(pngBuffer: Uint8Array): Promise<{
|
|
23
23
|
data: string;
|
|
24
|
+
smask?: string;
|
|
24
25
|
width: number;
|
|
25
26
|
height: number;
|
|
26
27
|
}>;
|
|
27
|
-
export declare function convertImageToGrayscaleBuffer(imagePath: string): Promise<
|
|
28
|
+
export declare function convertImageToGrayscaleBuffer(imagePath: string): Promise<Uint8Array>;
|
|
28
29
|
export interface FitResult {
|
|
29
30
|
width: number;
|
|
30
31
|
height: number;
|
|
@@ -1,23 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.getImageDimensions = getImageDimensions;
|
|
13
|
-
exports.decodePngToRgbFlate = decodePngToRgbFlate;
|
|
14
|
-
exports.convertImageToGrayscaleBuffer = convertImageToGrayscaleBuffer;
|
|
15
|
-
exports.applyContainFit = applyContainFit;
|
|
16
|
-
exports.applyCoverFit = applyCoverFit;
|
|
17
|
-
exports.applyFillFit = applyFillFit;
|
|
18
|
-
exports.applyFitNone = applyFitNone;
|
|
19
|
-
const jimp_1 = require("jimp");
|
|
20
|
-
const zlib_1 = require("zlib");
|
|
1
|
+
import { zlibSync } from "fflate";
|
|
2
|
+
import { latin1FromBytes } from "./bytes.js";
|
|
3
|
+
import { pngToRgba } from "../platform/node-image.js";
|
|
21
4
|
// Implement the method to handle 24-bit integers
|
|
22
5
|
DataView.prototype.getUint24 = function (byteOffset, littleEndian = false) {
|
|
23
6
|
const b1 = this.getUint8(byteOffset);
|
|
@@ -25,61 +8,59 @@ DataView.prototype.getUint24 = function (byteOffset, littleEndian = false) {
|
|
|
25
8
|
const b3 = this.getUint8(byteOffset + 2);
|
|
26
9
|
return littleEndian ? (b3 << 16) | (b2 << 8) | b1 : (b1 << 16) | (b2 << 8) | b3;
|
|
27
10
|
};
|
|
28
|
-
function getImageDimensions(buffer) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
offset += dataView.getUint16(offset, false);
|
|
46
|
-
}
|
|
11
|
+
export async function getImageDimensions(buffer) {
|
|
12
|
+
const dataView = new DataView(buffer.buffer);
|
|
13
|
+
// Check for JPEG (0xFFD8 is the start of JPEG file)
|
|
14
|
+
if (dataView.getUint16(0) === 0xffd8) {
|
|
15
|
+
let offset = 2;
|
|
16
|
+
while (offset < buffer.byteLength) {
|
|
17
|
+
const marker = dataView.getUint16(offset, false);
|
|
18
|
+
offset += 2;
|
|
19
|
+
if (marker === 0xffc0 || marker === 0xffc2) {
|
|
20
|
+
// SOF0 or SOF2
|
|
21
|
+
return {
|
|
22
|
+
height: dataView.getUint16(offset + 3, false),
|
|
23
|
+
width: dataView.getUint16(offset + 5, false),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
offset += dataView.getUint16(offset, false);
|
|
47
28
|
}
|
|
48
29
|
}
|
|
49
|
-
|
|
50
|
-
|
|
30
|
+
}
|
|
31
|
+
// Check for PNG (0x89504E47 is the PNG signature)
|
|
32
|
+
if (dataView.getUint32(0) === 0x89504e47) {
|
|
33
|
+
return {
|
|
34
|
+
width: dataView.getUint32(16, false),
|
|
35
|
+
height: dataView.getUint32(20, false),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Check for BMP (0x424D is the BMP signature)
|
|
39
|
+
if (dataView.getUint16(0) === 0x424d) {
|
|
40
|
+
return {
|
|
41
|
+
width: dataView.getUint32(18, true),
|
|
42
|
+
height: dataView.getUint32(22, true),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// Check for WebP (0x52494646 is the WebP signature 'RIFF')
|
|
46
|
+
if (dataView.getUint32(0) === 0x52494646 && dataView.getUint32(8) === 0x57454250) {
|
|
47
|
+
// 'WEBP'
|
|
48
|
+
if (dataView.getUint32(12) === 0x56503820) {
|
|
49
|
+
// 'VP8 '
|
|
51
50
|
return {
|
|
52
|
-
width: dataView.
|
|
53
|
-
height: dataView.
|
|
51
|
+
width: dataView.getUint16(26, true),
|
|
52
|
+
height: dataView.getUint16(28, true),
|
|
54
53
|
};
|
|
55
54
|
}
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
else if (dataView.getUint32(12) === 0x56503858) {
|
|
56
|
+
// 'VP8X'
|
|
58
57
|
return {
|
|
59
|
-
width: dataView.
|
|
60
|
-
height: dataView.
|
|
58
|
+
width: dataView.getUint24(24, true) + 1,
|
|
59
|
+
height: dataView.getUint24(27, true) + 1,
|
|
61
60
|
};
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
// 'WEBP'
|
|
66
|
-
if (dataView.getUint32(12) === 0x56503820) {
|
|
67
|
-
// 'VP8 '
|
|
68
|
-
return {
|
|
69
|
-
width: dataView.getUint16(26, true),
|
|
70
|
-
height: dataView.getUint16(28, true),
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
else if (dataView.getUint32(12) === 0x56503858) {
|
|
74
|
-
// 'VP8X'
|
|
75
|
-
return {
|
|
76
|
-
width: dataView.getUint24(24, true) + 1,
|
|
77
|
-
height: dataView.getUint24(27, true) + 1,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
throw new Error("Unsupported image format");
|
|
82
|
-
});
|
|
62
|
+
}
|
|
63
|
+
throw new Error("Unsupported image format");
|
|
83
64
|
}
|
|
84
65
|
/**
|
|
85
66
|
* Converts the given image to grayscale and returns its binary data.
|
|
@@ -89,50 +70,59 @@ function getImageDimensions(buffer) {
|
|
|
89
70
|
/**
|
|
90
71
|
* Decodes a PNG into raw DeviceRGB samples, Flate-compressed, ready to embed as a PDF
|
|
91
72
|
* image XObject. A PNG file is NOT a valid `/FlateDecode` stream (it's a signature plus
|
|
92
|
-
* chunks of filtered, zlib-compressed scanlines), so it must be decoded first.
|
|
93
|
-
* no alpha
|
|
73
|
+
* chunks of filtered, zlib-compressed scanlines), so it must be decoded first. DeviceRGB has
|
|
74
|
+
* no alpha itself, so any transparency rides alongside as a DeviceGray `/SMask` (the return's `smask`).
|
|
94
75
|
*/
|
|
95
|
-
function decodePngToRgbFlate(pngBuffer) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
76
|
+
export async function decodePngToRgbFlate(pngBuffer) {
|
|
77
|
+
// The decode (PNG bytes → RGBA) is platform-specific: jimp on Node, an OffscreenCanvas in the browser
|
|
78
|
+
// (`platform/{node,browser}-image.ts`, swapped by the package `browser` field). The split below is shared.
|
|
79
|
+
const { width, height, rgba } = await pngToRgba(pngBuffer);
|
|
80
|
+
// Raw DeviceRGB samples + (only when the PNG actually has transparency) a DeviceGray alpha channel that
|
|
81
|
+
// rides as the XObject's /SMask. An opaque PNG yields the same raw RGB as before and no SMask, so it stays
|
|
82
|
+
// byte-identical; a transparent one now composites correctly over whatever sits behind it.
|
|
83
|
+
const rgb = new Uint8Array(width * height * 3);
|
|
84
|
+
const alpha = new Uint8Array(width * height);
|
|
85
|
+
let hasAlpha = false;
|
|
86
|
+
for (let i = 0, j = 0, k = 0; i < rgba.length; i += 4, j += 3, k++) {
|
|
87
|
+
rgb[j] = rgba[i];
|
|
88
|
+
rgb[j + 1] = rgba[i + 1];
|
|
89
|
+
rgb[j + 2] = rgba[i + 2];
|
|
90
|
+
alpha[k] = rgba[i + 3];
|
|
91
|
+
if (rgba[i + 3] !== 255)
|
|
92
|
+
hasAlpha = true;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
data: latin1FromBytes(zlibSync(rgb)),
|
|
96
|
+
smask: hasAlpha ? latin1FromBytes(zlibSync(alpha)) : undefined,
|
|
97
|
+
width,
|
|
98
|
+
height,
|
|
99
|
+
};
|
|
109
100
|
}
|
|
110
|
-
function convertImageToGrayscaleBuffer(imagePath) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
});
|
|
101
|
+
export async function convertImageToGrayscaleBuffer(imagePath) {
|
|
102
|
+
// We get the image from the buffer (jimp lazily loaded - see decodePngToRgbFlate).
|
|
103
|
+
const { Jimp, JimpMime } = await import("jimp");
|
|
104
|
+
const image = await Jimp.read(imagePath);
|
|
105
|
+
// Get MIME type. If emtpy throw error
|
|
106
|
+
const mime = image.mime;
|
|
107
|
+
if (!mime)
|
|
108
|
+
throw new Error("Cannot read MIME type");
|
|
109
|
+
// We need to check the MIME type (the exact union `getBuffer` accepts).
|
|
110
|
+
let mimeType;
|
|
111
|
+
switch (mime) {
|
|
112
|
+
case JimpMime.png:
|
|
113
|
+
case JimpMime.jpeg:
|
|
114
|
+
case JimpMime.bmp:
|
|
115
|
+
mimeType = mime;
|
|
116
|
+
break;
|
|
117
|
+
default:
|
|
118
|
+
throw new Error("Unsupported MIME type");
|
|
119
|
+
}
|
|
120
|
+
image.greyscale();
|
|
121
|
+
// Convert the image back to buffer with current MIME type
|
|
122
|
+
const grayscaleBuffer = await image.getBuffer(mimeType);
|
|
123
|
+
return grayscaleBuffer;
|
|
134
124
|
}
|
|
135
|
-
function applyContainFit(imageWidth, imageHeight, containerWidth, containerHeight) {
|
|
125
|
+
export function applyContainFit(imageWidth, imageHeight, containerWidth, containerHeight) {
|
|
136
126
|
const imageAspectRatio = imageWidth / imageHeight;
|
|
137
127
|
const containerAspectRatio = containerWidth / containerHeight;
|
|
138
128
|
let width, height, offsetX, offsetY;
|
|
@@ -159,7 +149,7 @@ function applyContainFit(imageWidth, imageHeight, containerWidth, containerHeigh
|
|
|
159
149
|
offsetY,
|
|
160
150
|
};
|
|
161
151
|
}
|
|
162
|
-
function applyCoverFit(imageWidth, imageHeight, containerWidth, containerHeight) {
|
|
152
|
+
export function applyCoverFit(imageWidth, imageHeight, containerWidth, containerHeight) {
|
|
163
153
|
const imageAspectRatio = imageWidth / imageHeight;
|
|
164
154
|
const containerAspectRatio = containerWidth / containerHeight;
|
|
165
155
|
let width, height, offsetX, offsetY;
|
|
@@ -186,7 +176,7 @@ function applyCoverFit(imageWidth, imageHeight, containerWidth, containerHeight)
|
|
|
186
176
|
offsetY,
|
|
187
177
|
};
|
|
188
178
|
}
|
|
189
|
-
function applyFillFit(containerWidth, containerHeight) {
|
|
179
|
+
export function applyFillFit(containerWidth, containerHeight) {
|
|
190
180
|
return {
|
|
191
181
|
width: containerWidth,
|
|
192
182
|
height: containerHeight,
|
|
@@ -194,7 +184,7 @@ function applyFillFit(containerWidth, containerHeight) {
|
|
|
194
184
|
offsetY: 0,
|
|
195
185
|
};
|
|
196
186
|
}
|
|
197
|
-
function applyFitNone(imageWidth, imageHeight, containerWidth, containerHeight) {
|
|
187
|
+
export function applyFitNone(imageWidth, imageHeight, containerWidth, containerHeight) {
|
|
198
188
|
const offsetX = (containerWidth - imageWidth) / 2; // Center horizontally
|
|
199
189
|
const offsetY = (containerHeight - imageHeight) / 2; // Center vertically
|
|
200
190
|
return {
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// MD5 (RFC 1321), vendored + isomorphic - so the font subset tag and the documentId /ID hash work in the
|
|
2
|
+
// browser too: Node `crypto` is absent there, and Web Crypto has no MD5 and is async. Verified byte-for-byte
|
|
3
|
+
// identical to Node's `createHash("md5")`. Operates on bytes; callers UTF-8-encode strings (TextEncoder),
|
|
4
|
+
// matching `hash.update(string)`'s default encoding.
|
|
5
|
+
// Per-round left-rotate amounts.
|
|
6
|
+
const S = [
|
|
7
|
+
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
|
8
|
+
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
|
9
|
+
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
|
10
|
+
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
|
|
11
|
+
];
|
|
12
|
+
// Per-round constants K[i] = floor(abs(sin(i+1)) * 2^32).
|
|
13
|
+
const K = new Int32Array(64);
|
|
14
|
+
for (let i = 0; i < 64; i++)
|
|
15
|
+
K[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 2 ** 32) | 0;
|
|
16
|
+
/** The raw 16-byte MD5 digest of `input`. */
|
|
17
|
+
export function md5(input) {
|
|
18
|
+
const len = input.length;
|
|
19
|
+
// Pad to a multiple of 64: message + 0x80 + zeros + 64-bit little-endian bit length.
|
|
20
|
+
const total = (((len + 8) >> 6) + 1) << 6;
|
|
21
|
+
const b = new Uint8Array(total);
|
|
22
|
+
b.set(input);
|
|
23
|
+
b[len] = 0x80;
|
|
24
|
+
const bits = len * 8;
|
|
25
|
+
b[total - 8] = bits & 0xff;
|
|
26
|
+
b[total - 7] = (bits >>> 8) & 0xff;
|
|
27
|
+
b[total - 6] = (bits >>> 16) & 0xff;
|
|
28
|
+
b[total - 5] = (bits >>> 24) & 0xff;
|
|
29
|
+
// High 32 bits of the length stay 0 (inputs are well under 512 MB).
|
|
30
|
+
let a0 = 0x67452301, b0 = 0xefcdab89, c0 = 0x98badcfe, d0 = 0x10325476;
|
|
31
|
+
const M = new Int32Array(16);
|
|
32
|
+
for (let off = 0; off < total; off += 64) {
|
|
33
|
+
for (let i = 0; i < 16; i++) {
|
|
34
|
+
const j = off + i * 4;
|
|
35
|
+
M[i] = b[j] | (b[j + 1] << 8) | (b[j + 2] << 16) | (b[j + 3] << 24);
|
|
36
|
+
}
|
|
37
|
+
let A = a0, B = b0, C = c0, D = d0;
|
|
38
|
+
for (let i = 0; i < 64; i++) {
|
|
39
|
+
let f, g;
|
|
40
|
+
if (i < 16) {
|
|
41
|
+
f = (B & C) | (~B & D);
|
|
42
|
+
g = i;
|
|
43
|
+
}
|
|
44
|
+
else if (i < 32) {
|
|
45
|
+
f = (D & B) | (~D & C);
|
|
46
|
+
g = (5 * i + 1) % 16;
|
|
47
|
+
}
|
|
48
|
+
else if (i < 48) {
|
|
49
|
+
f = B ^ C ^ D;
|
|
50
|
+
g = (3 * i + 5) % 16;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
f = C ^ (B | ~D);
|
|
54
|
+
g = (7 * i) % 16;
|
|
55
|
+
}
|
|
56
|
+
f = (f + A + K[i] + M[g]) | 0;
|
|
57
|
+
A = D;
|
|
58
|
+
D = C;
|
|
59
|
+
C = B;
|
|
60
|
+
B = (B + (((f << S[i]) | (f >>> (32 - S[i]))) | 0)) | 0;
|
|
61
|
+
}
|
|
62
|
+
a0 = (a0 + A) | 0;
|
|
63
|
+
b0 = (b0 + B) | 0;
|
|
64
|
+
c0 = (c0 + C) | 0;
|
|
65
|
+
d0 = (d0 + D) | 0;
|
|
66
|
+
}
|
|
67
|
+
const out = new Uint8Array(16);
|
|
68
|
+
const w = [a0, b0, c0, d0];
|
|
69
|
+
for (let i = 0; i < 4; i++) {
|
|
70
|
+
out[i * 4] = w[i] & 0xff;
|
|
71
|
+
out[i * 4 + 1] = (w[i] >>> 8) & 0xff;
|
|
72
|
+
out[i * 4 + 2] = (w[i] >>> 16) & 0xff;
|
|
73
|
+
out[i * 4 + 3] = (w[i] >>> 24) & 0xff;
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
/** Lowercase hex of the MD5 digest (matches `digest("hex")`). */
|
|
78
|
+
export function md5Hex(input) {
|
|
79
|
+
return [...md5(input)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
80
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { OverflowPolicy } from "../layout/fragmentation.js";
|
|
2
|
+
import type { PDFConfig } from "../renderer/pdf-document-class.js";
|
|
3
|
+
import type { FontMetrics } from "./font-metrics.js";
|
|
3
4
|
interface FontIndexes {
|
|
4
5
|
fontIndex: number;
|
|
5
6
|
resourceIndex: number;
|
|
@@ -30,10 +31,13 @@ export declare class PDFObjectManager implements FontMetrics {
|
|
|
30
31
|
private pdfVersion;
|
|
31
32
|
private documentId;
|
|
32
33
|
private compress;
|
|
34
|
+
private overflowPolicy;
|
|
33
35
|
constructor();
|
|
34
36
|
addObject(content: string): number;
|
|
35
37
|
replaceObject(objectNumber: number, content: string): void;
|
|
36
38
|
setCompress(on: boolean): void;
|
|
39
|
+
setOverflowPolicy(policy: OverflowPolicy): void;
|
|
40
|
+
getOverflowPolicy(): OverflowPolicy;
|
|
37
41
|
private stream;
|
|
38
42
|
addContentStream(content: string): number;
|
|
39
43
|
changePDFConfig(config: PDFConfig): void;
|
|
@@ -42,8 +46,8 @@ export declare class PDFObjectManager implements FontMetrics {
|
|
|
42
46
|
private getCurrentByteLength;
|
|
43
47
|
setParentObjectNumber(number: number): void;
|
|
44
48
|
getParentObjectNumber(): number;
|
|
45
|
-
registerImage(width: number, height: number, imageType: string, imageData: string): number;
|
|
46
|
-
attachFile(name: string, data:
|
|
49
|
+
registerImage(width: number, height: number, imageType: string, imageData: string, smaskData?: string): number;
|
|
50
|
+
attachFile(name: string, data: Uint8Array, opts?: {
|
|
47
51
|
relationship?: string;
|
|
48
52
|
mimeType?: string;
|
|
49
53
|
description?: string;
|
|
@@ -54,7 +58,7 @@ export declare class PDFObjectManager implements FontMetrics {
|
|
|
54
58
|
}[];
|
|
55
59
|
setXmpMetadata(xml: string): void;
|
|
56
60
|
getXmpMetadata(): string | undefined;
|
|
57
|
-
setOutputIntent(icc:
|
|
61
|
+
setOutputIntent(icc: Uint8Array, opts?: {
|
|
58
62
|
identifier?: string;
|
|
59
63
|
info?: string;
|
|
60
64
|
}): void;
|
|
@@ -66,7 +70,7 @@ export declare class PDFObjectManager implements FontMetrics {
|
|
|
66
70
|
registerExtGState(fillAlpha: number, strokeAlpha: number): string;
|
|
67
71
|
getAllExtGStatesRaw(): Map<string, number>;
|
|
68
72
|
registerFont(fontName: string, fontStyle?: FontStyle, fullName?: string): FontIndexes;
|
|
69
|
-
registerCustomFont(name: string, data:
|
|
73
|
+
registerCustomFont(name: string, data: Uint8Array, style?: FontStyle): void;
|
|
70
74
|
private ensureEmitted;
|
|
71
75
|
finalizeCustomFonts(): void;
|
|
72
76
|
private subsetTag;
|