2
0

canvas.js 61 KB

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