2
0

canvas.js 63 KB

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