2
0

canvas.js 63 KB

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