pattern_helper.js 15 KB

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