1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.CanvasGraphics = void 0;
|
28 |
|
29 | var _util = require("../shared/util");
|
30 |
|
31 | var _pattern_helper = require("./pattern_helper");
|
32 |
|
33 | var MIN_FONT_SIZE = 16;
|
34 | var MAX_FONT_SIZE = 100;
|
35 | var MAX_GROUP_SIZE = 4096;
|
36 | var MIN_WIDTH_FACTOR = 0.65;
|
37 | var COMPILE_TYPE3_GLYPHS = true;
|
38 | var MAX_SIZE_TO_COMPILE = 1000;
|
39 | var FULL_CHUNK_HEIGHT = 16;
|
40 | var IsLittleEndianCached = {
|
41 | get value() {
|
42 | return (0, _util.shadow)(IsLittleEndianCached, 'value', (0, _util.isLittleEndian)());
|
43 | }
|
44 |
|
45 | };
|
46 |
|
47 | function addContextCurrentTransform(ctx) {
|
48 | if (!ctx.mozCurrentTransform) {
|
49 | ctx._originalSave = ctx.save;
|
50 | ctx._originalRestore = ctx.restore;
|
51 | ctx._originalRotate = ctx.rotate;
|
52 | ctx._originalScale = ctx.scale;
|
53 | ctx._originalTranslate = ctx.translate;
|
54 | ctx._originalTransform = ctx.transform;
|
55 | ctx._originalSetTransform = ctx.setTransform;
|
56 | ctx._transformMatrix = ctx._transformMatrix || [1, 0, 0, 1, 0, 0];
|
57 | ctx._transformStack = [];
|
58 | Object.defineProperty(ctx, 'mozCurrentTransform', {
|
59 | get: function getCurrentTransform() {
|
60 | return this._transformMatrix;
|
61 | }
|
62 | });
|
63 | Object.defineProperty(ctx, 'mozCurrentTransformInverse', {
|
64 | get: function getCurrentTransformInverse() {
|
65 | var m = this._transformMatrix;
|
66 | var a = m[0],
|
67 | b = m[1],
|
68 | c = m[2],
|
69 | d = m[3],
|
70 | e = m[4],
|
71 | f = m[5];
|
72 | var ad_bc = a * d - b * c;
|
73 | var bc_ad = b * c - a * d;
|
74 | return [d / ad_bc, b / bc_ad, c / bc_ad, a / ad_bc, (d * e - c * f) / bc_ad, (b * e - a * f) / ad_bc];
|
75 | }
|
76 | });
|
77 |
|
78 | ctx.save = function ctxSave() {
|
79 | var old = this._transformMatrix;
|
80 |
|
81 | this._transformStack.push(old);
|
82 |
|
83 | this._transformMatrix = old.slice(0, 6);
|
84 |
|
85 | this._originalSave();
|
86 | };
|
87 |
|
88 | ctx.restore = function ctxRestore() {
|
89 | var prev = this._transformStack.pop();
|
90 |
|
91 | if (prev) {
|
92 | this._transformMatrix = prev;
|
93 |
|
94 | this._originalRestore();
|
95 | }
|
96 | };
|
97 |
|
98 | ctx.translate = function ctxTranslate(x, y) {
|
99 | var m = this._transformMatrix;
|
100 | m[4] = m[0] * x + m[2] * y + m[4];
|
101 | m[5] = m[1] * x + m[3] * y + m[5];
|
102 |
|
103 | this._originalTranslate(x, y);
|
104 | };
|
105 |
|
106 | ctx.scale = function ctxScale(x, y) {
|
107 | var m = this._transformMatrix;
|
108 | m[0] = m[0] * x;
|
109 | m[1] = m[1] * x;
|
110 | m[2] = m[2] * y;
|
111 | m[3] = m[3] * y;
|
112 |
|
113 | this._originalScale(x, y);
|
114 | };
|
115 |
|
116 | ctx.transform = function ctxTransform(a, b, c, d, e, f) {
|
117 | var m = this._transformMatrix;
|
118 | this._transformMatrix = [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]];
|
119 |
|
120 | ctx._originalTransform(a, b, c, d, e, f);
|
121 | };
|
122 |
|
123 | ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
|
124 | this._transformMatrix = [a, b, c, d, e, f];
|
125 |
|
126 | ctx._originalSetTransform(a, b, c, d, e, f);
|
127 | };
|
128 |
|
129 | ctx.rotate = function ctxRotate(angle) {
|
130 | var cosValue = Math.cos(angle);
|
131 | var sinValue = Math.sin(angle);
|
132 | var m = this._transformMatrix;
|
133 | this._transformMatrix = [m[0] * cosValue + m[2] * sinValue, m[1] * cosValue + m[3] * sinValue, m[0] * -sinValue + m[2] * cosValue, m[1] * -sinValue + m[3] * cosValue, m[4], m[5]];
|
134 |
|
135 | this._originalRotate(angle);
|
136 | };
|
137 | }
|
138 | }
|
139 |
|
140 | var CachedCanvases = function CachedCanvasesClosure() {
|
141 | function CachedCanvases(canvasFactory) {
|
142 | this.canvasFactory = canvasFactory;
|
143 | this.cache = Object.create(null);
|
144 | }
|
145 |
|
146 | CachedCanvases.prototype = {
|
147 | getCanvas: function CachedCanvases_getCanvas(id, width, height, trackTransform) {
|
148 | var canvasEntry;
|
149 |
|
150 | if (this.cache[id] !== undefined) {
|
151 | canvasEntry = this.cache[id];
|
152 | this.canvasFactory.reset(canvasEntry, width, height);
|
153 | canvasEntry.context.setTransform(1, 0, 0, 1, 0, 0);
|
154 | } else {
|
155 | canvasEntry = this.canvasFactory.create(width, height);
|
156 | this.cache[id] = canvasEntry;
|
157 | }
|
158 |
|
159 | if (trackTransform) {
|
160 | addContextCurrentTransform(canvasEntry.context);
|
161 | }
|
162 |
|
163 | return canvasEntry;
|
164 | },
|
165 | clear: function clear() {
|
166 | for (var id in this.cache) {
|
167 | var canvasEntry = this.cache[id];
|
168 | this.canvasFactory.destroy(canvasEntry);
|
169 | delete this.cache[id];
|
170 | }
|
171 | }
|
172 | };
|
173 | return CachedCanvases;
|
174 | }();
|
175 |
|
176 | function compileType3Glyph(imgData) {
|
177 | var POINT_TO_PROCESS_LIMIT = 1000;
|
178 | var width = imgData.width,
|
179 | height = imgData.height;
|
180 | var i,
|
181 | j,
|
182 | j0,
|
183 | width1 = width + 1;
|
184 | var points = new Uint8Array(width1 * (height + 1));
|
185 | var POINT_TYPES = new Uint8Array([0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0]);
|
186 | var lineSize = width + 7 & ~7,
|
187 | data0 = imgData.data;
|
188 | var data = new Uint8Array(lineSize * height),
|
189 | pos = 0,
|
190 | ii;
|
191 |
|
192 | for (i = 0, ii = data0.length; i < ii; i++) {
|
193 | var mask = 128,
|
194 | elem = data0[i];
|
195 |
|
196 | while (mask > 0) {
|
197 | data[pos++] = elem & mask ? 0 : 255;
|
198 | mask >>= 1;
|
199 | }
|
200 | }
|
201 |
|
202 | var count = 0;
|
203 | pos = 0;
|
204 |
|
205 | if (data[pos] !== 0) {
|
206 | points[0] = 1;
|
207 | ++count;
|
208 | }
|
209 |
|
210 | for (j = 1; j < width; j++) {
|
211 | if (data[pos] !== data[pos + 1]) {
|
212 | points[j] = data[pos] ? 2 : 1;
|
213 | ++count;
|
214 | }
|
215 |
|
216 | pos++;
|
217 | }
|
218 |
|
219 | if (data[pos] !== 0) {
|
220 | points[j] = 2;
|
221 | ++count;
|
222 | }
|
223 |
|
224 | for (i = 1; i < height; i++) {
|
225 | pos = i * lineSize;
|
226 | j0 = i * width1;
|
227 |
|
228 | if (data[pos - lineSize] !== data[pos]) {
|
229 | points[j0] = data[pos] ? 1 : 8;
|
230 | ++count;
|
231 | }
|
232 |
|
233 | var sum = (data[pos] ? 4 : 0) + (data[pos - lineSize] ? 8 : 0);
|
234 |
|
235 | for (j = 1; j < width; j++) {
|
236 | sum = (sum >> 2) + (data[pos + 1] ? 4 : 0) + (data[pos - lineSize + 1] ? 8 : 0);
|
237 |
|
238 | if (POINT_TYPES[sum]) {
|
239 | points[j0 + j] = POINT_TYPES[sum];
|
240 | ++count;
|
241 | }
|
242 |
|
243 | pos++;
|
244 | }
|
245 |
|
246 | if (data[pos - lineSize] !== data[pos]) {
|
247 | points[j0 + j] = data[pos] ? 2 : 4;
|
248 | ++count;
|
249 | }
|
250 |
|
251 | if (count > POINT_TO_PROCESS_LIMIT) {
|
252 | return null;
|
253 | }
|
254 | }
|
255 |
|
256 | pos = lineSize * (height - 1);
|
257 | j0 = i * width1;
|
258 |
|
259 | if (data[pos] !== 0) {
|
260 | points[j0] = 8;
|
261 | ++count;
|
262 | }
|
263 |
|
264 | for (j = 1; j < width; j++) {
|
265 | if (data[pos] !== data[pos + 1]) {
|
266 | points[j0 + j] = data[pos] ? 4 : 8;
|
267 | ++count;
|
268 | }
|
269 |
|
270 | pos++;
|
271 | }
|
272 |
|
273 | if (data[pos] !== 0) {
|
274 | points[j0 + j] = 4;
|
275 | ++count;
|
276 | }
|
277 |
|
278 | if (count > POINT_TO_PROCESS_LIMIT) {
|
279 | return null;
|
280 | }
|
281 |
|
282 | var steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
|
283 | var outlines = [];
|
284 |
|
285 | for (i = 0; count && i <= height; i++) {
|
286 | var p = i * width1;
|
287 | var end = p + width;
|
288 |
|
289 | while (p < end && !points[p]) {
|
290 | p++;
|
291 | }
|
292 |
|
293 | if (p === end) {
|
294 | continue;
|
295 | }
|
296 |
|
297 | var coords = [p % width1, i];
|
298 | var type = points[p],
|
299 | p0 = p,
|
300 | pp;
|
301 |
|
302 | do {
|
303 | var step = steps[type];
|
304 |
|
305 | do {
|
306 | p += step;
|
307 | } while (!points[p]);
|
308 |
|
309 | pp = points[p];
|
310 |
|
311 | if (pp !== 5 && pp !== 10) {
|
312 | type = pp;
|
313 | points[p] = 0;
|
314 | } else {
|
315 | type = pp & 0x33 * type >> 4;
|
316 | points[p] &= type >> 2 | type << 2;
|
317 | }
|
318 |
|
319 | coords.push(p % width1);
|
320 | coords.push(p / width1 | 0);
|
321 |
|
322 | if (!points[p]) {
|
323 | --count;
|
324 | }
|
325 | } while (p0 !== p);
|
326 |
|
327 | outlines.push(coords);
|
328 | --i;
|
329 | }
|
330 |
|
331 | var drawOutline = function drawOutline(c) {
|
332 | c.save();
|
333 | c.scale(1 / width, -1 / height);
|
334 | c.translate(0, -height);
|
335 | c.beginPath();
|
336 |
|
337 | for (var i = 0, ii = outlines.length; i < ii; i++) {
|
338 | var o = outlines[i];
|
339 | c.moveTo(o[0], o[1]);
|
340 |
|
341 | for (var j = 2, jj = o.length; j < jj; j += 2) {
|
342 | c.lineTo(o[j], o[j + 1]);
|
343 | }
|
344 | }
|
345 |
|
346 | c.fill();
|
347 | c.beginPath();
|
348 | c.restore();
|
349 | };
|
350 |
|
351 | return drawOutline;
|
352 | }
|
353 |
|
354 | var CanvasExtraState = function CanvasExtraStateClosure() {
|
355 | function CanvasExtraState() {
|
356 | this.alphaIsShape = false;
|
357 | this.fontSize = 0;
|
358 | this.fontSizeScale = 1;
|
359 | this.textMatrix = _util.IDENTITY_MATRIX;
|
360 | this.textMatrixScale = 1;
|
361 | this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
|
362 | this.leading = 0;
|
363 | this.x = 0;
|
364 | this.y = 0;
|
365 | this.lineX = 0;
|
366 | this.lineY = 0;
|
367 | this.charSpacing = 0;
|
368 | this.wordSpacing = 0;
|
369 | this.textHScale = 1;
|
370 | this.textRenderingMode = _util.TextRenderingMode.FILL;
|
371 | this.textRise = 0;
|
372 | this.fillColor = '#000000';
|
373 | this.strokeColor = '#000000';
|
374 | this.patternFill = false;
|
375 | this.fillAlpha = 1;
|
376 | this.strokeAlpha = 1;
|
377 | this.lineWidth = 1;
|
378 | this.activeSMask = null;
|
379 | this.resumeSMaskCtx = null;
|
380 | }
|
381 |
|
382 | CanvasExtraState.prototype = {
|
383 | clone: function CanvasExtraState_clone() {
|
384 | return Object.create(this);
|
385 | },
|
386 | setCurrentPoint: function CanvasExtraState_setCurrentPoint(x, y) {
|
387 | this.x = x;
|
388 | this.y = y;
|
389 | }
|
390 | };
|
391 | return CanvasExtraState;
|
392 | }();
|
393 |
|
394 | var CanvasGraphics = function CanvasGraphicsClosure() {
|
395 | var EXECUTION_TIME = 15;
|
396 | var EXECUTION_STEPS = 10;
|
397 |
|
398 | function CanvasGraphics(canvasCtx, commonObjs, objs, canvasFactory, webGLContext, imageLayer) {
|
399 | this.ctx = canvasCtx;
|
400 | this.current = new CanvasExtraState();
|
401 | this.stateStack = [];
|
402 | this.pendingClip = null;
|
403 | this.pendingEOFill = false;
|
404 | this.res = null;
|
405 | this.xobjs = null;
|
406 | this.commonObjs = commonObjs;
|
407 | this.objs = objs;
|
408 | this.canvasFactory = canvasFactory;
|
409 | this.webGLContext = webGLContext;
|
410 | this.imageLayer = imageLayer;
|
411 | this.groupStack = [];
|
412 | this.processingType3 = null;
|
413 | this.baseTransform = null;
|
414 | this.baseTransformStack = [];
|
415 | this.groupLevel = 0;
|
416 | this.smaskStack = [];
|
417 | this.smaskCounter = 0;
|
418 | this.tempSMask = null;
|
419 | this.cachedCanvases = new CachedCanvases(this.canvasFactory);
|
420 |
|
421 | if (canvasCtx) {
|
422 | addContextCurrentTransform(canvasCtx);
|
423 | }
|
424 |
|
425 | this._cachedGetSinglePixelWidth = null;
|
426 | }
|
427 |
|
428 | function putBinaryImageData(ctx, imgData) {
|
429 | if (typeof ImageData !== 'undefined' && imgData instanceof ImageData) {
|
430 | ctx.putImageData(imgData, 0, 0);
|
431 | return;
|
432 | }
|
433 |
|
434 | var height = imgData.height,
|
435 | width = imgData.width;
|
436 | var partialChunkHeight = height % FULL_CHUNK_HEIGHT;
|
437 | var fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
|
438 | var totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
|
439 | var chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
|
440 | var srcPos = 0,
|
441 | destPos;
|
442 | var src = imgData.data;
|
443 | var dest = chunkImgData.data;
|
444 | var i, j, thisChunkHeight, elemsInThisChunk;
|
445 |
|
446 | if (imgData.kind === _util.ImageKind.GRAYSCALE_1BPP) {
|
447 | var srcLength = src.byteLength;
|
448 | var dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2);
|
449 | var dest32DataLength = dest32.length;
|
450 | var fullSrcDiff = width + 7 >> 3;
|
451 | var white = 0xFFFFFFFF;
|
452 | var black = IsLittleEndianCached.value ? 0xFF000000 : 0x000000FF;
|
453 |
|
454 | for (i = 0; i < totalChunks; i++) {
|
455 | thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
|
456 | destPos = 0;
|
457 |
|
458 | for (j = 0; j < thisChunkHeight; j++) {
|
459 | var srcDiff = srcLength - srcPos;
|
460 | var k = 0;
|
461 | var kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7;
|
462 | var kEndUnrolled = kEnd & ~7;
|
463 | var mask = 0;
|
464 | var srcByte = 0;
|
465 |
|
466 | for (; k < kEndUnrolled; k += 8) {
|
467 | srcByte = src[srcPos++];
|
468 | dest32[destPos++] = srcByte & 128 ? white : black;
|
469 | dest32[destPos++] = srcByte & 64 ? white : black;
|
470 | dest32[destPos++] = srcByte & 32 ? white : black;
|
471 | dest32[destPos++] = srcByte & 16 ? white : black;
|
472 | dest32[destPos++] = srcByte & 8 ? white : black;
|
473 | dest32[destPos++] = srcByte & 4 ? white : black;
|
474 | dest32[destPos++] = srcByte & 2 ? white : black;
|
475 | dest32[destPos++] = srcByte & 1 ? white : black;
|
476 | }
|
477 |
|
478 | for (; k < kEnd; k++) {
|
479 | if (mask === 0) {
|
480 | srcByte = src[srcPos++];
|
481 | mask = 128;
|
482 | }
|
483 |
|
484 | dest32[destPos++] = srcByte & mask ? white : black;
|
485 | mask >>= 1;
|
486 | }
|
487 | }
|
488 |
|
489 | while (destPos < dest32DataLength) {
|
490 | dest32[destPos++] = 0;
|
491 | }
|
492 |
|
493 | ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
|
494 | }
|
495 | } else if (imgData.kind === _util.ImageKind.RGBA_32BPP) {
|
496 | j = 0;
|
497 | elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
|
498 |
|
499 | for (i = 0; i < fullChunks; i++) {
|
500 | dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
|
501 | srcPos += elemsInThisChunk;
|
502 | ctx.putImageData(chunkImgData, 0, j);
|
503 | j += FULL_CHUNK_HEIGHT;
|
504 | }
|
505 |
|
506 | if (i < totalChunks) {
|
507 | elemsInThisChunk = width * partialChunkHeight * 4;
|
508 | dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
|
509 | ctx.putImageData(chunkImgData, 0, j);
|
510 | }
|
511 | } else if (imgData.kind === _util.ImageKind.RGB_24BPP) {
|
512 | thisChunkHeight = FULL_CHUNK_HEIGHT;
|
513 | elemsInThisChunk = width * thisChunkHeight;
|
514 |
|
515 | for (i = 0; i < totalChunks; i++) {
|
516 | if (i >= fullChunks) {
|
517 | thisChunkHeight = partialChunkHeight;
|
518 | elemsInThisChunk = width * thisChunkHeight;
|
519 | }
|
520 |
|
521 | destPos = 0;
|
522 |
|
523 | for (j = elemsInThisChunk; j--;) {
|
524 | dest[destPos++] = src[srcPos++];
|
525 | dest[destPos++] = src[srcPos++];
|
526 | dest[destPos++] = src[srcPos++];
|
527 | dest[destPos++] = 255;
|
528 | }
|
529 |
|
530 | ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
|
531 | }
|
532 | } else {
|
533 | throw new Error("bad image kind: ".concat(imgData.kind));
|
534 | }
|
535 | }
|
536 |
|
537 | function putBinaryImageMask(ctx, imgData) {
|
538 | var height = imgData.height,
|
539 | width = imgData.width;
|
540 | var partialChunkHeight = height % FULL_CHUNK_HEIGHT;
|
541 | var fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
|
542 | var totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
|
543 | var chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
|
544 | var srcPos = 0;
|
545 | var src = imgData.data;
|
546 | var dest = chunkImgData.data;
|
547 |
|
548 | for (var i = 0; i < totalChunks; i++) {
|
549 | var thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
|
550 | var destPos = 3;
|
551 |
|
552 | for (var j = 0; j < thisChunkHeight; j++) {
|
553 | var mask = 0;
|
554 |
|
555 | for (var k = 0; k < width; k++) {
|
556 | if (!mask) {
|
557 | var elem = src[srcPos++];
|
558 | mask = 128;
|
559 | }
|
560 |
|
561 | dest[destPos] = elem & mask ? 0 : 255;
|
562 | destPos += 4;
|
563 | mask >>= 1;
|
564 | }
|
565 | }
|
566 |
|
567 | ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
|
568 | }
|
569 | }
|
570 |
|
571 | function copyCtxState(sourceCtx, destCtx) {
|
572 | var properties = ['strokeStyle', 'fillStyle', 'fillRule', 'globalAlpha', 'lineWidth', 'lineCap', 'lineJoin', 'miterLimit', 'globalCompositeOperation', 'font'];
|
573 |
|
574 | for (var i = 0, ii = properties.length; i < ii; i++) {
|
575 | var property = properties[i];
|
576 |
|
577 | if (sourceCtx[property] !== undefined) {
|
578 | destCtx[property] = sourceCtx[property];
|
579 | }
|
580 | }
|
581 |
|
582 | if (sourceCtx.setLineDash !== undefined) {
|
583 | destCtx.setLineDash(sourceCtx.getLineDash());
|
584 | destCtx.lineDashOffset = sourceCtx.lineDashOffset;
|
585 | }
|
586 | }
|
587 |
|
588 | function resetCtxToDefault(ctx) {
|
589 | ctx.strokeStyle = '#000000';
|
590 | ctx.fillStyle = '#000000';
|
591 | ctx.fillRule = 'nonzero';
|
592 | ctx.globalAlpha = 1;
|
593 | ctx.lineWidth = 1;
|
594 | ctx.lineCap = 'butt';
|
595 | ctx.lineJoin = 'miter';
|
596 | ctx.miterLimit = 10;
|
597 | ctx.globalCompositeOperation = 'source-over';
|
598 | ctx.font = '10px sans-serif';
|
599 |
|
600 | if (ctx.setLineDash !== undefined) {
|
601 | ctx.setLineDash([]);
|
602 | ctx.lineDashOffset = 0;
|
603 | }
|
604 | }
|
605 |
|
606 | function composeSMaskBackdrop(bytes, r0, g0, b0) {
|
607 | var length = bytes.length;
|
608 |
|
609 | for (var i = 3; i < length; i += 4) {
|
610 | var alpha = bytes[i];
|
611 |
|
612 | if (alpha === 0) {
|
613 | bytes[i - 3] = r0;
|
614 | bytes[i - 2] = g0;
|
615 | bytes[i - 1] = b0;
|
616 | } else if (alpha < 255) {
|
617 | var alpha_ = 255 - alpha;
|
618 | bytes[i - 3] = bytes[i - 3] * alpha + r0 * alpha_ >> 8;
|
619 | bytes[i - 2] = bytes[i - 2] * alpha + g0 * alpha_ >> 8;
|
620 | bytes[i - 1] = bytes[i - 1] * alpha + b0 * alpha_ >> 8;
|
621 | }
|
622 | }
|
623 | }
|
624 |
|
625 | function composeSMaskAlpha(maskData, layerData, transferMap) {
|
626 | var length = maskData.length;
|
627 | var scale = 1 / 255;
|
628 |
|
629 | for (var i = 3; i < length; i += 4) {
|
630 | var alpha = transferMap ? transferMap[maskData[i]] : maskData[i];
|
631 | layerData[i] = layerData[i] * alpha * scale | 0;
|
632 | }
|
633 | }
|
634 |
|
635 | function composeSMaskLuminosity(maskData, layerData, transferMap) {
|
636 | var length = maskData.length;
|
637 |
|
638 | for (var i = 3; i < length; i += 4) {
|
639 | var y = maskData[i - 3] * 77 + maskData[i - 2] * 152 + maskData[i - 1] * 28;
|
640 | layerData[i] = transferMap ? layerData[i] * transferMap[y >> 8] >> 8 : layerData[i] * y >> 16;
|
641 | }
|
642 | }
|
643 |
|
644 | function genericComposeSMask(maskCtx, layerCtx, width, height, subtype, backdrop, transferMap) {
|
645 | var hasBackdrop = !!backdrop;
|
646 | var r0 = hasBackdrop ? backdrop[0] : 0;
|
647 | var g0 = hasBackdrop ? backdrop[1] : 0;
|
648 | var b0 = hasBackdrop ? backdrop[2] : 0;
|
649 | var composeFn;
|
650 |
|
651 | if (subtype === 'Luminosity') {
|
652 | composeFn = composeSMaskLuminosity;
|
653 | } else {
|
654 | composeFn = composeSMaskAlpha;
|
655 | }
|
656 |
|
657 | var PIXELS_TO_PROCESS = 1048576;
|
658 | var chunkSize = Math.min(height, Math.ceil(PIXELS_TO_PROCESS / width));
|
659 |
|
660 | for (var row = 0; row < height; row += chunkSize) {
|
661 | var chunkHeight = Math.min(chunkSize, height - row);
|
662 | var maskData = maskCtx.getImageData(0, row, width, chunkHeight);
|
663 | var layerData = layerCtx.getImageData(0, row, width, chunkHeight);
|
664 |
|
665 | if (hasBackdrop) {
|
666 | composeSMaskBackdrop(maskData.data, r0, g0, b0);
|
667 | }
|
668 |
|
669 | composeFn(maskData.data, layerData.data, transferMap);
|
670 | maskCtx.putImageData(layerData, 0, row);
|
671 | }
|
672 | }
|
673 |
|
674 | function composeSMask(ctx, smask, layerCtx, webGLContext) {
|
675 | var mask = smask.canvas;
|
676 | var maskCtx = smask.context;
|
677 | ctx.setTransform(smask.scaleX, 0, 0, smask.scaleY, smask.offsetX, smask.offsetY);
|
678 | var backdrop = smask.backdrop || null;
|
679 |
|
680 | if (!smask.transferMap && webGLContext.isEnabled) {
|
681 | var composed = webGLContext.composeSMask({
|
682 | layer: layerCtx.canvas,
|
683 | mask: mask,
|
684 | properties: {
|
685 | subtype: smask.subtype,
|
686 | backdrop: backdrop
|
687 | }
|
688 | });
|
689 | ctx.setTransform(1, 0, 0, 1, 0, 0);
|
690 | ctx.drawImage(composed, smask.offsetX, smask.offsetY);
|
691 | return;
|
692 | }
|
693 |
|
694 | genericComposeSMask(maskCtx, layerCtx, mask.width, mask.height, smask.subtype, backdrop, smask.transferMap);
|
695 | ctx.drawImage(mask, 0, 0);
|
696 | }
|
697 |
|
698 | var LINE_CAP_STYLES = ['butt', 'round', 'square'];
|
699 | var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
|
700 | var NORMAL_CLIP = {};
|
701 | var EO_CLIP = {};
|
702 | CanvasGraphics.prototype = {
|
703 | beginDrawing: function beginDrawing(_ref) {
|
704 | var transform = _ref.transform,
|
705 | viewport = _ref.viewport,
|
706 | _ref$transparency = _ref.transparency,
|
707 | transparency = _ref$transparency === void 0 ? false : _ref$transparency,
|
708 | _ref$background = _ref.background,
|
709 | background = _ref$background === void 0 ? null : _ref$background;
|
710 | var width = this.ctx.canvas.width;
|
711 | var height = this.ctx.canvas.height;
|
712 | this.ctx.save();
|
713 | this.ctx.fillStyle = background || 'rgb(255, 255, 255)';
|
714 | this.ctx.fillRect(0, 0, width, height);
|
715 | this.ctx.restore();
|
716 |
|
717 | if (transparency) {
|
718 | var transparentCanvas = this.cachedCanvases.getCanvas('transparent', width, height, true);
|
719 | this.compositeCtx = this.ctx;
|
720 | this.transparentCanvas = transparentCanvas.canvas;
|
721 | this.ctx = transparentCanvas.context;
|
722 | this.ctx.save();
|
723 | this.ctx.transform.apply(this.ctx, this.compositeCtx.mozCurrentTransform);
|
724 | }
|
725 |
|
726 | this.ctx.save();
|
727 | resetCtxToDefault(this.ctx);
|
728 |
|
729 | if (transform) {
|
730 | this.ctx.transform.apply(this.ctx, transform);
|
731 | }
|
732 |
|
733 | this.ctx.transform.apply(this.ctx, viewport.transform);
|
734 | this.baseTransform = this.ctx.mozCurrentTransform.slice();
|
735 |
|
736 | if (this.imageLayer) {
|
737 | this.imageLayer.beginLayout();
|
738 | }
|
739 | },
|
740 | executeOperatorList: function CanvasGraphics_executeOperatorList(operatorList, executionStartIdx, continueCallback, stepper) {
|
741 | var argsArray = operatorList.argsArray;
|
742 | var fnArray = operatorList.fnArray;
|
743 | var i = executionStartIdx || 0;
|
744 | var argsArrayLen = argsArray.length;
|
745 |
|
746 | if (argsArrayLen === i) {
|
747 | return i;
|
748 | }
|
749 |
|
750 | var chunkOperations = argsArrayLen - i > EXECUTION_STEPS && typeof continueCallback === 'function';
|
751 | var endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
|
752 | var steps = 0;
|
753 | var commonObjs = this.commonObjs;
|
754 | var objs = this.objs;
|
755 | var fnId;
|
756 |
|
757 | while (true) {
|
758 | if (stepper !== undefined && i === stepper.nextBreakPoint) {
|
759 | stepper.breakIt(i, continueCallback);
|
760 | return i;
|
761 | }
|
762 |
|
763 | fnId = fnArray[i];
|
764 |
|
765 | if (fnId !== _util.OPS.dependency) {
|
766 | this[fnId].apply(this, argsArray[i]);
|
767 | } else {
|
768 | var _iteratorNormalCompletion = true;
|
769 | var _didIteratorError = false;
|
770 | var _iteratorError = undefined;
|
771 |
|
772 | try {
|
773 | for (var _iterator = argsArray[i][Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
774 | var depObjId = _step.value;
|
775 | var objsPool = depObjId.startsWith('g_') ? commonObjs : objs;
|
776 |
|
777 | if (!objsPool.has(depObjId)) {
|
778 | objsPool.get(depObjId, continueCallback);
|
779 | return i;
|
780 | }
|
781 | }
|
782 | } catch (err) {
|
783 | _didIteratorError = true;
|
784 | _iteratorError = err;
|
785 | } finally {
|
786 | try {
|
787 | if (!_iteratorNormalCompletion && _iterator["return"] != null) {
|
788 | _iterator["return"]();
|
789 | }
|
790 | } finally {
|
791 | if (_didIteratorError) {
|
792 | throw _iteratorError;
|
793 | }
|
794 | }
|
795 | }
|
796 | }
|
797 |
|
798 | i++;
|
799 |
|
800 | if (i === argsArrayLen) {
|
801 | return i;
|
802 | }
|
803 |
|
804 | if (chunkOperations && ++steps > EXECUTION_STEPS) {
|
805 | if (Date.now() > endTime) {
|
806 | continueCallback();
|
807 | return i;
|
808 | }
|
809 |
|
810 | steps = 0;
|
811 | }
|
812 | }
|
813 | },
|
814 | endDrawing: function CanvasGraphics_endDrawing() {
|
815 | if (this.current.activeSMask !== null) {
|
816 | this.endSMaskGroup();
|
817 | }
|
818 |
|
819 | this.ctx.restore();
|
820 |
|
821 | if (this.transparentCanvas) {
|
822 | this.ctx = this.compositeCtx;
|
823 | this.ctx.save();
|
824 | this.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
825 | this.ctx.drawImage(this.transparentCanvas, 0, 0);
|
826 | this.ctx.restore();
|
827 | this.transparentCanvas = null;
|
828 | }
|
829 |
|
830 | this.cachedCanvases.clear();
|
831 | this.webGLContext.clear();
|
832 |
|
833 | if (this.imageLayer) {
|
834 | this.imageLayer.endLayout();
|
835 | }
|
836 | },
|
837 | setLineWidth: function CanvasGraphics_setLineWidth(width) {
|
838 | this.current.lineWidth = width;
|
839 | this.ctx.lineWidth = width;
|
840 | },
|
841 | setLineCap: function CanvasGraphics_setLineCap(style) {
|
842 | this.ctx.lineCap = LINE_CAP_STYLES[style];
|
843 | },
|
844 | setLineJoin: function CanvasGraphics_setLineJoin(style) {
|
845 | this.ctx.lineJoin = LINE_JOIN_STYLES[style];
|
846 | },
|
847 | setMiterLimit: function CanvasGraphics_setMiterLimit(limit) {
|
848 | this.ctx.miterLimit = limit;
|
849 | },
|
850 | setDash: function CanvasGraphics_setDash(dashArray, dashPhase) {
|
851 | var ctx = this.ctx;
|
852 |
|
853 | if (ctx.setLineDash !== undefined) {
|
854 | ctx.setLineDash(dashArray);
|
855 | ctx.lineDashOffset = dashPhase;
|
856 | }
|
857 | },
|
858 | setRenderingIntent: function setRenderingIntent(intent) {},
|
859 | setFlatness: function setFlatness(flatness) {},
|
860 | setGState: function CanvasGraphics_setGState(states) {
|
861 | for (var i = 0, ii = states.length; i < ii; i++) {
|
862 | var state = states[i];
|
863 | var key = state[0];
|
864 | var value = state[1];
|
865 |
|
866 | switch (key) {
|
867 | case 'LW':
|
868 | this.setLineWidth(value);
|
869 | break;
|
870 |
|
871 | case 'LC':
|
872 | this.setLineCap(value);
|
873 | break;
|
874 |
|
875 | case 'LJ':
|
876 | this.setLineJoin(value);
|
877 | break;
|
878 |
|
879 | case 'ML':
|
880 | this.setMiterLimit(value);
|
881 | break;
|
882 |
|
883 | case 'D':
|
884 | this.setDash(value[0], value[1]);
|
885 | break;
|
886 |
|
887 | case 'RI':
|
888 | this.setRenderingIntent(value);
|
889 | break;
|
890 |
|
891 | case 'FL':
|
892 | this.setFlatness(value);
|
893 | break;
|
894 |
|
895 | case 'Font':
|
896 | this.setFont(value[0], value[1]);
|
897 | break;
|
898 |
|
899 | case 'CA':
|
900 | this.current.strokeAlpha = state[1];
|
901 | break;
|
902 |
|
903 | case 'ca':
|
904 | this.current.fillAlpha = state[1];
|
905 | this.ctx.globalAlpha = state[1];
|
906 | break;
|
907 |
|
908 | case 'BM':
|
909 | this.ctx.globalCompositeOperation = value;
|
910 | break;
|
911 |
|
912 | case 'SMask':
|
913 | if (this.current.activeSMask) {
|
914 | if (this.stateStack.length > 0 && this.stateStack[this.stateStack.length - 1].activeSMask === this.current.activeSMask) {
|
915 | this.suspendSMaskGroup();
|
916 | } else {
|
917 | this.endSMaskGroup();
|
918 | }
|
919 | }
|
920 |
|
921 | this.current.activeSMask = value ? this.tempSMask : null;
|
922 |
|
923 | if (this.current.activeSMask) {
|
924 | this.beginSMaskGroup();
|
925 | }
|
926 |
|
927 | this.tempSMask = null;
|
928 | break;
|
929 | }
|
930 | }
|
931 | },
|
932 | beginSMaskGroup: function CanvasGraphics_beginSMaskGroup() {
|
933 | var activeSMask = this.current.activeSMask;
|
934 | var drawnWidth = activeSMask.canvas.width;
|
935 | var drawnHeight = activeSMask.canvas.height;
|
936 | var cacheId = 'smaskGroupAt' + this.groupLevel;
|
937 | var scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
|
938 | var currentCtx = this.ctx;
|
939 | var currentTransform = currentCtx.mozCurrentTransform;
|
940 | this.ctx.save();
|
941 | var groupCtx = scratchCanvas.context;
|
942 | groupCtx.scale(1 / activeSMask.scaleX, 1 / activeSMask.scaleY);
|
943 | groupCtx.translate(-activeSMask.offsetX, -activeSMask.offsetY);
|
944 | groupCtx.transform.apply(groupCtx, currentTransform);
|
945 | activeSMask.startTransformInverse = groupCtx.mozCurrentTransformInverse;
|
946 | copyCtxState(currentCtx, groupCtx);
|
947 | this.ctx = groupCtx;
|
948 | this.setGState([['BM', 'source-over'], ['ca', 1], ['CA', 1]]);
|
949 | this.groupStack.push(currentCtx);
|
950 | this.groupLevel++;
|
951 | },
|
952 | suspendSMaskGroup: function CanvasGraphics_endSMaskGroup() {
|
953 | var groupCtx = this.ctx;
|
954 | this.groupLevel--;
|
955 | this.ctx = this.groupStack.pop();
|
956 | composeSMask(this.ctx, this.current.activeSMask, groupCtx, this.webGLContext);
|
957 | this.ctx.restore();
|
958 | this.ctx.save();
|
959 | copyCtxState(groupCtx, this.ctx);
|
960 | this.current.resumeSMaskCtx = groupCtx;
|
961 |
|
962 | var deltaTransform = _util.Util.transform(this.current.activeSMask.startTransformInverse, groupCtx.mozCurrentTransform);
|
963 |
|
964 | this.ctx.transform.apply(this.ctx, deltaTransform);
|
965 | groupCtx.save();
|
966 | groupCtx.setTransform(1, 0, 0, 1, 0, 0);
|
967 | groupCtx.clearRect(0, 0, groupCtx.canvas.width, groupCtx.canvas.height);
|
968 | groupCtx.restore();
|
969 | },
|
970 | resumeSMaskGroup: function CanvasGraphics_endSMaskGroup() {
|
971 | var groupCtx = this.current.resumeSMaskCtx;
|
972 | var currentCtx = this.ctx;
|
973 | this.ctx = groupCtx;
|
974 | this.groupStack.push(currentCtx);
|
975 | this.groupLevel++;
|
976 | },
|
977 | endSMaskGroup: function CanvasGraphics_endSMaskGroup() {
|
978 | var groupCtx = this.ctx;
|
979 | this.groupLevel--;
|
980 | this.ctx = this.groupStack.pop();
|
981 | composeSMask(this.ctx, this.current.activeSMask, groupCtx, this.webGLContext);
|
982 | this.ctx.restore();
|
983 | copyCtxState(groupCtx, this.ctx);
|
984 |
|
985 | var deltaTransform = _util.Util.transform(this.current.activeSMask.startTransformInverse, groupCtx.mozCurrentTransform);
|
986 |
|
987 | this.ctx.transform.apply(this.ctx, deltaTransform);
|
988 | },
|
989 | save: function CanvasGraphics_save() {
|
990 | this.ctx.save();
|
991 | var old = this.current;
|
992 | this.stateStack.push(old);
|
993 | this.current = old.clone();
|
994 | this.current.resumeSMaskCtx = null;
|
995 | },
|
996 | restore: function CanvasGraphics_restore() {
|
997 | if (this.current.resumeSMaskCtx) {
|
998 | this.resumeSMaskGroup();
|
999 | }
|
1000 |
|
1001 | if (this.current.activeSMask !== null && (this.stateStack.length === 0 || this.stateStack[this.stateStack.length - 1].activeSMask !== this.current.activeSMask)) {
|
1002 | this.endSMaskGroup();
|
1003 | }
|
1004 |
|
1005 | if (this.stateStack.length !== 0) {
|
1006 | this.current = this.stateStack.pop();
|
1007 | this.ctx.restore();
|
1008 | this.pendingClip = null;
|
1009 | this._cachedGetSinglePixelWidth = null;
|
1010 | }
|
1011 | },
|
1012 | transform: function CanvasGraphics_transform(a, b, c, d, e, f) {
|
1013 | this.ctx.transform(a, b, c, d, e, f);
|
1014 | this._cachedGetSinglePixelWidth = null;
|
1015 | },
|
1016 | constructPath: function CanvasGraphics_constructPath(ops, args) {
|
1017 | var ctx = this.ctx;
|
1018 | var current = this.current;
|
1019 | var x = current.x,
|
1020 | y = current.y;
|
1021 |
|
1022 | for (var i = 0, j = 0, ii = ops.length; i < ii; i++) {
|
1023 | switch (ops[i] | 0) {
|
1024 | case _util.OPS.rectangle:
|
1025 | x = args[j++];
|
1026 | y = args[j++];
|
1027 | var width = args[j++];
|
1028 | var height = args[j++];
|
1029 |
|
1030 | if (width === 0) {
|
1031 | width = this.getSinglePixelWidth();
|
1032 | }
|
1033 |
|
1034 | if (height === 0) {
|
1035 | height = this.getSinglePixelWidth();
|
1036 | }
|
1037 |
|
1038 | var xw = x + width;
|
1039 | var yh = y + height;
|
1040 | this.ctx.moveTo(x, y);
|
1041 | this.ctx.lineTo(xw, y);
|
1042 | this.ctx.lineTo(xw, yh);
|
1043 | this.ctx.lineTo(x, yh);
|
1044 | this.ctx.lineTo(x, y);
|
1045 | this.ctx.closePath();
|
1046 | break;
|
1047 |
|
1048 | case _util.OPS.moveTo:
|
1049 | x = args[j++];
|
1050 | y = args[j++];
|
1051 | ctx.moveTo(x, y);
|
1052 | break;
|
1053 |
|
1054 | case _util.OPS.lineTo:
|
1055 | x = args[j++];
|
1056 | y = args[j++];
|
1057 | ctx.lineTo(x, y);
|
1058 | break;
|
1059 |
|
1060 | case _util.OPS.curveTo:
|
1061 | x = args[j + 4];
|
1062 | y = args[j + 5];
|
1063 | ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3], x, y);
|
1064 | j += 6;
|
1065 | break;
|
1066 |
|
1067 | case _util.OPS.curveTo2:
|
1068 | ctx.bezierCurveTo(x, y, args[j], args[j + 1], args[j + 2], args[j + 3]);
|
1069 | x = args[j + 2];
|
1070 | y = args[j + 3];
|
1071 | j += 4;
|
1072 | break;
|
1073 |
|
1074 | case _util.OPS.curveTo3:
|
1075 | x = args[j + 2];
|
1076 | y = args[j + 3];
|
1077 | ctx.bezierCurveTo(args[j], args[j + 1], x, y, x, y);
|
1078 | j += 4;
|
1079 | break;
|
1080 |
|
1081 | case _util.OPS.closePath:
|
1082 | ctx.closePath();
|
1083 | break;
|
1084 | }
|
1085 | }
|
1086 |
|
1087 | current.setCurrentPoint(x, y);
|
1088 | },
|
1089 | closePath: function CanvasGraphics_closePath() {
|
1090 | this.ctx.closePath();
|
1091 | },
|
1092 | stroke: function CanvasGraphics_stroke(consumePath) {
|
1093 | consumePath = typeof consumePath !== 'undefined' ? consumePath : true;
|
1094 | var ctx = this.ctx;
|
1095 | var strokeColor = this.current.strokeColor;
|
1096 | ctx.lineWidth = Math.max(this.getSinglePixelWidth() * MIN_WIDTH_FACTOR, this.current.lineWidth);
|
1097 | ctx.globalAlpha = this.current.strokeAlpha;
|
1098 |
|
1099 | if (strokeColor && strokeColor.hasOwnProperty('type') && strokeColor.type === 'Pattern') {
|
1100 | ctx.save();
|
1101 | ctx.strokeStyle = strokeColor.getPattern(ctx, this);
|
1102 | ctx.stroke();
|
1103 | ctx.restore();
|
1104 | } else {
|
1105 | ctx.stroke();
|
1106 | }
|
1107 |
|
1108 | if (consumePath) {
|
1109 | this.consumePath();
|
1110 | }
|
1111 |
|
1112 | ctx.globalAlpha = this.current.fillAlpha;
|
1113 | },
|
1114 | closeStroke: function CanvasGraphics_closeStroke() {
|
1115 | this.closePath();
|
1116 | this.stroke();
|
1117 | },
|
1118 | fill: function CanvasGraphics_fill(consumePath) {
|
1119 | consumePath = typeof consumePath !== 'undefined' ? consumePath : true;
|
1120 | var ctx = this.ctx;
|
1121 | var fillColor = this.current.fillColor;
|
1122 | var isPatternFill = this.current.patternFill;
|
1123 | var needRestore = false;
|
1124 |
|
1125 | if (isPatternFill) {
|
1126 | ctx.save();
|
1127 |
|
1128 | if (this.baseTransform) {
|
1129 | ctx.setTransform.apply(ctx, this.baseTransform);
|
1130 | }
|
1131 |
|
1132 | ctx.fillStyle = fillColor.getPattern(ctx, this);
|
1133 | needRestore = true;
|
1134 | }
|
1135 |
|
1136 | if (this.pendingEOFill) {
|
1137 | ctx.fill('evenodd');
|
1138 | this.pendingEOFill = false;
|
1139 | } else {
|
1140 | ctx.fill();
|
1141 | }
|
1142 |
|
1143 | if (needRestore) {
|
1144 | ctx.restore();
|
1145 | }
|
1146 |
|
1147 | if (consumePath) {
|
1148 | this.consumePath();
|
1149 | }
|
1150 | },
|
1151 | eoFill: function CanvasGraphics_eoFill() {
|
1152 | this.pendingEOFill = true;
|
1153 | this.fill();
|
1154 | },
|
1155 | fillStroke: function CanvasGraphics_fillStroke() {
|
1156 | this.fill(false);
|
1157 | this.stroke(false);
|
1158 | this.consumePath();
|
1159 | },
|
1160 | eoFillStroke: function CanvasGraphics_eoFillStroke() {
|
1161 | this.pendingEOFill = true;
|
1162 | this.fillStroke();
|
1163 | },
|
1164 | closeFillStroke: function CanvasGraphics_closeFillStroke() {
|
1165 | this.closePath();
|
1166 | this.fillStroke();
|
1167 | },
|
1168 | closeEOFillStroke: function CanvasGraphics_closeEOFillStroke() {
|
1169 | this.pendingEOFill = true;
|
1170 | this.closePath();
|
1171 | this.fillStroke();
|
1172 | },
|
1173 | endPath: function CanvasGraphics_endPath() {
|
1174 | this.consumePath();
|
1175 | },
|
1176 | clip: function CanvasGraphics_clip() {
|
1177 | this.pendingClip = NORMAL_CLIP;
|
1178 | },
|
1179 | eoClip: function CanvasGraphics_eoClip() {
|
1180 | this.pendingClip = EO_CLIP;
|
1181 | },
|
1182 | beginText: function CanvasGraphics_beginText() {
|
1183 | this.current.textMatrix = _util.IDENTITY_MATRIX;
|
1184 | this.current.textMatrixScale = 1;
|
1185 | this.current.x = this.current.lineX = 0;
|
1186 | this.current.y = this.current.lineY = 0;
|
1187 | },
|
1188 | endText: function CanvasGraphics_endText() {
|
1189 | var paths = this.pendingTextPaths;
|
1190 | var ctx = this.ctx;
|
1191 |
|
1192 | if (paths === undefined) {
|
1193 | ctx.beginPath();
|
1194 | return;
|
1195 | }
|
1196 |
|
1197 | ctx.save();
|
1198 | ctx.beginPath();
|
1199 |
|
1200 | for (var i = 0; i < paths.length; i++) {
|
1201 | var path = paths[i];
|
1202 | ctx.setTransform.apply(ctx, path.transform);
|
1203 | ctx.translate(path.x, path.y);
|
1204 | path.addToPath(ctx, path.fontSize);
|
1205 | }
|
1206 |
|
1207 | ctx.restore();
|
1208 | ctx.clip();
|
1209 | ctx.beginPath();
|
1210 | delete this.pendingTextPaths;
|
1211 | },
|
1212 | setCharSpacing: function CanvasGraphics_setCharSpacing(spacing) {
|
1213 | this.current.charSpacing = spacing;
|
1214 | },
|
1215 | setWordSpacing: function CanvasGraphics_setWordSpacing(spacing) {
|
1216 | this.current.wordSpacing = spacing;
|
1217 | },
|
1218 | setHScale: function CanvasGraphics_setHScale(scale) {
|
1219 | this.current.textHScale = scale / 100;
|
1220 | },
|
1221 | setLeading: function CanvasGraphics_setLeading(leading) {
|
1222 | this.current.leading = -leading;
|
1223 | },
|
1224 | setFont: function CanvasGraphics_setFont(fontRefName, size) {
|
1225 | var fontObj = this.commonObjs.get(fontRefName);
|
1226 | var current = this.current;
|
1227 |
|
1228 | if (!fontObj) {
|
1229 | throw new Error("Can't find font for ".concat(fontRefName));
|
1230 | }
|
1231 |
|
1232 | current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
|
1233 |
|
1234 | if (current.fontMatrix[0] === 0 || current.fontMatrix[3] === 0) {
|
1235 | (0, _util.warn)('Invalid font matrix for font ' + fontRefName);
|
1236 | }
|
1237 |
|
1238 | if (size < 0) {
|
1239 | size = -size;
|
1240 | current.fontDirection = -1;
|
1241 | } else {
|
1242 | current.fontDirection = 1;
|
1243 | }
|
1244 |
|
1245 | this.current.font = fontObj;
|
1246 | this.current.fontSize = size;
|
1247 |
|
1248 | if (fontObj.isType3Font) {
|
1249 | return;
|
1250 | }
|
1251 |
|
1252 | var name = fontObj.loadedName || 'sans-serif';
|
1253 | var bold = fontObj.black ? '900' : fontObj.bold ? 'bold' : 'normal';
|
1254 | var italic = fontObj.italic ? 'italic' : 'normal';
|
1255 | var typeface = "\"".concat(name, "\", ").concat(fontObj.fallbackName);
|
1256 | var browserFontSize = size < MIN_FONT_SIZE ? MIN_FONT_SIZE : size > MAX_FONT_SIZE ? MAX_FONT_SIZE : size;
|
1257 | this.current.fontSizeScale = size / browserFontSize;
|
1258 | this.ctx.font = "".concat(italic, " ").concat(bold, " ").concat(browserFontSize, "px ").concat(typeface);
|
1259 | },
|
1260 | setTextRenderingMode: function CanvasGraphics_setTextRenderingMode(mode) {
|
1261 | this.current.textRenderingMode = mode;
|
1262 | },
|
1263 | setTextRise: function CanvasGraphics_setTextRise(rise) {
|
1264 | this.current.textRise = rise;
|
1265 | },
|
1266 | moveText: function CanvasGraphics_moveText(x, y) {
|
1267 | this.current.x = this.current.lineX += x;
|
1268 | this.current.y = this.current.lineY += y;
|
1269 | },
|
1270 | setLeadingMoveText: function CanvasGraphics_setLeadingMoveText(x, y) {
|
1271 | this.setLeading(-y);
|
1272 | this.moveText(x, y);
|
1273 | },
|
1274 | setTextMatrix: function CanvasGraphics_setTextMatrix(a, b, c, d, e, f) {
|
1275 | this.current.textMatrix = [a, b, c, d, e, f];
|
1276 | this.current.textMatrixScale = Math.sqrt(a * a + b * b);
|
1277 | this.current.x = this.current.lineX = 0;
|
1278 | this.current.y = this.current.lineY = 0;
|
1279 | },
|
1280 | nextLine: function CanvasGraphics_nextLine() {
|
1281 | this.moveText(0, this.current.leading);
|
1282 | },
|
1283 | paintChar: function paintChar(character, x, y, patternTransform) {
|
1284 | var ctx = this.ctx;
|
1285 | var current = this.current;
|
1286 | var font = current.font;
|
1287 | var textRenderingMode = current.textRenderingMode;
|
1288 | var fontSize = current.fontSize / current.fontSizeScale;
|
1289 | var fillStrokeMode = textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
|
1290 | var isAddToPathSet = !!(textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG);
|
1291 | var patternFill = current.patternFill && font.data;
|
1292 | var addToPath;
|
1293 |
|
1294 | if (font.disableFontFace || isAddToPathSet || patternFill) {
|
1295 | addToPath = font.getPathGenerator(this.commonObjs, character);
|
1296 | }
|
1297 |
|
1298 | if (font.disableFontFace || patternFill) {
|
1299 | ctx.save();
|
1300 | ctx.translate(x, y);
|
1301 | ctx.beginPath();
|
1302 | addToPath(ctx, fontSize);
|
1303 |
|
1304 | if (patternTransform) {
|
1305 | ctx.setTransform.apply(ctx, patternTransform);
|
1306 | }
|
1307 |
|
1308 | if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
|
1309 | ctx.fill();
|
1310 | }
|
1311 |
|
1312 | if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
|
1313 | ctx.stroke();
|
1314 | }
|
1315 |
|
1316 | ctx.restore();
|
1317 | } else {
|
1318 | if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
|
1319 | ctx.fillText(character, x, y);
|
1320 | }
|
1321 |
|
1322 | if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
|
1323 | ctx.strokeText(character, x, y);
|
1324 | }
|
1325 | }
|
1326 |
|
1327 | if (isAddToPathSet) {
|
1328 | var paths = this.pendingTextPaths || (this.pendingTextPaths = []);
|
1329 | paths.push({
|
1330 | transform: ctx.mozCurrentTransform,
|
1331 | x: x,
|
1332 | y: y,
|
1333 | fontSize: fontSize,
|
1334 | addToPath: addToPath
|
1335 | });
|
1336 | }
|
1337 | },
|
1338 |
|
1339 | get isFontSubpixelAAEnabled() {
|
1340 | var _this$cachedCanvases$ = this.cachedCanvases.getCanvas('isFontSubpixelAAEnabled', 10, 10),
|
1341 | ctx = _this$cachedCanvases$.context;
|
1342 |
|
1343 | ctx.scale(1.5, 1);
|
1344 | ctx.fillText('I', 0, 10);
|
1345 | var data = ctx.getImageData(0, 0, 10, 10).data;
|
1346 | var enabled = false;
|
1347 |
|
1348 | for (var i = 3; i < data.length; i += 4) {
|
1349 | if (data[i] > 0 && data[i] < 255) {
|
1350 | enabled = true;
|
1351 | break;
|
1352 | }
|
1353 | }
|
1354 |
|
1355 | return (0, _util.shadow)(this, 'isFontSubpixelAAEnabled', enabled);
|
1356 | },
|
1357 |
|
1358 | showText: function CanvasGraphics_showText(glyphs) {
|
1359 | var current = this.current;
|
1360 | var font = current.font;
|
1361 |
|
1362 | if (font.isType3Font) {
|
1363 | return this.showType3Text(glyphs);
|
1364 | }
|
1365 |
|
1366 | var fontSize = current.fontSize;
|
1367 |
|
1368 | if (fontSize === 0) {
|
1369 | return undefined;
|
1370 | }
|
1371 |
|
1372 | var ctx = this.ctx;
|
1373 | var fontSizeScale = current.fontSizeScale;
|
1374 | var charSpacing = current.charSpacing;
|
1375 | var wordSpacing = current.wordSpacing;
|
1376 | var fontDirection = current.fontDirection;
|
1377 | var textHScale = current.textHScale * fontDirection;
|
1378 | var glyphsLength = glyphs.length;
|
1379 | var vertical = font.vertical;
|
1380 | var spacingDir = vertical ? 1 : -1;
|
1381 | var defaultVMetrics = font.defaultVMetrics;
|
1382 | var widthAdvanceScale = fontSize * current.fontMatrix[0];
|
1383 | var simpleFillText = current.textRenderingMode === _util.TextRenderingMode.FILL && !font.disableFontFace && !current.patternFill;
|
1384 | ctx.save();
|
1385 | var patternTransform;
|
1386 |
|
1387 | if (current.patternFill) {
|
1388 | ctx.save();
|
1389 | var pattern = current.fillColor.getPattern(ctx, this);
|
1390 | patternTransform = ctx.mozCurrentTransform;
|
1391 | ctx.restore();
|
1392 | ctx.fillStyle = pattern;
|
1393 | }
|
1394 |
|
1395 | ctx.transform.apply(ctx, current.textMatrix);
|
1396 | ctx.translate(current.x, current.y + current.textRise);
|
1397 |
|
1398 | if (fontDirection > 0) {
|
1399 | ctx.scale(textHScale, -1);
|
1400 | } else {
|
1401 | ctx.scale(textHScale, 1);
|
1402 | }
|
1403 |
|
1404 | var lineWidth = current.lineWidth;
|
1405 | var scale = current.textMatrixScale;
|
1406 |
|
1407 | if (scale === 0 || lineWidth === 0) {
|
1408 | var fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
|
1409 |
|
1410 | if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
|
1411 | this._cachedGetSinglePixelWidth = null;
|
1412 | lineWidth = this.getSinglePixelWidth() * MIN_WIDTH_FACTOR;
|
1413 | }
|
1414 | } else {
|
1415 | lineWidth /= scale;
|
1416 | }
|
1417 |
|
1418 | if (fontSizeScale !== 1.0) {
|
1419 | ctx.scale(fontSizeScale, fontSizeScale);
|
1420 | lineWidth /= fontSizeScale;
|
1421 | }
|
1422 |
|
1423 | ctx.lineWidth = lineWidth;
|
1424 | var x = 0,
|
1425 | i;
|
1426 |
|
1427 | for (i = 0; i < glyphsLength; ++i) {
|
1428 | var glyph = glyphs[i];
|
1429 |
|
1430 | if ((0, _util.isNum)(glyph)) {
|
1431 | x += spacingDir * glyph * fontSize / 1000;
|
1432 | continue;
|
1433 | }
|
1434 |
|
1435 | var restoreNeeded = false;
|
1436 | var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
|
1437 | var character = glyph.fontChar;
|
1438 | var accent = glyph.accent;
|
1439 | var scaledX, scaledY, scaledAccentX, scaledAccentY;
|
1440 | var width = glyph.width;
|
1441 |
|
1442 | if (vertical) {
|
1443 | var vmetric, vx, vy;
|
1444 | vmetric = glyph.vmetric || defaultVMetrics;
|
1445 | vx = glyph.vmetric ? vmetric[1] : width * 0.5;
|
1446 | vx = -vx * widthAdvanceScale;
|
1447 | vy = vmetric[2] * widthAdvanceScale;
|
1448 | width = vmetric ? -vmetric[0] : width;
|
1449 | scaledX = vx / fontSizeScale;
|
1450 | scaledY = (x + vy) / fontSizeScale;
|
1451 | } else {
|
1452 | scaledX = x / fontSizeScale;
|
1453 | scaledY = 0;
|
1454 | }
|
1455 |
|
1456 | if (font.remeasure && width > 0) {
|
1457 | var measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
|
1458 |
|
1459 | if (width < measuredWidth && this.isFontSubpixelAAEnabled) {
|
1460 | var characterScaleX = width / measuredWidth;
|
1461 | restoreNeeded = true;
|
1462 | ctx.save();
|
1463 | ctx.scale(characterScaleX, 1);
|
1464 | scaledX /= characterScaleX;
|
1465 | } else if (width !== measuredWidth) {
|
1466 | scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
|
1467 | }
|
1468 | }
|
1469 |
|
1470 | if (glyph.isInFont || font.missingFile) {
|
1471 | if (simpleFillText && !accent) {
|
1472 | ctx.fillText(character, scaledX, scaledY);
|
1473 | } else {
|
1474 | this.paintChar(character, scaledX, scaledY, patternTransform);
|
1475 |
|
1476 | if (accent) {
|
1477 | scaledAccentX = scaledX + accent.offset.x / fontSizeScale;
|
1478 | scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
|
1479 | this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY, patternTransform);
|
1480 | }
|
1481 | }
|
1482 | }
|
1483 |
|
1484 | var charWidth = width * widthAdvanceScale + spacing * fontDirection;
|
1485 | x += charWidth;
|
1486 |
|
1487 | if (restoreNeeded) {
|
1488 | ctx.restore();
|
1489 | }
|
1490 | }
|
1491 |
|
1492 | if (vertical) {
|
1493 | current.y -= x * textHScale;
|
1494 | } else {
|
1495 | current.x += x * textHScale;
|
1496 | }
|
1497 |
|
1498 | ctx.restore();
|
1499 | },
|
1500 | showType3Text: function CanvasGraphics_showType3Text(glyphs) {
|
1501 | var ctx = this.ctx;
|
1502 | var current = this.current;
|
1503 | var font = current.font;
|
1504 | var fontSize = current.fontSize;
|
1505 | var fontDirection = current.fontDirection;
|
1506 | var spacingDir = font.vertical ? 1 : -1;
|
1507 | var charSpacing = current.charSpacing;
|
1508 | var wordSpacing = current.wordSpacing;
|
1509 | var textHScale = current.textHScale * fontDirection;
|
1510 | var fontMatrix = current.fontMatrix || _util.FONT_IDENTITY_MATRIX;
|
1511 | var glyphsLength = glyphs.length;
|
1512 | var isTextInvisible = current.textRenderingMode === _util.TextRenderingMode.INVISIBLE;
|
1513 | var i, glyph, width, spacingLength;
|
1514 |
|
1515 | if (isTextInvisible || fontSize === 0) {
|
1516 | return;
|
1517 | }
|
1518 |
|
1519 | this._cachedGetSinglePixelWidth = null;
|
1520 | ctx.save();
|
1521 | ctx.transform.apply(ctx, current.textMatrix);
|
1522 | ctx.translate(current.x, current.y);
|
1523 | ctx.scale(textHScale, fontDirection);
|
1524 |
|
1525 | for (i = 0; i < glyphsLength; ++i) {
|
1526 | glyph = glyphs[i];
|
1527 |
|
1528 | if ((0, _util.isNum)(glyph)) {
|
1529 | spacingLength = spacingDir * glyph * fontSize / 1000;
|
1530 | this.ctx.translate(spacingLength, 0);
|
1531 | current.x += spacingLength * textHScale;
|
1532 | continue;
|
1533 | }
|
1534 |
|
1535 | var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
|
1536 | var operatorList = font.charProcOperatorList[glyph.operatorListId];
|
1537 |
|
1538 | if (!operatorList) {
|
1539 | (0, _util.warn)("Type3 character \"".concat(glyph.operatorListId, "\" is not available."));
|
1540 | continue;
|
1541 | }
|
1542 |
|
1543 | this.processingType3 = glyph;
|
1544 | this.save();
|
1545 | ctx.scale(fontSize, fontSize);
|
1546 | ctx.transform.apply(ctx, fontMatrix);
|
1547 | this.executeOperatorList(operatorList);
|
1548 | this.restore();
|
1549 |
|
1550 | var transformed = _util.Util.applyTransform([glyph.width, 0], fontMatrix);
|
1551 |
|
1552 | width = transformed[0] * fontSize + spacing;
|
1553 | ctx.translate(width, 0);
|
1554 | current.x += width * textHScale;
|
1555 | }
|
1556 |
|
1557 | ctx.restore();
|
1558 | this.processingType3 = null;
|
1559 | },
|
1560 | setCharWidth: function CanvasGraphics_setCharWidth(xWidth, yWidth) {},
|
1561 | setCharWidthAndBounds: function CanvasGraphics_setCharWidthAndBounds(xWidth, yWidth, llx, lly, urx, ury) {
|
1562 | this.ctx.rect(llx, lly, urx - llx, ury - lly);
|
1563 | this.clip();
|
1564 | this.endPath();
|
1565 | },
|
1566 | getColorN_Pattern: function CanvasGraphics_getColorN_Pattern(IR) {
|
1567 | var _this = this;
|
1568 |
|
1569 | var pattern;
|
1570 |
|
1571 | if (IR[0] === 'TilingPattern') {
|
1572 | var color = IR[1];
|
1573 | var baseTransform = this.baseTransform || this.ctx.mozCurrentTransform.slice();
|
1574 | var canvasGraphicsFactory = {
|
1575 | createCanvasGraphics: function createCanvasGraphics(ctx) {
|
1576 | return new CanvasGraphics(ctx, _this.commonObjs, _this.objs, _this.canvasFactory, _this.webGLContext);
|
1577 | }
|
1578 | };
|
1579 | pattern = new _pattern_helper.TilingPattern(IR, color, this.ctx, canvasGraphicsFactory, baseTransform);
|
1580 | } else {
|
1581 | pattern = (0, _pattern_helper.getShadingPatternFromIR)(IR);
|
1582 | }
|
1583 |
|
1584 | return pattern;
|
1585 | },
|
1586 | setStrokeColorN: function CanvasGraphics_setStrokeColorN() {
|
1587 | this.current.strokeColor = this.getColorN_Pattern(arguments);
|
1588 | },
|
1589 | setFillColorN: function CanvasGraphics_setFillColorN() {
|
1590 | this.current.fillColor = this.getColorN_Pattern(arguments);
|
1591 | this.current.patternFill = true;
|
1592 | },
|
1593 | setStrokeRGBColor: function CanvasGraphics_setStrokeRGBColor(r, g, b) {
|
1594 | var color = _util.Util.makeCssRgb(r, g, b);
|
1595 |
|
1596 | this.ctx.strokeStyle = color;
|
1597 | this.current.strokeColor = color;
|
1598 | },
|
1599 | setFillRGBColor: function CanvasGraphics_setFillRGBColor(r, g, b) {
|
1600 | var color = _util.Util.makeCssRgb(r, g, b);
|
1601 |
|
1602 | this.ctx.fillStyle = color;
|
1603 | this.current.fillColor = color;
|
1604 | this.current.patternFill = false;
|
1605 | },
|
1606 | shadingFill: function CanvasGraphics_shadingFill(patternIR) {
|
1607 | var ctx = this.ctx;
|
1608 | this.save();
|
1609 | var pattern = (0, _pattern_helper.getShadingPatternFromIR)(patternIR);
|
1610 | ctx.fillStyle = pattern.getPattern(ctx, this, true);
|
1611 | var inv = ctx.mozCurrentTransformInverse;
|
1612 |
|
1613 | if (inv) {
|
1614 | var canvas = ctx.canvas;
|
1615 | var width = canvas.width;
|
1616 | var height = canvas.height;
|
1617 |
|
1618 | var bl = _util.Util.applyTransform([0, 0], inv);
|
1619 |
|
1620 | var br = _util.Util.applyTransform([0, height], inv);
|
1621 |
|
1622 | var ul = _util.Util.applyTransform([width, 0], inv);
|
1623 |
|
1624 | var ur = _util.Util.applyTransform([width, height], inv);
|
1625 |
|
1626 | var x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
|
1627 | var y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
|
1628 | var x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
|
1629 | var y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
|
1630 | this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
|
1631 | } else {
|
1632 | this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
|
1633 | }
|
1634 |
|
1635 | this.restore();
|
1636 | },
|
1637 | beginInlineImage: function CanvasGraphics_beginInlineImage() {
|
1638 | (0, _util.unreachable)('Should not call beginInlineImage');
|
1639 | },
|
1640 | beginImageData: function CanvasGraphics_beginImageData() {
|
1641 | (0, _util.unreachable)('Should not call beginImageData');
|
1642 | },
|
1643 | paintFormXObjectBegin: function CanvasGraphics_paintFormXObjectBegin(matrix, bbox) {
|
1644 | this.save();
|
1645 | this.baseTransformStack.push(this.baseTransform);
|
1646 |
|
1647 | if (Array.isArray(matrix) && matrix.length === 6) {
|
1648 | this.transform.apply(this, matrix);
|
1649 | }
|
1650 |
|
1651 | this.baseTransform = this.ctx.mozCurrentTransform;
|
1652 |
|
1653 | if (bbox) {
|
1654 | var width = bbox[2] - bbox[0];
|
1655 | var height = bbox[3] - bbox[1];
|
1656 | this.ctx.rect(bbox[0], bbox[1], width, height);
|
1657 | this.clip();
|
1658 | this.endPath();
|
1659 | }
|
1660 | },
|
1661 | paintFormXObjectEnd: function CanvasGraphics_paintFormXObjectEnd() {
|
1662 | this.restore();
|
1663 | this.baseTransform = this.baseTransformStack.pop();
|
1664 | },
|
1665 | beginGroup: function CanvasGraphics_beginGroup(group) {
|
1666 | this.save();
|
1667 | var currentCtx = this.ctx;
|
1668 |
|
1669 | if (!group.isolated) {
|
1670 | (0, _util.info)('TODO: Support non-isolated groups.');
|
1671 | }
|
1672 |
|
1673 | if (group.knockout) {
|
1674 | (0, _util.warn)('Knockout groups not supported.');
|
1675 | }
|
1676 |
|
1677 | var currentTransform = currentCtx.mozCurrentTransform;
|
1678 |
|
1679 | if (group.matrix) {
|
1680 | currentCtx.transform.apply(currentCtx, group.matrix);
|
1681 | }
|
1682 |
|
1683 | if (!group.bbox) {
|
1684 | throw new Error('Bounding box is required.');
|
1685 | }
|
1686 |
|
1687 | var bounds = _util.Util.getAxialAlignedBoundingBox(group.bbox, currentCtx.mozCurrentTransform);
|
1688 |
|
1689 | var canvasBounds = [0, 0, currentCtx.canvas.width, currentCtx.canvas.height];
|
1690 | bounds = _util.Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
|
1691 | var offsetX = Math.floor(bounds[0]);
|
1692 | var offsetY = Math.floor(bounds[1]);
|
1693 | var drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
|
1694 | var drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
|
1695 | var scaleX = 1,
|
1696 | scaleY = 1;
|
1697 |
|
1698 | if (drawnWidth > MAX_GROUP_SIZE) {
|
1699 | scaleX = drawnWidth / MAX_GROUP_SIZE;
|
1700 | drawnWidth = MAX_GROUP_SIZE;
|
1701 | }
|
1702 |
|
1703 | if (drawnHeight > MAX_GROUP_SIZE) {
|
1704 | scaleY = drawnHeight / MAX_GROUP_SIZE;
|
1705 | drawnHeight = MAX_GROUP_SIZE;
|
1706 | }
|
1707 |
|
1708 | var cacheId = 'groupAt' + this.groupLevel;
|
1709 |
|
1710 | if (group.smask) {
|
1711 | cacheId += '_smask_' + this.smaskCounter++ % 2;
|
1712 | }
|
1713 |
|
1714 | var scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
|
1715 | var groupCtx = scratchCanvas.context;
|
1716 | groupCtx.scale(1 / scaleX, 1 / scaleY);
|
1717 | groupCtx.translate(-offsetX, -offsetY);
|
1718 | groupCtx.transform.apply(groupCtx, currentTransform);
|
1719 |
|
1720 | if (group.smask) {
|
1721 | this.smaskStack.push({
|
1722 | canvas: scratchCanvas.canvas,
|
1723 | context: groupCtx,
|
1724 | offsetX: offsetX,
|
1725 | offsetY: offsetY,
|
1726 | scaleX: scaleX,
|
1727 | scaleY: scaleY,
|
1728 | subtype: group.smask.subtype,
|
1729 | backdrop: group.smask.backdrop,
|
1730 | transferMap: group.smask.transferMap || null,
|
1731 | startTransformInverse: null
|
1732 | });
|
1733 | } else {
|
1734 | currentCtx.setTransform(1, 0, 0, 1, 0, 0);
|
1735 | currentCtx.translate(offsetX, offsetY);
|
1736 | currentCtx.scale(scaleX, scaleY);
|
1737 | }
|
1738 |
|
1739 | copyCtxState(currentCtx, groupCtx);
|
1740 | this.ctx = groupCtx;
|
1741 | this.setGState([['BM', 'source-over'], ['ca', 1], ['CA', 1]]);
|
1742 | this.groupStack.push(currentCtx);
|
1743 | this.groupLevel++;
|
1744 | this.current.activeSMask = null;
|
1745 | },
|
1746 | endGroup: function CanvasGraphics_endGroup(group) {
|
1747 | this.groupLevel--;
|
1748 | var groupCtx = this.ctx;
|
1749 | this.ctx = this.groupStack.pop();
|
1750 |
|
1751 | if (this.ctx.imageSmoothingEnabled !== undefined) {
|
1752 | this.ctx.imageSmoothingEnabled = false;
|
1753 | } else {
|
1754 | this.ctx.mozImageSmoothingEnabled = false;
|
1755 | }
|
1756 |
|
1757 | if (group.smask) {
|
1758 | this.tempSMask = this.smaskStack.pop();
|
1759 | } else {
|
1760 | this.ctx.drawImage(groupCtx.canvas, 0, 0);
|
1761 | }
|
1762 |
|
1763 | this.restore();
|
1764 | },
|
1765 | beginAnnotations: function CanvasGraphics_beginAnnotations() {
|
1766 | this.save();
|
1767 |
|
1768 | if (this.baseTransform) {
|
1769 | this.ctx.setTransform.apply(this.ctx, this.baseTransform);
|
1770 | }
|
1771 | },
|
1772 | endAnnotations: function CanvasGraphics_endAnnotations() {
|
1773 | this.restore();
|
1774 | },
|
1775 | beginAnnotation: function CanvasGraphics_beginAnnotation(rect, transform, matrix) {
|
1776 | this.save();
|
1777 | resetCtxToDefault(this.ctx);
|
1778 | this.current = new CanvasExtraState();
|
1779 |
|
1780 | if (Array.isArray(rect) && rect.length === 4) {
|
1781 | var width = rect[2] - rect[0];
|
1782 | var height = rect[3] - rect[1];
|
1783 | this.ctx.rect(rect[0], rect[1], width, height);
|
1784 | this.clip();
|
1785 | this.endPath();
|
1786 | }
|
1787 |
|
1788 | this.transform.apply(this, transform);
|
1789 | this.transform.apply(this, matrix);
|
1790 | },
|
1791 | endAnnotation: function CanvasGraphics_endAnnotation() {
|
1792 | this.restore();
|
1793 | },
|
1794 | paintJpegXObject: function CanvasGraphics_paintJpegXObject(objId, w, h) {
|
1795 | var domImage = this.processingType3 ? this.commonObjs.get(objId) : this.objs.get(objId);
|
1796 |
|
1797 | if (!domImage) {
|
1798 | (0, _util.warn)('Dependent image isn\'t ready yet');
|
1799 | return;
|
1800 | }
|
1801 |
|
1802 | this.save();
|
1803 | var ctx = this.ctx;
|
1804 | ctx.scale(1 / w, -1 / h);
|
1805 | ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height, 0, -h, w, h);
|
1806 |
|
1807 | if (this.imageLayer) {
|
1808 | var currentTransform = ctx.mozCurrentTransformInverse;
|
1809 | var position = this.getCanvasPosition(0, 0);
|
1810 | this.imageLayer.appendImage({
|
1811 | objId: objId,
|
1812 | left: position[0],
|
1813 | top: position[1],
|
1814 | width: w / currentTransform[0],
|
1815 | height: h / currentTransform[3]
|
1816 | });
|
1817 | }
|
1818 |
|
1819 | this.restore();
|
1820 | },
|
1821 | paintImageMaskXObject: function CanvasGraphics_paintImageMaskXObject(img) {
|
1822 | var ctx = this.ctx;
|
1823 | var width = img.width,
|
1824 | height = img.height;
|
1825 | var fillColor = this.current.fillColor;
|
1826 | var isPatternFill = this.current.patternFill;
|
1827 | var glyph = this.processingType3;
|
1828 |
|
1829 | if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) {
|
1830 | if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) {
|
1831 | glyph.compiled = compileType3Glyph({
|
1832 | data: img.data,
|
1833 | width: width,
|
1834 | height: height
|
1835 | });
|
1836 | } else {
|
1837 | glyph.compiled = null;
|
1838 | }
|
1839 | }
|
1840 |
|
1841 | if (glyph && glyph.compiled) {
|
1842 | glyph.compiled(ctx);
|
1843 | return;
|
1844 | }
|
1845 |
|
1846 | var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas', width, height);
|
1847 | var maskCtx = maskCanvas.context;
|
1848 | maskCtx.save();
|
1849 | putBinaryImageMask(maskCtx, img);
|
1850 | maskCtx.globalCompositeOperation = 'source-in';
|
1851 | maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this) : fillColor;
|
1852 | maskCtx.fillRect(0, 0, width, height);
|
1853 | maskCtx.restore();
|
1854 | this.paintInlineImageXObject(maskCanvas.canvas);
|
1855 | },
|
1856 | paintImageMaskXObjectRepeat: function CanvasGraphics_paintImageMaskXObjectRepeat(imgData, scaleX, scaleY, positions) {
|
1857 | var width = imgData.width;
|
1858 | var height = imgData.height;
|
1859 | var fillColor = this.current.fillColor;
|
1860 | var isPatternFill = this.current.patternFill;
|
1861 | var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas', width, height);
|
1862 | var maskCtx = maskCanvas.context;
|
1863 | maskCtx.save();
|
1864 | putBinaryImageMask(maskCtx, imgData);
|
1865 | maskCtx.globalCompositeOperation = 'source-in';
|
1866 | maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this) : fillColor;
|
1867 | maskCtx.fillRect(0, 0, width, height);
|
1868 | maskCtx.restore();
|
1869 | var ctx = this.ctx;
|
1870 |
|
1871 | for (var i = 0, ii = positions.length; i < ii; i += 2) {
|
1872 | ctx.save();
|
1873 | ctx.transform(scaleX, 0, 0, scaleY, positions[i], positions[i + 1]);
|
1874 | ctx.scale(1, -1);
|
1875 | ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
|
1876 | ctx.restore();
|
1877 | }
|
1878 | },
|
1879 | paintImageMaskXObjectGroup: function CanvasGraphics_paintImageMaskXObjectGroup(images) {
|
1880 | var ctx = this.ctx;
|
1881 | var fillColor = this.current.fillColor;
|
1882 | var isPatternFill = this.current.patternFill;
|
1883 |
|
1884 | for (var i = 0, ii = images.length; i < ii; i++) {
|
1885 | var image = images[i];
|
1886 | var width = image.width,
|
1887 | height = image.height;
|
1888 | var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas', width, height);
|
1889 | var maskCtx = maskCanvas.context;
|
1890 | maskCtx.save();
|
1891 | putBinaryImageMask(maskCtx, image);
|
1892 | maskCtx.globalCompositeOperation = 'source-in';
|
1893 | maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this) : fillColor;
|
1894 | maskCtx.fillRect(0, 0, width, height);
|
1895 | maskCtx.restore();
|
1896 | ctx.save();
|
1897 | ctx.transform.apply(ctx, image.transform);
|
1898 | ctx.scale(1, -1);
|
1899 | ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
|
1900 | ctx.restore();
|
1901 | }
|
1902 | },
|
1903 | paintImageXObject: function CanvasGraphics_paintImageXObject(objId) {
|
1904 | var imgData = this.processingType3 ? this.commonObjs.get(objId) : this.objs.get(objId);
|
1905 |
|
1906 | if (!imgData) {
|
1907 | (0, _util.warn)('Dependent image isn\'t ready yet');
|
1908 | return;
|
1909 | }
|
1910 |
|
1911 | this.paintInlineImageXObject(imgData);
|
1912 | },
|
1913 | paintImageXObjectRepeat: function CanvasGraphics_paintImageXObjectRepeat(objId, scaleX, scaleY, positions) {
|
1914 | var imgData = this.processingType3 ? this.commonObjs.get(objId) : this.objs.get(objId);
|
1915 |
|
1916 | if (!imgData) {
|
1917 | (0, _util.warn)('Dependent image isn\'t ready yet');
|
1918 | return;
|
1919 | }
|
1920 |
|
1921 | var width = imgData.width;
|
1922 | var height = imgData.height;
|
1923 | var map = [];
|
1924 |
|
1925 | for (var i = 0, ii = positions.length; i < ii; i += 2) {
|
1926 | map.push({
|
1927 | transform: [scaleX, 0, 0, scaleY, positions[i], positions[i + 1]],
|
1928 | x: 0,
|
1929 | y: 0,
|
1930 | w: width,
|
1931 | h: height
|
1932 | });
|
1933 | }
|
1934 |
|
1935 | this.paintInlineImageXObjectGroup(imgData, map);
|
1936 | },
|
1937 | paintInlineImageXObject: function CanvasGraphics_paintInlineImageXObject(imgData) {
|
1938 | var width = imgData.width;
|
1939 | var height = imgData.height;
|
1940 | var ctx = this.ctx;
|
1941 | this.save();
|
1942 | ctx.scale(1 / width, -1 / height);
|
1943 | var currentTransform = ctx.mozCurrentTransformInverse;
|
1944 | var a = currentTransform[0],
|
1945 | b = currentTransform[1];
|
1946 | var widthScale = Math.max(Math.sqrt(a * a + b * b), 1);
|
1947 | var c = currentTransform[2],
|
1948 | d = currentTransform[3];
|
1949 | var heightScale = Math.max(Math.sqrt(c * c + d * d), 1);
|
1950 | var imgToPaint, tmpCanvas;
|
1951 |
|
1952 | if (typeof HTMLElement === 'function' && imgData instanceof HTMLElement || !imgData.data) {
|
1953 | imgToPaint = imgData;
|
1954 | } else {
|
1955 | tmpCanvas = this.cachedCanvases.getCanvas('inlineImage', width, height);
|
1956 | var tmpCtx = tmpCanvas.context;
|
1957 | putBinaryImageData(tmpCtx, imgData);
|
1958 | imgToPaint = tmpCanvas.canvas;
|
1959 | }
|
1960 |
|
1961 | var paintWidth = width,
|
1962 | paintHeight = height;
|
1963 | var tmpCanvasId = 'prescale1';
|
1964 |
|
1965 | while (widthScale > 2 && paintWidth > 1 || heightScale > 2 && paintHeight > 1) {
|
1966 | var newWidth = paintWidth,
|
1967 | newHeight = paintHeight;
|
1968 |
|
1969 | if (widthScale > 2 && paintWidth > 1) {
|
1970 | newWidth = Math.ceil(paintWidth / 2);
|
1971 | widthScale /= paintWidth / newWidth;
|
1972 | }
|
1973 |
|
1974 | if (heightScale > 2 && paintHeight > 1) {
|
1975 | newHeight = Math.ceil(paintHeight / 2);
|
1976 | heightScale /= paintHeight / newHeight;
|
1977 | }
|
1978 |
|
1979 | tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
|
1980 | tmpCtx = tmpCanvas.context;
|
1981 | tmpCtx.clearRect(0, 0, newWidth, newHeight);
|
1982 | tmpCtx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight, 0, 0, newWidth, newHeight);
|
1983 | imgToPaint = tmpCanvas.canvas;
|
1984 | paintWidth = newWidth;
|
1985 | paintHeight = newHeight;
|
1986 | tmpCanvasId = tmpCanvasId === 'prescale1' ? 'prescale2' : 'prescale1';
|
1987 | }
|
1988 |
|
1989 | ctx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight, 0, -height, width, height);
|
1990 |
|
1991 | if (this.imageLayer) {
|
1992 | var position = this.getCanvasPosition(0, -height);
|
1993 | this.imageLayer.appendImage({
|
1994 | imgData: imgData,
|
1995 | left: position[0],
|
1996 | top: position[1],
|
1997 | width: width / currentTransform[0],
|
1998 | height: height / currentTransform[3]
|
1999 | });
|
2000 | }
|
2001 |
|
2002 | this.restore();
|
2003 | },
|
2004 | paintInlineImageXObjectGroup: function CanvasGraphics_paintInlineImageXObjectGroup(imgData, map) {
|
2005 | var ctx = this.ctx;
|
2006 | var w = imgData.width;
|
2007 | var h = imgData.height;
|
2008 | var tmpCanvas = this.cachedCanvases.getCanvas('inlineImage', w, h);
|
2009 | var tmpCtx = tmpCanvas.context;
|
2010 | putBinaryImageData(tmpCtx, imgData);
|
2011 |
|
2012 | for (var i = 0, ii = map.length; i < ii; i++) {
|
2013 | var entry = map[i];
|
2014 | ctx.save();
|
2015 | ctx.transform.apply(ctx, entry.transform);
|
2016 | ctx.scale(1, -1);
|
2017 | ctx.drawImage(tmpCanvas.canvas, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1);
|
2018 |
|
2019 | if (this.imageLayer) {
|
2020 | var position = this.getCanvasPosition(entry.x, entry.y);
|
2021 | this.imageLayer.appendImage({
|
2022 | imgData: imgData,
|
2023 | left: position[0],
|
2024 | top: position[1],
|
2025 | width: w,
|
2026 | height: h
|
2027 | });
|
2028 | }
|
2029 |
|
2030 | ctx.restore();
|
2031 | }
|
2032 | },
|
2033 | paintSolidColorImageMask: function CanvasGraphics_paintSolidColorImageMask() {
|
2034 | this.ctx.fillRect(0, 0, 1, 1);
|
2035 | },
|
2036 | paintXObject: function CanvasGraphics_paintXObject() {
|
2037 | (0, _util.warn)('Unsupported \'paintXObject\' command.');
|
2038 | },
|
2039 | markPoint: function CanvasGraphics_markPoint(tag) {},
|
2040 | markPointProps: function CanvasGraphics_markPointProps(tag, properties) {},
|
2041 | beginMarkedContent: function CanvasGraphics_beginMarkedContent(tag) {},
|
2042 | beginMarkedContentProps: function CanvasGraphics_beginMarkedContentProps(tag, properties) {},
|
2043 | endMarkedContent: function CanvasGraphics_endMarkedContent() {},
|
2044 | beginCompat: function CanvasGraphics_beginCompat() {},
|
2045 | endCompat: function CanvasGraphics_endCompat() {},
|
2046 | consumePath: function CanvasGraphics_consumePath() {
|
2047 | var ctx = this.ctx;
|
2048 |
|
2049 | if (this.pendingClip) {
|
2050 | if (this.pendingClip === EO_CLIP) {
|
2051 | ctx.clip('evenodd');
|
2052 | } else {
|
2053 | ctx.clip();
|
2054 | }
|
2055 |
|
2056 | this.pendingClip = null;
|
2057 | }
|
2058 |
|
2059 | ctx.beginPath();
|
2060 | },
|
2061 | getSinglePixelWidth: function getSinglePixelWidth(scale) {
|
2062 | if (this._cachedGetSinglePixelWidth === null) {
|
2063 | var inverse = this.ctx.mozCurrentTransformInverse;
|
2064 | this._cachedGetSinglePixelWidth = Math.sqrt(Math.max(inverse[0] * inverse[0] + inverse[1] * inverse[1], inverse[2] * inverse[2] + inverse[3] * inverse[3]));
|
2065 | }
|
2066 |
|
2067 | return this._cachedGetSinglePixelWidth;
|
2068 | },
|
2069 | getCanvasPosition: function CanvasGraphics_getCanvasPosition(x, y) {
|
2070 | var transform = this.ctx.mozCurrentTransform;
|
2071 | return [transform[0] * x + transform[2] * y + transform[4], transform[1] * x + transform[3] * y + transform[5]];
|
2072 | }
|
2073 | };
|
2074 |
|
2075 | for (var op in _util.OPS) {
|
2076 | CanvasGraphics.prototype[_util.OPS[op]] = CanvasGraphics.prototype[op];
|
2077 | }
|
2078 |
|
2079 | return CanvasGraphics;
|
2080 | }();
|
2081 |
|
2082 | exports.CanvasGraphics = CanvasGraphics; |
\ | No newline at end of file |