pattern.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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.getTilingPatternIR = getTilingPatternIR;
  27. exports.Pattern = void 0;
  28. var _util = require("../shared/util.js");
  29. var _colorspace = require("./colorspace.js");
  30. var _primitives = require("./primitives.js");
  31. var _core_utils = require("./core_utils.js");
  32. const ShadingType = {
  33. FUNCTION_BASED: 1,
  34. AXIAL: 2,
  35. RADIAL: 3,
  36. FREE_FORM_MESH: 4,
  37. LATTICE_FORM_MESH: 5,
  38. COONS_PATCH_MESH: 6,
  39. TENSOR_PATCH_MESH: 7
  40. };
  41. class Pattern {
  42. constructor() {
  43. (0, _util.unreachable)("Cannot initialize Pattern.");
  44. }
  45. static parseShading(shading, matrix, xref, res, handler, pdfFunctionFactory, localColorSpaceCache) {
  46. const dict = (0, _primitives.isStream)(shading) ? shading.dict : shading;
  47. const type = dict.get("ShadingType");
  48. try {
  49. switch (type) {
  50. case ShadingType.AXIAL:
  51. case ShadingType.RADIAL:
  52. return new RadialAxialShading(dict, matrix, xref, res, pdfFunctionFactory, localColorSpaceCache);
  53. case ShadingType.FREE_FORM_MESH:
  54. case ShadingType.LATTICE_FORM_MESH:
  55. case ShadingType.COONS_PATCH_MESH:
  56. case ShadingType.TENSOR_PATCH_MESH:
  57. return new MeshShading(shading, matrix, xref, res, pdfFunctionFactory, localColorSpaceCache);
  58. default:
  59. throw new _util.FormatError("Unsupported ShadingType: " + type);
  60. }
  61. } catch (ex) {
  62. if (ex instanceof _core_utils.MissingDataException) {
  63. throw ex;
  64. }
  65. handler.send("UnsupportedFeature", {
  66. featureId: _util.UNSUPPORTED_FEATURES.shadingPattern
  67. });
  68. (0, _util.warn)(ex);
  69. return new DummyShading();
  70. }
  71. }
  72. }
  73. exports.Pattern = Pattern;
  74. class BaseShading {
  75. static get SMALL_NUMBER() {
  76. return (0, _util.shadow)(this, "SMALL_NUMBER", 1e-6);
  77. }
  78. constructor() {
  79. if (this.constructor === BaseShading) {
  80. (0, _util.unreachable)("Cannot initialize BaseShading.");
  81. }
  82. }
  83. getIR() {
  84. (0, _util.unreachable)("Abstract method `getIR` called.");
  85. }
  86. }
  87. class RadialAxialShading extends BaseShading {
  88. constructor(dict, matrix, xref, resources, pdfFunctionFactory, localColorSpaceCache) {
  89. super();
  90. this.matrix = matrix;
  91. this.coordsArr = dict.getArray("Coords");
  92. this.shadingType = dict.get("ShadingType");
  93. const cs = _colorspace.ColorSpace.parse({
  94. cs: dict.getRaw("ColorSpace") || dict.getRaw("CS"),
  95. xref,
  96. resources,
  97. pdfFunctionFactory,
  98. localColorSpaceCache
  99. });
  100. const bbox = dict.getArray("BBox");
  101. if (Array.isArray(bbox) && bbox.length === 4) {
  102. this.bbox = _util.Util.normalizeRect(bbox);
  103. } else {
  104. this.bbox = null;
  105. }
  106. let t0 = 0.0,
  107. t1 = 1.0;
  108. if (dict.has("Domain")) {
  109. const domainArr = dict.getArray("Domain");
  110. t0 = domainArr[0];
  111. t1 = domainArr[1];
  112. }
  113. let extendStart = false,
  114. extendEnd = false;
  115. if (dict.has("Extend")) {
  116. const extendArr = dict.getArray("Extend");
  117. extendStart = extendArr[0];
  118. extendEnd = extendArr[1];
  119. }
  120. if (this.shadingType === ShadingType.RADIAL && (!extendStart || !extendEnd)) {
  121. const [x1, y1, r1, x2, y2, r2] = this.coordsArr;
  122. const distance = Math.hypot(x1 - x2, y1 - y2);
  123. if (r1 <= r2 + distance && r2 <= r1 + distance) {
  124. (0, _util.warn)("Unsupported radial gradient.");
  125. }
  126. }
  127. this.extendStart = extendStart;
  128. this.extendEnd = extendEnd;
  129. const fnObj = dict.getRaw("Function");
  130. const fn = pdfFunctionFactory.createFromArray(fnObj);
  131. const NUMBER_OF_SAMPLES = 10;
  132. const step = (t1 - t0) / NUMBER_OF_SAMPLES;
  133. const colorStops = this.colorStops = [];
  134. if (t0 >= t1 || step <= 0) {
  135. (0, _util.info)("Bad shading domain.");
  136. return;
  137. }
  138. const color = new Float32Array(cs.numComps),
  139. ratio = new Float32Array(1);
  140. let rgbColor;
  141. for (let i = 0; i <= NUMBER_OF_SAMPLES; i++) {
  142. ratio[0] = t0 + i * step;
  143. fn(ratio, 0, color, 0);
  144. rgbColor = cs.getRgb(color, 0);
  145. const cssColor = _util.Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
  146. colorStops.push([i / NUMBER_OF_SAMPLES, cssColor]);
  147. }
  148. let background = "transparent";
  149. if (dict.has("Background")) {
  150. rgbColor = cs.getRgb(dict.get("Background"), 0);
  151. background = _util.Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
  152. }
  153. if (!extendStart) {
  154. colorStops.unshift([0, background]);
  155. colorStops[1][0] += BaseShading.SMALL_NUMBER;
  156. }
  157. if (!extendEnd) {
  158. colorStops[colorStops.length - 1][0] -= BaseShading.SMALL_NUMBER;
  159. colorStops.push([1, background]);
  160. }
  161. this.colorStops = colorStops;
  162. }
  163. getIR() {
  164. const coordsArr = this.coordsArr;
  165. const shadingType = this.shadingType;
  166. let type, p0, p1, r0, r1;
  167. if (shadingType === ShadingType.AXIAL) {
  168. p0 = [coordsArr[0], coordsArr[1]];
  169. p1 = [coordsArr[2], coordsArr[3]];
  170. r0 = null;
  171. r1 = null;
  172. type = "axial";
  173. } else if (shadingType === ShadingType.RADIAL) {
  174. p0 = [coordsArr[0], coordsArr[1]];
  175. p1 = [coordsArr[3], coordsArr[4]];
  176. r0 = coordsArr[2];
  177. r1 = coordsArr[5];
  178. type = "radial";
  179. } else {
  180. (0, _util.unreachable)(`getPattern type unknown: ${shadingType}`);
  181. }
  182. return ["RadialAxial", type, this.bbox, this.colorStops, p0, p1, r0, r1, this.matrix];
  183. }
  184. }
  185. class MeshStreamReader {
  186. constructor(stream, context) {
  187. this.stream = stream;
  188. this.context = context;
  189. this.buffer = 0;
  190. this.bufferLength = 0;
  191. const numComps = context.numComps;
  192. this.tmpCompsBuf = new Float32Array(numComps);
  193. const csNumComps = context.colorSpace.numComps;
  194. this.tmpCsCompsBuf = context.colorFn ? new Float32Array(csNumComps) : this.tmpCompsBuf;
  195. }
  196. get hasData() {
  197. if (this.stream.end) {
  198. return this.stream.pos < this.stream.end;
  199. }
  200. if (this.bufferLength > 0) {
  201. return true;
  202. }
  203. const nextByte = this.stream.getByte();
  204. if (nextByte < 0) {
  205. return false;
  206. }
  207. this.buffer = nextByte;
  208. this.bufferLength = 8;
  209. return true;
  210. }
  211. readBits(n) {
  212. let buffer = this.buffer;
  213. let bufferLength = this.bufferLength;
  214. if (n === 32) {
  215. if (bufferLength === 0) {
  216. return (this.stream.getByte() << 24 | this.stream.getByte() << 16 | this.stream.getByte() << 8 | this.stream.getByte()) >>> 0;
  217. }
  218. buffer = buffer << 24 | this.stream.getByte() << 16 | this.stream.getByte() << 8 | this.stream.getByte();
  219. const nextByte = this.stream.getByte();
  220. this.buffer = nextByte & (1 << bufferLength) - 1;
  221. return (buffer << 8 - bufferLength | (nextByte & 0xff) >> bufferLength) >>> 0;
  222. }
  223. if (n === 8 && bufferLength === 0) {
  224. return this.stream.getByte();
  225. }
  226. while (bufferLength < n) {
  227. buffer = buffer << 8 | this.stream.getByte();
  228. bufferLength += 8;
  229. }
  230. bufferLength -= n;
  231. this.bufferLength = bufferLength;
  232. this.buffer = buffer & (1 << bufferLength) - 1;
  233. return buffer >> bufferLength;
  234. }
  235. align() {
  236. this.buffer = 0;
  237. this.bufferLength = 0;
  238. }
  239. readFlag() {
  240. return this.readBits(this.context.bitsPerFlag);
  241. }
  242. readCoordinate() {
  243. const bitsPerCoordinate = this.context.bitsPerCoordinate;
  244. const xi = this.readBits(bitsPerCoordinate);
  245. const yi = this.readBits(bitsPerCoordinate);
  246. const decode = this.context.decode;
  247. const scale = bitsPerCoordinate < 32 ? 1 / ((1 << bitsPerCoordinate) - 1) : 2.3283064365386963e-10;
  248. return [xi * scale * (decode[1] - decode[0]) + decode[0], yi * scale * (decode[3] - decode[2]) + decode[2]];
  249. }
  250. readComponents() {
  251. const numComps = this.context.numComps;
  252. const bitsPerComponent = this.context.bitsPerComponent;
  253. const scale = bitsPerComponent < 32 ? 1 / ((1 << bitsPerComponent) - 1) : 2.3283064365386963e-10;
  254. const decode = this.context.decode;
  255. const components = this.tmpCompsBuf;
  256. for (let i = 0, j = 4; i < numComps; i++, j += 2) {
  257. const ci = this.readBits(bitsPerComponent);
  258. components[i] = ci * scale * (decode[j + 1] - decode[j]) + decode[j];
  259. }
  260. const color = this.tmpCsCompsBuf;
  261. if (this.context.colorFn) {
  262. this.context.colorFn(components, 0, color, 0);
  263. }
  264. return this.context.colorSpace.getRgb(color, 0);
  265. }
  266. }
  267. const getB = function getBClosure() {
  268. function buildB(count) {
  269. const lut = [];
  270. for (let i = 0; i <= count; i++) {
  271. const t = i / count,
  272. t_ = 1 - t;
  273. lut.push(new Float32Array([t_ * t_ * t_, 3 * t * t_ * t_, 3 * t * t * t_, t * t * t]));
  274. }
  275. return lut;
  276. }
  277. const cache = [];
  278. return function (count) {
  279. if (!cache[count]) {
  280. cache[count] = buildB(count);
  281. }
  282. return cache[count];
  283. };
  284. }();
  285. class MeshShading extends BaseShading {
  286. static get MIN_SPLIT_PATCH_CHUNKS_AMOUNT() {
  287. return (0, _util.shadow)(this, "MIN_SPLIT_PATCH_CHUNKS_AMOUNT", 3);
  288. }
  289. static get MAX_SPLIT_PATCH_CHUNKS_AMOUNT() {
  290. return (0, _util.shadow)(this, "MAX_SPLIT_PATCH_CHUNKS_AMOUNT", 20);
  291. }
  292. static get TRIANGLE_DENSITY() {
  293. return (0, _util.shadow)(this, "TRIANGLE_DENSITY", 20);
  294. }
  295. constructor(stream, matrix, xref, resources, pdfFunctionFactory, localColorSpaceCache) {
  296. super();
  297. if (!(0, _primitives.isStream)(stream)) {
  298. throw new _util.FormatError("Mesh data is not a stream");
  299. }
  300. const dict = stream.dict;
  301. this.matrix = matrix;
  302. this.shadingType = dict.get("ShadingType");
  303. const bbox = dict.getArray("BBox");
  304. if (Array.isArray(bbox) && bbox.length === 4) {
  305. this.bbox = _util.Util.normalizeRect(bbox);
  306. } else {
  307. this.bbox = null;
  308. }
  309. const cs = _colorspace.ColorSpace.parse({
  310. cs: dict.getRaw("ColorSpace") || dict.getRaw("CS"),
  311. xref,
  312. resources,
  313. pdfFunctionFactory,
  314. localColorSpaceCache
  315. });
  316. this.background = dict.has("Background") ? cs.getRgb(dict.get("Background"), 0) : null;
  317. const fnObj = dict.getRaw("Function");
  318. const fn = fnObj ? pdfFunctionFactory.createFromArray(fnObj) : null;
  319. this.coords = [];
  320. this.colors = [];
  321. this.figures = [];
  322. const decodeContext = {
  323. bitsPerCoordinate: dict.get("BitsPerCoordinate"),
  324. bitsPerComponent: dict.get("BitsPerComponent"),
  325. bitsPerFlag: dict.get("BitsPerFlag"),
  326. decode: dict.getArray("Decode"),
  327. colorFn: fn,
  328. colorSpace: cs,
  329. numComps: fn ? 1 : cs.numComps
  330. };
  331. const reader = new MeshStreamReader(stream, decodeContext);
  332. let patchMesh = false;
  333. switch (this.shadingType) {
  334. case ShadingType.FREE_FORM_MESH:
  335. this._decodeType4Shading(reader);
  336. break;
  337. case ShadingType.LATTICE_FORM_MESH:
  338. const verticesPerRow = dict.get("VerticesPerRow") | 0;
  339. if (verticesPerRow < 2) {
  340. throw new _util.FormatError("Invalid VerticesPerRow");
  341. }
  342. this._decodeType5Shading(reader, verticesPerRow);
  343. break;
  344. case ShadingType.COONS_PATCH_MESH:
  345. this._decodeType6Shading(reader);
  346. patchMesh = true;
  347. break;
  348. case ShadingType.TENSOR_PATCH_MESH:
  349. this._decodeType7Shading(reader);
  350. patchMesh = true;
  351. break;
  352. default:
  353. (0, _util.unreachable)("Unsupported mesh type.");
  354. break;
  355. }
  356. if (patchMesh) {
  357. this._updateBounds();
  358. for (let i = 0, ii = this.figures.length; i < ii; i++) {
  359. this._buildFigureFromPatch(i);
  360. }
  361. }
  362. this._updateBounds();
  363. this._packData();
  364. }
  365. _decodeType4Shading(reader) {
  366. const coords = this.coords;
  367. const colors = this.colors;
  368. const operators = [];
  369. const ps = [];
  370. let verticesLeft = 0;
  371. while (reader.hasData) {
  372. const f = reader.readFlag();
  373. const coord = reader.readCoordinate();
  374. const color = reader.readComponents();
  375. if (verticesLeft === 0) {
  376. if (!(0 <= f && f <= 2)) {
  377. throw new _util.FormatError("Unknown type4 flag");
  378. }
  379. switch (f) {
  380. case 0:
  381. verticesLeft = 3;
  382. break;
  383. case 1:
  384. ps.push(ps[ps.length - 2], ps[ps.length - 1]);
  385. verticesLeft = 1;
  386. break;
  387. case 2:
  388. ps.push(ps[ps.length - 3], ps[ps.length - 1]);
  389. verticesLeft = 1;
  390. break;
  391. }
  392. operators.push(f);
  393. }
  394. ps.push(coords.length);
  395. coords.push(coord);
  396. colors.push(color);
  397. verticesLeft--;
  398. reader.align();
  399. }
  400. this.figures.push({
  401. type: "triangles",
  402. coords: new Int32Array(ps),
  403. colors: new Int32Array(ps)
  404. });
  405. }
  406. _decodeType5Shading(reader, verticesPerRow) {
  407. const coords = this.coords;
  408. const colors = this.colors;
  409. const ps = [];
  410. while (reader.hasData) {
  411. const coord = reader.readCoordinate();
  412. const color = reader.readComponents();
  413. ps.push(coords.length);
  414. coords.push(coord);
  415. colors.push(color);
  416. }
  417. this.figures.push({
  418. type: "lattice",
  419. coords: new Int32Array(ps),
  420. colors: new Int32Array(ps),
  421. verticesPerRow
  422. });
  423. }
  424. _decodeType6Shading(reader) {
  425. const coords = this.coords;
  426. const colors = this.colors;
  427. const ps = new Int32Array(16);
  428. const cs = new Int32Array(4);
  429. while (reader.hasData) {
  430. const f = reader.readFlag();
  431. if (!(0 <= f && f <= 3)) {
  432. throw new _util.FormatError("Unknown type6 flag");
  433. }
  434. const pi = coords.length;
  435. for (let i = 0, ii = f !== 0 ? 8 : 12; i < ii; i++) {
  436. coords.push(reader.readCoordinate());
  437. }
  438. const ci = colors.length;
  439. for (let i = 0, ii = f !== 0 ? 2 : 4; i < ii; i++) {
  440. colors.push(reader.readComponents());
  441. }
  442. let tmp1, tmp2, tmp3, tmp4;
  443. switch (f) {
  444. case 0:
  445. ps[12] = pi + 3;
  446. ps[13] = pi + 4;
  447. ps[14] = pi + 5;
  448. ps[15] = pi + 6;
  449. ps[8] = pi + 2;
  450. ps[11] = pi + 7;
  451. ps[4] = pi + 1;
  452. ps[7] = pi + 8;
  453. ps[0] = pi;
  454. ps[1] = pi + 11;
  455. ps[2] = pi + 10;
  456. ps[3] = pi + 9;
  457. cs[2] = ci + 1;
  458. cs[3] = ci + 2;
  459. cs[0] = ci;
  460. cs[1] = ci + 3;
  461. break;
  462. case 1:
  463. tmp1 = ps[12];
  464. tmp2 = ps[13];
  465. tmp3 = ps[14];
  466. tmp4 = ps[15];
  467. ps[12] = tmp4;
  468. ps[13] = pi + 0;
  469. ps[14] = pi + 1;
  470. ps[15] = pi + 2;
  471. ps[8] = tmp3;
  472. ps[11] = pi + 3;
  473. ps[4] = tmp2;
  474. ps[7] = pi + 4;
  475. ps[0] = tmp1;
  476. ps[1] = pi + 7;
  477. ps[2] = pi + 6;
  478. ps[3] = pi + 5;
  479. tmp1 = cs[2];
  480. tmp2 = cs[3];
  481. cs[2] = tmp2;
  482. cs[3] = ci;
  483. cs[0] = tmp1;
  484. cs[1] = ci + 1;
  485. break;
  486. case 2:
  487. tmp1 = ps[15];
  488. tmp2 = ps[11];
  489. ps[12] = ps[3];
  490. ps[13] = pi + 0;
  491. ps[14] = pi + 1;
  492. ps[15] = pi + 2;
  493. ps[8] = ps[7];
  494. ps[11] = pi + 3;
  495. ps[4] = tmp2;
  496. ps[7] = pi + 4;
  497. ps[0] = tmp1;
  498. ps[1] = pi + 7;
  499. ps[2] = pi + 6;
  500. ps[3] = pi + 5;
  501. tmp1 = cs[3];
  502. cs[2] = cs[1];
  503. cs[3] = ci;
  504. cs[0] = tmp1;
  505. cs[1] = ci + 1;
  506. break;
  507. case 3:
  508. ps[12] = ps[0];
  509. ps[13] = pi + 0;
  510. ps[14] = pi + 1;
  511. ps[15] = pi + 2;
  512. ps[8] = ps[1];
  513. ps[11] = pi + 3;
  514. ps[4] = ps[2];
  515. ps[7] = pi + 4;
  516. ps[0] = ps[3];
  517. ps[1] = pi + 7;
  518. ps[2] = pi + 6;
  519. ps[3] = pi + 5;
  520. cs[2] = cs[0];
  521. cs[3] = ci;
  522. cs[0] = cs[1];
  523. cs[1] = ci + 1;
  524. break;
  525. }
  526. ps[5] = coords.length;
  527. coords.push([(-4 * coords[ps[0]][0] - coords[ps[15]][0] + 6 * (coords[ps[4]][0] + coords[ps[1]][0]) - 2 * (coords[ps[12]][0] + coords[ps[3]][0]) + 3 * (coords[ps[13]][0] + coords[ps[7]][0])) / 9, (-4 * coords[ps[0]][1] - coords[ps[15]][1] + 6 * (coords[ps[4]][1] + coords[ps[1]][1]) - 2 * (coords[ps[12]][1] + coords[ps[3]][1]) + 3 * (coords[ps[13]][1] + coords[ps[7]][1])) / 9]);
  528. ps[6] = coords.length;
  529. coords.push([(-4 * coords[ps[3]][0] - coords[ps[12]][0] + 6 * (coords[ps[2]][0] + coords[ps[7]][0]) - 2 * (coords[ps[0]][0] + coords[ps[15]][0]) + 3 * (coords[ps[4]][0] + coords[ps[14]][0])) / 9, (-4 * coords[ps[3]][1] - coords[ps[12]][1] + 6 * (coords[ps[2]][1] + coords[ps[7]][1]) - 2 * (coords[ps[0]][1] + coords[ps[15]][1]) + 3 * (coords[ps[4]][1] + coords[ps[14]][1])) / 9]);
  530. ps[9] = coords.length;
  531. coords.push([(-4 * coords[ps[12]][0] - coords[ps[3]][0] + 6 * (coords[ps[8]][0] + coords[ps[13]][0]) - 2 * (coords[ps[0]][0] + coords[ps[15]][0]) + 3 * (coords[ps[11]][0] + coords[ps[1]][0])) / 9, (-4 * coords[ps[12]][1] - coords[ps[3]][1] + 6 * (coords[ps[8]][1] + coords[ps[13]][1]) - 2 * (coords[ps[0]][1] + coords[ps[15]][1]) + 3 * (coords[ps[11]][1] + coords[ps[1]][1])) / 9]);
  532. ps[10] = coords.length;
  533. coords.push([(-4 * coords[ps[15]][0] - coords[ps[0]][0] + 6 * (coords[ps[11]][0] + coords[ps[14]][0]) - 2 * (coords[ps[12]][0] + coords[ps[3]][0]) + 3 * (coords[ps[2]][0] + coords[ps[8]][0])) / 9, (-4 * coords[ps[15]][1] - coords[ps[0]][1] + 6 * (coords[ps[11]][1] + coords[ps[14]][1]) - 2 * (coords[ps[12]][1] + coords[ps[3]][1]) + 3 * (coords[ps[2]][1] + coords[ps[8]][1])) / 9]);
  534. this.figures.push({
  535. type: "patch",
  536. coords: new Int32Array(ps),
  537. colors: new Int32Array(cs)
  538. });
  539. }
  540. }
  541. _decodeType7Shading(reader) {
  542. const coords = this.coords;
  543. const colors = this.colors;
  544. const ps = new Int32Array(16);
  545. const cs = new Int32Array(4);
  546. while (reader.hasData) {
  547. const f = reader.readFlag();
  548. if (!(0 <= f && f <= 3)) {
  549. throw new _util.FormatError("Unknown type7 flag");
  550. }
  551. const pi = coords.length;
  552. for (let i = 0, ii = f !== 0 ? 12 : 16; i < ii; i++) {
  553. coords.push(reader.readCoordinate());
  554. }
  555. const ci = colors.length;
  556. for (let i = 0, ii = f !== 0 ? 2 : 4; i < ii; i++) {
  557. colors.push(reader.readComponents());
  558. }
  559. let tmp1, tmp2, tmp3, tmp4;
  560. switch (f) {
  561. case 0:
  562. ps[12] = pi + 3;
  563. ps[13] = pi + 4;
  564. ps[14] = pi + 5;
  565. ps[15] = pi + 6;
  566. ps[8] = pi + 2;
  567. ps[9] = pi + 13;
  568. ps[10] = pi + 14;
  569. ps[11] = pi + 7;
  570. ps[4] = pi + 1;
  571. ps[5] = pi + 12;
  572. ps[6] = pi + 15;
  573. ps[7] = pi + 8;
  574. ps[0] = pi;
  575. ps[1] = pi + 11;
  576. ps[2] = pi + 10;
  577. ps[3] = pi + 9;
  578. cs[2] = ci + 1;
  579. cs[3] = ci + 2;
  580. cs[0] = ci;
  581. cs[1] = ci + 3;
  582. break;
  583. case 1:
  584. tmp1 = ps[12];
  585. tmp2 = ps[13];
  586. tmp3 = ps[14];
  587. tmp4 = ps[15];
  588. ps[12] = tmp4;
  589. ps[13] = pi + 0;
  590. ps[14] = pi + 1;
  591. ps[15] = pi + 2;
  592. ps[8] = tmp3;
  593. ps[9] = pi + 9;
  594. ps[10] = pi + 10;
  595. ps[11] = pi + 3;
  596. ps[4] = tmp2;
  597. ps[5] = pi + 8;
  598. ps[6] = pi + 11;
  599. ps[7] = pi + 4;
  600. ps[0] = tmp1;
  601. ps[1] = pi + 7;
  602. ps[2] = pi + 6;
  603. ps[3] = pi + 5;
  604. tmp1 = cs[2];
  605. tmp2 = cs[3];
  606. cs[2] = tmp2;
  607. cs[3] = ci;
  608. cs[0] = tmp1;
  609. cs[1] = ci + 1;
  610. break;
  611. case 2:
  612. tmp1 = ps[15];
  613. tmp2 = ps[11];
  614. ps[12] = ps[3];
  615. ps[13] = pi + 0;
  616. ps[14] = pi + 1;
  617. ps[15] = pi + 2;
  618. ps[8] = ps[7];
  619. ps[9] = pi + 9;
  620. ps[10] = pi + 10;
  621. ps[11] = pi + 3;
  622. ps[4] = tmp2;
  623. ps[5] = pi + 8;
  624. ps[6] = pi + 11;
  625. ps[7] = pi + 4;
  626. ps[0] = tmp1;
  627. ps[1] = pi + 7;
  628. ps[2] = pi + 6;
  629. ps[3] = pi + 5;
  630. tmp1 = cs[3];
  631. cs[2] = cs[1];
  632. cs[3] = ci;
  633. cs[0] = tmp1;
  634. cs[1] = ci + 1;
  635. break;
  636. case 3:
  637. ps[12] = ps[0];
  638. ps[13] = pi + 0;
  639. ps[14] = pi + 1;
  640. ps[15] = pi + 2;
  641. ps[8] = ps[1];
  642. ps[9] = pi + 9;
  643. ps[10] = pi + 10;
  644. ps[11] = pi + 3;
  645. ps[4] = ps[2];
  646. ps[5] = pi + 8;
  647. ps[6] = pi + 11;
  648. ps[7] = pi + 4;
  649. ps[0] = ps[3];
  650. ps[1] = pi + 7;
  651. ps[2] = pi + 6;
  652. ps[3] = pi + 5;
  653. cs[2] = cs[0];
  654. cs[3] = ci;
  655. cs[0] = cs[1];
  656. cs[1] = ci + 1;
  657. break;
  658. }
  659. this.figures.push({
  660. type: "patch",
  661. coords: new Int32Array(ps),
  662. colors: new Int32Array(cs)
  663. });
  664. }
  665. }
  666. _buildFigureFromPatch(index) {
  667. const figure = this.figures[index];
  668. (0, _util.assert)(figure.type === "patch", "Unexpected patch mesh figure");
  669. const coords = this.coords,
  670. colors = this.colors;
  671. const pi = figure.coords;
  672. const ci = figure.colors;
  673. const figureMinX = Math.min(coords[pi[0]][0], coords[pi[3]][0], coords[pi[12]][0], coords[pi[15]][0]);
  674. const figureMinY = Math.min(coords[pi[0]][1], coords[pi[3]][1], coords[pi[12]][1], coords[pi[15]][1]);
  675. const figureMaxX = Math.max(coords[pi[0]][0], coords[pi[3]][0], coords[pi[12]][0], coords[pi[15]][0]);
  676. const figureMaxY = Math.max(coords[pi[0]][1], coords[pi[3]][1], coords[pi[12]][1], coords[pi[15]][1]);
  677. let splitXBy = Math.ceil((figureMaxX - figureMinX) * MeshShading.TRIANGLE_DENSITY / (this.bounds[2] - this.bounds[0]));
  678. splitXBy = Math.max(MeshShading.MIN_SPLIT_PATCH_CHUNKS_AMOUNT, Math.min(MeshShading.MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitXBy));
  679. let splitYBy = Math.ceil((figureMaxY - figureMinY) * MeshShading.TRIANGLE_DENSITY / (this.bounds[3] - this.bounds[1]));
  680. splitYBy = Math.max(MeshShading.MIN_SPLIT_PATCH_CHUNKS_AMOUNT, Math.min(MeshShading.MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitYBy));
  681. const verticesPerRow = splitXBy + 1;
  682. const figureCoords = new Int32Array((splitYBy + 1) * verticesPerRow);
  683. const figureColors = new Int32Array((splitYBy + 1) * verticesPerRow);
  684. let k = 0;
  685. const cl = new Uint8Array(3),
  686. cr = new Uint8Array(3);
  687. const c0 = colors[ci[0]],
  688. c1 = colors[ci[1]],
  689. c2 = colors[ci[2]],
  690. c3 = colors[ci[3]];
  691. const bRow = getB(splitYBy),
  692. bCol = getB(splitXBy);
  693. for (let row = 0; row <= splitYBy; row++) {
  694. cl[0] = (c0[0] * (splitYBy - row) + c2[0] * row) / splitYBy | 0;
  695. cl[1] = (c0[1] * (splitYBy - row) + c2[1] * row) / splitYBy | 0;
  696. cl[2] = (c0[2] * (splitYBy - row) + c2[2] * row) / splitYBy | 0;
  697. cr[0] = (c1[0] * (splitYBy - row) + c3[0] * row) / splitYBy | 0;
  698. cr[1] = (c1[1] * (splitYBy - row) + c3[1] * row) / splitYBy | 0;
  699. cr[2] = (c1[2] * (splitYBy - row) + c3[2] * row) / splitYBy | 0;
  700. for (let col = 0; col <= splitXBy; col++, k++) {
  701. if ((row === 0 || row === splitYBy) && (col === 0 || col === splitXBy)) {
  702. continue;
  703. }
  704. let x = 0,
  705. y = 0;
  706. let q = 0;
  707. for (let i = 0; i <= 3; i++) {
  708. for (let j = 0; j <= 3; j++, q++) {
  709. const m = bRow[row][i] * bCol[col][j];
  710. x += coords[pi[q]][0] * m;
  711. y += coords[pi[q]][1] * m;
  712. }
  713. }
  714. figureCoords[k] = coords.length;
  715. coords.push([x, y]);
  716. figureColors[k] = colors.length;
  717. const newColor = new Uint8Array(3);
  718. newColor[0] = (cl[0] * (splitXBy - col) + cr[0] * col) / splitXBy | 0;
  719. newColor[1] = (cl[1] * (splitXBy - col) + cr[1] * col) / splitXBy | 0;
  720. newColor[2] = (cl[2] * (splitXBy - col) + cr[2] * col) / splitXBy | 0;
  721. colors.push(newColor);
  722. }
  723. }
  724. figureCoords[0] = pi[0];
  725. figureColors[0] = ci[0];
  726. figureCoords[splitXBy] = pi[3];
  727. figureColors[splitXBy] = ci[1];
  728. figureCoords[verticesPerRow * splitYBy] = pi[12];
  729. figureColors[verticesPerRow * splitYBy] = ci[2];
  730. figureCoords[verticesPerRow * splitYBy + splitXBy] = pi[15];
  731. figureColors[verticesPerRow * splitYBy + splitXBy] = ci[3];
  732. this.figures[index] = {
  733. type: "lattice",
  734. coords: figureCoords,
  735. colors: figureColors,
  736. verticesPerRow
  737. };
  738. }
  739. _updateBounds() {
  740. let minX = this.coords[0][0],
  741. minY = this.coords[0][1],
  742. maxX = minX,
  743. maxY = minY;
  744. for (let i = 1, ii = this.coords.length; i < ii; i++) {
  745. const x = this.coords[i][0],
  746. y = this.coords[i][1];
  747. minX = minX > x ? x : minX;
  748. minY = minY > y ? y : minY;
  749. maxX = maxX < x ? x : maxX;
  750. maxY = maxY < y ? y : maxY;
  751. }
  752. this.bounds = [minX, minY, maxX, maxY];
  753. }
  754. _packData() {
  755. let i, ii, j, jj;
  756. const coords = this.coords;
  757. const coordsPacked = new Float32Array(coords.length * 2);
  758. for (i = 0, j = 0, ii = coords.length; i < ii; i++) {
  759. const xy = coords[i];
  760. coordsPacked[j++] = xy[0];
  761. coordsPacked[j++] = xy[1];
  762. }
  763. this.coords = coordsPacked;
  764. const colors = this.colors;
  765. const colorsPacked = new Uint8Array(colors.length * 3);
  766. for (i = 0, j = 0, ii = colors.length; i < ii; i++) {
  767. const c = colors[i];
  768. colorsPacked[j++] = c[0];
  769. colorsPacked[j++] = c[1];
  770. colorsPacked[j++] = c[2];
  771. }
  772. this.colors = colorsPacked;
  773. const figures = this.figures;
  774. for (i = 0, ii = figures.length; i < ii; i++) {
  775. const figure = figures[i],
  776. ps = figure.coords,
  777. cs = figure.colors;
  778. for (j = 0, jj = ps.length; j < jj; j++) {
  779. ps[j] *= 2;
  780. cs[j] *= 3;
  781. }
  782. }
  783. }
  784. getIR() {
  785. return ["Mesh", this.shadingType, this.coords, this.colors, this.figures, this.bounds, this.matrix, this.bbox, this.background];
  786. }
  787. }
  788. class DummyShading extends BaseShading {
  789. getIR() {
  790. return ["Dummy"];
  791. }
  792. }
  793. function getTilingPatternIR(operatorList, dict, color) {
  794. const matrix = dict.getArray("Matrix");
  795. const bbox = _util.Util.normalizeRect(dict.getArray("BBox"));
  796. const xstep = dict.get("XStep");
  797. const ystep = dict.get("YStep");
  798. const paintType = dict.get("PaintType");
  799. const tilingType = dict.get("TilingType");
  800. if (bbox[2] - bbox[0] === 0 || bbox[3] - bbox[1] === 0) {
  801. throw new _util.FormatError(`Invalid getTilingPatternIR /BBox array: [${bbox}].`);
  802. }
  803. return ["TilingPattern", color, operatorList, matrix, bbox, xstep, ystep, paintType, tilingType];
  804. }