PageRenderTime 105ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/display/pattern_helper.js

https://gitlab.com/itesoft/pdfjs.dist
JavaScript | 397 lines | 373 code | 3 blank | 21 comment | 31 complexity | 13086a18a80ef9db9b422e3eb696197b MD5 | raw file
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.TilingPattern = exports.getShadingPatternFromIR = undefined;
  27. var _util = require('../shared/util');
  28. var ShadingIRs = {};
  29. ShadingIRs.RadialAxial = {
  30. fromIR: function RadialAxial_fromIR(raw) {
  31. var type = raw[1];
  32. var colorStops = raw[2];
  33. var p0 = raw[3];
  34. var p1 = raw[4];
  35. var r0 = raw[5];
  36. var r1 = raw[6];
  37. return {
  38. type: 'Pattern',
  39. getPattern: function RadialAxial_getPattern(ctx) {
  40. var grad;
  41. if (type === 'axial') {
  42. grad = ctx.createLinearGradient(p0[0], p0[1], p1[0], p1[1]);
  43. } else if (type === 'radial') {
  44. grad = ctx.createRadialGradient(p0[0], p0[1], r0, p1[0], p1[1], r1);
  45. }
  46. for (var i = 0, ii = colorStops.length; i < ii; ++i) {
  47. var c = colorStops[i];
  48. grad.addColorStop(c[0], c[1]);
  49. }
  50. return grad;
  51. }
  52. };
  53. }
  54. };
  55. var createMeshCanvas = function createMeshCanvasClosure() {
  56. function drawTriangle(data, context, p1, p2, p3, c1, c2, c3) {
  57. var coords = context.coords,
  58. colors = context.colors;
  59. var bytes = data.data,
  60. rowSize = data.width * 4;
  61. var tmp;
  62. if (coords[p1 + 1] > coords[p2 + 1]) {
  63. tmp = p1;
  64. p1 = p2;
  65. p2 = tmp;
  66. tmp = c1;
  67. c1 = c2;
  68. c2 = tmp;
  69. }
  70. if (coords[p2 + 1] > coords[p3 + 1]) {
  71. tmp = p2;
  72. p2 = p3;
  73. p3 = tmp;
  74. tmp = c2;
  75. c2 = c3;
  76. c3 = tmp;
  77. }
  78. if (coords[p1 + 1] > coords[p2 + 1]) {
  79. tmp = p1;
  80. p1 = p2;
  81. p2 = tmp;
  82. tmp = c1;
  83. c1 = c2;
  84. c2 = tmp;
  85. }
  86. var x1 = (coords[p1] + context.offsetX) * context.scaleX;
  87. var y1 = (coords[p1 + 1] + context.offsetY) * context.scaleY;
  88. var x2 = (coords[p2] + context.offsetX) * context.scaleX;
  89. var y2 = (coords[p2 + 1] + context.offsetY) * context.scaleY;
  90. var x3 = (coords[p3] + context.offsetX) * context.scaleX;
  91. var y3 = (coords[p3 + 1] + context.offsetY) * context.scaleY;
  92. if (y1 >= y3) {
  93. return;
  94. }
  95. var c1r = colors[c1],
  96. c1g = colors[c1 + 1],
  97. c1b = colors[c1 + 2];
  98. var c2r = colors[c2],
  99. c2g = colors[c2 + 1],
  100. c2b = colors[c2 + 2];
  101. var c3r = colors[c3],
  102. c3g = colors[c3 + 1],
  103. c3b = colors[c3 + 2];
  104. var minY = Math.round(y1),
  105. maxY = Math.round(y3);
  106. var xa, car, cag, cab;
  107. var xb, cbr, cbg, cbb;
  108. var k;
  109. for (var y = minY; y <= maxY; y++) {
  110. if (y < y2) {
  111. k = y < y1 ? 0 : y1 === y2 ? 1 : (y1 - y) / (y1 - y2);
  112. xa = x1 - (x1 - x2) * k;
  113. car = c1r - (c1r - c2r) * k;
  114. cag = c1g - (c1g - c2g) * k;
  115. cab = c1b - (c1b - c2b) * k;
  116. } else {
  117. k = y > y3 ? 1 : y2 === y3 ? 0 : (y2 - y) / (y2 - y3);
  118. xa = x2 - (x2 - x3) * k;
  119. car = c2r - (c2r - c3r) * k;
  120. cag = c2g - (c2g - c3g) * k;
  121. cab = c2b - (c2b - c3b) * k;
  122. }
  123. k = y < y1 ? 0 : y > y3 ? 1 : (y1 - y) / (y1 - y3);
  124. xb = x1 - (x1 - x3) * k;
  125. cbr = c1r - (c1r - c3r) * k;
  126. cbg = c1g - (c1g - c3g) * k;
  127. cbb = c1b - (c1b - c3b) * k;
  128. var x1_ = Math.round(Math.min(xa, xb));
  129. var x2_ = Math.round(Math.max(xa, xb));
  130. var j = rowSize * y + x1_ * 4;
  131. for (var x = x1_; x <= x2_; x++) {
  132. k = (xa - x) / (xa - xb);
  133. k = k < 0 ? 0 : k > 1 ? 1 : k;
  134. bytes[j++] = car - (car - cbr) * k | 0;
  135. bytes[j++] = cag - (cag - cbg) * k | 0;
  136. bytes[j++] = cab - (cab - cbb) * k | 0;
  137. bytes[j++] = 255;
  138. }
  139. }
  140. }
  141. function drawFigure(data, figure, context) {
  142. var ps = figure.coords;
  143. var cs = figure.colors;
  144. var i, ii;
  145. switch (figure.type) {
  146. case 'lattice':
  147. var verticesPerRow = figure.verticesPerRow;
  148. var rows = Math.floor(ps.length / verticesPerRow) - 1;
  149. var cols = verticesPerRow - 1;
  150. for (i = 0; i < rows; i++) {
  151. var q = i * verticesPerRow;
  152. for (var j = 0; j < cols; j++, q++) {
  153. drawTriangle(data, context, ps[q], ps[q + 1], ps[q + verticesPerRow], cs[q], cs[q + 1], cs[q + verticesPerRow]);
  154. drawTriangle(data, context, ps[q + verticesPerRow + 1], ps[q + 1], ps[q + verticesPerRow], cs[q + verticesPerRow + 1], cs[q + 1], cs[q + verticesPerRow]);
  155. }
  156. }
  157. break;
  158. case 'triangles':
  159. for (i = 0, ii = ps.length; i < ii; i += 3) {
  160. drawTriangle(data, context, ps[i], ps[i + 1], ps[i + 2], cs[i], cs[i + 1], cs[i + 2]);
  161. }
  162. break;
  163. default:
  164. throw new Error('illegal figure');
  165. }
  166. }
  167. function createMeshCanvas(bounds, combinesScale, coords, colors, figures, backgroundColor, cachedCanvases, webGLContext) {
  168. var EXPECTED_SCALE = 1.1;
  169. var MAX_PATTERN_SIZE = 3000;
  170. var BORDER_SIZE = 2;
  171. var offsetX = Math.floor(bounds[0]);
  172. var offsetY = Math.floor(bounds[1]);
  173. var boundsWidth = Math.ceil(bounds[2]) - offsetX;
  174. var boundsHeight = Math.ceil(bounds[3]) - offsetY;
  175. var width = Math.min(Math.ceil(Math.abs(boundsWidth * combinesScale[0] * EXPECTED_SCALE)), MAX_PATTERN_SIZE);
  176. var height = Math.min(Math.ceil(Math.abs(boundsHeight * combinesScale[1] * EXPECTED_SCALE)), MAX_PATTERN_SIZE);
  177. var scaleX = boundsWidth / width;
  178. var scaleY = boundsHeight / height;
  179. var context = {
  180. coords: coords,
  181. colors: colors,
  182. offsetX: -offsetX,
  183. offsetY: -offsetY,
  184. scaleX: 1 / scaleX,
  185. scaleY: 1 / scaleY
  186. };
  187. var paddedWidth = width + BORDER_SIZE * 2;
  188. var paddedHeight = height + BORDER_SIZE * 2;
  189. var canvas, tmpCanvas, i, ii;
  190. if (webGLContext.isEnabled) {
  191. canvas = webGLContext.drawFigures({
  192. width: width,
  193. height: height,
  194. backgroundColor: backgroundColor,
  195. figures: figures,
  196. context: context
  197. });
  198. tmpCanvas = cachedCanvases.getCanvas('mesh', paddedWidth, paddedHeight, false);
  199. tmpCanvas.context.drawImage(canvas, BORDER_SIZE, BORDER_SIZE);
  200. canvas = tmpCanvas.canvas;
  201. } else {
  202. tmpCanvas = cachedCanvases.getCanvas('mesh', paddedWidth, paddedHeight, false);
  203. var tmpCtx = tmpCanvas.context;
  204. var data = tmpCtx.createImageData(width, height);
  205. if (backgroundColor) {
  206. var bytes = data.data;
  207. for (i = 0, ii = bytes.length; i < ii; i += 4) {
  208. bytes[i] = backgroundColor[0];
  209. bytes[i + 1] = backgroundColor[1];
  210. bytes[i + 2] = backgroundColor[2];
  211. bytes[i + 3] = 255;
  212. }
  213. }
  214. for (i = 0; i < figures.length; i++) {
  215. drawFigure(data, figures[i], context);
  216. }
  217. tmpCtx.putImageData(data, BORDER_SIZE, BORDER_SIZE);
  218. canvas = tmpCanvas.canvas;
  219. }
  220. return {
  221. canvas: canvas,
  222. offsetX: offsetX - BORDER_SIZE * scaleX,
  223. offsetY: offsetY - BORDER_SIZE * scaleY,
  224. scaleX: scaleX,
  225. scaleY: scaleY
  226. };
  227. }
  228. return createMeshCanvas;
  229. }();
  230. ShadingIRs.Mesh = {
  231. fromIR: function Mesh_fromIR(raw) {
  232. var coords = raw[2];
  233. var colors = raw[3];
  234. var figures = raw[4];
  235. var bounds = raw[5];
  236. var matrix = raw[6];
  237. var background = raw[8];
  238. return {
  239. type: 'Pattern',
  240. getPattern: function Mesh_getPattern(ctx, owner, shadingFill) {
  241. var scale;
  242. if (shadingFill) {
  243. scale = _util.Util.singularValueDecompose2dScale(ctx.mozCurrentTransform);
  244. } else {
  245. scale = _util.Util.singularValueDecompose2dScale(owner.baseTransform);
  246. if (matrix) {
  247. var matrixScale = _util.Util.singularValueDecompose2dScale(matrix);
  248. scale = [scale[0] * matrixScale[0], scale[1] * matrixScale[1]];
  249. }
  250. }
  251. var temporaryPatternCanvas = createMeshCanvas(bounds, scale, coords, colors, figures, shadingFill ? null : background, owner.cachedCanvases, owner.webGLContext);
  252. if (!shadingFill) {
  253. ctx.setTransform.apply(ctx, owner.baseTransform);
  254. if (matrix) {
  255. ctx.transform.apply(ctx, matrix);
  256. }
  257. }
  258. ctx.translate(temporaryPatternCanvas.offsetX, temporaryPatternCanvas.offsetY);
  259. ctx.scale(temporaryPatternCanvas.scaleX, temporaryPatternCanvas.scaleY);
  260. return ctx.createPattern(temporaryPatternCanvas.canvas, 'no-repeat');
  261. }
  262. };
  263. }
  264. };
  265. ShadingIRs.Dummy = {
  266. fromIR: function Dummy_fromIR() {
  267. return {
  268. type: 'Pattern',
  269. getPattern: function Dummy_fromIR_getPattern() {
  270. return 'hotpink';
  271. }
  272. };
  273. }
  274. };
  275. function getShadingPatternFromIR(raw) {
  276. var shadingIR = ShadingIRs[raw[0]];
  277. if (!shadingIR) {
  278. throw new Error('Unknown IR type: ' + raw[0]);
  279. }
  280. return shadingIR.fromIR(raw);
  281. }
  282. var TilingPattern = function TilingPatternClosure() {
  283. var PaintType = {
  284. COLORED: 1,
  285. UNCOLORED: 2
  286. };
  287. var MAX_PATTERN_SIZE = 3000;
  288. function TilingPattern(IR, color, ctx, canvasGraphicsFactory, baseTransform) {
  289. this.operatorList = IR[2];
  290. this.matrix = IR[3] || [1, 0, 0, 1, 0, 0];
  291. this.bbox = IR[4];
  292. this.xstep = IR[5];
  293. this.ystep = IR[6];
  294. this.paintType = IR[7];
  295. this.tilingType = IR[8];
  296. this.color = color;
  297. this.canvasGraphicsFactory = canvasGraphicsFactory;
  298. this.baseTransform = baseTransform;
  299. this.type = 'Pattern';
  300. this.ctx = ctx;
  301. }
  302. TilingPattern.prototype = {
  303. createPatternCanvas: function TilinPattern_createPatternCanvas(owner) {
  304. var operatorList = this.operatorList;
  305. var bbox = this.bbox;
  306. var xstep = this.xstep;
  307. var ystep = this.ystep;
  308. var paintType = this.paintType;
  309. var tilingType = this.tilingType;
  310. var color = this.color;
  311. var canvasGraphicsFactory = this.canvasGraphicsFactory;
  312. (0, _util.info)('TilingType: ' + tilingType);
  313. var x0 = bbox[0],
  314. y0 = bbox[1],
  315. x1 = bbox[2],
  316. y1 = bbox[3];
  317. var topLeft = [x0, y0];
  318. var botRight = [x0 + xstep, y0 + ystep];
  319. var width = botRight[0] - topLeft[0];
  320. var height = botRight[1] - topLeft[1];
  321. var matrixScale = _util.Util.singularValueDecompose2dScale(this.matrix);
  322. var curMatrixScale = _util.Util.singularValueDecompose2dScale(this.baseTransform);
  323. var combinedScale = [matrixScale[0] * curMatrixScale[0], matrixScale[1] * curMatrixScale[1]];
  324. width = Math.min(Math.ceil(Math.abs(width * combinedScale[0])), MAX_PATTERN_SIZE);
  325. height = Math.min(Math.ceil(Math.abs(height * combinedScale[1])), MAX_PATTERN_SIZE);
  326. var tmpCanvas = owner.cachedCanvases.getCanvas('pattern', width, height, true);
  327. var tmpCtx = tmpCanvas.context;
  328. var graphics = canvasGraphicsFactory.createCanvasGraphics(tmpCtx);
  329. graphics.groupLevel = owner.groupLevel;
  330. this.setFillAndStrokeStyleToContext(graphics, paintType, color);
  331. this.setScale(width, height, xstep, ystep);
  332. this.transformToScale(graphics);
  333. var tmpTranslate = [1, 0, 0, 1, -topLeft[0], -topLeft[1]];
  334. graphics.transform.apply(graphics, tmpTranslate);
  335. this.clipBbox(graphics, bbox, x0, y0, x1, y1);
  336. graphics.executeOperatorList(operatorList);
  337. return tmpCanvas.canvas;
  338. },
  339. setScale: function TilingPattern_setScale(width, height, xstep, ystep) {
  340. this.scale = [width / xstep, height / ystep];
  341. },
  342. transformToScale: function TilingPattern_transformToScale(graphics) {
  343. var scale = this.scale;
  344. var tmpScale = [scale[0], 0, 0, scale[1], 0, 0];
  345. graphics.transform.apply(graphics, tmpScale);
  346. },
  347. scaleToContext: function TilingPattern_scaleToContext() {
  348. var scale = this.scale;
  349. this.ctx.scale(1 / scale[0], 1 / scale[1]);
  350. },
  351. clipBbox: function clipBbox(graphics, bbox, x0, y0, x1, y1) {
  352. if (Array.isArray(bbox) && bbox.length === 4) {
  353. var bboxWidth = x1 - x0;
  354. var bboxHeight = y1 - y0;
  355. graphics.ctx.rect(x0, y0, bboxWidth, bboxHeight);
  356. graphics.clip();
  357. graphics.endPath();
  358. }
  359. },
  360. setFillAndStrokeStyleToContext: function setFillAndStrokeStyleToContext(graphics, paintType, color) {
  361. var context = graphics.ctx,
  362. current = graphics.current;
  363. switch (paintType) {
  364. case PaintType.COLORED:
  365. var ctx = this.ctx;
  366. context.fillStyle = ctx.fillStyle;
  367. context.strokeStyle = ctx.strokeStyle;
  368. current.fillColor = ctx.fillStyle;
  369. current.strokeColor = ctx.strokeStyle;
  370. break;
  371. case PaintType.UNCOLORED:
  372. var cssColor = _util.Util.makeCssRgb(color[0], color[1], color[2]);
  373. context.fillStyle = cssColor;
  374. context.strokeStyle = cssColor;
  375. current.fillColor = cssColor;
  376. current.strokeColor = cssColor;
  377. break;
  378. default:
  379. throw new _util.FormatError('Unsupported paint type: ' + paintType);
  380. }
  381. },
  382. getPattern: function TilingPattern_getPattern(ctx, owner) {
  383. var temporaryPatternCanvas = this.createPatternCanvas(owner);
  384. ctx = this.ctx;
  385. ctx.setTransform.apply(ctx, this.baseTransform);
  386. ctx.transform.apply(ctx, this.matrix);
  387. this.scaleToContext();
  388. return ctx.createPattern(temporaryPatternCanvas, 'repeat');
  389. }
  390. };
  391. return TilingPattern;
  392. }();
  393. exports.getShadingPatternFromIR = getShadingPatternFromIR;
  394. exports.TilingPattern = TilingPattern;