canvas.js 57 KB

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