2
0

canvas.js 64 KB

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