pattern_helper.js 14 KB

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