UNPKG

27.4 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.Pattern = void 0;
28exports.getTilingPatternIR = getTilingPatternIR;
29
30var _util = require("../shared/util.js");
31
32var _base_stream = require("./base_stream.js");
33
34var _colorspace = require("./colorspace.js");
35
36var _core_utils = require("./core_utils.js");
37
38const ShadingType = {
39 FUNCTION_BASED: 1,
40 AXIAL: 2,
41 RADIAL: 3,
42 FREE_FORM_MESH: 4,
43 LATTICE_FORM_MESH: 5,
44 COONS_PATCH_MESH: 6,
45 TENSOR_PATCH_MESH: 7
46};
47
48class Pattern {
49 constructor() {
50 (0, _util.unreachable)("Cannot initialize Pattern.");
51 }
52
53 static parseShading(shading, xref, res, handler, pdfFunctionFactory, localColorSpaceCache) {
54 const dict = shading instanceof _base_stream.BaseStream ? shading.dict : shading;
55 const type = dict.get("ShadingType");
56
57 try {
58 switch (type) {
59 case ShadingType.AXIAL:
60 case ShadingType.RADIAL:
61 return new RadialAxialShading(dict, xref, res, pdfFunctionFactory, localColorSpaceCache);
62
63 case ShadingType.FREE_FORM_MESH:
64 case ShadingType.LATTICE_FORM_MESH:
65 case ShadingType.COONS_PATCH_MESH:
66 case ShadingType.TENSOR_PATCH_MESH:
67 return new MeshShading(shading, xref, res, pdfFunctionFactory, localColorSpaceCache);
68
69 default:
70 throw new _util.FormatError("Unsupported ShadingType: " + type);
71 }
72 } catch (ex) {
73 if (ex instanceof _core_utils.MissingDataException) {
74 throw ex;
75 }
76
77 handler.send("UnsupportedFeature", {
78 featureId: _util.UNSUPPORTED_FEATURES.shadingPattern
79 });
80 (0, _util.warn)(ex);
81 return new DummyShading();
82 }
83 }
84
85}
86
87exports.Pattern = Pattern;
88
89class BaseShading {
90 static get SMALL_NUMBER() {
91 return (0, _util.shadow)(this, "SMALL_NUMBER", 1e-6);
92 }
93
94 constructor() {
95 if (this.constructor === BaseShading) {
96 (0, _util.unreachable)("Cannot initialize BaseShading.");
97 }
98 }
99
100 getIR() {
101 (0, _util.unreachable)("Abstract method `getIR` called.");
102 }
103
104}
105
106class RadialAxialShading extends BaseShading {
107 constructor(dict, xref, resources, pdfFunctionFactory, localColorSpaceCache) {
108 super();
109 this.coordsArr = dict.getArray("Coords");
110 this.shadingType = dict.get("ShadingType");
111
112 const cs = _colorspace.ColorSpace.parse({
113 cs: dict.getRaw("CS") || dict.getRaw("ColorSpace"),
114 xref,
115 resources,
116 pdfFunctionFactory,
117 localColorSpaceCache
118 });
119
120 const bbox = dict.getArray("BBox");
121
122 if (Array.isArray(bbox) && bbox.length === 4) {
123 this.bbox = _util.Util.normalizeRect(bbox);
124 } else {
125 this.bbox = null;
126 }
127
128 let t0 = 0.0,
129 t1 = 1.0;
130
131 if (dict.has("Domain")) {
132 const domainArr = dict.getArray("Domain");
133 t0 = domainArr[0];
134 t1 = domainArr[1];
135 }
136
137 let extendStart = false,
138 extendEnd = false;
139
140 if (dict.has("Extend")) {
141 const extendArr = dict.getArray("Extend");
142 extendStart = extendArr[0];
143 extendEnd = extendArr[1];
144 }
145
146 if (this.shadingType === ShadingType.RADIAL && (!extendStart || !extendEnd)) {
147 const [x1, y1, r1, x2, y2, r2] = this.coordsArr;
148 const distance = Math.hypot(x1 - x2, y1 - y2);
149
150 if (r1 <= r2 + distance && r2 <= r1 + distance) {
151 (0, _util.warn)("Unsupported radial gradient.");
152 }
153 }
154
155 this.extendStart = extendStart;
156 this.extendEnd = extendEnd;
157 const fnObj = dict.getRaw("Function");
158 const fn = pdfFunctionFactory.createFromArray(fnObj);
159 const NUMBER_OF_SAMPLES = 10;
160 const step = (t1 - t0) / NUMBER_OF_SAMPLES;
161 const colorStops = this.colorStops = [];
162
163 if (t0 >= t1 || step <= 0) {
164 (0, _util.info)("Bad shading domain.");
165 return;
166 }
167
168 const color = new Float32Array(cs.numComps),
169 ratio = new Float32Array(1);
170 let rgbColor;
171
172 for (let i = 0; i <= NUMBER_OF_SAMPLES; i++) {
173 ratio[0] = t0 + i * step;
174 fn(ratio, 0, color, 0);
175 rgbColor = cs.getRgb(color, 0);
176
177 const cssColor = _util.Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
178
179 colorStops.push([i / NUMBER_OF_SAMPLES, cssColor]);
180 }
181
182 let background = "transparent";
183
184 if (dict.has("Background")) {
185 rgbColor = cs.getRgb(dict.get("Background"), 0);
186 background = _util.Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
187 }
188
189 if (!extendStart) {
190 colorStops.unshift([0, background]);
191 colorStops[1][0] += BaseShading.SMALL_NUMBER;
192 }
193
194 if (!extendEnd) {
195 colorStops.at(-1)[0] -= BaseShading.SMALL_NUMBER;
196 colorStops.push([1, background]);
197 }
198
199 this.colorStops = colorStops;
200 }
201
202 getIR() {
203 const coordsArr = this.coordsArr;
204 const shadingType = this.shadingType;
205 let type, p0, p1, r0, r1;
206
207 if (shadingType === ShadingType.AXIAL) {
208 p0 = [coordsArr[0], coordsArr[1]];
209 p1 = [coordsArr[2], coordsArr[3]];
210 r0 = null;
211 r1 = null;
212 type = "axial";
213 } else if (shadingType === ShadingType.RADIAL) {
214 p0 = [coordsArr[0], coordsArr[1]];
215 p1 = [coordsArr[3], coordsArr[4]];
216 r0 = coordsArr[2];
217 r1 = coordsArr[5];
218 type = "radial";
219 } else {
220 (0, _util.unreachable)(`getPattern type unknown: ${shadingType}`);
221 }
222
223 return ["RadialAxial", type, this.bbox, this.colorStops, p0, p1, r0, r1];
224 }
225
226}
227
228class MeshStreamReader {
229 constructor(stream, context) {
230 this.stream = stream;
231 this.context = context;
232 this.buffer = 0;
233 this.bufferLength = 0;
234 const numComps = context.numComps;
235 this.tmpCompsBuf = new Float32Array(numComps);
236 const csNumComps = context.colorSpace.numComps;
237 this.tmpCsCompsBuf = context.colorFn ? new Float32Array(csNumComps) : this.tmpCompsBuf;
238 }
239
240 get hasData() {
241 if (this.stream.end) {
242 return this.stream.pos < this.stream.end;
243 }
244
245 if (this.bufferLength > 0) {
246 return true;
247 }
248
249 const nextByte = this.stream.getByte();
250
251 if (nextByte < 0) {
252 return false;
253 }
254
255 this.buffer = nextByte;
256 this.bufferLength = 8;
257 return true;
258 }
259
260 readBits(n) {
261 let buffer = this.buffer;
262 let bufferLength = this.bufferLength;
263
264 if (n === 32) {
265 if (bufferLength === 0) {
266 return (this.stream.getByte() << 24 | this.stream.getByte() << 16 | this.stream.getByte() << 8 | this.stream.getByte()) >>> 0;
267 }
268
269 buffer = buffer << 24 | this.stream.getByte() << 16 | this.stream.getByte() << 8 | this.stream.getByte();
270 const nextByte = this.stream.getByte();
271 this.buffer = nextByte & (1 << bufferLength) - 1;
272 return (buffer << 8 - bufferLength | (nextByte & 0xff) >> bufferLength) >>> 0;
273 }
274
275 if (n === 8 && bufferLength === 0) {
276 return this.stream.getByte();
277 }
278
279 while (bufferLength < n) {
280 buffer = buffer << 8 | this.stream.getByte();
281 bufferLength += 8;
282 }
283
284 bufferLength -= n;
285 this.bufferLength = bufferLength;
286 this.buffer = buffer & (1 << bufferLength) - 1;
287 return buffer >> bufferLength;
288 }
289
290 align() {
291 this.buffer = 0;
292 this.bufferLength = 0;
293 }
294
295 readFlag() {
296 return this.readBits(this.context.bitsPerFlag);
297 }
298
299 readCoordinate() {
300 const bitsPerCoordinate = this.context.bitsPerCoordinate;
301 const xi = this.readBits(bitsPerCoordinate);
302 const yi = this.readBits(bitsPerCoordinate);
303 const decode = this.context.decode;
304 const scale = bitsPerCoordinate < 32 ? 1 / ((1 << bitsPerCoordinate) - 1) : 2.3283064365386963e-10;
305 return [xi * scale * (decode[1] - decode[0]) + decode[0], yi * scale * (decode[3] - decode[2]) + decode[2]];
306 }
307
308 readComponents() {
309 const numComps = this.context.numComps;
310 const bitsPerComponent = this.context.bitsPerComponent;
311 const scale = bitsPerComponent < 32 ? 1 / ((1 << bitsPerComponent) - 1) : 2.3283064365386963e-10;
312 const decode = this.context.decode;
313 const components = this.tmpCompsBuf;
314
315 for (let i = 0, j = 4; i < numComps; i++, j += 2) {
316 const ci = this.readBits(bitsPerComponent);
317 components[i] = ci * scale * (decode[j + 1] - decode[j]) + decode[j];
318 }
319
320 const color = this.tmpCsCompsBuf;
321
322 if (this.context.colorFn) {
323 this.context.colorFn(components, 0, color, 0);
324 }
325
326 return this.context.colorSpace.getRgb(color, 0);
327 }
328
329}
330
331const getB = function getBClosure() {
332 function buildB(count) {
333 const lut = [];
334
335 for (let i = 0; i <= count; i++) {
336 const t = i / count,
337 t_ = 1 - t;
338 lut.push(new Float32Array([t_ * t_ * t_, 3 * t * t_ * t_, 3 * t * t * t_, t * t * t]));
339 }
340
341 return lut;
342 }
343
344 const cache = [];
345 return function (count) {
346 if (!cache[count]) {
347 cache[count] = buildB(count);
348 }
349
350 return cache[count];
351 };
352}();
353
354class MeshShading extends BaseShading {
355 static get MIN_SPLIT_PATCH_CHUNKS_AMOUNT() {
356 return (0, _util.shadow)(this, "MIN_SPLIT_PATCH_CHUNKS_AMOUNT", 3);
357 }
358
359 static get MAX_SPLIT_PATCH_CHUNKS_AMOUNT() {
360 return (0, _util.shadow)(this, "MAX_SPLIT_PATCH_CHUNKS_AMOUNT", 20);
361 }
362
363 static get TRIANGLE_DENSITY() {
364 return (0, _util.shadow)(this, "TRIANGLE_DENSITY", 20);
365 }
366
367 constructor(stream, xref, resources, pdfFunctionFactory, localColorSpaceCache) {
368 super();
369
370 if (!(stream instanceof _base_stream.BaseStream)) {
371 throw new _util.FormatError("Mesh data is not a stream");
372 }
373
374 const dict = stream.dict;
375 this.shadingType = dict.get("ShadingType");
376 const bbox = dict.getArray("BBox");
377
378 if (Array.isArray(bbox) && bbox.length === 4) {
379 this.bbox = _util.Util.normalizeRect(bbox);
380 } else {
381 this.bbox = null;
382 }
383
384 const cs = _colorspace.ColorSpace.parse({
385 cs: dict.getRaw("CS") || dict.getRaw("ColorSpace"),
386 xref,
387 resources,
388 pdfFunctionFactory,
389 localColorSpaceCache
390 });
391
392 this.background = dict.has("Background") ? cs.getRgb(dict.get("Background"), 0) : null;
393 const fnObj = dict.getRaw("Function");
394 const fn = fnObj ? pdfFunctionFactory.createFromArray(fnObj) : null;
395 this.coords = [];
396 this.colors = [];
397 this.figures = [];
398 const decodeContext = {
399 bitsPerCoordinate: dict.get("BitsPerCoordinate"),
400 bitsPerComponent: dict.get("BitsPerComponent"),
401 bitsPerFlag: dict.get("BitsPerFlag"),
402 decode: dict.getArray("Decode"),
403 colorFn: fn,
404 colorSpace: cs,
405 numComps: fn ? 1 : cs.numComps
406 };
407 const reader = new MeshStreamReader(stream, decodeContext);
408 let patchMesh = false;
409
410 switch (this.shadingType) {
411 case ShadingType.FREE_FORM_MESH:
412 this._decodeType4Shading(reader);
413
414 break;
415
416 case ShadingType.LATTICE_FORM_MESH:
417 const verticesPerRow = dict.get("VerticesPerRow") | 0;
418
419 if (verticesPerRow < 2) {
420 throw new _util.FormatError("Invalid VerticesPerRow");
421 }
422
423 this._decodeType5Shading(reader, verticesPerRow);
424
425 break;
426
427 case ShadingType.COONS_PATCH_MESH:
428 this._decodeType6Shading(reader);
429
430 patchMesh = true;
431 break;
432
433 case ShadingType.TENSOR_PATCH_MESH:
434 this._decodeType7Shading(reader);
435
436 patchMesh = true;
437 break;
438
439 default:
440 (0, _util.unreachable)("Unsupported mesh type.");
441 break;
442 }
443
444 if (patchMesh) {
445 this._updateBounds();
446
447 for (let i = 0, ii = this.figures.length; i < ii; i++) {
448 this._buildFigureFromPatch(i);
449 }
450 }
451
452 this._updateBounds();
453
454 this._packData();
455 }
456
457 _decodeType4Shading(reader) {
458 const coords = this.coords;
459 const colors = this.colors;
460 const operators = [];
461 const ps = [];
462 let verticesLeft = 0;
463
464 while (reader.hasData) {
465 const f = reader.readFlag();
466 const coord = reader.readCoordinate();
467 const color = reader.readComponents();
468
469 if (verticesLeft === 0) {
470 if (!(0 <= f && f <= 2)) {
471 throw new _util.FormatError("Unknown type4 flag");
472 }
473
474 switch (f) {
475 case 0:
476 verticesLeft = 3;
477 break;
478
479 case 1:
480 ps.push(ps.at(-2), ps.at(-1));
481 verticesLeft = 1;
482 break;
483
484 case 2:
485 ps.push(ps.at(-3), ps.at(-1));
486 verticesLeft = 1;
487 break;
488 }
489
490 operators.push(f);
491 }
492
493 ps.push(coords.length);
494 coords.push(coord);
495 colors.push(color);
496 verticesLeft--;
497 reader.align();
498 }
499
500 this.figures.push({
501 type: "triangles",
502 coords: new Int32Array(ps),
503 colors: new Int32Array(ps)
504 });
505 }
506
507 _decodeType5Shading(reader, verticesPerRow) {
508 const coords = this.coords;
509 const colors = this.colors;
510 const ps = [];
511
512 while (reader.hasData) {
513 const coord = reader.readCoordinate();
514 const color = reader.readComponents();
515 ps.push(coords.length);
516 coords.push(coord);
517 colors.push(color);
518 }
519
520 this.figures.push({
521 type: "lattice",
522 coords: new Int32Array(ps),
523 colors: new Int32Array(ps),
524 verticesPerRow
525 });
526 }
527
528 _decodeType6Shading(reader) {
529 const coords = this.coords;
530 const colors = this.colors;
531 const ps = new Int32Array(16);
532 const cs = new Int32Array(4);
533
534 while (reader.hasData) {
535 const f = reader.readFlag();
536
537 if (!(0 <= f && f <= 3)) {
538 throw new _util.FormatError("Unknown type6 flag");
539 }
540
541 const pi = coords.length;
542
543 for (let i = 0, ii = f !== 0 ? 8 : 12; i < ii; i++) {
544 coords.push(reader.readCoordinate());
545 }
546
547 const ci = colors.length;
548
549 for (let i = 0, ii = f !== 0 ? 2 : 4; i < ii; i++) {
550 colors.push(reader.readComponents());
551 }
552
553 let tmp1, tmp2, tmp3, tmp4;
554
555 switch (f) {
556 case 0:
557 ps[12] = pi + 3;
558 ps[13] = pi + 4;
559 ps[14] = pi + 5;
560 ps[15] = pi + 6;
561 ps[8] = pi + 2;
562 ps[11] = pi + 7;
563 ps[4] = pi + 1;
564 ps[7] = pi + 8;
565 ps[0] = pi;
566 ps[1] = pi + 11;
567 ps[2] = pi + 10;
568 ps[3] = pi + 9;
569 cs[2] = ci + 1;
570 cs[3] = ci + 2;
571 cs[0] = ci;
572 cs[1] = ci + 3;
573 break;
574
575 case 1:
576 tmp1 = ps[12];
577 tmp2 = ps[13];
578 tmp3 = ps[14];
579 tmp4 = ps[15];
580 ps[12] = tmp4;
581 ps[13] = pi + 0;
582 ps[14] = pi + 1;
583 ps[15] = pi + 2;
584 ps[8] = tmp3;
585 ps[11] = pi + 3;
586 ps[4] = tmp2;
587 ps[7] = pi + 4;
588 ps[0] = tmp1;
589 ps[1] = pi + 7;
590 ps[2] = pi + 6;
591 ps[3] = pi + 5;
592 tmp1 = cs[2];
593 tmp2 = cs[3];
594 cs[2] = tmp2;
595 cs[3] = ci;
596 cs[0] = tmp1;
597 cs[1] = ci + 1;
598 break;
599
600 case 2:
601 tmp1 = ps[15];
602 tmp2 = ps[11];
603 ps[12] = ps[3];
604 ps[13] = pi + 0;
605 ps[14] = pi + 1;
606 ps[15] = pi + 2;
607 ps[8] = ps[7];
608 ps[11] = pi + 3;
609 ps[4] = tmp2;
610 ps[7] = pi + 4;
611 ps[0] = tmp1;
612 ps[1] = pi + 7;
613 ps[2] = pi + 6;
614 ps[3] = pi + 5;
615 tmp1 = cs[3];
616 cs[2] = cs[1];
617 cs[3] = ci;
618 cs[0] = tmp1;
619 cs[1] = ci + 1;
620 break;
621
622 case 3:
623 ps[12] = ps[0];
624 ps[13] = pi + 0;
625 ps[14] = pi + 1;
626 ps[15] = pi + 2;
627 ps[8] = ps[1];
628 ps[11] = pi + 3;
629 ps[4] = ps[2];
630 ps[7] = pi + 4;
631 ps[0] = ps[3];
632 ps[1] = pi + 7;
633 ps[2] = pi + 6;
634 ps[3] = pi + 5;
635 cs[2] = cs[0];
636 cs[3] = ci;
637 cs[0] = cs[1];
638 cs[1] = ci + 1;
639 break;
640 }
641
642 ps[5] = coords.length;
643 coords.push([(-4 * coords[ps[0]][0] - coords[ps[15]][0] + 6 * (coords[ps[4]][0] + coords[ps[1]][0]) - 2 * (coords[ps[12]][0] + coords[ps[3]][0]) + 3 * (coords[ps[13]][0] + coords[ps[7]][0])) / 9, (-4 * coords[ps[0]][1] - coords[ps[15]][1] + 6 * (coords[ps[4]][1] + coords[ps[1]][1]) - 2 * (coords[ps[12]][1] + coords[ps[3]][1]) + 3 * (coords[ps[13]][1] + coords[ps[7]][1])) / 9]);
644 ps[6] = coords.length;
645 coords.push([(-4 * coords[ps[3]][0] - coords[ps[12]][0] + 6 * (coords[ps[2]][0] + coords[ps[7]][0]) - 2 * (coords[ps[0]][0] + coords[ps[15]][0]) + 3 * (coords[ps[4]][0] + coords[ps[14]][0])) / 9, (-4 * coords[ps[3]][1] - coords[ps[12]][1] + 6 * (coords[ps[2]][1] + coords[ps[7]][1]) - 2 * (coords[ps[0]][1] + coords[ps[15]][1]) + 3 * (coords[ps[4]][1] + coords[ps[14]][1])) / 9]);
646 ps[9] = coords.length;
647 coords.push([(-4 * coords[ps[12]][0] - coords[ps[3]][0] + 6 * (coords[ps[8]][0] + coords[ps[13]][0]) - 2 * (coords[ps[0]][0] + coords[ps[15]][0]) + 3 * (coords[ps[11]][0] + coords[ps[1]][0])) / 9, (-4 * coords[ps[12]][1] - coords[ps[3]][1] + 6 * (coords[ps[8]][1] + coords[ps[13]][1]) - 2 * (coords[ps[0]][1] + coords[ps[15]][1]) + 3 * (coords[ps[11]][1] + coords[ps[1]][1])) / 9]);
648 ps[10] = coords.length;
649 coords.push([(-4 * coords[ps[15]][0] - coords[ps[0]][0] + 6 * (coords[ps[11]][0] + coords[ps[14]][0]) - 2 * (coords[ps[12]][0] + coords[ps[3]][0]) + 3 * (coords[ps[2]][0] + coords[ps[8]][0])) / 9, (-4 * coords[ps[15]][1] - coords[ps[0]][1] + 6 * (coords[ps[11]][1] + coords[ps[14]][1]) - 2 * (coords[ps[12]][1] + coords[ps[3]][1]) + 3 * (coords[ps[2]][1] + coords[ps[8]][1])) / 9]);
650 this.figures.push({
651 type: "patch",
652 coords: new Int32Array(ps),
653 colors: new Int32Array(cs)
654 });
655 }
656 }
657
658 _decodeType7Shading(reader) {
659 const coords = this.coords;
660 const colors = this.colors;
661 const ps = new Int32Array(16);
662 const cs = new Int32Array(4);
663
664 while (reader.hasData) {
665 const f = reader.readFlag();
666
667 if (!(0 <= f && f <= 3)) {
668 throw new _util.FormatError("Unknown type7 flag");
669 }
670
671 const pi = coords.length;
672
673 for (let i = 0, ii = f !== 0 ? 12 : 16; i < ii; i++) {
674 coords.push(reader.readCoordinate());
675 }
676
677 const ci = colors.length;
678
679 for (let i = 0, ii = f !== 0 ? 2 : 4; i < ii; i++) {
680 colors.push(reader.readComponents());
681 }
682
683 let tmp1, tmp2, tmp3, tmp4;
684
685 switch (f) {
686 case 0:
687 ps[12] = pi + 3;
688 ps[13] = pi + 4;
689 ps[14] = pi + 5;
690 ps[15] = pi + 6;
691 ps[8] = pi + 2;
692 ps[9] = pi + 13;
693 ps[10] = pi + 14;
694 ps[11] = pi + 7;
695 ps[4] = pi + 1;
696 ps[5] = pi + 12;
697 ps[6] = pi + 15;
698 ps[7] = pi + 8;
699 ps[0] = pi;
700 ps[1] = pi + 11;
701 ps[2] = pi + 10;
702 ps[3] = pi + 9;
703 cs[2] = ci + 1;
704 cs[3] = ci + 2;
705 cs[0] = ci;
706 cs[1] = ci + 3;
707 break;
708
709 case 1:
710 tmp1 = ps[12];
711 tmp2 = ps[13];
712 tmp3 = ps[14];
713 tmp4 = ps[15];
714 ps[12] = tmp4;
715 ps[13] = pi + 0;
716 ps[14] = pi + 1;
717 ps[15] = pi + 2;
718 ps[8] = tmp3;
719 ps[9] = pi + 9;
720 ps[10] = pi + 10;
721 ps[11] = pi + 3;
722 ps[4] = tmp2;
723 ps[5] = pi + 8;
724 ps[6] = pi + 11;
725 ps[7] = pi + 4;
726 ps[0] = tmp1;
727 ps[1] = pi + 7;
728 ps[2] = pi + 6;
729 ps[3] = pi + 5;
730 tmp1 = cs[2];
731 tmp2 = cs[3];
732 cs[2] = tmp2;
733 cs[3] = ci;
734 cs[0] = tmp1;
735 cs[1] = ci + 1;
736 break;
737
738 case 2:
739 tmp1 = ps[15];
740 tmp2 = ps[11];
741 ps[12] = ps[3];
742 ps[13] = pi + 0;
743 ps[14] = pi + 1;
744 ps[15] = pi + 2;
745 ps[8] = ps[7];
746 ps[9] = pi + 9;
747 ps[10] = pi + 10;
748 ps[11] = pi + 3;
749 ps[4] = tmp2;
750 ps[5] = pi + 8;
751 ps[6] = pi + 11;
752 ps[7] = pi + 4;
753 ps[0] = tmp1;
754 ps[1] = pi + 7;
755 ps[2] = pi + 6;
756 ps[3] = pi + 5;
757 tmp1 = cs[3];
758 cs[2] = cs[1];
759 cs[3] = ci;
760 cs[0] = tmp1;
761 cs[1] = ci + 1;
762 break;
763
764 case 3:
765 ps[12] = ps[0];
766 ps[13] = pi + 0;
767 ps[14] = pi + 1;
768 ps[15] = pi + 2;
769 ps[8] = ps[1];
770 ps[9] = pi + 9;
771 ps[10] = pi + 10;
772 ps[11] = pi + 3;
773 ps[4] = ps[2];
774 ps[5] = pi + 8;
775 ps[6] = pi + 11;
776 ps[7] = pi + 4;
777 ps[0] = ps[3];
778 ps[1] = pi + 7;
779 ps[2] = pi + 6;
780 ps[3] = pi + 5;
781 cs[2] = cs[0];
782 cs[3] = ci;
783 cs[0] = cs[1];
784 cs[1] = ci + 1;
785 break;
786 }
787
788 this.figures.push({
789 type: "patch",
790 coords: new Int32Array(ps),
791 colors: new Int32Array(cs)
792 });
793 }
794 }
795
796 _buildFigureFromPatch(index) {
797 const figure = this.figures[index];
798 (0, _util.assert)(figure.type === "patch", "Unexpected patch mesh figure");
799 const coords = this.coords,
800 colors = this.colors;
801 const pi = figure.coords;
802 const ci = figure.colors;
803 const figureMinX = Math.min(coords[pi[0]][0], coords[pi[3]][0], coords[pi[12]][0], coords[pi[15]][0]);
804 const figureMinY = Math.min(coords[pi[0]][1], coords[pi[3]][1], coords[pi[12]][1], coords[pi[15]][1]);
805 const figureMaxX = Math.max(coords[pi[0]][0], coords[pi[3]][0], coords[pi[12]][0], coords[pi[15]][0]);
806 const figureMaxY = Math.max(coords[pi[0]][1], coords[pi[3]][1], coords[pi[12]][1], coords[pi[15]][1]);
807 let splitXBy = Math.ceil((figureMaxX - figureMinX) * MeshShading.TRIANGLE_DENSITY / (this.bounds[2] - this.bounds[0]));
808 splitXBy = Math.max(MeshShading.MIN_SPLIT_PATCH_CHUNKS_AMOUNT, Math.min(MeshShading.MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitXBy));
809 let splitYBy = Math.ceil((figureMaxY - figureMinY) * MeshShading.TRIANGLE_DENSITY / (this.bounds[3] - this.bounds[1]));
810 splitYBy = Math.max(MeshShading.MIN_SPLIT_PATCH_CHUNKS_AMOUNT, Math.min(MeshShading.MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitYBy));
811 const verticesPerRow = splitXBy + 1;
812 const figureCoords = new Int32Array((splitYBy + 1) * verticesPerRow);
813 const figureColors = new Int32Array((splitYBy + 1) * verticesPerRow);
814 let k = 0;
815 const cl = new Uint8Array(3),
816 cr = new Uint8Array(3);
817 const c0 = colors[ci[0]],
818 c1 = colors[ci[1]],
819 c2 = colors[ci[2]],
820 c3 = colors[ci[3]];
821 const bRow = getB(splitYBy),
822 bCol = getB(splitXBy);
823
824 for (let row = 0; row <= splitYBy; row++) {
825 cl[0] = (c0[0] * (splitYBy - row) + c2[0] * row) / splitYBy | 0;
826 cl[1] = (c0[1] * (splitYBy - row) + c2[1] * row) / splitYBy | 0;
827 cl[2] = (c0[2] * (splitYBy - row) + c2[2] * row) / splitYBy | 0;
828 cr[0] = (c1[0] * (splitYBy - row) + c3[0] * row) / splitYBy | 0;
829 cr[1] = (c1[1] * (splitYBy - row) + c3[1] * row) / splitYBy | 0;
830 cr[2] = (c1[2] * (splitYBy - row) + c3[2] * row) / splitYBy | 0;
831
832 for (let col = 0; col <= splitXBy; col++, k++) {
833 if ((row === 0 || row === splitYBy) && (col === 0 || col === splitXBy)) {
834 continue;
835 }
836
837 let x = 0,
838 y = 0;
839 let q = 0;
840
841 for (let i = 0; i <= 3; i++) {
842 for (let j = 0; j <= 3; j++, q++) {
843 const m = bRow[row][i] * bCol[col][j];
844 x += coords[pi[q]][0] * m;
845 y += coords[pi[q]][1] * m;
846 }
847 }
848
849 figureCoords[k] = coords.length;
850 coords.push([x, y]);
851 figureColors[k] = colors.length;
852 const newColor = new Uint8Array(3);
853 newColor[0] = (cl[0] * (splitXBy - col) + cr[0] * col) / splitXBy | 0;
854 newColor[1] = (cl[1] * (splitXBy - col) + cr[1] * col) / splitXBy | 0;
855 newColor[2] = (cl[2] * (splitXBy - col) + cr[2] * col) / splitXBy | 0;
856 colors.push(newColor);
857 }
858 }
859
860 figureCoords[0] = pi[0];
861 figureColors[0] = ci[0];
862 figureCoords[splitXBy] = pi[3];
863 figureColors[splitXBy] = ci[1];
864 figureCoords[verticesPerRow * splitYBy] = pi[12];
865 figureColors[verticesPerRow * splitYBy] = ci[2];
866 figureCoords[verticesPerRow * splitYBy + splitXBy] = pi[15];
867 figureColors[verticesPerRow * splitYBy + splitXBy] = ci[3];
868 this.figures[index] = {
869 type: "lattice",
870 coords: figureCoords,
871 colors: figureColors,
872 verticesPerRow
873 };
874 }
875
876 _updateBounds() {
877 let minX = this.coords[0][0],
878 minY = this.coords[0][1],
879 maxX = minX,
880 maxY = minY;
881
882 for (let i = 1, ii = this.coords.length; i < ii; i++) {
883 const x = this.coords[i][0],
884 y = this.coords[i][1];
885 minX = minX > x ? x : minX;
886 minY = minY > y ? y : minY;
887 maxX = maxX < x ? x : maxX;
888 maxY = maxY < y ? y : maxY;
889 }
890
891 this.bounds = [minX, minY, maxX, maxY];
892 }
893
894 _packData() {
895 let i, ii, j, jj;
896 const coords = this.coords;
897 const coordsPacked = new Float32Array(coords.length * 2);
898
899 for (i = 0, j = 0, ii = coords.length; i < ii; i++) {
900 const xy = coords[i];
901 coordsPacked[j++] = xy[0];
902 coordsPacked[j++] = xy[1];
903 }
904
905 this.coords = coordsPacked;
906 const colors = this.colors;
907 const colorsPacked = new Uint8Array(colors.length * 3);
908
909 for (i = 0, j = 0, ii = colors.length; i < ii; i++) {
910 const c = colors[i];
911 colorsPacked[j++] = c[0];
912 colorsPacked[j++] = c[1];
913 colorsPacked[j++] = c[2];
914 }
915
916 this.colors = colorsPacked;
917 const figures = this.figures;
918
919 for (i = 0, ii = figures.length; i < ii; i++) {
920 const figure = figures[i],
921 ps = figure.coords,
922 cs = figure.colors;
923
924 for (j = 0, jj = ps.length; j < jj; j++) {
925 ps[j] *= 2;
926 cs[j] *= 3;
927 }
928 }
929 }
930
931 getIR() {
932 return ["Mesh", this.shadingType, this.coords, this.colors, this.figures, this.bounds, this.bbox, this.background];
933 }
934
935}
936
937class DummyShading extends BaseShading {
938 getIR() {
939 return ["Dummy"];
940 }
941
942}
943
944function getTilingPatternIR(operatorList, dict, color) {
945 const matrix = dict.getArray("Matrix");
946
947 const bbox = _util.Util.normalizeRect(dict.getArray("BBox"));
948
949 const xstep = dict.get("XStep");
950 const ystep = dict.get("YStep");
951 const paintType = dict.get("PaintType");
952 const tilingType = dict.get("TilingType");
953
954 if (bbox[2] - bbox[0] === 0 || bbox[3] - bbox[1] === 0) {
955 throw new _util.FormatError(`Invalid getTilingPatternIR /BBox array: [${bbox}].`);
956 }
957
958 return ["TilingPattern", color, operatorList, matrix, bbox, xstep, ystep, paintType, tilingType];
959}
\No newline at end of file