pattern_helper.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* Copyright 2014 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. import {
  16. FormatError,
  17. info,
  18. shadow,
  19. unreachable,
  20. Util,
  21. } from "../shared/util.js";
  22. import { getCurrentTransform } from "./display_utils.js";
  23. import { isNodeJS } from "../shared/is_node.js";
  24. const PathType = {
  25. FILL: "Fill",
  26. STROKE: "Stroke",
  27. SHADING: "Shading",
  28. };
  29. function applyBoundingBox(ctx, bbox) {
  30. if (!bbox || isNodeJS) {
  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. unreachable("Cannot initialize BaseShadingPattern.");
  43. }
  44. }
  45. getPattern() {
  46. 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 = null;
  60. }
  61. _createGradient(ctx) {
  62. let grad;
  63. if (this._type === "axial") {
  64. grad = ctx.createLinearGradient(
  65. this._p0[0],
  66. this._p0[1],
  67. this._p1[0],
  68. this._p1[1]
  69. );
  70. } else if (this._type === "radial") {
  71. grad = ctx.createRadialGradient(
  72. this._p0[0],
  73. this._p0[1],
  74. this._r0,
  75. this._p1[0],
  76. this._p1[1],
  77. this._r1
  78. );
  79. }
  80. for (const colorStop of this._colorStops) {
  81. grad.addColorStop(colorStop[0], colorStop[1]);
  82. }
  83. return grad;
  84. }
  85. getPattern(ctx, owner, inverse, pathType) {
  86. let pattern;
  87. if (pathType === PathType.STROKE || pathType === PathType.FILL) {
  88. const ownerBBox = owner.current.getClippedPathBoundingBox(
  89. pathType,
  90. getCurrentTransform(ctx)
  91. ) || [0, 0, 0, 0];
  92. // Create a canvas that is only as big as the current path. This doesn't
  93. // allow us to cache the pattern, but it generally creates much smaller
  94. // canvases and saves memory use. See bug 1722807 for an example.
  95. const width = Math.ceil(ownerBBox[2] - ownerBBox[0]) || 1;
  96. const height = Math.ceil(ownerBBox[3] - ownerBBox[1]) || 1;
  97. const tmpCanvas = owner.cachedCanvases.getCanvas(
  98. "pattern",
  99. width,
  100. height,
  101. true
  102. );
  103. const tmpCtx = tmpCanvas.context;
  104. tmpCtx.clearRect(0, 0, tmpCtx.canvas.width, tmpCtx.canvas.height);
  105. tmpCtx.beginPath();
  106. tmpCtx.rect(0, 0, tmpCtx.canvas.width, tmpCtx.canvas.height);
  107. // Non shading fill patterns are positioned relative to the base transform
  108. // (usually the page's initial transform), but we may have created a
  109. // smaller canvas based on the path, so we must account for the shift.
  110. tmpCtx.translate(-ownerBBox[0], -ownerBBox[1]);
  111. inverse = Util.transform(inverse, [
  112. 1,
  113. 0,
  114. 0,
  115. 1,
  116. ownerBBox[0],
  117. ownerBBox[1],
  118. ]);
  119. tmpCtx.transform(...owner.baseTransform);
  120. if (this.matrix) {
  121. tmpCtx.transform(...this.matrix);
  122. }
  123. applyBoundingBox(tmpCtx, this._bbox);
  124. tmpCtx.fillStyle = this._createGradient(tmpCtx);
  125. tmpCtx.fill();
  126. pattern = ctx.createPattern(tmpCanvas.canvas, "no-repeat");
  127. const domMatrix = new DOMMatrix(inverse);
  128. pattern.setTransform(domMatrix);
  129. } else {
  130. // Shading fills are applied relative to the current matrix which is also
  131. // how canvas gradients work, so there's no need to do anything special
  132. // here.
  133. applyBoundingBox(ctx, this._bbox);
  134. pattern = this._createGradient(ctx);
  135. }
  136. return pattern;
  137. }
  138. }
  139. function drawTriangle(data, context, p1, p2, p3, c1, c2, c3) {
  140. // Very basic Gouraud-shaded triangle rasterization algorithm.
  141. const coords = context.coords,
  142. colors = context.colors;
  143. const bytes = data.data,
  144. rowSize = data.width * 4;
  145. let tmp;
  146. if (coords[p1 + 1] > coords[p2 + 1]) {
  147. tmp = p1;
  148. p1 = p2;
  149. p2 = tmp;
  150. tmp = c1;
  151. c1 = c2;
  152. c2 = tmp;
  153. }
  154. if (coords[p2 + 1] > coords[p3 + 1]) {
  155. tmp = p2;
  156. p2 = p3;
  157. p3 = tmp;
  158. tmp = c2;
  159. c2 = c3;
  160. c3 = tmp;
  161. }
  162. if (coords[p1 + 1] > coords[p2 + 1]) {
  163. tmp = p1;
  164. p1 = p2;
  165. p2 = tmp;
  166. tmp = c1;
  167. c1 = c2;
  168. c2 = tmp;
  169. }
  170. const x1 = (coords[p1] + context.offsetX) * context.scaleX;
  171. const y1 = (coords[p1 + 1] + context.offsetY) * context.scaleY;
  172. const x2 = (coords[p2] + context.offsetX) * context.scaleX;
  173. const y2 = (coords[p2 + 1] + context.offsetY) * context.scaleY;
  174. const x3 = (coords[p3] + context.offsetX) * context.scaleX;
  175. const y3 = (coords[p3 + 1] + context.offsetY) * context.scaleY;
  176. if (y1 >= y3) {
  177. return;
  178. }
  179. const c1r = colors[c1],
  180. c1g = colors[c1 + 1],
  181. c1b = colors[c1 + 2];
  182. const c2r = colors[c2],
  183. c2g = colors[c2 + 1],
  184. c2b = colors[c2 + 2];
  185. const c3r = colors[c3],
  186. c3g = colors[c3 + 1],
  187. c3b = colors[c3 + 2];
  188. const minY = Math.round(y1),
  189. maxY = Math.round(y3);
  190. let xa, car, cag, cab;
  191. let xb, cbr, cbg, cbb;
  192. for (let y = minY; y <= maxY; y++) {
  193. if (y < y2) {
  194. let k;
  195. if (y < y1) {
  196. k = 0;
  197. } else {
  198. k = (y1 - y) / (y1 - y2);
  199. }
  200. xa = x1 - (x1 - x2) * k;
  201. car = c1r - (c1r - c2r) * k;
  202. cag = c1g - (c1g - c2g) * k;
  203. cab = c1b - (c1b - c2b) * k;
  204. } else {
  205. let k;
  206. if (y > y3) {
  207. k = 1;
  208. } else if (y2 === y3) {
  209. k = 0;
  210. } else {
  211. k = (y2 - y) / (y2 - y3);
  212. }
  213. xa = x2 - (x2 - x3) * k;
  214. car = c2r - (c2r - c3r) * k;
  215. cag = c2g - (c2g - c3g) * k;
  216. cab = c2b - (c2b - c3b) * k;
  217. }
  218. let k;
  219. if (y < y1) {
  220. k = 0;
  221. } else if (y > y3) {
  222. k = 1;
  223. } else {
  224. k = (y1 - y) / (y1 - y3);
  225. }
  226. xb = x1 - (x1 - x3) * k;
  227. cbr = c1r - (c1r - c3r) * k;
  228. cbg = c1g - (c1g - c3g) * k;
  229. cbb = c1b - (c1b - c3b) * k;
  230. const x1_ = Math.round(Math.min(xa, xb));
  231. const x2_ = Math.round(Math.max(xa, xb));
  232. let j = rowSize * y + x1_ * 4;
  233. for (let x = x1_; x <= x2_; x++) {
  234. k = (xa - x) / (xa - xb);
  235. if (k < 0) {
  236. k = 0;
  237. } else if (k > 1) {
  238. k = 1;
  239. }
  240. bytes[j++] = (car - (car - cbr) * k) | 0;
  241. bytes[j++] = (cag - (cag - cbg) * k) | 0;
  242. bytes[j++] = (cab - (cab - cbb) * k) | 0;
  243. bytes[j++] = 255;
  244. }
  245. }
  246. }
  247. function drawFigure(data, figure, context) {
  248. const ps = figure.coords;
  249. const cs = figure.colors;
  250. let i, ii;
  251. switch (figure.type) {
  252. case "lattice":
  253. const verticesPerRow = figure.verticesPerRow;
  254. const rows = Math.floor(ps.length / verticesPerRow) - 1;
  255. const cols = verticesPerRow - 1;
  256. for (i = 0; i < rows; i++) {
  257. let q = i * verticesPerRow;
  258. for (let j = 0; j < cols; j++, q++) {
  259. drawTriangle(
  260. data,
  261. context,
  262. ps[q],
  263. ps[q + 1],
  264. ps[q + verticesPerRow],
  265. cs[q],
  266. cs[q + 1],
  267. cs[q + verticesPerRow]
  268. );
  269. drawTriangle(
  270. data,
  271. context,
  272. ps[q + verticesPerRow + 1],
  273. ps[q + 1],
  274. ps[q + verticesPerRow],
  275. cs[q + verticesPerRow + 1],
  276. cs[q + 1],
  277. cs[q + verticesPerRow]
  278. );
  279. }
  280. }
  281. break;
  282. case "triangles":
  283. for (i = 0, ii = ps.length; i < ii; i += 3) {
  284. drawTriangle(
  285. data,
  286. context,
  287. ps[i],
  288. ps[i + 1],
  289. ps[i + 2],
  290. cs[i],
  291. cs[i + 1],
  292. cs[i + 2]
  293. );
  294. }
  295. break;
  296. default:
  297. throw new Error("illegal figure");
  298. }
  299. }
  300. class MeshShadingPattern extends BaseShadingPattern {
  301. constructor(IR) {
  302. super();
  303. this._coords = IR[2];
  304. this._colors = IR[3];
  305. this._figures = IR[4];
  306. this._bounds = IR[5];
  307. this._bbox = IR[7];
  308. this._background = IR[8];
  309. this.matrix = null;
  310. }
  311. _createMeshCanvas(combinedScale, backgroundColor, cachedCanvases) {
  312. // we will increase scale on some weird factor to let antialiasing take
  313. // care of "rough" edges
  314. const EXPECTED_SCALE = 1.1;
  315. // MAX_PATTERN_SIZE is used to avoid OOM situation.
  316. const MAX_PATTERN_SIZE = 3000; // 10in @ 300dpi shall be enough
  317. // We need to keep transparent border around our pattern for fill():
  318. // createPattern with 'no-repeat' will bleed edges across entire area.
  319. const BORDER_SIZE = 2;
  320. const offsetX = Math.floor(this._bounds[0]);
  321. const offsetY = Math.floor(this._bounds[1]);
  322. const boundsWidth = Math.ceil(this._bounds[2]) - offsetX;
  323. const boundsHeight = Math.ceil(this._bounds[3]) - offsetY;
  324. const width = Math.min(
  325. Math.ceil(Math.abs(boundsWidth * combinedScale[0] * EXPECTED_SCALE)),
  326. MAX_PATTERN_SIZE
  327. );
  328. const height = Math.min(
  329. Math.ceil(Math.abs(boundsHeight * combinedScale[1] * EXPECTED_SCALE)),
  330. MAX_PATTERN_SIZE
  331. );
  332. const scaleX = boundsWidth / width;
  333. const scaleY = boundsHeight / height;
  334. const context = {
  335. coords: this._coords,
  336. colors: this._colors,
  337. offsetX: -offsetX,
  338. offsetY: -offsetY,
  339. scaleX: 1 / scaleX,
  340. scaleY: 1 / scaleY,
  341. };
  342. const paddedWidth = width + BORDER_SIZE * 2;
  343. const paddedHeight = height + BORDER_SIZE * 2;
  344. const tmpCanvas = cachedCanvases.getCanvas(
  345. "mesh",
  346. paddedWidth,
  347. paddedHeight,
  348. false
  349. );
  350. const tmpCtx = tmpCanvas.context;
  351. const data = tmpCtx.createImageData(width, height);
  352. if (backgroundColor) {
  353. const bytes = data.data;
  354. for (let i = 0, ii = bytes.length; i < ii; i += 4) {
  355. bytes[i] = backgroundColor[0];
  356. bytes[i + 1] = backgroundColor[1];
  357. bytes[i + 2] = backgroundColor[2];
  358. bytes[i + 3] = 255;
  359. }
  360. }
  361. for (const figure of this._figures) {
  362. drawFigure(data, figure, context);
  363. }
  364. tmpCtx.putImageData(data, BORDER_SIZE, BORDER_SIZE);
  365. const canvas = tmpCanvas.canvas;
  366. return {
  367. canvas,
  368. offsetX: offsetX - BORDER_SIZE * scaleX,
  369. offsetY: offsetY - BORDER_SIZE * scaleY,
  370. scaleX,
  371. scaleY,
  372. };
  373. }
  374. getPattern(ctx, owner, inverse, pathType) {
  375. applyBoundingBox(ctx, this._bbox);
  376. let scale;
  377. if (pathType === PathType.SHADING) {
  378. scale = Util.singularValueDecompose2dScale(getCurrentTransform(ctx));
  379. } else {
  380. // Obtain scale from matrix and current transformation matrix.
  381. scale = Util.singularValueDecompose2dScale(owner.baseTransform);
  382. if (this.matrix) {
  383. const matrixScale = Util.singularValueDecompose2dScale(this.matrix);
  384. scale = [scale[0] * matrixScale[0], scale[1] * matrixScale[1]];
  385. }
  386. }
  387. // Rasterizing on the main thread since sending/queue large canvases
  388. // might cause OOM.
  389. const temporaryPatternCanvas = this._createMeshCanvas(
  390. scale,
  391. pathType === PathType.SHADING ? null : this._background,
  392. owner.cachedCanvases
  393. );
  394. if (pathType !== PathType.SHADING) {
  395. ctx.setTransform(...owner.baseTransform);
  396. if (this.matrix) {
  397. ctx.transform(...this.matrix);
  398. }
  399. }
  400. ctx.translate(
  401. temporaryPatternCanvas.offsetX,
  402. temporaryPatternCanvas.offsetY
  403. );
  404. ctx.scale(temporaryPatternCanvas.scaleX, temporaryPatternCanvas.scaleY);
  405. return ctx.createPattern(temporaryPatternCanvas.canvas, "no-repeat");
  406. }
  407. }
  408. class DummyShadingPattern extends BaseShadingPattern {
  409. getPattern() {
  410. return "hotpink";
  411. }
  412. }
  413. function getShadingPattern(IR) {
  414. switch (IR[0]) {
  415. case "RadialAxial":
  416. return new RadialAxialShadingPattern(IR);
  417. case "Mesh":
  418. return new MeshShadingPattern(IR);
  419. case "Dummy":
  420. return new DummyShadingPattern();
  421. }
  422. throw new Error(`Unknown IR type: ${IR[0]}`);
  423. }
  424. const PaintType = {
  425. COLORED: 1,
  426. UNCOLORED: 2,
  427. };
  428. class TilingPattern {
  429. // 10in @ 300dpi shall be enough.
  430. static get MAX_PATTERN_SIZE() {
  431. return shadow(this, "MAX_PATTERN_SIZE", 3000);
  432. }
  433. constructor(IR, color, ctx, canvasGraphicsFactory, baseTransform) {
  434. this.operatorList = IR[2];
  435. this.matrix = IR[3] || [1, 0, 0, 1, 0, 0];
  436. this.bbox = IR[4];
  437. this.xstep = IR[5];
  438. this.ystep = IR[6];
  439. this.paintType = IR[7];
  440. this.tilingType = IR[8];
  441. this.color = color;
  442. this.ctx = ctx;
  443. this.canvasGraphicsFactory = canvasGraphicsFactory;
  444. this.baseTransform = baseTransform;
  445. }
  446. createPatternCanvas(owner) {
  447. const operatorList = this.operatorList;
  448. const bbox = this.bbox;
  449. const xstep = this.xstep;
  450. const ystep = this.ystep;
  451. const paintType = this.paintType;
  452. const tilingType = this.tilingType;
  453. const color = this.color;
  454. const canvasGraphicsFactory = this.canvasGraphicsFactory;
  455. info("TilingType: " + tilingType);
  456. // A tiling pattern as defined by PDF spec 8.7.2 is a cell whose size is
  457. // described by bbox, and may repeat regularly by shifting the cell by
  458. // xstep and ystep.
  459. // Because the HTML5 canvas API does not support pattern repetition with
  460. // gaps in between, we use the xstep/ystep instead of the bbox's size.
  461. //
  462. // This has the following consequences (similarly for ystep):
  463. //
  464. // - If xstep is the same as bbox, then there is no observable difference.
  465. //
  466. // - If xstep is larger than bbox, then the pattern canvas is partially
  467. // empty: the area bounded by bbox is painted, the outside area is void.
  468. //
  469. // - If xstep is smaller than bbox, then the pixels between xstep and the
  470. // bbox boundary will be missing. This is INCORRECT behavior.
  471. // "Figures on adjacent tiles should not overlap" (PDF spec 8.7.3.1),
  472. // but overlapping cells without common pixels are still valid.
  473. // TODO: Fix the implementation, to allow this scenario to be painted
  474. // correctly.
  475. const x0 = bbox[0],
  476. y0 = bbox[1],
  477. x1 = bbox[2],
  478. y1 = bbox[3];
  479. // Obtain scale from matrix and current transformation matrix.
  480. const matrixScale = Util.singularValueDecompose2dScale(this.matrix);
  481. const curMatrixScale = Util.singularValueDecompose2dScale(
  482. this.baseTransform
  483. );
  484. const combinedScale = [
  485. matrixScale[0] * curMatrixScale[0],
  486. matrixScale[1] * curMatrixScale[1],
  487. ];
  488. // Use width and height values that are as close as possible to the end
  489. // result when the pattern is used. Too low value makes the pattern look
  490. // blurry. Too large value makes it look too crispy.
  491. const dimx = this.getSizeAndScale(
  492. xstep,
  493. this.ctx.canvas.width,
  494. combinedScale[0]
  495. );
  496. const dimy = this.getSizeAndScale(
  497. ystep,
  498. this.ctx.canvas.height,
  499. combinedScale[1]
  500. );
  501. const tmpCanvas = owner.cachedCanvases.getCanvas(
  502. "pattern",
  503. dimx.size,
  504. dimy.size,
  505. true
  506. );
  507. const tmpCtx = tmpCanvas.context;
  508. const graphics = canvasGraphicsFactory.createCanvasGraphics(tmpCtx);
  509. graphics.groupLevel = owner.groupLevel;
  510. this.setFillAndStrokeStyleToContext(graphics, paintType, color);
  511. let adjustedX0 = x0;
  512. let adjustedY0 = y0;
  513. let adjustedX1 = x1;
  514. let adjustedY1 = y1;
  515. // Some bounding boxes have negative x0/y0 coordinates which will cause the
  516. // some of the drawing to be off of the canvas. To avoid this shift the
  517. // bounding box over.
  518. if (x0 < 0) {
  519. adjustedX0 = 0;
  520. adjustedX1 += Math.abs(x0);
  521. }
  522. if (y0 < 0) {
  523. adjustedY0 = 0;
  524. adjustedY1 += Math.abs(y0);
  525. }
  526. tmpCtx.translate(-(dimx.scale * adjustedX0), -(dimy.scale * adjustedY0));
  527. graphics.transform(dimx.scale, 0, 0, dimy.scale, 0, 0);
  528. // To match CanvasGraphics beginDrawing we must save the context here or
  529. // else we end up with unbalanced save/restores.
  530. tmpCtx.save();
  531. this.clipBbox(graphics, adjustedX0, adjustedY0, adjustedX1, adjustedY1);
  532. graphics.baseTransform = getCurrentTransform(graphics.ctx);
  533. graphics.executeOperatorList(operatorList);
  534. graphics.endDrawing();
  535. return {
  536. canvas: tmpCanvas.canvas,
  537. scaleX: dimx.scale,
  538. scaleY: dimy.scale,
  539. offsetX: adjustedX0,
  540. offsetY: adjustedY0,
  541. };
  542. }
  543. getSizeAndScale(step, realOutputSize, scale) {
  544. // xstep / ystep may be negative -- normalize.
  545. step = Math.abs(step);
  546. // MAX_PATTERN_SIZE is used to avoid OOM situation.
  547. // Use the destination canvas's size if it is bigger than the hard-coded
  548. // limit of MAX_PATTERN_SIZE to avoid clipping patterns that cover the
  549. // whole canvas.
  550. const maxSize = Math.max(TilingPattern.MAX_PATTERN_SIZE, realOutputSize);
  551. let size = Math.ceil(step * scale);
  552. if (size >= maxSize) {
  553. size = maxSize;
  554. } else {
  555. scale = size / step;
  556. }
  557. return { scale, size };
  558. }
  559. clipBbox(graphics, x0, y0, x1, y1) {
  560. const bboxWidth = x1 - x0;
  561. const bboxHeight = y1 - y0;
  562. graphics.ctx.rect(x0, y0, bboxWidth, bboxHeight);
  563. graphics.current.updateRectMinMax(getCurrentTransform(graphics.ctx), [
  564. x0,
  565. y0,
  566. x1,
  567. y1,
  568. ]);
  569. graphics.clip();
  570. graphics.endPath();
  571. }
  572. setFillAndStrokeStyleToContext(graphics, paintType, color) {
  573. const context = graphics.ctx,
  574. current = graphics.current;
  575. switch (paintType) {
  576. case PaintType.COLORED:
  577. const ctx = this.ctx;
  578. context.fillStyle = ctx.fillStyle;
  579. context.strokeStyle = ctx.strokeStyle;
  580. current.fillColor = ctx.fillStyle;
  581. current.strokeColor = ctx.strokeStyle;
  582. break;
  583. case PaintType.UNCOLORED:
  584. const cssColor = Util.makeHexColor(color[0], color[1], color[2]);
  585. context.fillStyle = cssColor;
  586. context.strokeStyle = cssColor;
  587. // Set color needed by image masks (fixes issues 3226 and 8741).
  588. current.fillColor = cssColor;
  589. current.strokeColor = cssColor;
  590. break;
  591. default:
  592. throw new FormatError(`Unsupported paint type: ${paintType}`);
  593. }
  594. }
  595. getPattern(ctx, owner, inverse, pathType) {
  596. // PDF spec 8.7.2 NOTE 1: pattern's matrix is relative to initial matrix.
  597. let matrix = inverse;
  598. if (pathType !== PathType.SHADING) {
  599. matrix = Util.transform(matrix, owner.baseTransform);
  600. if (this.matrix) {
  601. matrix = Util.transform(matrix, this.matrix);
  602. }
  603. }
  604. const temporaryPatternCanvas = this.createPatternCanvas(owner);
  605. let domMatrix = new DOMMatrix(matrix);
  606. // Rescale and so that the ctx.createPattern call generates a pattern with
  607. // the desired size.
  608. domMatrix = domMatrix.translate(
  609. temporaryPatternCanvas.offsetX,
  610. temporaryPatternCanvas.offsetY
  611. );
  612. domMatrix = domMatrix.scale(
  613. 1 / temporaryPatternCanvas.scaleX,
  614. 1 / temporaryPatternCanvas.scaleY
  615. );
  616. const pattern = ctx.createPattern(temporaryPatternCanvas.canvas, "repeat");
  617. pattern.setTransform(domMatrix);
  618. return pattern;
  619. }
  620. }
  621. export { getShadingPattern, PathType, TilingPattern };