@mlightcad/pdf-renderer 1.7.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/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/index.js +40188 -0
- package/dist/index.umd.cjs +159 -0
- package/lib/AcPdfExport.d.ts +10 -0
- package/lib/AcPdfExport.d.ts.map +1 -0
- package/lib/AcPdfExport.js +354 -0
- package/lib/AcPdfExport.js.map +1 -0
- package/lib/AcPdfExportOptions.d.ts +105 -0
- package/lib/AcPdfExportOptions.d.ts.map +1 -0
- package/lib/AcPdfExportOptions.js +2 -0
- package/lib/AcPdfExportOptions.js.map +1 -0
- package/lib/AcPdfUnits.d.ts +17 -0
- package/lib/AcPdfUnits.d.ts.map +1 -0
- package/lib/AcPdfUnits.js +70 -0
- package/lib/AcPdfUnits.js.map +1 -0
- package/lib/hatch/AcPdfGradient.d.ts +43 -0
- package/lib/hatch/AcPdfGradient.d.ts.map +1 -0
- package/lib/hatch/AcPdfGradient.js +222 -0
- package/lib/hatch/AcPdfGradient.js.map +1 -0
- package/lib/hatch/AcPdfHatchTessellator.d.ts +24 -0
- package/lib/hatch/AcPdfHatchTessellator.d.ts.map +1 -0
- package/lib/hatch/AcPdfHatchTessellator.js +362 -0
- package/lib/hatch/AcPdfHatchTessellator.js.map +1 -0
- package/lib/index.d.ts +21 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +16 -0
- package/lib/index.js.map +1 -0
- package/lib/linetype/AcPdfLineTypeStroker.d.ts +36 -0
- package/lib/linetype/AcPdfLineTypeStroker.d.ts.map +1 -0
- package/lib/linetype/AcPdfLineTypeStroker.js +160 -0
- package/lib/linetype/AcPdfLineTypeStroker.js.map +1 -0
- package/lib/pdf/AcPdfContentWriter.d.ts +197 -0
- package/lib/pdf/AcPdfContentWriter.d.ts.map +1 -0
- package/lib/pdf/AcPdfContentWriter.js +977 -0
- package/lib/pdf/AcPdfContentWriter.js.map +1 -0
- package/lib/pdf/AcPdfDocumentWriter.d.ts +66 -0
- package/lib/pdf/AcPdfDocumentWriter.d.ts.map +1 -0
- package/lib/pdf/AcPdfDocumentWriter.js +360 -0
- package/lib/pdf/AcPdfDocumentWriter.js.map +1 -0
- package/lib/pdf/AcPdfEffectiveLayer.d.ts +7 -0
- package/lib/pdf/AcPdfEffectiveLayer.d.ts.map +1 -0
- package/lib/pdf/AcPdfEffectiveLayer.js +13 -0
- package/lib/pdf/AcPdfEffectiveLayer.js.map +1 -0
- package/lib/pdf/AcPdfFontManager.d.ts +95 -0
- package/lib/pdf/AcPdfFontManager.d.ts.map +1 -0
- package/lib/pdf/AcPdfFontManager.js +517 -0
- package/lib/pdf/AcPdfFontManager.js.map +1 -0
- package/lib/pdf/AcPdfMarkedContent.d.ts +14 -0
- package/lib/pdf/AcPdfMarkedContent.d.ts.map +1 -0
- package/lib/pdf/AcPdfMarkedContent.js +28 -0
- package/lib/pdf/AcPdfMarkedContent.js.map +1 -0
- package/lib/pdf/AcPdfOcgManager.d.ts +20 -0
- package/lib/pdf/AcPdfOcgManager.d.ts.map +1 -0
- package/lib/pdf/AcPdfOcgManager.js +147 -0
- package/lib/pdf/AcPdfOcgManager.js.map +1 -0
- package/lib/pdf/AcPdfPageLayout.d.ts +47 -0
- package/lib/pdf/AcPdfPageLayout.d.ts.map +1 -0
- package/lib/pdf/AcPdfPageLayout.js +79 -0
- package/lib/pdf/AcPdfPageLayout.js.map +1 -0
- package/lib/renderer/AcPdfEntity.d.ts +96 -0
- package/lib/renderer/AcPdfEntity.d.ts.map +1 -0
- package/lib/renderer/AcPdfEntity.js +661 -0
- package/lib/renderer/AcPdfEntity.js.map +1 -0
- package/lib/renderer/AcPdfGroup.d.ts +9 -0
- package/lib/renderer/AcPdfGroup.d.ts.map +1 -0
- package/lib/renderer/AcPdfGroup.js +65 -0
- package/lib/renderer/AcPdfGroup.js.map +1 -0
- package/lib/renderer/AcPdfMTextParser.d.ts +46 -0
- package/lib/renderer/AcPdfMTextParser.d.ts.map +1 -0
- package/lib/renderer/AcPdfMTextParser.js +225 -0
- package/lib/renderer/AcPdfMTextParser.js.map +1 -0
- package/lib/renderer/AcPdfMatrixUtil.d.ts +23 -0
- package/lib/renderer/AcPdfMatrixUtil.d.ts.map +1 -0
- package/lib/renderer/AcPdfMatrixUtil.js +85 -0
- package/lib/renderer/AcPdfMatrixUtil.js.map +1 -0
- package/lib/renderer/AcPdfRenderer.d.ts +139 -0
- package/lib/renderer/AcPdfRenderer.d.ts.map +1 -0
- package/lib/renderer/AcPdfRenderer.js +1188 -0
- package/lib/renderer/AcPdfRenderer.js.map +1 -0
- package/lib/renderer/AcPdfStyle.d.ts +132 -0
- package/lib/renderer/AcPdfStyle.d.ts.map +1 -0
- package/lib/renderer/AcPdfStyle.js +8 -0
- package/lib/renderer/AcPdfStyle.js.map +1 -0
- package/lib/renderer/AcPdfStyleUtil.d.ts +49 -0
- package/lib/renderer/AcPdfStyleUtil.d.ts.map +1 -0
- package/lib/renderer/AcPdfStyleUtil.js +140 -0
- package/lib/renderer/AcPdfStyleUtil.js.map +1 -0
- package/lib/renderer/AcPdfTextLayout.d.ts +103 -0
- package/lib/renderer/AcPdfTextLayout.d.ts.map +1 -0
- package/lib/renderer/AcPdfTextLayout.js +539 -0
- package/lib/renderer/AcPdfTextLayout.js.map +1 -0
- package/lib/text/AcPdfGlyphProvider.d.ts +52 -0
- package/lib/text/AcPdfGlyphProvider.d.ts.map +1 -0
- package/lib/text/AcPdfGlyphProvider.js +2 -0
- package/lib/text/AcPdfGlyphProvider.js.map +1 -0
- package/lib/viewport/AcPdfPaperSpaceExport.d.ts +28 -0
- package/lib/viewport/AcPdfPaperSpaceExport.d.ts.map +1 -0
- package/lib/viewport/AcPdfPaperSpaceExport.js +129 -0
- package/lib/viewport/AcPdfPaperSpaceExport.js.map +1 -0
- package/lib/viewport/AcPdfPaperViewport.d.ts +42 -0
- package/lib/viewport/AcPdfPaperViewport.d.ts.map +1 -0
- package/lib/viewport/AcPdfPaperViewport.js +80 -0
- package/lib/viewport/AcPdfPaperViewport.js.map +1 -0
- package/lib/viewport/AcPdfViewportContent.d.ts +37 -0
- package/lib/viewport/AcPdfViewportContent.d.ts.map +1 -0
- package/lib/viewport/AcPdfViewportContent.js +163 -0
- package/lib/viewport/AcPdfViewportContent.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,977 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
23
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var __values = (this && this.__values) || function(o) {
|
|
49
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
50
|
+
if (m) return m.call(o);
|
|
51
|
+
if (o && typeof o.length === "number") return {
|
|
52
|
+
next: function () {
|
|
53
|
+
if (o && i >= o.length) o = void 0;
|
|
54
|
+
return { value: o && o[i++], done: !o };
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
58
|
+
};
|
|
59
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
60
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
61
|
+
if (!m) return o;
|
|
62
|
+
var i = m.call(o), r, ar = [], e;
|
|
63
|
+
try {
|
|
64
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
65
|
+
}
|
|
66
|
+
catch (error) { e = { error: error }; }
|
|
67
|
+
finally {
|
|
68
|
+
try {
|
|
69
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
70
|
+
}
|
|
71
|
+
finally { if (e) throw e.error; }
|
|
72
|
+
}
|
|
73
|
+
return ar;
|
|
74
|
+
};
|
|
75
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
76
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
77
|
+
if (ar || !(i in from)) {
|
|
78
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
79
|
+
ar[i] = from[i];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
83
|
+
};
|
|
84
|
+
import { deflate } from 'pako';
|
|
85
|
+
import { PDFArray, PDFName, PDFRawStream } from 'pdf-lib';
|
|
86
|
+
import { AcPdfMatrixUtil } from '../renderer/AcPdfMatrixUtil';
|
|
87
|
+
import { pdfHexText } from './AcPdfMarkedContent';
|
|
88
|
+
import { setPageNamedResource } from './AcPdfOcgManager';
|
|
89
|
+
var KAPPA = 0.5522847498307936;
|
|
90
|
+
export function createPdfFormRegistry() {
|
|
91
|
+
return {
|
|
92
|
+
entries: new Map(),
|
|
93
|
+
bufferIds: new WeakMap(),
|
|
94
|
+
nameSeq: 0,
|
|
95
|
+
bufferSeq: 0
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Flate-compresses stream bytes for embedding (`/Filter /FlateDecode`).
|
|
100
|
+
* Returns `null` for tiny streams where the compression overhead outweighs
|
|
101
|
+
* the savings, or when the platform deflate fails.
|
|
102
|
+
*/
|
|
103
|
+
function flateStreamBytes(bytes) {
|
|
104
|
+
if (bytes.length < 256) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
return deflate(bytes);
|
|
109
|
+
}
|
|
110
|
+
catch (_a) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Formats a number as a PDF real/integer operand.
|
|
116
|
+
*
|
|
117
|
+
* `String(n)` gives the shortest round-trip form; exponent notation (invalid
|
|
118
|
+
* inside content streams) is expanded to plain decimal.
|
|
119
|
+
*/
|
|
120
|
+
function pdfNum(n) {
|
|
121
|
+
if (Number.isInteger(n) && Math.abs(n) < 1e15) {
|
|
122
|
+
return String(n);
|
|
123
|
+
}
|
|
124
|
+
if (!Number.isFinite(n)) {
|
|
125
|
+
return '0';
|
|
126
|
+
}
|
|
127
|
+
var s = String(n);
|
|
128
|
+
if (s.indexOf('e') < 0 && s.indexOf('E') < 0) {
|
|
129
|
+
return s;
|
|
130
|
+
}
|
|
131
|
+
var t = n.toFixed(20);
|
|
132
|
+
if (t.indexOf('e') >= 0 || t.indexOf('E') >= 0) {
|
|
133
|
+
return '0';
|
|
134
|
+
}
|
|
135
|
+
if (t.indexOf('.') >= 0) {
|
|
136
|
+
t = t.replace(/0+$/, '').replace(/\.$/, '');
|
|
137
|
+
}
|
|
138
|
+
return t === '' || t === '-' ? '0' : t;
|
|
139
|
+
}
|
|
140
|
+
function pdfLiteral(text) {
|
|
141
|
+
return ('(' +
|
|
142
|
+
text
|
|
143
|
+
.replace(/\\/g, '\\\\')
|
|
144
|
+
.replace(/\(/g, '\\(')
|
|
145
|
+
.replace(/\)/g, '\\)')
|
|
146
|
+
.replace(/\r/g, '\\r')
|
|
147
|
+
.replace(/\n/g, '\\n') +
|
|
148
|
+
')');
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Formats a glyph-space coordinate. Float32 buffers carry binary artifacts
|
|
152
|
+
* (`0.1` reads back as `0.10000000149011612`), so values are rounded to 4
|
|
153
|
+
* decimals — far below visual tolerance for text geometry — keeping both the
|
|
154
|
+
* content streams and the JS string churn small.
|
|
155
|
+
*/
|
|
156
|
+
function pdfCompact(n) {
|
|
157
|
+
var r = Math.round(n * 1e4) / 1e4;
|
|
158
|
+
return Number.isFinite(r) ? String(r) : '0';
|
|
159
|
+
}
|
|
160
|
+
/** Bounding box of a flat `[x, y, ...]` coordinate buffer. */
|
|
161
|
+
function flatBounds(data) {
|
|
162
|
+
var minX = Infinity;
|
|
163
|
+
var minY = Infinity;
|
|
164
|
+
var maxX = -Infinity;
|
|
165
|
+
var maxY = -Infinity;
|
|
166
|
+
for (var i = 0; i + 1 < data.length; i += 2) {
|
|
167
|
+
var x = data[i];
|
|
168
|
+
var y = data[i + 1];
|
|
169
|
+
if (x < minX) {
|
|
170
|
+
minX = x;
|
|
171
|
+
}
|
|
172
|
+
if (y < minY) {
|
|
173
|
+
minY = y;
|
|
174
|
+
}
|
|
175
|
+
if (x > maxX) {
|
|
176
|
+
maxX = x;
|
|
177
|
+
}
|
|
178
|
+
if (y > maxY) {
|
|
179
|
+
maxY = y;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return { minX: minX, minY: minY, maxX: maxX, maxY: maxY };
|
|
183
|
+
}
|
|
184
|
+
/** Mean of the X/Y affine scale factors of `matrix`. */
|
|
185
|
+
function affineScale2d(matrix) {
|
|
186
|
+
var el = matrix.elements;
|
|
187
|
+
var sx = Math.hypot(el[0], el[1]);
|
|
188
|
+
var sy = Math.hypot(el[4], el[5]);
|
|
189
|
+
var scale = (sx + sy) / 2;
|
|
190
|
+
return Number.isFinite(scale) && scale > 0 ? scale : 1;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Serializes CAD drawables into raw PDF content-stream text.
|
|
194
|
+
*
|
|
195
|
+
* pdf-lib's `pushOperators` path retains one JS object graph per operand
|
|
196
|
+
* (PDFNumber wrappers, arg arrays, operator objects), which multiplies large
|
|
197
|
+
* drawings' content by ~20-30x in heap terms — enough to exhaust the tab on
|
|
198
|
+
* drawings with millions of points. This writer emits the exact same
|
|
199
|
+
* operators as compact text instead, and {@link flushToPage} registers the
|
|
200
|
+
* bytes as one `PDFRawStream` appended to the page's `/Contents` array.
|
|
201
|
+
*/
|
|
202
|
+
var AcPdfContentWriter = /** @class */ (function () {
|
|
203
|
+
function AcPdfContentWriter(page, doc, options) {
|
|
204
|
+
if (options === void 0) { options = {}; }
|
|
205
|
+
var _a, _b, _c;
|
|
206
|
+
this._opacityStates = new Map();
|
|
207
|
+
this._imageNames = new Map();
|
|
208
|
+
this._chunks = [];
|
|
209
|
+
this._segments = [];
|
|
210
|
+
this._segmentLength = 0;
|
|
211
|
+
this._shadingCount = 0;
|
|
212
|
+
this._page = page;
|
|
213
|
+
this._doc = doc;
|
|
214
|
+
this._minUserLineWidth = Math.max((_a = options.minUserLineWidth) !== null && _a !== void 0 ? _a : 0, 0);
|
|
215
|
+
this._images = (_b = options.imageCache) !== null && _b !== void 0 ? _b : new Map();
|
|
216
|
+
this._forms = (_c = options.formRegistry) !== null && _c !== void 0 ? _c : createPdfFormRegistry();
|
|
217
|
+
this._fonts = options.fonts;
|
|
218
|
+
}
|
|
219
|
+
AcPdfContentWriter.prototype.createNestedWriter = function () {
|
|
220
|
+
return new AcPdfContentWriter(null, this._doc, {
|
|
221
|
+
minUserLineWidth: this._minUserLineWidth,
|
|
222
|
+
imageCache: this._images,
|
|
223
|
+
formRegistry: this._forms,
|
|
224
|
+
fonts: this._fonts
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Paints `paint` into a cached Form XObject and returns its resource name.
|
|
229
|
+
*/
|
|
230
|
+
AcPdfContentWriter.prototype.ensureForm = function (key, bbox, paint) {
|
|
231
|
+
var existing = this.hasForm(key);
|
|
232
|
+
if (existing) {
|
|
233
|
+
return existing;
|
|
234
|
+
}
|
|
235
|
+
var formWriter = this.createNestedWriter();
|
|
236
|
+
paint(formWriter);
|
|
237
|
+
var bytes = formWriter.buildBytes();
|
|
238
|
+
if (bytes.length === 0) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
var ref = this.createFormXObject(bytes, bbox);
|
|
242
|
+
return this.registerForm(key, ref);
|
|
243
|
+
};
|
|
244
|
+
AcPdfContentWriter.prototype.flushToPage = function (page) {
|
|
245
|
+
var bytes = this.buildBytes();
|
|
246
|
+
if (bytes.length === 0) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
var ref = this.registerRawStream(bytes);
|
|
250
|
+
// normalize() turns a direct Contents ref into an array and (on first
|
|
251
|
+
// call) wraps prior streams with q/Q. Appending keeps paint order: our
|
|
252
|
+
// stream is self-contained (opens with `q`, installs its own `cm`).
|
|
253
|
+
page.node.normalize();
|
|
254
|
+
var contents = page.node.Contents();
|
|
255
|
+
if (contents instanceof PDFArray) {
|
|
256
|
+
contents.push(ref);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
page.node.set(PDFName.of('Contents'), ref);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* Registers raw stream bytes, Flate-compressing large payloads
|
|
264
|
+
* (`/Filter /FlateDecode`) — content streams of big drawings compress
|
|
265
|
+
* ~10x, which is most of the exported file's size.
|
|
266
|
+
*/
|
|
267
|
+
AcPdfContentWriter.prototype.registerRawStream = function (bytes) {
|
|
268
|
+
var compressed = flateStreamBytes(bytes);
|
|
269
|
+
var dict = this._doc.context.obj(compressed ? { Filter: 'FlateDecode' } : {});
|
|
270
|
+
return this._doc.context.register(PDFRawStream.of(dict, compressed !== null && compressed !== void 0 ? compressed : bytes));
|
|
271
|
+
};
|
|
272
|
+
AcPdfContentWriter.prototype.compact = function () {
|
|
273
|
+
if (this._chunks.length === 0) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
var joined = this._chunks.join('');
|
|
277
|
+
this._chunks.length = 0;
|
|
278
|
+
var bytes = new Uint8Array(joined.length);
|
|
279
|
+
for (var i = 0; i < joined.length; i++) {
|
|
280
|
+
bytes[i] = joined.charCodeAt(i) & 0xff;
|
|
281
|
+
}
|
|
282
|
+
this._segments.push(bytes);
|
|
283
|
+
this._segmentLength += bytes.length;
|
|
284
|
+
};
|
|
285
|
+
AcPdfContentWriter.prototype.buildBytes = function () {
|
|
286
|
+
var e_1, _a;
|
|
287
|
+
this.compact();
|
|
288
|
+
var out = new Uint8Array(this._segmentLength);
|
|
289
|
+
var offset = 0;
|
|
290
|
+
try {
|
|
291
|
+
for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
292
|
+
var segment = _c.value;
|
|
293
|
+
out.set(segment, offset);
|
|
294
|
+
offset += segment.length;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
298
|
+
finally {
|
|
299
|
+
try {
|
|
300
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
301
|
+
}
|
|
302
|
+
finally { if (e_1) throw e_1.error; }
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
};
|
|
306
|
+
/** Appends a raw content-stream chunk (also used by the document writer). */
|
|
307
|
+
AcPdfContentWriter.prototype.push = function (chunk) {
|
|
308
|
+
this._chunks.push(chunk);
|
|
309
|
+
// Bound the string-object count on huge drawings: join periodically.
|
|
310
|
+
if (this._chunks.length >= 1024) {
|
|
311
|
+
this.compact();
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
AcPdfContentWriter.prototype.save = function () {
|
|
315
|
+
this.push('q\n');
|
|
316
|
+
};
|
|
317
|
+
AcPdfContentWriter.prototype.restore = function () {
|
|
318
|
+
this.push('Q\n');
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Installs a rectangular clip in the current user space (even-odd).
|
|
322
|
+
* Caller must {@link save} first and {@link restore} after clipped draws.
|
|
323
|
+
*/
|
|
324
|
+
AcPdfContentWriter.prototype.clipRect = function (box) {
|
|
325
|
+
this.push("".concat(pdfNum(box.min.x), " ").concat(pdfNum(box.min.y), " m ") +
|
|
326
|
+
"".concat(pdfNum(box.max.x), " ").concat(pdfNum(box.min.y), " l ") +
|
|
327
|
+
"".concat(pdfNum(box.max.x), " ").concat(pdfNum(box.max.y), " l ") +
|
|
328
|
+
"".concat(pdfNum(box.min.x), " ").concat(pdfNum(box.max.y), " l h W* n\n"));
|
|
329
|
+
};
|
|
330
|
+
AcPdfContentWriter.prototype.concatMatrix = function (matrix) {
|
|
331
|
+
var m = AcPdfMatrixUtil.toPdfMatrix(matrix);
|
|
332
|
+
this.push("".concat(pdfNum(m.a), " ").concat(pdfNum(m.b), " ").concat(pdfNum(m.c), " ").concat(pdfNum(m.d), " ") +
|
|
333
|
+
"".concat(pdfNum(m.e), " ").concat(pdfNum(m.f), " cm\n"));
|
|
334
|
+
};
|
|
335
|
+
AcPdfContentWriter.prototype.beginOcg = function (resourceName) {
|
|
336
|
+
this.push("/OC /".concat(resourceName, " BDC\n"));
|
|
337
|
+
};
|
|
338
|
+
AcPdfContentWriter.prototype.beginActualText = function (text) {
|
|
339
|
+
this.push("/Span << /ActualText ".concat(pdfHexText(text), " >> BDC\n"));
|
|
340
|
+
};
|
|
341
|
+
AcPdfContentWriter.prototype.beginEntity = function (payload) {
|
|
342
|
+
var dict = '';
|
|
343
|
+
if (payload.handle) {
|
|
344
|
+
dict += " /Handle ".concat(pdfLiteral(payload.handle));
|
|
345
|
+
}
|
|
346
|
+
if (payload.type) {
|
|
347
|
+
dict += " /Type ".concat(pdfLiteral(payload.type));
|
|
348
|
+
}
|
|
349
|
+
if (payload.name) {
|
|
350
|
+
dict += " /Name ".concat(pdfLiteral(payload.name));
|
|
351
|
+
}
|
|
352
|
+
if (payload.layer) {
|
|
353
|
+
dict += " /Layer ".concat(pdfLiteral(payload.layer));
|
|
354
|
+
}
|
|
355
|
+
this.push("/Entity <<".concat(dict, " >> BDC\n"));
|
|
356
|
+
};
|
|
357
|
+
AcPdfContentWriter.prototype.endMarked = function () {
|
|
358
|
+
this.push('EMC\n');
|
|
359
|
+
};
|
|
360
|
+
AcPdfContentWriter.prototype.invokeForm = function (resourceName) {
|
|
361
|
+
this.push("/".concat(resourceName, " Do\n"));
|
|
362
|
+
};
|
|
363
|
+
/**
|
|
364
|
+
* Registers (or reuses) a Form XObject under `key` and returns its resource
|
|
365
|
+
* name. Names are unique document-wide through the shared registry; the
|
|
366
|
+
* resource is added to this page's `/XObject` dict so cross-page reuse
|
|
367
|
+
* keeps working.
|
|
368
|
+
*/
|
|
369
|
+
AcPdfContentWriter.prototype.registerForm = function (key, ref) {
|
|
370
|
+
var entry = this._forms.entries.get(key);
|
|
371
|
+
if (entry) {
|
|
372
|
+
this.ensurePageForm(entry.name, entry.ref);
|
|
373
|
+
return entry.name;
|
|
374
|
+
}
|
|
375
|
+
var name = "Fm".concat(++this._forms.nameSeq);
|
|
376
|
+
this._forms.entries.set(key, { name: name, ref: ref });
|
|
377
|
+
this.ensurePageForm(name, ref);
|
|
378
|
+
return name;
|
|
379
|
+
};
|
|
380
|
+
AcPdfContentWriter.prototype.hasForm = function (key) {
|
|
381
|
+
var _a;
|
|
382
|
+
return (_a = this._forms.entries.get(key)) === null || _a === void 0 ? void 0 : _a.name;
|
|
383
|
+
};
|
|
384
|
+
/** Adds a form (or image) resource to the current page if one exists. */
|
|
385
|
+
AcPdfContentWriter.prototype.ensurePageForm = function (name, ref) {
|
|
386
|
+
if (this._page) {
|
|
387
|
+
this._page.node.setXObject(PDFName.of(name), ref);
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
AcPdfContentWriter.prototype.embedImage = function (op) {
|
|
391
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
392
|
+
var image, _a, _b;
|
|
393
|
+
return __generator(this, function (_c) {
|
|
394
|
+
switch (_c.label) {
|
|
395
|
+
case 0:
|
|
396
|
+
if (this._images.has(op) || op.bytes.byteLength === 0) {
|
|
397
|
+
return [2 /*return*/];
|
|
398
|
+
}
|
|
399
|
+
_c.label = 1;
|
|
400
|
+
case 1:
|
|
401
|
+
_c.trys.push([1, 6, , 7]);
|
|
402
|
+
if (!(op.format === 'jpg')) return [3 /*break*/, 3];
|
|
403
|
+
return [4 /*yield*/, this._doc.embedJpg(op.bytes)];
|
|
404
|
+
case 2:
|
|
405
|
+
_a = _c.sent();
|
|
406
|
+
return [3 /*break*/, 5];
|
|
407
|
+
case 3: return [4 /*yield*/, this._doc.embedPng(op.bytes)];
|
|
408
|
+
case 4:
|
|
409
|
+
_a = _c.sent();
|
|
410
|
+
_c.label = 5;
|
|
411
|
+
case 5:
|
|
412
|
+
image = _a;
|
|
413
|
+
this._images.set(op, image);
|
|
414
|
+
return [3 /*break*/, 7];
|
|
415
|
+
case 6:
|
|
416
|
+
_b = _c.sent();
|
|
417
|
+
return [3 /*break*/, 7];
|
|
418
|
+
case 7: return [2 /*return*/];
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
};
|
|
423
|
+
AcPdfContentWriter.prototype.drawOp = function (op) {
|
|
424
|
+
if (op.kind === 'stroke') {
|
|
425
|
+
this.strokePaths([{ points: op.points, closed: op.closed === true }], op.style);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (op.kind === 'fill') {
|
|
429
|
+
this.fillLoops(op.loops, op.style);
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (op.kind === 'text') {
|
|
433
|
+
this.paintText(op);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (op.kind === 'circle') {
|
|
437
|
+
this.fillCircle(op.x, op.y, op.r, op.style);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (op.kind === 'gradient') {
|
|
441
|
+
this.fillGradient(op);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (op.kind === 'triangles') {
|
|
445
|
+
this.paintTriangles(op);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (op.kind === 'polylines') {
|
|
449
|
+
this.paintFlatPolylines(op);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
this.drawEmbeddedImage(op);
|
|
453
|
+
};
|
|
454
|
+
/**
|
|
455
|
+
* Paints a run of ops from one entity, coalescing adjacent draws that share
|
|
456
|
+
* the same style.
|
|
457
|
+
*
|
|
458
|
+
* Dense geometry (polyline meshes, patterned hatches, complex linetypes)
|
|
459
|
+
* produces hundreds of stroke ops whose styles are identical. Independent
|
|
460
|
+
* subpaths in a single stroked path render identically (caps apply per
|
|
461
|
+
* subpath, no joins across moves). Consecutive opaque fills likewise
|
|
462
|
+
* combine under even-odd.
|
|
463
|
+
*
|
|
464
|
+
* Compact glyph ops (`triangles`/`polylines`) are matched on the raw op
|
|
465
|
+
* before `map` so their shared float32 buffers are never copied per
|
|
466
|
+
* instance: triangles go through one Form XObject per unique glyph set
|
|
467
|
+
* (color/opacity set outside, `cm` per instance), polylines serialize
|
|
468
|
+
* inline with the matrix baked numerically.
|
|
469
|
+
*/
|
|
470
|
+
AcPdfContentWriter.prototype.drawOps = function (ops, map, matrix) {
|
|
471
|
+
var resolve = map !== null && map !== void 0 ? map : (function (op) { return op; });
|
|
472
|
+
for (var i = 0; i < ops.length;) {
|
|
473
|
+
var raw = ops[i];
|
|
474
|
+
if (raw.kind === 'triangles') {
|
|
475
|
+
this.paintTriangles(raw, matrix);
|
|
476
|
+
i++;
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (raw.kind === 'polylines') {
|
|
480
|
+
this.paintFlatPolylines(raw, matrix);
|
|
481
|
+
i++;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
var op = resolve(raw);
|
|
485
|
+
if (op.kind === 'text') {
|
|
486
|
+
this.paintText(op);
|
|
487
|
+
i++;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (op.kind === 'stroke') {
|
|
491
|
+
var paths = [
|
|
492
|
+
{ points: op.points, closed: op.closed === true }
|
|
493
|
+
];
|
|
494
|
+
var j = i + 1;
|
|
495
|
+
while (j < ops.length) {
|
|
496
|
+
var rawNext = ops[j];
|
|
497
|
+
if (rawNext.kind === 'triangles' ||
|
|
498
|
+
rawNext.kind === 'polylines' ||
|
|
499
|
+
rawNext.kind === 'image' ||
|
|
500
|
+
rawNext.kind === 'gradient' ||
|
|
501
|
+
rawNext.kind === 'circle' ||
|
|
502
|
+
rawNext.kind === 'text') {
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
var next = resolve(rawNext);
|
|
506
|
+
if (next.kind !== 'stroke' || !sameStrokeStyle(op.style, next.style)) {
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
paths.push({ points: next.points, closed: next.closed === true });
|
|
510
|
+
j++;
|
|
511
|
+
}
|
|
512
|
+
this.strokePaths(paths, op.style);
|
|
513
|
+
i = j;
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
if (op.kind === 'fill' && op.style.opacity >= 0.999) {
|
|
517
|
+
var loops = __spreadArray([], __read(op.loops), false);
|
|
518
|
+
var j = i + 1;
|
|
519
|
+
while (j < ops.length) {
|
|
520
|
+
var rawNext = ops[j];
|
|
521
|
+
if (rawNext.kind === 'triangles' ||
|
|
522
|
+
rawNext.kind === 'polylines' ||
|
|
523
|
+
rawNext.kind === 'image' ||
|
|
524
|
+
rawNext.kind === 'gradient' ||
|
|
525
|
+
rawNext.kind === 'circle' ||
|
|
526
|
+
rawNext.kind === 'text') {
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
var next = resolve(rawNext);
|
|
530
|
+
if (next.kind !== 'fill' ||
|
|
531
|
+
next.style.opacity < 0.999 ||
|
|
532
|
+
!sameFillStyle(op.style, next.style)) {
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
loops.push.apply(loops, __spreadArray([], __read(next.loops), false));
|
|
536
|
+
j++;
|
|
537
|
+
}
|
|
538
|
+
this.fillLoops(loops, op.style);
|
|
539
|
+
i = j;
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
this.drawOp(op);
|
|
543
|
+
i++;
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
/**
|
|
547
|
+
* Paints one laid-out line of real PDF text (`BT … ET`).
|
|
548
|
+
*
|
|
549
|
+
* The op's glyph string was encoded through the embedded subset font by
|
|
550
|
+
* {@link AcPdfFontManager.embedOp} before painting; ops whose font failed
|
|
551
|
+
* to embed (empty `hex`/`glyphHex`) are skipped. Rotation, the CAD width
|
|
552
|
+
* factor and the `\Q` oblique shear collapse into the text matrix:
|
|
553
|
+
* glyph advances scale by `size * hScale` along the baseline and `size`
|
|
554
|
+
* perpendicular to it, sheared by `tan(oblique)` after scaling. Tracking
|
|
555
|
+
* runs paint as one TJ array with per-glyph displacement numbers.
|
|
556
|
+
*/
|
|
557
|
+
AcPdfContentWriter.prototype.paintText = function (op) {
|
|
558
|
+
var _a, _b;
|
|
559
|
+
var glyphHex = op.glyphHex;
|
|
560
|
+
var tracked = glyphHex !== undefined && glyphHex.length > 0;
|
|
561
|
+
if (!op.hex && !tracked) {
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
var resource = (_a = this._fonts) === null || _a === void 0 ? void 0 : _a.resourceFor(op.font);
|
|
565
|
+
if (!resource) {
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (this._page) {
|
|
569
|
+
setPageNamedResource(this._page, 'Font', resource.name, resource.ref);
|
|
570
|
+
}
|
|
571
|
+
var rad = (op.angleDeg * Math.PI) / 180;
|
|
572
|
+
var cos = Math.cos(rad);
|
|
573
|
+
var sin = Math.sin(rad);
|
|
574
|
+
// `flipX` mirrors glyphs about their local vertical axis: the text
|
|
575
|
+
// matrix x-axis is negated (R(θ)·S(-size·hScale, size)). `\Q` oblique
|
|
576
|
+
// shears glyph space after scaling: Tm = R(θ)·Sh(tan)·S, so the c/d
|
|
577
|
+
// columns gain a `tan·cos` / `tan·sin` term.
|
|
578
|
+
var flip = op.flipX === true ? -1 : 1;
|
|
579
|
+
var shear = op.obliqueDeg
|
|
580
|
+
? Math.tan((op.obliqueDeg * Math.PI) / 180)
|
|
581
|
+
: 0;
|
|
582
|
+
var chunk = 'q\n';
|
|
583
|
+
chunk += this.applyOpacity(op.style.opacity);
|
|
584
|
+
chunk += "".concat(pdfNum(op.style.rgb.r), " ").concat(pdfNum(op.style.rgb.g), " ").concat(pdfNum(op.style.rgb.b), " rg\n");
|
|
585
|
+
chunk += 'BT\n';
|
|
586
|
+
chunk += "/".concat(resource.name, " 1 Tf\n");
|
|
587
|
+
chunk +=
|
|
588
|
+
"".concat(pdfNum(op.size * op.hScale * cos * flip), " ").concat(pdfNum(op.size * op.hScale * sin * flip), " ") +
|
|
589
|
+
"".concat(pdfNum(op.size * (shear * cos - sin)), " ").concat(pdfNum(op.size * (shear * sin + cos)), " ") +
|
|
590
|
+
"".concat(pdfNum(op.x), " ").concat(pdfNum(op.y), " Tm\n");
|
|
591
|
+
if (tracked) {
|
|
592
|
+
// Per-glyph tracking: each displacement number adds the advance that
|
|
593
|
+
// tracking owes (see embedOp); zero adjustments stay out of the array.
|
|
594
|
+
var adjust_1 = (_b = op.charAdjust) !== null && _b !== void 0 ? _b : [];
|
|
595
|
+
var parts_1 = [];
|
|
596
|
+
glyphHex.forEach(function (hex, index) {
|
|
597
|
+
var _a;
|
|
598
|
+
if (index > 0) {
|
|
599
|
+
var t = (_a = adjust_1[index - 1]) !== null && _a !== void 0 ? _a : 0;
|
|
600
|
+
if (t !== 0) {
|
|
601
|
+
parts_1.push(pdfNum(t));
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
parts_1.push("<".concat(hex, ">"));
|
|
605
|
+
});
|
|
606
|
+
chunk += "[".concat(parts_1.join(' '), "] TJ\nET\nQ\n");
|
|
607
|
+
this.push(chunk);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
chunk += "<".concat(op.hex, "> Tj\nET\nQ\n");
|
|
611
|
+
this.push(chunk);
|
|
612
|
+
};
|
|
613
|
+
/**
|
|
614
|
+
* Paints a flat triangle glyph set.
|
|
615
|
+
*
|
|
616
|
+
* The unique glyph geometry is emitted once into a Form XObject keyed by
|
|
617
|
+
* buffer identity; every instance then costs one `q cm /FmN Do Q`. Fill
|
|
618
|
+
* color and opacity are installed in the graphics state before `Do` — PDF
|
|
619
|
+
* forms execute inside the current graphics state, so one form serves all
|
|
620
|
+
* layers/colors.
|
|
621
|
+
*/
|
|
622
|
+
AcPdfContentWriter.prototype.paintTriangles = function (op, matrix) {
|
|
623
|
+
var data = op.data;
|
|
624
|
+
if (data.length < 6) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
if (!this._page) {
|
|
628
|
+
// Nested writer (form content): serialize inline, no color operators.
|
|
629
|
+
this.push(this.bakedTrianglesChunk(data, matrix) + 'f\n');
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
var name = this.ensureTrianglesForm(data);
|
|
633
|
+
if (!name) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
var chunk = 'q\n';
|
|
637
|
+
chunk += this.applyOpacity(op.style.opacity);
|
|
638
|
+
chunk += "".concat(pdfNum(op.style.rgb.r), " ").concat(pdfNum(op.style.rgb.g), " ").concat(pdfNum(op.style.rgb.b), " rg\n");
|
|
639
|
+
if (matrix) {
|
|
640
|
+
var m = AcPdfMatrixUtil.toPdfMatrix(matrix);
|
|
641
|
+
chunk += "".concat(pdfNum(m.a), " ").concat(pdfNum(m.b), " ").concat(pdfNum(m.c), " ").concat(pdfNum(m.d), " ").concat(pdfNum(m.e), " ").concat(pdfNum(m.f), " cm\n");
|
|
642
|
+
}
|
|
643
|
+
chunk += "/".concat(name, " Do\nQ\n");
|
|
644
|
+
this.push(chunk);
|
|
645
|
+
};
|
|
646
|
+
/** Creates (once) and names the Form XObject for a triangle buffer. */
|
|
647
|
+
AcPdfContentWriter.prototype.ensureTrianglesForm = function (data) {
|
|
648
|
+
var key = this.compactFormKey(data);
|
|
649
|
+
var existing = this.hasForm(key);
|
|
650
|
+
if (existing) {
|
|
651
|
+
return existing;
|
|
652
|
+
}
|
|
653
|
+
var bbox = flatBounds(data);
|
|
654
|
+
if (!Number.isFinite(bbox.minX) ||
|
|
655
|
+
!Number.isFinite(bbox.minY) ||
|
|
656
|
+
!Number.isFinite(bbox.maxX) ||
|
|
657
|
+
!Number.isFinite(bbox.maxY)) {
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
var bytes = this.trianglesFormBytes(data);
|
|
661
|
+
if (bytes.length === 0) {
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
var ref = this.createFormXObject(bytes, bbox);
|
|
665
|
+
return this.registerForm(key, ref);
|
|
666
|
+
};
|
|
667
|
+
/** Serializes triangle geometry (paths + single fill) into PDF bytes. */
|
|
668
|
+
AcPdfContentWriter.prototype.trianglesFormBytes = function (data) {
|
|
669
|
+
var writer = this.createNestedWriter();
|
|
670
|
+
writer.push(this.bakedTrianglesChunk(data, undefined) + 'f\n');
|
|
671
|
+
return writer.buildBytes();
|
|
672
|
+
};
|
|
673
|
+
/** Closed-triangle subpaths for a flat triangle buffer. */
|
|
674
|
+
AcPdfContentWriter.prototype.bakedTrianglesChunk = function (data, matrix) {
|
|
675
|
+
var el = matrix === null || matrix === void 0 ? void 0 : matrix.elements;
|
|
676
|
+
var chunk = '';
|
|
677
|
+
for (var i = 0; i + 5 < data.length; i += 6) {
|
|
678
|
+
chunk += this.flatPointChunk(data, i, el) + ' m ';
|
|
679
|
+
chunk += this.flatPointChunk(data, i + 2, el) + ' l ';
|
|
680
|
+
chunk += this.flatPointChunk(data, i + 4, el) + ' l h\n';
|
|
681
|
+
}
|
|
682
|
+
return chunk;
|
|
683
|
+
};
|
|
684
|
+
AcPdfContentWriter.prototype.flatPointChunk = function (data, i, el) {
|
|
685
|
+
var x = data[i];
|
|
686
|
+
var y = data[i + 1];
|
|
687
|
+
if (el) {
|
|
688
|
+
var tx = el[0] * x + el[4] * y + el[12];
|
|
689
|
+
var ty = el[1] * x + el[5] * y + el[13];
|
|
690
|
+
x = tx;
|
|
691
|
+
y = ty;
|
|
692
|
+
}
|
|
693
|
+
return "".concat(pdfCompact(x), " ").concat(pdfCompact(y));
|
|
694
|
+
};
|
|
695
|
+
/**
|
|
696
|
+
* Paints a flat polyline glyph set inline (strokes carry per-instance line
|
|
697
|
+
* widths, so they are not form-shared), baking the matrix numerically.
|
|
698
|
+
*/
|
|
699
|
+
AcPdfContentWriter.prototype.paintFlatPolylines = function (op, matrix) {
|
|
700
|
+
var data = op.data;
|
|
701
|
+
if (data.length < 5) {
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
var el = matrix === null || matrix === void 0 ? void 0 : matrix.elements;
|
|
705
|
+
var scale = matrix ? affineScale2d(matrix) : 1;
|
|
706
|
+
var lineWidth = Math.max(op.style.lineWidth * scale, this._minUserLineWidth);
|
|
707
|
+
var chunk = 'q\n';
|
|
708
|
+
chunk += this.applyOpacity(op.style.opacity);
|
|
709
|
+
chunk += "".concat(pdfNum(op.style.rgb.r), " ").concat(pdfNum(op.style.rgb.g), " ").concat(pdfNum(op.style.rgb.b), " RG ").concat(pdfNum(lineWidth), " w 1 J 1 j\n");
|
|
710
|
+
if (op.style.dashArray && op.style.dashArray.length > 0) {
|
|
711
|
+
chunk += "[".concat(op.style.dashArray.map(function (d) { return pdfNum(d * scale); }).join(' '), "] 0 d\n");
|
|
712
|
+
}
|
|
713
|
+
var i = 0;
|
|
714
|
+
while (i < data.length) {
|
|
715
|
+
var n = data[i];
|
|
716
|
+
if (!(n >= 2) || i + 1 + n * 2 > data.length) {
|
|
717
|
+
break;
|
|
718
|
+
}
|
|
719
|
+
i++;
|
|
720
|
+
for (var k = 0; k < n; k++) {
|
|
721
|
+
var x = data[i];
|
|
722
|
+
var y = data[i + 1];
|
|
723
|
+
i += 2;
|
|
724
|
+
if (el) {
|
|
725
|
+
var tx = el[0] * x + el[4] * y + el[12];
|
|
726
|
+
var ty = el[1] * x + el[5] * y + el[13];
|
|
727
|
+
x = tx;
|
|
728
|
+
y = ty;
|
|
729
|
+
}
|
|
730
|
+
chunk += "".concat(pdfCompact(x), " ").concat(pdfCompact(y), " ").concat(k === 0 ? 'm' : 'l', "\n");
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
chunk += 'S\nQ\n';
|
|
734
|
+
this.push(chunk);
|
|
735
|
+
};
|
|
736
|
+
/** Stable string key for a shared flat buffer (Form XObject cache key). */
|
|
737
|
+
AcPdfContentWriter.prototype.compactFormKey = function (data) {
|
|
738
|
+
var id = this._forms.bufferIds.get(data);
|
|
739
|
+
if (!id) {
|
|
740
|
+
id = "tri".concat(++this._forms.bufferSeq);
|
|
741
|
+
this._forms.bufferIds.set(data, id);
|
|
742
|
+
}
|
|
743
|
+
return id;
|
|
744
|
+
};
|
|
745
|
+
AcPdfContentWriter.prototype.createFormXObject = function (bytes, bbox, resources) {
|
|
746
|
+
var compressed = flateStreamBytes(bytes);
|
|
747
|
+
var dict = this._doc.context.obj(__assign(__assign({ Type: 'XObject', Subtype: 'Form', BBox: [bbox.minX, bbox.minY, bbox.maxX, bbox.maxY], Matrix: [1, 0, 0, 1, 0, 0] }, (compressed ? { Filter: 'FlateDecode' } : {})), (resources ? { Resources: resources } : {})));
|
|
748
|
+
return this._doc.context.register(PDFRawStream.of(dict, compressed !== null && compressed !== void 0 ? compressed : bytes));
|
|
749
|
+
};
|
|
750
|
+
AcPdfContentWriter.prototype.drawEmbeddedImage = function (op) {
|
|
751
|
+
var image = this._images.get(op);
|
|
752
|
+
if (!image || !(op.width > 0) || !(op.height > 0)) {
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
var name = this.ensureImageName(image);
|
|
756
|
+
this.push("q ".concat(pdfNum(op.width), " 0 0 ").concat(pdfNum(op.height), " ") +
|
|
757
|
+
"".concat(pdfNum(op.x), " ").concat(pdfNum(op.y), " cm /").concat(name, " Do Q\n"));
|
|
758
|
+
};
|
|
759
|
+
AcPdfContentWriter.prototype.ensureImageName = function (image) {
|
|
760
|
+
var existing = this._imageNames.get(image);
|
|
761
|
+
if (existing) {
|
|
762
|
+
return existing;
|
|
763
|
+
}
|
|
764
|
+
var name = "Im".concat(this._imageNames.size + 1);
|
|
765
|
+
this._imageNames.set(image, name);
|
|
766
|
+
if (this._page) {
|
|
767
|
+
this._page.node.setXObject(PDFName.of(name), image.ref);
|
|
768
|
+
}
|
|
769
|
+
return name;
|
|
770
|
+
};
|
|
771
|
+
AcPdfContentWriter.prototype.applyOpacity = function (opacity) {
|
|
772
|
+
if (opacity >= 0.999) {
|
|
773
|
+
return '';
|
|
774
|
+
}
|
|
775
|
+
var key = this.ensureOpacityState(opacity);
|
|
776
|
+
return "/".concat(key, " gs\n");
|
|
777
|
+
};
|
|
778
|
+
AcPdfContentWriter.prototype.ensureOpacityState = function (opacity) {
|
|
779
|
+
var rounded = Math.round(opacity * 1000) / 1000;
|
|
780
|
+
var existing = this._opacityStates.get(rounded);
|
|
781
|
+
if (existing) {
|
|
782
|
+
return existing;
|
|
783
|
+
}
|
|
784
|
+
var name = "GS".concat(this._opacityStates.size + 1);
|
|
785
|
+
var dict = this._doc.context.obj({
|
|
786
|
+
Type: 'ExtGState',
|
|
787
|
+
CA: rounded,
|
|
788
|
+
ca: rounded
|
|
789
|
+
});
|
|
790
|
+
if (this._page) {
|
|
791
|
+
this._page.node.setExtGState(PDFName.of(name), dict);
|
|
792
|
+
}
|
|
793
|
+
this._opacityStates.set(rounded, name);
|
|
794
|
+
return name;
|
|
795
|
+
};
|
|
796
|
+
AcPdfContentWriter.prototype.strokePaths = function (paths, style) {
|
|
797
|
+
var e_2, _a;
|
|
798
|
+
var drawable = paths.filter(function (path) { return path.points.length >= 2; });
|
|
799
|
+
if (drawable.length === 0) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
var chunk = 'q\n';
|
|
803
|
+
chunk += this.applyOpacity(style.opacity);
|
|
804
|
+
var lineWidth = Math.max(style.lineWidth, this._minUserLineWidth);
|
|
805
|
+
chunk +=
|
|
806
|
+
"".concat(pdfNum(style.rgb.r), " ").concat(pdfNum(style.rgb.g), " ").concat(pdfNum(style.rgb.b), " RG ") +
|
|
807
|
+
"".concat(pdfNum(lineWidth), " w 1 J 1 j\n");
|
|
808
|
+
if (style.dashArray && style.dashArray.length > 0) {
|
|
809
|
+
chunk += "[".concat(style.dashArray.map(pdfNum).join(' '), "] 0 d\n");
|
|
810
|
+
}
|
|
811
|
+
try {
|
|
812
|
+
for (var drawable_1 = __values(drawable), drawable_1_1 = drawable_1.next(); !drawable_1_1.done; drawable_1_1 = drawable_1.next()) {
|
|
813
|
+
var path = drawable_1_1.value;
|
|
814
|
+
chunk += this.polylineChunk(path.points, path.closed);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
818
|
+
finally {
|
|
819
|
+
try {
|
|
820
|
+
if (drawable_1_1 && !drawable_1_1.done && (_a = drawable_1.return)) _a.call(drawable_1);
|
|
821
|
+
}
|
|
822
|
+
finally { if (e_2) throw e_2.error; }
|
|
823
|
+
}
|
|
824
|
+
chunk += 'S\nQ\n';
|
|
825
|
+
this.push(chunk);
|
|
826
|
+
};
|
|
827
|
+
AcPdfContentWriter.prototype.fillLoops = function (loops, style) {
|
|
828
|
+
var e_3, _a;
|
|
829
|
+
if (loops.length === 0) {
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
var chunk = 'q\n';
|
|
833
|
+
chunk += this.applyOpacity(style.opacity);
|
|
834
|
+
chunk += "".concat(pdfNum(style.rgb.r), " ").concat(pdfNum(style.rgb.g), " ").concat(pdfNum(style.rgb.b), " rg\n");
|
|
835
|
+
try {
|
|
836
|
+
for (var loops_1 = __values(loops), loops_1_1 = loops_1.next(); !loops_1_1.done; loops_1_1 = loops_1.next()) {
|
|
837
|
+
var loop = loops_1_1.value;
|
|
838
|
+
if (loop.length < 3) {
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
chunk += this.polylineChunk(loop, true);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
845
|
+
finally {
|
|
846
|
+
try {
|
|
847
|
+
if (loops_1_1 && !loops_1_1.done && (_a = loops_1.return)) _a.call(loops_1);
|
|
848
|
+
}
|
|
849
|
+
finally { if (e_3) throw e_3.error; }
|
|
850
|
+
}
|
|
851
|
+
chunk += 'f*\nQ\n';
|
|
852
|
+
this.push(chunk);
|
|
853
|
+
};
|
|
854
|
+
AcPdfContentWriter.prototype.fillGradient = function (op) {
|
|
855
|
+
var e_4, _a, e_5, _b;
|
|
856
|
+
if (op.loops.length === 0 || !this._page) {
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
var chunk = 'q\n';
|
|
860
|
+
chunk += this.applyOpacity(op.style.opacity);
|
|
861
|
+
try {
|
|
862
|
+
for (var _c = __values(op.loops), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
863
|
+
var loop = _d.value;
|
|
864
|
+
if (loop.length < 3) {
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
chunk += this.polylineChunk(loop, true);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
871
|
+
finally {
|
|
872
|
+
try {
|
|
873
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
874
|
+
}
|
|
875
|
+
finally { if (e_4) throw e_4.error; }
|
|
876
|
+
}
|
|
877
|
+
chunk += 'W* n\n';
|
|
878
|
+
if (op.strips && op.strips.length > 0) {
|
|
879
|
+
try {
|
|
880
|
+
for (var _e = __values(op.strips), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
881
|
+
var strip = _f.value;
|
|
882
|
+
if (strip.points.length < 3) {
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
chunk +=
|
|
886
|
+
"".concat(pdfNum(strip.rgb.r), " ").concat(pdfNum(strip.rgb.g), " ").concat(pdfNum(strip.rgb.b), " rg\n");
|
|
887
|
+
chunk += this.polylineChunk(strip.points, true);
|
|
888
|
+
chunk += 'f*\n';
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
892
|
+
finally {
|
|
893
|
+
try {
|
|
894
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
895
|
+
}
|
|
896
|
+
finally { if (e_5) throw e_5.error; }
|
|
897
|
+
}
|
|
898
|
+
this.push(chunk + 'Q\n');
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
var name = "Sh".concat(++this._shadingCount);
|
|
902
|
+
var functionDict = this._doc.context.obj({
|
|
903
|
+
FunctionType: 2,
|
|
904
|
+
Domain: [0, 1],
|
|
905
|
+
C0: [op.c0.r, op.c0.g, op.c0.b],
|
|
906
|
+
C1: [op.c1.r, op.c1.g, op.c1.b],
|
|
907
|
+
N: 1
|
|
908
|
+
});
|
|
909
|
+
var shading = this._doc.context.obj({
|
|
910
|
+
ShadingType: op.shadingType,
|
|
911
|
+
ColorSpace: 'DeviceRGB',
|
|
912
|
+
Coords: op.coords,
|
|
913
|
+
Function: functionDict,
|
|
914
|
+
Extend: [true, true]
|
|
915
|
+
});
|
|
916
|
+
var shadingRef = this._doc.context.register(shading);
|
|
917
|
+
setPageNamedResource(this._page, 'Shading', name, shadingRef);
|
|
918
|
+
chunk += "/".concat(name, " sh\nQ\n");
|
|
919
|
+
this.push(chunk);
|
|
920
|
+
};
|
|
921
|
+
AcPdfContentWriter.prototype.fillCircle = function (x, y, r, style) {
|
|
922
|
+
if (!(r > 0)) {
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
var ox = r * KAPPA;
|
|
926
|
+
var chunk = 'q\n';
|
|
927
|
+
chunk += this.applyOpacity(style.opacity);
|
|
928
|
+
chunk += "".concat(pdfNum(style.rgb.r), " ").concat(pdfNum(style.rgb.g), " ").concat(pdfNum(style.rgb.b), " rg\n");
|
|
929
|
+
chunk += "".concat(pdfNum(x + r), " ").concat(pdfNum(y), " m ");
|
|
930
|
+
chunk += "".concat(pdfNum(x + r), " ").concat(pdfNum(y + ox), " ").concat(pdfNum(x + ox), " ").concat(pdfNum(y + r), " ").concat(pdfNum(x), " ").concat(pdfNum(y + r), " c ");
|
|
931
|
+
chunk += "".concat(pdfNum(x - ox), " ").concat(pdfNum(y + r), " ").concat(pdfNum(x - r), " ").concat(pdfNum(y + ox), " ").concat(pdfNum(x - r), " ").concat(pdfNum(y), " c ");
|
|
932
|
+
chunk += "".concat(pdfNum(x - r), " ").concat(pdfNum(y - ox), " ").concat(pdfNum(x - ox), " ").concat(pdfNum(y - r), " ").concat(pdfNum(x), " ").concat(pdfNum(y - r), " c ");
|
|
933
|
+
chunk += "".concat(pdfNum(x + ox), " ").concat(pdfNum(y - r), " ").concat(pdfNum(x + r), " ").concat(pdfNum(y - ox), " ").concat(pdfNum(x + r), " ").concat(pdfNum(y), " c ");
|
|
934
|
+
chunk += 'h f*\nQ\n';
|
|
935
|
+
this.push(chunk);
|
|
936
|
+
};
|
|
937
|
+
AcPdfContentWriter.prototype.polylineChunk = function (points, closed) {
|
|
938
|
+
var chunk = "".concat(pdfNum(points[0].x), " ").concat(pdfNum(points[0].y), " m");
|
|
939
|
+
for (var i = 1; i < points.length; i++) {
|
|
940
|
+
chunk += " ".concat(pdfNum(points[i].x), " ").concat(pdfNum(points[i].y), " l");
|
|
941
|
+
}
|
|
942
|
+
if (closed) {
|
|
943
|
+
chunk += ' h';
|
|
944
|
+
}
|
|
945
|
+
return chunk + '\n';
|
|
946
|
+
};
|
|
947
|
+
return AcPdfContentWriter;
|
|
948
|
+
}());
|
|
949
|
+
export { AcPdfContentWriter };
|
|
950
|
+
function sameRgb(a, b) {
|
|
951
|
+
return a.r === b.r && a.g === b.g && a.b === b.b;
|
|
952
|
+
}
|
|
953
|
+
function sameDashArray(a, b) {
|
|
954
|
+
if (a === b) {
|
|
955
|
+
return true;
|
|
956
|
+
}
|
|
957
|
+
if (!a || !b || a.length !== b.length) {
|
|
958
|
+
return false;
|
|
959
|
+
}
|
|
960
|
+
for (var i = 0; i < a.length; i++) {
|
|
961
|
+
if (a[i] !== b[i]) {
|
|
962
|
+
return false;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
return true;
|
|
966
|
+
}
|
|
967
|
+
function sameStrokeStyle(a, b) {
|
|
968
|
+
return (a === b ||
|
|
969
|
+
(a.opacity === b.opacity &&
|
|
970
|
+
a.lineWidth === b.lineWidth &&
|
|
971
|
+
sameRgb(a.rgb, b.rgb) &&
|
|
972
|
+
sameDashArray(a.dashArray, b.dashArray)));
|
|
973
|
+
}
|
|
974
|
+
function sameFillStyle(a, b) {
|
|
975
|
+
return a === b || (a.opacity === b.opacity && sameRgb(a.rgb, b.rgb));
|
|
976
|
+
}
|
|
977
|
+
//# sourceMappingURL=AcPdfContentWriter.js.map
|