canvas.js 61 KB

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