pattern_helper.js 14 KB

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