pdf-codec 1.6.0 → 1.7.0
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 +67 -9
- package/dist/cff-bounds.cjs +432 -0
- package/dist/cff-bounds.d.cts +9 -0
- package/dist/cff-bounds.d.ts +9 -0
- package/dist/cff-bounds.js +431 -0
- package/dist/cff-probe.cjs +8 -106
- package/dist/cff-probe.js +9 -107
- package/dist/cff.cjs +158 -0
- package/dist/cff.d.cts +17 -0
- package/dist/cff.d.ts +17 -0
- package/dist/cff.js +150 -0
- package/dist/filters.cjs +45 -2
- package/dist/filters.d.cts +4 -3
- package/dist/filters.d.ts +4 -3
- package/dist/filters.js +45 -2
- package/dist/formula.d.cts +1 -1
- package/dist/formula.d.ts +1 -1
- package/dist/glyf.cjs +62 -6
- package/dist/glyf.d.cts +2 -0
- package/dist/glyf.d.ts +2 -0
- package/dist/glyf.js +62 -6
- package/dist/glyph-bounds-BV_Z40KS.d.cts +10 -0
- package/dist/glyph-bounds-BV_Z40KS.d.ts +10 -0
- package/dist/glyph-bounds.cjs +12 -0
- package/dist/glyph-bounds.d.cts +2 -0
- package/dist/glyph-bounds.d.ts +2 -0
- package/dist/glyph-bounds.js +11 -0
- package/dist/image/jbig2-arith.cjs +432 -0
- package/dist/image/jbig2-arith.d.cts +23 -0
- package/dist/image/jbig2-arith.d.ts +23 -0
- package/dist/image/jbig2-arith.js +426 -0
- package/dist/image/jbig2-bitmap.cjs +80 -0
- package/dist/image/jbig2-bitmap.d.cts +20 -0
- package/dist/image/jbig2-bitmap.d.ts +20 -0
- package/dist/image/jbig2-bitmap.js +72 -0
- package/dist/image/jbig2-errors.cjs +17 -0
- package/dist/image/jbig2-errors.d.cts +2 -0
- package/dist/image/jbig2-errors.d.ts +2 -0
- package/dist/image/jbig2-errors.js +15 -0
- package/dist/image/jbig2-generic.cjs +242 -0
- package/dist/image/jbig2-generic.d.cts +27 -0
- package/dist/image/jbig2-generic.d.ts +27 -0
- package/dist/image/jbig2-generic.js +237 -0
- package/dist/image/jbig2-text.cjs +175 -0
- package/dist/image/jbig2-text.d.cts +58 -0
- package/dist/image/jbig2-text.d.ts +58 -0
- package/dist/image/jbig2-text.js +170 -0
- package/dist/image/jbig2.cjs +333 -0
- package/dist/image/jbig2.d.cts +15 -0
- package/dist/image/jbig2.d.ts +15 -0
- package/dist/image/jbig2.js +332 -0
- package/dist/image/jp2-boxes.cjs +148 -0
- package/dist/image/jp2-boxes.d.cts +26 -0
- package/dist/image/jp2-boxes.d.ts +26 -0
- package/dist/image/jp2-boxes.js +146 -0
- package/dist/image/jpeg2000-codestream.cjs +309 -0
- package/dist/image/jpeg2000-codestream.d.cts +78 -0
- package/dist/image/jpeg2000-codestream.d.ts +78 -0
- package/dist/image/jpeg2000-codestream.js +308 -0
- package/dist/image/jpeg2000-dwt.cjs +148 -0
- package/dist/image/jpeg2000-dwt.d.cts +34 -0
- package/dist/image/jpeg2000-dwt.d.ts +34 -0
- package/dist/image/jpeg2000-dwt.js +145 -0
- package/dist/image/jpeg2000-errors.cjs +17 -0
- package/dist/image/jpeg2000-errors.d.cts +9 -0
- package/dist/image/jpeg2000-errors.d.ts +9 -0
- package/dist/image/jpeg2000-errors.js +15 -0
- package/dist/image/jpeg2000-t1.cjs +254 -0
- package/dist/image/jpeg2000-t1.d.cts +18 -0
- package/dist/image/jpeg2000-t1.d.ts +18 -0
- package/dist/image/jpeg2000-t1.js +253 -0
- package/dist/image/jpeg2000-t2.cjs +316 -0
- package/dist/image/jpeg2000-t2.d.cts +79 -0
- package/dist/image/jpeg2000-t2.d.ts +79 -0
- package/dist/image/jpeg2000-t2.js +311 -0
- package/dist/image/jpeg2000-tagtree.cjs +105 -0
- package/dist/image/jpeg2000-tagtree.d.cts +29 -0
- package/dist/image/jpeg2000-tagtree.d.ts +29 -0
- package/dist/image/jpeg2000-tagtree.js +103 -0
- package/dist/image/jpeg2000.cjs +322 -0
- package/dist/image/jpeg2000.d.cts +54 -0
- package/dist/image/jpeg2000.d.ts +54 -0
- package/dist/image/jpeg2000.js +320 -0
- package/dist/images-read.cjs +78 -1
- package/dist/images-read.js +78 -1
- package/dist/index.cjs +17 -0
- package/dist/index.d.cts +11 -2
- package/dist/index.d.ts +11 -2
- package/dist/index.js +7 -1
- package/dist/jbig2-errors-Bj6MPxrx.d.cts +9 -0
- package/dist/jbig2-errors-Bj6MPxrx.d.ts +9 -0
- package/dist/math-font.cjs +43 -18
- package/dist/math-font.d.cts +8 -1
- package/dist/math-font.d.ts +8 -1
- package/dist/math-font.js +43 -18
- package/dist/math-stretch-Bwk_bxh1.d.ts +24 -0
- package/dist/math-stretch-DXWjs8gL.d.cts +24 -0
- package/dist/math-stretch.cjs +96 -0
- package/dist/math-stretch.d.cts +2 -0
- package/dist/math-stretch.d.ts +2 -0
- package/dist/math-stretch.js +94 -0
- package/dist/math-table-DhPrNoPa.d.ts +70 -0
- package/dist/math-table-gFNB8DmQ.d.cts +70 -0
- package/dist/math-table.cjs +69 -1
- package/dist/math-table.d.cts +2 -45
- package/dist/math-table.d.ts +2 -45
- package/dist/math-table.js +69 -1
- package/dist/{math-types-Ba8qAf1p.d.cts → math-types-YSQUTviw.d.cts} +2 -0
- package/dist/{math-types-Ba8qAf1p.d.ts → math-types-YSQUTviw.d.ts} +2 -0
- package/dist/math-types.d.cts +1 -1
- package/dist/math-types.d.ts +1 -1
- package/package.json +1 -1
package/dist/glyf.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { f2dot14, hasBytes, i16, sfntTableBytes, u16, u32, u8 } from "./sfnt.js";
|
|
2
|
+
import { unionGlyphInkBounds } from "./glyph-bounds.js";
|
|
2
3
|
//#region src/glyf.ts
|
|
3
4
|
const LOCA_SHORT_ENTRY_SIZE = 2;
|
|
4
5
|
const LOCA_LONG_ENTRY_SIZE = 4;
|
|
@@ -20,6 +21,9 @@ const WE_HAVE_A_SCALE = 8;
|
|
|
20
21
|
const MORE_COMPONENTS = 32;
|
|
21
22
|
const WE_HAVE_AN_X_AND_Y_SCALE = 64;
|
|
22
23
|
const WE_HAVE_A_TWO_BY_TWO = 128;
|
|
24
|
+
const SCALED_COMPONENT_OFFSET = 2048;
|
|
25
|
+
const UNSCALED_COMPONENT_OFFSET = 4096;
|
|
26
|
+
const MAX_COMPOSITE_DEPTH = 5;
|
|
23
27
|
const COMPONENT_RECORD_HEADER_SIZE = 4;
|
|
24
28
|
function signedByte(value) {
|
|
25
29
|
return value >= 128 ? value - 256 : value;
|
|
@@ -87,6 +91,31 @@ function readComponents(glyph) {
|
|
|
87
91
|
if ((flags & MORE_COMPONENTS) === 0) return components;
|
|
88
92
|
}
|
|
89
93
|
}
|
|
94
|
+
function placeComponentBounds(bounds, component) {
|
|
95
|
+
const [a, b, c, d] = component.transform ?? [
|
|
96
|
+
1,
|
|
97
|
+
0,
|
|
98
|
+
0,
|
|
99
|
+
1
|
|
100
|
+
];
|
|
101
|
+
const corners = [
|
|
102
|
+
[bounds.xMin, bounds.yMin],
|
|
103
|
+
[bounds.xMax, bounds.yMin],
|
|
104
|
+
[bounds.xMin, bounds.yMax],
|
|
105
|
+
[bounds.xMax, bounds.yMax]
|
|
106
|
+
];
|
|
107
|
+
const xs = corners.map(([x, y]) => a * x + c * y);
|
|
108
|
+
const ys = corners.map(([x, y]) => b * x + d * y);
|
|
109
|
+
const scaledOffset = (component.flags & SCALED_COMPONENT_OFFSET) !== 0 && (component.flags & UNSCALED_COMPONENT_OFFSET) === 0;
|
|
110
|
+
const dx = scaledOffset ? a * component.argument1 + c * component.argument2 : component.argument1;
|
|
111
|
+
const dy = scaledOffset ? b * component.argument1 + d * component.argument2 : component.argument2;
|
|
112
|
+
return {
|
|
113
|
+
xMin: Math.min(...xs) + dx,
|
|
114
|
+
yMin: Math.min(...ys) + dy,
|
|
115
|
+
xMax: Math.max(...xs) + dx,
|
|
116
|
+
yMax: Math.max(...ys) + dy
|
|
117
|
+
};
|
|
118
|
+
}
|
|
90
119
|
function parseGlyf(font, options) {
|
|
91
120
|
const glyfBytes = sfntTableBytes(font, "glyf");
|
|
92
121
|
const loca = parseLoca(font, options);
|
|
@@ -109,16 +138,43 @@ function parseGlyf(font, options) {
|
|
|
109
138
|
yMax: i16(glyph, 8)
|
|
110
139
|
};
|
|
111
140
|
};
|
|
141
|
+
const compositeComponents = (glyphId) => {
|
|
142
|
+
const header = glyphHeader(glyphId);
|
|
143
|
+
if (header === void 0 || header.numberOfContours >= 0) return;
|
|
144
|
+
const glyph = glyphBytes(glyphId);
|
|
145
|
+
return glyph === void 0 ? void 0 : readComponents(glyph);
|
|
146
|
+
};
|
|
147
|
+
const resolveInkBounds = (glyphId, depth) => {
|
|
148
|
+
const header = glyphHeader(glyphId);
|
|
149
|
+
if (header === void 0) return;
|
|
150
|
+
if (header.numberOfContours >= 0) return {
|
|
151
|
+
xMin: header.xMin,
|
|
152
|
+
yMin: header.yMin,
|
|
153
|
+
xMax: header.xMax,
|
|
154
|
+
yMax: header.yMax
|
|
155
|
+
};
|
|
156
|
+
if (depth >= MAX_COMPOSITE_DEPTH) return;
|
|
157
|
+
const components = compositeComponents(glyphId);
|
|
158
|
+
if (components === void 0) return;
|
|
159
|
+
let bounds;
|
|
160
|
+
for (const component of components) {
|
|
161
|
+
if (!component.argsAreXyValues) return;
|
|
162
|
+
const componentBytes = glyphBytes(component.glyphIndex);
|
|
163
|
+
if (componentBytes === void 0) return;
|
|
164
|
+
if (componentBytes.length === 0) continue;
|
|
165
|
+
const componentBounds = resolveInkBounds(component.glyphIndex, depth + 1);
|
|
166
|
+
if (componentBounds === void 0) return;
|
|
167
|
+
const placed = placeComponentBounds(componentBounds, component);
|
|
168
|
+
bounds = bounds === void 0 ? placed : unionGlyphInkBounds(bounds, placed);
|
|
169
|
+
}
|
|
170
|
+
return bounds;
|
|
171
|
+
};
|
|
112
172
|
return {
|
|
113
173
|
numGlyphs: options.numGlyphs,
|
|
114
174
|
glyphBytes,
|
|
115
175
|
glyphHeader,
|
|
116
|
-
compositeComponents
|
|
117
|
-
|
|
118
|
-
if (header === void 0 || header.numberOfContours >= 0) return;
|
|
119
|
-
const glyph = glyphBytes(glyphId);
|
|
120
|
-
return glyph === void 0 ? void 0 : readComponents(glyph);
|
|
121
|
-
}
|
|
176
|
+
compositeComponents,
|
|
177
|
+
glyphInkBounds: (glyphId) => resolveInkBounds(glyphId, 0)
|
|
122
178
|
};
|
|
123
179
|
}
|
|
124
180
|
//#endregion
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/glyph-bounds.d.ts
|
|
2
|
+
interface GlyphInkBounds {
|
|
3
|
+
readonly xMin: number;
|
|
4
|
+
readonly yMin: number;
|
|
5
|
+
readonly xMax: number;
|
|
6
|
+
readonly yMax: number;
|
|
7
|
+
}
|
|
8
|
+
declare function unionGlyphInkBounds(a: GlyphInkBounds, b: GlyphInkBounds): GlyphInkBounds;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { unionGlyphInkBounds as n, GlyphInkBounds as t };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/glyph-bounds.d.ts
|
|
2
|
+
interface GlyphInkBounds {
|
|
3
|
+
readonly xMin: number;
|
|
4
|
+
readonly yMin: number;
|
|
5
|
+
readonly xMax: number;
|
|
6
|
+
readonly yMax: number;
|
|
7
|
+
}
|
|
8
|
+
declare function unionGlyphInkBounds(a: GlyphInkBounds, b: GlyphInkBounds): GlyphInkBounds;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { unionGlyphInkBounds as n, GlyphInkBounds as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/glyph-bounds.ts
|
|
3
|
+
function unionGlyphInkBounds(a, b) {
|
|
4
|
+
return {
|
|
5
|
+
xMin: Math.min(a.xMin, b.xMin),
|
|
6
|
+
yMin: Math.min(a.yMin, b.yMin),
|
|
7
|
+
xMax: Math.max(a.xMax, b.xMax),
|
|
8
|
+
yMax: Math.max(a.yMax, b.yMax)
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.unionGlyphInkBounds = unionGlyphInkBounds;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/glyph-bounds.ts
|
|
2
|
+
function unionGlyphInkBounds(a, b) {
|
|
3
|
+
return {
|
|
4
|
+
xMin: Math.min(a.xMin, b.xMin),
|
|
5
|
+
yMin: Math.min(a.yMin, b.yMin),
|
|
6
|
+
xMax: Math.max(a.xMax, b.xMax),
|
|
7
|
+
yMax: Math.max(a.yMax, b.yMax)
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { unionGlyphInkBounds };
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/image/jbig2-arith.ts
|
|
3
|
+
const QE_STATES = [
|
|
4
|
+
[
|
|
5
|
+
22017,
|
|
6
|
+
1,
|
|
7
|
+
1,
|
|
8
|
+
1
|
|
9
|
+
],
|
|
10
|
+
[
|
|
11
|
+
13313,
|
|
12
|
+
2,
|
|
13
|
+
6,
|
|
14
|
+
0
|
|
15
|
+
],
|
|
16
|
+
[
|
|
17
|
+
6145,
|
|
18
|
+
3,
|
|
19
|
+
9,
|
|
20
|
+
0
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
2753,
|
|
24
|
+
4,
|
|
25
|
+
12,
|
|
26
|
+
0
|
|
27
|
+
],
|
|
28
|
+
[
|
|
29
|
+
1313,
|
|
30
|
+
5,
|
|
31
|
+
29,
|
|
32
|
+
0
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
545,
|
|
36
|
+
38,
|
|
37
|
+
33,
|
|
38
|
+
0
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
22017,
|
|
42
|
+
7,
|
|
43
|
+
6,
|
|
44
|
+
1
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
21505,
|
|
48
|
+
8,
|
|
49
|
+
14,
|
|
50
|
+
0
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
18433,
|
|
54
|
+
9,
|
|
55
|
+
14,
|
|
56
|
+
0
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
14337,
|
|
60
|
+
10,
|
|
61
|
+
14,
|
|
62
|
+
0
|
|
63
|
+
],
|
|
64
|
+
[
|
|
65
|
+
12289,
|
|
66
|
+
11,
|
|
67
|
+
17,
|
|
68
|
+
0
|
|
69
|
+
],
|
|
70
|
+
[
|
|
71
|
+
9217,
|
|
72
|
+
12,
|
|
73
|
+
18,
|
|
74
|
+
0
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
7169,
|
|
78
|
+
13,
|
|
79
|
+
20,
|
|
80
|
+
0
|
|
81
|
+
],
|
|
82
|
+
[
|
|
83
|
+
5633,
|
|
84
|
+
29,
|
|
85
|
+
21,
|
|
86
|
+
0
|
|
87
|
+
],
|
|
88
|
+
[
|
|
89
|
+
22017,
|
|
90
|
+
15,
|
|
91
|
+
14,
|
|
92
|
+
1
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
21505,
|
|
96
|
+
16,
|
|
97
|
+
14,
|
|
98
|
+
0
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
20737,
|
|
102
|
+
17,
|
|
103
|
+
15,
|
|
104
|
+
0
|
|
105
|
+
],
|
|
106
|
+
[
|
|
107
|
+
18433,
|
|
108
|
+
18,
|
|
109
|
+
16,
|
|
110
|
+
0
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
14337,
|
|
114
|
+
19,
|
|
115
|
+
17,
|
|
116
|
+
0
|
|
117
|
+
],
|
|
118
|
+
[
|
|
119
|
+
13313,
|
|
120
|
+
20,
|
|
121
|
+
18,
|
|
122
|
+
0
|
|
123
|
+
],
|
|
124
|
+
[
|
|
125
|
+
12289,
|
|
126
|
+
21,
|
|
127
|
+
19,
|
|
128
|
+
0
|
|
129
|
+
],
|
|
130
|
+
[
|
|
131
|
+
10241,
|
|
132
|
+
22,
|
|
133
|
+
19,
|
|
134
|
+
0
|
|
135
|
+
],
|
|
136
|
+
[
|
|
137
|
+
9217,
|
|
138
|
+
23,
|
|
139
|
+
20,
|
|
140
|
+
0
|
|
141
|
+
],
|
|
142
|
+
[
|
|
143
|
+
8705,
|
|
144
|
+
24,
|
|
145
|
+
21,
|
|
146
|
+
0
|
|
147
|
+
],
|
|
148
|
+
[
|
|
149
|
+
7169,
|
|
150
|
+
25,
|
|
151
|
+
22,
|
|
152
|
+
0
|
|
153
|
+
],
|
|
154
|
+
[
|
|
155
|
+
6145,
|
|
156
|
+
26,
|
|
157
|
+
23,
|
|
158
|
+
0
|
|
159
|
+
],
|
|
160
|
+
[
|
|
161
|
+
5633,
|
|
162
|
+
27,
|
|
163
|
+
24,
|
|
164
|
+
0
|
|
165
|
+
],
|
|
166
|
+
[
|
|
167
|
+
5121,
|
|
168
|
+
28,
|
|
169
|
+
25,
|
|
170
|
+
0
|
|
171
|
+
],
|
|
172
|
+
[
|
|
173
|
+
4609,
|
|
174
|
+
29,
|
|
175
|
+
26,
|
|
176
|
+
0
|
|
177
|
+
],
|
|
178
|
+
[
|
|
179
|
+
4353,
|
|
180
|
+
30,
|
|
181
|
+
27,
|
|
182
|
+
0
|
|
183
|
+
],
|
|
184
|
+
[
|
|
185
|
+
2753,
|
|
186
|
+
31,
|
|
187
|
+
28,
|
|
188
|
+
0
|
|
189
|
+
],
|
|
190
|
+
[
|
|
191
|
+
2497,
|
|
192
|
+
32,
|
|
193
|
+
29,
|
|
194
|
+
0
|
|
195
|
+
],
|
|
196
|
+
[
|
|
197
|
+
2209,
|
|
198
|
+
33,
|
|
199
|
+
30,
|
|
200
|
+
0
|
|
201
|
+
],
|
|
202
|
+
[
|
|
203
|
+
1313,
|
|
204
|
+
34,
|
|
205
|
+
31,
|
|
206
|
+
0
|
|
207
|
+
],
|
|
208
|
+
[
|
|
209
|
+
1089,
|
|
210
|
+
35,
|
|
211
|
+
32,
|
|
212
|
+
0
|
|
213
|
+
],
|
|
214
|
+
[
|
|
215
|
+
673,
|
|
216
|
+
36,
|
|
217
|
+
33,
|
|
218
|
+
0
|
|
219
|
+
],
|
|
220
|
+
[
|
|
221
|
+
545,
|
|
222
|
+
37,
|
|
223
|
+
34,
|
|
224
|
+
0
|
|
225
|
+
],
|
|
226
|
+
[
|
|
227
|
+
321,
|
|
228
|
+
38,
|
|
229
|
+
35,
|
|
230
|
+
0
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
273,
|
|
234
|
+
39,
|
|
235
|
+
36,
|
|
236
|
+
0
|
|
237
|
+
],
|
|
238
|
+
[
|
|
239
|
+
133,
|
|
240
|
+
40,
|
|
241
|
+
37,
|
|
242
|
+
0
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
73,
|
|
246
|
+
41,
|
|
247
|
+
38,
|
|
248
|
+
0
|
|
249
|
+
],
|
|
250
|
+
[
|
|
251
|
+
37,
|
|
252
|
+
42,
|
|
253
|
+
39,
|
|
254
|
+
0
|
|
255
|
+
],
|
|
256
|
+
[
|
|
257
|
+
21,
|
|
258
|
+
43,
|
|
259
|
+
40,
|
|
260
|
+
0
|
|
261
|
+
],
|
|
262
|
+
[
|
|
263
|
+
9,
|
|
264
|
+
44,
|
|
265
|
+
41,
|
|
266
|
+
0
|
|
267
|
+
],
|
|
268
|
+
[
|
|
269
|
+
5,
|
|
270
|
+
45,
|
|
271
|
+
42,
|
|
272
|
+
0
|
|
273
|
+
],
|
|
274
|
+
[
|
|
275
|
+
1,
|
|
276
|
+
45,
|
|
277
|
+
43,
|
|
278
|
+
0
|
|
279
|
+
],
|
|
280
|
+
[
|
|
281
|
+
22017,
|
|
282
|
+
46,
|
|
283
|
+
46,
|
|
284
|
+
0
|
|
285
|
+
]
|
|
286
|
+
];
|
|
287
|
+
const QE_VALUE = Uint16Array.from(QE_STATES, (row) => row[0]);
|
|
288
|
+
const QE_NMPS = Uint8Array.from(QE_STATES, (row) => row[1]);
|
|
289
|
+
const QE_NLPS = Uint8Array.from(QE_STATES, (row) => row[2]);
|
|
290
|
+
const QE_SWITCH = Uint8Array.from(QE_STATES, (row) => row[3]);
|
|
291
|
+
function createArithContexts(contextBits) {
|
|
292
|
+
return new Uint8Array(1 << contextBits);
|
|
293
|
+
}
|
|
294
|
+
const A_INITIAL = 32768;
|
|
295
|
+
const C_INITIAL_SHIFT = 16;
|
|
296
|
+
const INITDEC_C_SHIFT = 7;
|
|
297
|
+
const PAST_END_BYTE = 255;
|
|
298
|
+
var MqDecoder = class {
|
|
299
|
+
data;
|
|
300
|
+
start;
|
|
301
|
+
end;
|
|
302
|
+
bp;
|
|
303
|
+
c = 0;
|
|
304
|
+
a = 0;
|
|
305
|
+
ct = 0;
|
|
306
|
+
constructor(data, start = 0, end = data.length) {
|
|
307
|
+
this.data = data;
|
|
308
|
+
this.start = start;
|
|
309
|
+
this.end = end;
|
|
310
|
+
this.bp = start;
|
|
311
|
+
this.c = this.byteAt(this.bp) << C_INITIAL_SHIFT >>> 0;
|
|
312
|
+
this.byteIn();
|
|
313
|
+
this.c = this.c << INITDEC_C_SHIFT >>> 0;
|
|
314
|
+
this.ct -= INITDEC_C_SHIFT;
|
|
315
|
+
this.a = A_INITIAL;
|
|
316
|
+
}
|
|
317
|
+
byteAt(index) {
|
|
318
|
+
return index >= this.start && index < this.end ? this.data[index] ?? PAST_END_BYTE : PAST_END_BYTE;
|
|
319
|
+
}
|
|
320
|
+
byteIn() {
|
|
321
|
+
if (this.byteAt(this.bp) === 255) {
|
|
322
|
+
if (this.byteAt(this.bp + 1) > 143) {
|
|
323
|
+
this.c = this.c + 65280 >>> 0;
|
|
324
|
+
this.ct = 8;
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
this.bp++;
|
|
328
|
+
this.c = this.c + (this.byteAt(this.bp) << 9) >>> 0;
|
|
329
|
+
this.ct = 7;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
this.bp++;
|
|
333
|
+
this.c = this.c + (this.byteAt(this.bp) << 8) >>> 0;
|
|
334
|
+
this.ct = 8;
|
|
335
|
+
}
|
|
336
|
+
renormalise() {
|
|
337
|
+
do {
|
|
338
|
+
if (this.ct === 0) this.byteIn();
|
|
339
|
+
this.a = this.a << 1 & 65535;
|
|
340
|
+
this.c = this.c << 1 >>> 0;
|
|
341
|
+
this.ct--;
|
|
342
|
+
} while ((this.a & A_INITIAL) === 0);
|
|
343
|
+
}
|
|
344
|
+
decode(contexts, contextIndex) {
|
|
345
|
+
const state = contexts[contextIndex] ?? 0;
|
|
346
|
+
let index = state >> 1;
|
|
347
|
+
let mps = state & 1;
|
|
348
|
+
const qe = QE_VALUE[index] ?? 0;
|
|
349
|
+
this.a -= qe;
|
|
350
|
+
let decision;
|
|
351
|
+
if (this.c >>> 16 < qe) {
|
|
352
|
+
if (this.a < qe) {
|
|
353
|
+
decision = mps;
|
|
354
|
+
index = QE_NMPS[index] ?? 0;
|
|
355
|
+
} else {
|
|
356
|
+
decision = 1 - mps;
|
|
357
|
+
if (QE_SWITCH[index] === 1) mps = 1 - mps;
|
|
358
|
+
index = QE_NLPS[index] ?? 0;
|
|
359
|
+
}
|
|
360
|
+
this.a = qe;
|
|
361
|
+
} else {
|
|
362
|
+
this.c = this.c - (qe << 16) >>> 0;
|
|
363
|
+
if ((this.a & A_INITIAL) !== 0) return mps;
|
|
364
|
+
if (this.a < qe) {
|
|
365
|
+
decision = 1 - mps;
|
|
366
|
+
if (QE_SWITCH[index] === 1) mps = 1 - mps;
|
|
367
|
+
index = QE_NLPS[index] ?? 0;
|
|
368
|
+
} else {
|
|
369
|
+
decision = mps;
|
|
370
|
+
index = QE_NMPS[index] ?? 0;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
this.renormalise();
|
|
374
|
+
contexts[contextIndex] = index << 1 | mps;
|
|
375
|
+
return decision;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const INTEGER_CONTEXT_BITS = 9;
|
|
379
|
+
function createIntegerContexts() {
|
|
380
|
+
return createArithContexts(INTEGER_CONTEXT_BITS);
|
|
381
|
+
}
|
|
382
|
+
const INTEGER_RANGES = [
|
|
383
|
+
[2, 0],
|
|
384
|
+
[4, 4],
|
|
385
|
+
[6, 20],
|
|
386
|
+
[8, 84],
|
|
387
|
+
[12, 340],
|
|
388
|
+
[32, 4436]
|
|
389
|
+
];
|
|
390
|
+
function decodeInteger(mq, contexts) {
|
|
391
|
+
let prev = 1;
|
|
392
|
+
const nextBit = () => {
|
|
393
|
+
const bit = mq.decode(contexts, prev);
|
|
394
|
+
prev = prev < 256 ? prev << 1 | bit : (prev << 1 | bit) & 511 | 256;
|
|
395
|
+
return bit;
|
|
396
|
+
};
|
|
397
|
+
const readBits = (count) => {
|
|
398
|
+
let value = 0;
|
|
399
|
+
for (let i = 0; i < count; i++) value = value * 2 + nextBit();
|
|
400
|
+
return value;
|
|
401
|
+
};
|
|
402
|
+
const sign = nextBit();
|
|
403
|
+
let magnitude = 0;
|
|
404
|
+
let matched = false;
|
|
405
|
+
for (let i = 0; i < INTEGER_RANGES.length - 1; i++) if (nextBit() === 0) {
|
|
406
|
+
const [bits, offset] = INTEGER_RANGES[i] ?? [0, 0];
|
|
407
|
+
magnitude = readBits(bits) + offset;
|
|
408
|
+
matched = true;
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
if (!matched) {
|
|
412
|
+
const [bits, offset] = INTEGER_RANGES[INTEGER_RANGES.length - 1] ?? [0, 0];
|
|
413
|
+
magnitude = readBits(bits) + offset;
|
|
414
|
+
}
|
|
415
|
+
if (sign === 1) return magnitude === 0 ? void 0 : -magnitude;
|
|
416
|
+
return magnitude;
|
|
417
|
+
}
|
|
418
|
+
function createSymbolIdContexts(codeLength) {
|
|
419
|
+
return createArithContexts(codeLength + 1);
|
|
420
|
+
}
|
|
421
|
+
function decodeSymbolId(mq, contexts, codeLength) {
|
|
422
|
+
let prev = 1;
|
|
423
|
+
for (let i = 0; i < codeLength; i++) prev = prev << 1 | mq.decode(contexts, prev);
|
|
424
|
+
return prev - (1 << codeLength);
|
|
425
|
+
}
|
|
426
|
+
//#endregion
|
|
427
|
+
exports.MqDecoder = MqDecoder;
|
|
428
|
+
exports.createArithContexts = createArithContexts;
|
|
429
|
+
exports.createIntegerContexts = createIntegerContexts;
|
|
430
|
+
exports.createSymbolIdContexts = createSymbolIdContexts;
|
|
431
|
+
exports.decodeInteger = decodeInteger;
|
|
432
|
+
exports.decodeSymbolId = decodeSymbolId;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/image/jbig2-arith.d.ts
|
|
2
|
+
type ArithContexts = Uint8Array<ArrayBuffer>;
|
|
3
|
+
declare function createArithContexts(contextBits: number): ArithContexts;
|
|
4
|
+
declare class MqDecoder {
|
|
5
|
+
private readonly data;
|
|
6
|
+
private readonly start;
|
|
7
|
+
private readonly end;
|
|
8
|
+
private bp;
|
|
9
|
+
private c;
|
|
10
|
+
private a;
|
|
11
|
+
private ct;
|
|
12
|
+
constructor(data: Uint8Array<ArrayBuffer>, start?: number, end?: number);
|
|
13
|
+
private byteAt;
|
|
14
|
+
private byteIn;
|
|
15
|
+
private renormalise;
|
|
16
|
+
decode(contexts: ArithContexts, contextIndex: number): number;
|
|
17
|
+
}
|
|
18
|
+
declare function createIntegerContexts(): ArithContexts;
|
|
19
|
+
declare function decodeInteger(mq: MqDecoder, contexts: ArithContexts): number | undefined;
|
|
20
|
+
declare function createSymbolIdContexts(codeLength: number): ArithContexts;
|
|
21
|
+
declare function decodeSymbolId(mq: MqDecoder, contexts: ArithContexts, codeLength: number): number;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ArithContexts, MqDecoder, createArithContexts, createIntegerContexts, createSymbolIdContexts, decodeInteger, decodeSymbolId };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/image/jbig2-arith.d.ts
|
|
2
|
+
type ArithContexts = Uint8Array<ArrayBuffer>;
|
|
3
|
+
declare function createArithContexts(contextBits: number): ArithContexts;
|
|
4
|
+
declare class MqDecoder {
|
|
5
|
+
private readonly data;
|
|
6
|
+
private readonly start;
|
|
7
|
+
private readonly end;
|
|
8
|
+
private bp;
|
|
9
|
+
private c;
|
|
10
|
+
private a;
|
|
11
|
+
private ct;
|
|
12
|
+
constructor(data: Uint8Array<ArrayBuffer>, start?: number, end?: number);
|
|
13
|
+
private byteAt;
|
|
14
|
+
private byteIn;
|
|
15
|
+
private renormalise;
|
|
16
|
+
decode(contexts: ArithContexts, contextIndex: number): number;
|
|
17
|
+
}
|
|
18
|
+
declare function createIntegerContexts(): ArithContexts;
|
|
19
|
+
declare function decodeInteger(mq: MqDecoder, contexts: ArithContexts): number | undefined;
|
|
20
|
+
declare function createSymbolIdContexts(codeLength: number): ArithContexts;
|
|
21
|
+
declare function decodeSymbolId(mq: MqDecoder, contexts: ArithContexts, codeLength: number): number;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ArithContexts, MqDecoder, createArithContexts, createIntegerContexts, createSymbolIdContexts, decodeInteger, decodeSymbolId };
|