canvas.js 62 KB

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