@poe-platform/safe-bash 0.1.758 → 0.1.760

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/image-ast/index.browser.js +41 -16
  2. package/dist/image-ast/index.js +41 -16
  3. package/dist/pdf-ast/ast.d.ts +8 -3
  4. package/dist/pdf-ast/index.js +45 -19
  5. package/dist/safe-bash/chunks/{chunk-WNWIBBJZ.js → chunk-CRQDMBQA.js} +130 -9
  6. package/dist/safe-bash/command-chunks/{chunk-L2C5TIII.js → chunk-MC4XXPQJ.js} +2 -2
  7. package/dist/safe-bash/command-chunks/{chunk-TCAGGM44.js → chunk-S3SCPBHC.js} +41 -17
  8. package/dist/safe-bash/command-chunks/{chunk-XYCWDGOG.js → chunk-UCI6ZQFB.js} +1 -1
  9. package/dist/safe-bash/commands/python/index.browser.js +1 -1
  10. package/dist/safe-bash/commands/python/index.d.ts +1 -1
  11. package/dist/safe-bash/commands/python/llm-capability.d.ts +7 -3
  12. package/dist/safe-bash/commands/python/llm-capability.js +149 -10
  13. package/dist/safe-bash/commands/python/shell-capability.js +10 -2
  14. package/dist/safe-bash/core.browser.js +1 -1
  15. package/dist/safe-bash-command-ffmpeg/index.js +3513 -3500
  16. package/dist/safe-bash-command-gh/index.js +128 -27
  17. package/dist/safe-bash-command-imagemagick/index.js +2 -2
  18. package/dist/safe-bash-command-pandoc/command.js +3 -3
  19. package/dist/safe-bash-command-pandoc/index.js +3 -3
  20. package/dist/safe-bash-command-pdfimages/index.js +41 -16
  21. package/dist/safe-bash-command-pdfinfo/index.js +45 -19
  22. package/dist/safe-bash-command-pdftk/index.js +41 -16
  23. package/dist/safe-bash-command-pdftoppm/index.js +45 -19
  24. package/dist/safe-bash-command-pdftotext/index.js +41 -16
  25. package/dist/safe-bash-command-qpdf/index.js +41 -16
  26. package/dist/safe-bash-command-sips/index.js +2 -2
  27. package/dist/safe-bash-command-soffice/index.js +1 -1
  28. package/dist/safe-bash-command-ssconvert/index.js +63 -38
  29. package/dist/safe-bash-command-wkhtmltopdf/index.js +42 -16
  30. package/dist/safe-bash-io-engine/internal.d.ts +2 -2
  31. package/dist/safe-bash-mcp/index.d.ts +1 -1
  32. package/package.json +2 -2
  33. package/third-party/pdf-ast/THIRD_PARTY_NOTICES.md +22 -0
@@ -656,6 +656,7 @@ var messages_default = {
656
656
  "-5": "buffer error",
657
657
  "-6": "incompatible version"
658
658
  };
659
+ var Z_BUF_ERROR = -5;
659
660
  var MAX_MEM_LEVEL = 9;
660
661
  var HEAP_SIZE = 573;
661
662
  var MIN_MATCH = 3;
@@ -22741,22 +22742,31 @@ function decodeFlate(bytes, parms, maxDecodedBytes = DEFAULT_MAX_DECODED_BYTES)
22741
22742
  inflated = new BoundedFlateStream(bytes, maxDecodedBytes).getBytes();
22742
22743
  } catch (error) {
22743
22744
  if (error instanceof PdfError && error.code === "E_LIMIT") throw error;
22745
+ const wrapped = bytes[0] === 31 && bytes[1] === 139 || bytes.length >= 2 && (bytes[0] & 15) === 8 && bytes[0] >>> 4 <= 7 && (bytes[0] << 8 | bytes[1]) % 31 === 0;
22744
22746
  const decoder = new Inflate({
22745
- raw: !(bytes[0] === 31 && bytes[1] === 139),
22747
+ raw: !wrapped,
22746
22748
  chunkSize: Math.max(1, Math.min(64 * 1024, maxDecodedBytes + 1))
22747
22749
  });
22750
+ const chunks = [];
22748
22751
  let length = 0;
22749
22752
  decoder.onData = (chunk) => {
22750
22753
  length += chunk.length;
22751
22754
  if (length > maxDecodedBytes) {
22752
22755
  throw new PdfError("E_LIMIT", "FlateDecode output exceeds maximum decoded byte budget");
22753
22756
  }
22754
- decoder.chunks.push(chunk);
22757
+ chunks.push(chunk);
22755
22758
  };
22756
- if (!decoder.push(bytes, true) || decoder.err || !decoder.ended) {
22759
+ const complete = decoder.push(bytes, true);
22760
+ const truncated = wrapped && decoder.err === Z_BUF_ERROR && length > 0;
22761
+ if ((!complete || decoder.err || !decoder.ended) && !truncated) {
22757
22762
  throw new PdfError("E_CAPABILITY", "Invalid FlateDecode compressed stream");
22758
22763
  }
22759
- inflated = decoder.result;
22764
+ inflated = new Uint8Array(length);
22765
+ let offset = 0;
22766
+ for (const chunk of chunks) {
22767
+ inflated.set(chunk, offset);
22768
+ offset += chunk.length;
22769
+ }
22760
22770
  }
22761
22771
  return applyPredictor(inflated, parms);
22762
22772
  }
@@ -34990,7 +35000,7 @@ function resolvePageFonts(doc, resourcesDict) {
34990
35000
  const glyphNames = buildFontEncodingGlyphNamesMap(encNode);
34991
35001
  const widths = /* @__PURE__ */ new Map();
34992
35002
  let defaultWidth = 556;
34993
- const isTwoByteCid = subtype === "Type0" || Boolean(cmap?.isTwoByte);
35003
+ const isTwoByteCid = subtype === "Type0";
34994
35004
  let fontMatrix;
34995
35005
  let charProcs;
34996
35006
  let fontResources;
@@ -36443,7 +36453,7 @@ function evaluateContentStreamToDisplayList(params) {
36443
36453
  const [x, y] = transformPoint(st.ctm, seg.x, seg.y);
36444
36454
  transformedSegments.push({ kind: "cubic", x1, y1, x2, y2, x, y });
36445
36455
  } else if (seg.kind === "rect") {
36446
- if (hasRotOrShear) {
36456
+ if (hasRotOrShear || seg.width * st.ctm[0] < 0 || seg.height * st.ctm[3] < 0) {
36447
36457
  const [p0x, p0y] = transformPoint(st.ctm, seg.x, seg.y);
36448
36458
  const [p1x, p1y] = transformPoint(st.ctm, seg.x + seg.width, seg.y);
36449
36459
  const [p2x, p2y] = transformPoint(st.ctm, seg.x + seg.width, seg.y + seg.height);
@@ -36476,7 +36486,12 @@ function evaluateContentStreamToDisplayList(params) {
36476
36486
  transformedSegments.push({ kind: "close" });
36477
36487
  }
36478
36488
  }
36479
- if (node.clip && transformedSegments.length > 0) {
36489
+ const applyClip = () => {
36490
+ if (!node.clip) return;
36491
+ st.clipPaths = [...st.clipPaths ?? [], {
36492
+ segments: transformedSegments,
36493
+ fillRule: node.clip === "W*" ? "evenodd" : "nonzero"
36494
+ }];
36480
36495
  let cMinX = Infinity, cMinY = Infinity, cMaxX = -Infinity, cMaxY = -Infinity;
36481
36496
  for (const s of transformedSegments) {
36482
36497
  if (s.kind === "move" || s.kind === "line") {
@@ -36504,8 +36519,11 @@ function evaluateContentStreamToDisplayList(params) {
36504
36519
  Math.min(st.clipRect[3], cMaxY)
36505
36520
  ] : [cMinX, cMinY, cMaxX, cMaxY];
36506
36521
  }
36522
+ };
36523
+ if (node.paint === "n") {
36524
+ applyClip();
36525
+ break;
36507
36526
  }
36508
- if (node.paint === "n") break;
36509
36527
  const isFill = ["f", "F", "f*", "B", "B*", "b", "b*"].includes(node.paint);
36510
36528
  const isStroke = ["S", "s", "B", "B*", "b", "b*"].includes(node.paint);
36511
36529
  let evaluatedFillPattern = false;
@@ -36638,7 +36656,10 @@ function evaluateContentStreamToDisplayList(params) {
36638
36656
  }
36639
36657
  }
36640
36658
  }
36641
- if (evaluatedFillPattern && !isStroke) break;
36659
+ if (evaluatedFillPattern && !isStroke) {
36660
+ applyClip();
36661
+ break;
36662
+ }
36642
36663
  const fillRule = node.paint.includes("*") ? "evenodd" : "nonzero";
36643
36664
  const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36644
36665
  const scaledStrokeWidth = st.strokeWidth === 0 ? 0 : st.strokeWidth * ctmScale;
@@ -36658,6 +36679,7 @@ function evaluateContentStreamToDisplayList(params) {
36658
36679
  ...st.dashPhase !== void 0 ? { dashPhase: st.dashPhase } : {},
36659
36680
  ...st.clipRect ? { clipRect: [...st.clipRect] } : {}
36660
36681
  } });
36682
+ applyClip();
36661
36683
  break;
36662
36684
  }
36663
36685
  case "xobject": {
@@ -40377,8 +40399,8 @@ function segmentsToScreenEdges(segments, pageHeight, scale, closeSubpaths = fals
40377
40399
  if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40378
40400
  edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40379
40401
  }
40380
- const [rx0, ry0] = toScreen(seg.x, seg.y + seg.height);
40381
- const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y);
40402
+ const [rx0, ry0] = toScreen(seg.x, seg.y);
40403
+ const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y + seg.height);
40382
40404
  edges.push(
40383
40405
  { x0: rx0, y0: ry0, x1: rx1, y1: ry0 },
40384
40406
  { x0: rx1, y0: ry0, x1: rx1, y1: ry1 },
@@ -40521,21 +40543,24 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40521
40543
  }
40522
40544
  const aaVec = options.antialiasVector !== false;
40523
40545
  const aaTxt = options.antialiasText !== false;
40524
- const clipMasks = /* @__PURE__ */ new Map();
40546
+ let cachedClips;
40547
+ let cachedClipMask;
40525
40548
  const imageClipMasks = /* @__PURE__ */ new Map();
40526
40549
  for (const original of paintOperations(displayList)) {
40527
40550
  if (original.kind === "glyph" && (original.value.renderMode === 3 || !original.value.outline && !original.value.unicode.trim())) continue;
40528
40551
  const operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
40529
40552
  const clips = original.value.clipPaths;
40530
- let clipMask = clips ? clipMasks.get(clips) : void 0;
40553
+ let clipMask = clips && clips === cachedClips ? cachedClipMask : void 0;
40531
40554
  if (clips && !clipMask) {
40532
40555
  clipMask = new Uint8Array(width * height * 4).fill(255);
40533
- for (const segments of clips) {
40556
+ for (const clip of clips) {
40557
+ const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40534
40558
  const layer = new Uint8Array(width * height * 4);
40535
- fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 });
40559
+ fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40536
40560
  for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
40537
40561
  }
40538
- clipMasks.set(clips, clipMask);
40562
+ cachedClips = clips;
40563
+ cachedClipMask = clipMask;
40539
40564
  }
40540
40565
  const imageClips = original.value.clipImages;
40541
40566
  if (imageClips) {
@@ -40729,7 +40754,7 @@ function svgPathData(segments, height) {
40729
40754
  } else if (seg.kind === "cubic") {
40730
40755
  dParts.push(`C ${seg.x1} ${height - seg.y1} ${seg.x2} ${height - seg.y2} ${seg.x} ${height - seg.y}`);
40731
40756
  } else if (seg.kind === "rect") {
40732
- dParts.push(`M ${seg.x} ${height - seg.y - seg.height} h ${seg.width} v ${seg.height} h ${-seg.width} Z`);
40757
+ dParts.push(`M ${seg.x} ${height - seg.y} h ${seg.width} v ${-seg.height} h ${-seg.width} Z`);
40733
40758
  } else if (seg.kind === "close") {
40734
40759
  dParts.push("Z");
40735
40760
  }
@@ -40768,9 +40793,10 @@ function renderDisplayListToSvg(displayList, options = {}) {
40768
40793
  const operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
40769
40794
  const clips = original.value.clipPaths ?? [];
40770
40795
  const imageClips = original.value.clipImages ?? [];
40771
- for (const segments of clips) {
40796
+ for (const clip of clips) {
40797
+ const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40772
40798
  const id = `text-clip-${clipId++}`;
40773
- const path = `<path d="${svgPathData(segments, displayList.height)}"/>`;
40799
+ const path = `<path d="${svgPathData(segments, displayList.height)}" clip-rule="${fillRule}"/>`;
40774
40800
  parts.push(`<defs><clipPath id="${id}" clipPathUnits="userSpaceOnUse">${path}</clipPath></defs><g clip-path="url(#${id})">`);
40775
40801
  }
40776
40802
  for (const image of imageClips) {
@@ -656,6 +656,7 @@ var messages_default = {
656
656
  "-5": "buffer error",
657
657
  "-6": "incompatible version"
658
658
  };
659
+ var Z_BUF_ERROR = -5;
659
660
  var MAX_MEM_LEVEL = 9;
660
661
  var HEAP_SIZE = 573;
661
662
  var MIN_MATCH = 3;
@@ -22547,22 +22548,31 @@ function decodeFlate(bytes, parms, maxDecodedBytes = DEFAULT_MAX_DECODED_BYTES)
22547
22548
  inflated = new BoundedFlateStream(bytes, maxDecodedBytes).getBytes();
22548
22549
  } catch (error) {
22549
22550
  if (error instanceof PdfError && error.code === "E_LIMIT") throw error;
22551
+ const wrapped = bytes[0] === 31 && bytes[1] === 139 || bytes.length >= 2 && (bytes[0] & 15) === 8 && bytes[0] >>> 4 <= 7 && (bytes[0] << 8 | bytes[1]) % 31 === 0;
22550
22552
  const decoder = new Inflate({
22551
- raw: !(bytes[0] === 31 && bytes[1] === 139),
22553
+ raw: !wrapped,
22552
22554
  chunkSize: Math.max(1, Math.min(64 * 1024, maxDecodedBytes + 1))
22553
22555
  });
22556
+ const chunks = [];
22554
22557
  let length = 0;
22555
22558
  decoder.onData = (chunk) => {
22556
22559
  length += chunk.length;
22557
22560
  if (length > maxDecodedBytes) {
22558
22561
  throw new PdfError("E_LIMIT", "FlateDecode output exceeds maximum decoded byte budget");
22559
22562
  }
22560
- decoder.chunks.push(chunk);
22563
+ chunks.push(chunk);
22561
22564
  };
22562
- if (!decoder.push(bytes, true) || decoder.err || !decoder.ended) {
22565
+ const complete = decoder.push(bytes, true);
22566
+ const truncated = wrapped && decoder.err === Z_BUF_ERROR && length > 0;
22567
+ if ((!complete || decoder.err || !decoder.ended) && !truncated) {
22563
22568
  throw new PdfError("E_CAPABILITY", "Invalid FlateDecode compressed stream");
22564
22569
  }
22565
- inflated = decoder.result;
22570
+ inflated = new Uint8Array(length);
22571
+ let offset = 0;
22572
+ for (const chunk of chunks) {
22573
+ inflated.set(chunk, offset);
22574
+ offset += chunk.length;
22575
+ }
22566
22576
  }
22567
22577
  return applyPredictor(inflated, parms);
22568
22578
  }
@@ -34451,7 +34461,7 @@ function resolvePageFonts(doc, resourcesDict) {
34451
34461
  const glyphNames = buildFontEncodingGlyphNamesMap(encNode);
34452
34462
  const widths = /* @__PURE__ */ new Map();
34453
34463
  let defaultWidth = 556;
34454
- const isTwoByteCid = subtype === "Type0" || Boolean(cmap?.isTwoByte);
34464
+ const isTwoByteCid = subtype === "Type0";
34455
34465
  let fontMatrix;
34456
34466
  let charProcs;
34457
34467
  let fontResources;
@@ -35904,7 +35914,7 @@ function evaluateContentStreamToDisplayList(params) {
35904
35914
  const [x, y] = transformPoint(st.ctm, seg.x, seg.y);
35905
35915
  transformedSegments.push({ kind: "cubic", x1, y1, x2, y2, x, y });
35906
35916
  } else if (seg.kind === "rect") {
35907
- if (hasRotOrShear) {
35917
+ if (hasRotOrShear || seg.width * st.ctm[0] < 0 || seg.height * st.ctm[3] < 0) {
35908
35918
  const [p0x, p0y] = transformPoint(st.ctm, seg.x, seg.y);
35909
35919
  const [p1x, p1y] = transformPoint(st.ctm, seg.x + seg.width, seg.y);
35910
35920
  const [p2x, p2y] = transformPoint(st.ctm, seg.x + seg.width, seg.y + seg.height);
@@ -35937,7 +35947,12 @@ function evaluateContentStreamToDisplayList(params) {
35937
35947
  transformedSegments.push({ kind: "close" });
35938
35948
  }
35939
35949
  }
35940
- if (node.clip && transformedSegments.length > 0) {
35950
+ const applyClip = () => {
35951
+ if (!node.clip) return;
35952
+ st.clipPaths = [...st.clipPaths ?? [], {
35953
+ segments: transformedSegments,
35954
+ fillRule: node.clip === "W*" ? "evenodd" : "nonzero"
35955
+ }];
35941
35956
  let cMinX = Infinity, cMinY = Infinity, cMaxX = -Infinity, cMaxY = -Infinity;
35942
35957
  for (const s of transformedSegments) {
35943
35958
  if (s.kind === "move" || s.kind === "line") {
@@ -35965,8 +35980,11 @@ function evaluateContentStreamToDisplayList(params) {
35965
35980
  Math.min(st.clipRect[3], cMaxY)
35966
35981
  ] : [cMinX, cMinY, cMaxX, cMaxY];
35967
35982
  }
35983
+ };
35984
+ if (node.paint === "n") {
35985
+ applyClip();
35986
+ break;
35968
35987
  }
35969
- if (node.paint === "n") break;
35970
35988
  const isFill = ["f", "F", "f*", "B", "B*", "b", "b*"].includes(node.paint);
35971
35989
  const isStroke = ["S", "s", "B", "B*", "b", "b*"].includes(node.paint);
35972
35990
  let evaluatedFillPattern = false;
@@ -36099,7 +36117,10 @@ function evaluateContentStreamToDisplayList(params) {
36099
36117
  }
36100
36118
  }
36101
36119
  }
36102
- if (evaluatedFillPattern && !isStroke) break;
36120
+ if (evaluatedFillPattern && !isStroke) {
36121
+ applyClip();
36122
+ break;
36123
+ }
36103
36124
  const fillRule = node.paint.includes("*") ? "evenodd" : "nonzero";
36104
36125
  const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36105
36126
  const scaledStrokeWidth = st.strokeWidth === 0 ? 0 : st.strokeWidth * ctmScale;
@@ -36119,6 +36140,7 @@ function evaluateContentStreamToDisplayList(params) {
36119
36140
  ...st.dashPhase !== void 0 ? { dashPhase: st.dashPhase } : {},
36120
36141
  ...st.clipRect ? { clipRect: [...st.clipRect] } : {}
36121
36142
  } });
36143
+ applyClip();
36122
36144
  break;
36123
36145
  }
36124
36146
  case "xobject": {
@@ -40006,8 +40028,8 @@ function segmentsToScreenEdges(segments, pageHeight, scale, closeSubpaths = fals
40006
40028
  if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40007
40029
  edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40008
40030
  }
40009
- const [rx0, ry0] = toScreen(seg.x, seg.y + seg.height);
40010
- const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y);
40031
+ const [rx0, ry0] = toScreen(seg.x, seg.y);
40032
+ const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y + seg.height);
40011
40033
  edges.push(
40012
40034
  { x0: rx0, y0: ry0, x1: rx1, y1: ry0 },
40013
40035
  { x0: rx1, y0: ry0, x1: rx1, y1: ry1 },
@@ -40150,21 +40172,24 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40150
40172
  }
40151
40173
  const aaVec = options.antialiasVector !== false;
40152
40174
  const aaTxt = options.antialiasText !== false;
40153
- const clipMasks = /* @__PURE__ */ new Map();
40175
+ let cachedClips;
40176
+ let cachedClipMask;
40154
40177
  const imageClipMasks = /* @__PURE__ */ new Map();
40155
40178
  for (const original of paintOperations(displayList)) {
40156
40179
  if (original.kind === "glyph" && (original.value.renderMode === 3 || !original.value.outline && !original.value.unicode.trim())) continue;
40157
40180
  const operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
40158
40181
  const clips = original.value.clipPaths;
40159
- let clipMask = clips ? clipMasks.get(clips) : void 0;
40182
+ let clipMask = clips && clips === cachedClips ? cachedClipMask : void 0;
40160
40183
  if (clips && !clipMask) {
40161
40184
  clipMask = new Uint8Array(width * height * 4).fill(255);
40162
- for (const segments of clips) {
40185
+ for (const clip of clips) {
40186
+ const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40163
40187
  const layer = new Uint8Array(width * height * 4);
40164
- fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 });
40188
+ fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40165
40189
  for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
40166
40190
  }
40167
- clipMasks.set(clips, clipMask);
40191
+ cachedClips = clips;
40192
+ cachedClipMask = clipMask;
40168
40193
  }
40169
40194
  const imageClips = original.value.clipImages;
40170
40195
  if (imageClips) {
@@ -656,6 +656,7 @@ var messages_default = {
656
656
  "-5": "buffer error",
657
657
  "-6": "incompatible version"
658
658
  };
659
+ var Z_BUF_ERROR = -5;
659
660
  var MAX_MEM_LEVEL = 9;
660
661
  var HEAP_SIZE = 573;
661
662
  var MIN_MATCH = 3;
@@ -22741,22 +22742,31 @@ function decodeFlate(bytes, parms, maxDecodedBytes = DEFAULT_MAX_DECODED_BYTES)
22741
22742
  inflated = new BoundedFlateStream(bytes, maxDecodedBytes).getBytes();
22742
22743
  } catch (error) {
22743
22744
  if (error instanceof PdfError && error.code === "E_LIMIT") throw error;
22745
+ const wrapped = bytes[0] === 31 && bytes[1] === 139 || bytes.length >= 2 && (bytes[0] & 15) === 8 && bytes[0] >>> 4 <= 7 && (bytes[0] << 8 | bytes[1]) % 31 === 0;
22744
22746
  const decoder = new Inflate({
22745
- raw: !(bytes[0] === 31 && bytes[1] === 139),
22747
+ raw: !wrapped,
22746
22748
  chunkSize: Math.max(1, Math.min(64 * 1024, maxDecodedBytes + 1))
22747
22749
  });
22750
+ const chunks = [];
22748
22751
  let length = 0;
22749
22752
  decoder.onData = (chunk) => {
22750
22753
  length += chunk.length;
22751
22754
  if (length > maxDecodedBytes) {
22752
22755
  throw new PdfError("E_LIMIT", "FlateDecode output exceeds maximum decoded byte budget");
22753
22756
  }
22754
- decoder.chunks.push(chunk);
22757
+ chunks.push(chunk);
22755
22758
  };
22756
- if (!decoder.push(bytes, true) || decoder.err || !decoder.ended) {
22759
+ const complete = decoder.push(bytes, true);
22760
+ const truncated = wrapped && decoder.err === Z_BUF_ERROR && length > 0;
22761
+ if ((!complete || decoder.err || !decoder.ended) && !truncated) {
22757
22762
  throw new PdfError("E_CAPABILITY", "Invalid FlateDecode compressed stream");
22758
22763
  }
22759
- inflated = decoder.result;
22764
+ inflated = new Uint8Array(length);
22765
+ let offset = 0;
22766
+ for (const chunk of chunks) {
22767
+ inflated.set(chunk, offset);
22768
+ offset += chunk.length;
22769
+ }
22760
22770
  }
22761
22771
  return applyPredictor(inflated, parms);
22762
22772
  }
@@ -34645,7 +34655,7 @@ function resolvePageFonts(doc, resourcesDict) {
34645
34655
  const glyphNames = buildFontEncodingGlyphNamesMap(encNode);
34646
34656
  const widths = /* @__PURE__ */ new Map();
34647
34657
  let defaultWidth = 556;
34648
- const isTwoByteCid = subtype === "Type0" || Boolean(cmap?.isTwoByte);
34658
+ const isTwoByteCid = subtype === "Type0";
34649
34659
  let fontMatrix;
34650
34660
  let charProcs;
34651
34661
  let fontResources;
@@ -36098,7 +36108,7 @@ function evaluateContentStreamToDisplayList(params) {
36098
36108
  const [x, y] = transformPoint(st.ctm, seg.x, seg.y);
36099
36109
  transformedSegments.push({ kind: "cubic", x1, y1, x2, y2, x, y });
36100
36110
  } else if (seg.kind === "rect") {
36101
- if (hasRotOrShear) {
36111
+ if (hasRotOrShear || seg.width * st.ctm[0] < 0 || seg.height * st.ctm[3] < 0) {
36102
36112
  const [p0x, p0y] = transformPoint(st.ctm, seg.x, seg.y);
36103
36113
  const [p1x, p1y] = transformPoint(st.ctm, seg.x + seg.width, seg.y);
36104
36114
  const [p2x, p2y] = transformPoint(st.ctm, seg.x + seg.width, seg.y + seg.height);
@@ -36131,7 +36141,12 @@ function evaluateContentStreamToDisplayList(params) {
36131
36141
  transformedSegments.push({ kind: "close" });
36132
36142
  }
36133
36143
  }
36134
- if (node.clip && transformedSegments.length > 0) {
36144
+ const applyClip = () => {
36145
+ if (!node.clip) return;
36146
+ st.clipPaths = [...st.clipPaths ?? [], {
36147
+ segments: transformedSegments,
36148
+ fillRule: node.clip === "W*" ? "evenodd" : "nonzero"
36149
+ }];
36135
36150
  let cMinX = Infinity, cMinY = Infinity, cMaxX = -Infinity, cMaxY = -Infinity;
36136
36151
  for (const s of transformedSegments) {
36137
36152
  if (s.kind === "move" || s.kind === "line") {
@@ -36159,8 +36174,11 @@ function evaluateContentStreamToDisplayList(params) {
36159
36174
  Math.min(st.clipRect[3], cMaxY)
36160
36175
  ] : [cMinX, cMinY, cMaxX, cMaxY];
36161
36176
  }
36177
+ };
36178
+ if (node.paint === "n") {
36179
+ applyClip();
36180
+ break;
36162
36181
  }
36163
- if (node.paint === "n") break;
36164
36182
  const isFill = ["f", "F", "f*", "B", "B*", "b", "b*"].includes(node.paint);
36165
36183
  const isStroke = ["S", "s", "B", "B*", "b", "b*"].includes(node.paint);
36166
36184
  let evaluatedFillPattern = false;
@@ -36293,7 +36311,10 @@ function evaluateContentStreamToDisplayList(params) {
36293
36311
  }
36294
36312
  }
36295
36313
  }
36296
- if (evaluatedFillPattern && !isStroke) break;
36314
+ if (evaluatedFillPattern && !isStroke) {
36315
+ applyClip();
36316
+ break;
36317
+ }
36297
36318
  const fillRule = node.paint.includes("*") ? "evenodd" : "nonzero";
36298
36319
  const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36299
36320
  const scaledStrokeWidth = st.strokeWidth === 0 ? 0 : st.strokeWidth * ctmScale;
@@ -36313,6 +36334,7 @@ function evaluateContentStreamToDisplayList(params) {
36313
36334
  ...st.dashPhase !== void 0 ? { dashPhase: st.dashPhase } : {},
36314
36335
  ...st.clipRect ? { clipRect: [...st.clipRect] } : {}
36315
36336
  } });
36337
+ applyClip();
36316
36338
  break;
36317
36339
  }
36318
36340
  case "xobject": {
@@ -40032,8 +40054,8 @@ function segmentsToScreenEdges(segments, pageHeight, scale, closeSubpaths = fals
40032
40054
  if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40033
40055
  edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40034
40056
  }
40035
- const [rx0, ry0] = toScreen(seg.x, seg.y + seg.height);
40036
- const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y);
40057
+ const [rx0, ry0] = toScreen(seg.x, seg.y);
40058
+ const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y + seg.height);
40037
40059
  edges.push(
40038
40060
  { x0: rx0, y0: ry0, x1: rx1, y1: ry0 },
40039
40061
  { x0: rx1, y0: ry0, x1: rx1, y1: ry1 },
@@ -40176,21 +40198,24 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40176
40198
  }
40177
40199
  const aaVec = options.antialiasVector !== false;
40178
40200
  const aaTxt = options.antialiasText !== false;
40179
- const clipMasks = /* @__PURE__ */ new Map();
40201
+ let cachedClips;
40202
+ let cachedClipMask;
40180
40203
  const imageClipMasks = /* @__PURE__ */ new Map();
40181
40204
  for (const original of paintOperations(displayList)) {
40182
40205
  if (original.kind === "glyph" && (original.value.renderMode === 3 || !original.value.outline && !original.value.unicode.trim())) continue;
40183
40206
  const operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
40184
40207
  const clips = original.value.clipPaths;
40185
- let clipMask = clips ? clipMasks.get(clips) : void 0;
40208
+ let clipMask = clips && clips === cachedClips ? cachedClipMask : void 0;
40186
40209
  if (clips && !clipMask) {
40187
40210
  clipMask = new Uint8Array(width * height * 4).fill(255);
40188
- for (const segments of clips) {
40211
+ for (const clip of clips) {
40212
+ const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40189
40213
  const layer = new Uint8Array(width * height * 4);
40190
- fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 });
40214
+ fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40191
40215
  for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
40192
40216
  }
40193
- clipMasks.set(clips, clipMask);
40217
+ cachedClips = clips;
40218
+ cachedClipMask = clipMask;
40194
40219
  }
40195
40220
  const imageClips = original.value.clipImages;
40196
40221
  if (imageClips) {
@@ -40384,7 +40409,7 @@ function svgPathData(segments, height) {
40384
40409
  } else if (seg.kind === "cubic") {
40385
40410
  dParts.push(`C ${seg.x1} ${height - seg.y1} ${seg.x2} ${height - seg.y2} ${seg.x} ${height - seg.y}`);
40386
40411
  } else if (seg.kind === "rect") {
40387
- dParts.push(`M ${seg.x} ${height - seg.y - seg.height} h ${seg.width} v ${seg.height} h ${-seg.width} Z`);
40412
+ dParts.push(`M ${seg.x} ${height - seg.y} h ${seg.width} v ${-seg.height} h ${-seg.width} Z`);
40388
40413
  } else if (seg.kind === "close") {
40389
40414
  dParts.push("Z");
40390
40415
  }
@@ -40423,9 +40448,10 @@ function renderDisplayListToSvg(displayList, options = {}) {
40423
40448
  const operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
40424
40449
  const clips = original.value.clipPaths ?? [];
40425
40450
  const imageClips = original.value.clipImages ?? [];
40426
- for (const segments of clips) {
40451
+ for (const clip of clips) {
40452
+ const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40427
40453
  const id = `text-clip-${clipId++}`;
40428
- const path = `<path d="${svgPathData(segments, displayList.height)}"/>`;
40454
+ const path = `<path d="${svgPathData(segments, displayList.height)}" clip-rule="${fillRule}"/>`;
40429
40455
  parts.push(`<defs><clipPath id="${id}" clipPathUnits="userSpaceOnUse">${path}</clipPath></defs><g clip-path="url(#${id})">`);
40430
40456
  }
40431
40457
  for (const image of imageClips) {
@@ -656,6 +656,7 @@ var messages_default = {
656
656
  "-5": "buffer error",
657
657
  "-6": "incompatible version"
658
658
  };
659
+ var Z_BUF_ERROR = -5;
659
660
  var MAX_MEM_LEVEL = 9;
660
661
  var HEAP_SIZE = 573;
661
662
  var MIN_MATCH = 3;
@@ -22547,22 +22548,31 @@ function decodeFlate(bytes, parms, maxDecodedBytes = DEFAULT_MAX_DECODED_BYTES)
22547
22548
  inflated = new BoundedFlateStream(bytes, maxDecodedBytes).getBytes();
22548
22549
  } catch (error) {
22549
22550
  if (error instanceof PdfError && error.code === "E_LIMIT") throw error;
22551
+ const wrapped = bytes[0] === 31 && bytes[1] === 139 || bytes.length >= 2 && (bytes[0] & 15) === 8 && bytes[0] >>> 4 <= 7 && (bytes[0] << 8 | bytes[1]) % 31 === 0;
22550
22552
  const decoder = new Inflate({
22551
- raw: !(bytes[0] === 31 && bytes[1] === 139),
22553
+ raw: !wrapped,
22552
22554
  chunkSize: Math.max(1, Math.min(64 * 1024, maxDecodedBytes + 1))
22553
22555
  });
22556
+ const chunks = [];
22554
22557
  let length = 0;
22555
22558
  decoder.onData = (chunk) => {
22556
22559
  length += chunk.length;
22557
22560
  if (length > maxDecodedBytes) {
22558
22561
  throw new PdfError("E_LIMIT", "FlateDecode output exceeds maximum decoded byte budget");
22559
22562
  }
22560
- decoder.chunks.push(chunk);
22563
+ chunks.push(chunk);
22561
22564
  };
22562
- if (!decoder.push(bytes, true) || decoder.err || !decoder.ended) {
22565
+ const complete = decoder.push(bytes, true);
22566
+ const truncated = wrapped && decoder.err === Z_BUF_ERROR && length > 0;
22567
+ if ((!complete || decoder.err || !decoder.ended) && !truncated) {
22563
22568
  throw new PdfError("E_CAPABILITY", "Invalid FlateDecode compressed stream");
22564
22569
  }
22565
- inflated = decoder.result;
22570
+ inflated = new Uint8Array(length);
22571
+ let offset = 0;
22572
+ for (const chunk of chunks) {
22573
+ inflated.set(chunk, offset);
22574
+ offset += chunk.length;
22575
+ }
22566
22576
  }
22567
22577
  return applyPredictor(inflated, parms);
22568
22578
  }
@@ -34796,7 +34806,7 @@ function resolvePageFonts(doc, resourcesDict) {
34796
34806
  const glyphNames = buildFontEncodingGlyphNamesMap(encNode);
34797
34807
  const widths = /* @__PURE__ */ new Map();
34798
34808
  let defaultWidth = 556;
34799
- const isTwoByteCid = subtype === "Type0" || Boolean(cmap?.isTwoByte);
34809
+ const isTwoByteCid = subtype === "Type0";
34800
34810
  let fontMatrix;
34801
34811
  let charProcs;
34802
34812
  let fontResources;
@@ -36249,7 +36259,7 @@ function evaluateContentStreamToDisplayList(params) {
36249
36259
  const [x, y] = transformPoint(st.ctm, seg.x, seg.y);
36250
36260
  transformedSegments.push({ kind: "cubic", x1, y1, x2, y2, x, y });
36251
36261
  } else if (seg.kind === "rect") {
36252
- if (hasRotOrShear) {
36262
+ if (hasRotOrShear || seg.width * st.ctm[0] < 0 || seg.height * st.ctm[3] < 0) {
36253
36263
  const [p0x, p0y] = transformPoint(st.ctm, seg.x, seg.y);
36254
36264
  const [p1x, p1y] = transformPoint(st.ctm, seg.x + seg.width, seg.y);
36255
36265
  const [p2x, p2y] = transformPoint(st.ctm, seg.x + seg.width, seg.y + seg.height);
@@ -36282,7 +36292,12 @@ function evaluateContentStreamToDisplayList(params) {
36282
36292
  transformedSegments.push({ kind: "close" });
36283
36293
  }
36284
36294
  }
36285
- if (node.clip && transformedSegments.length > 0) {
36295
+ const applyClip = () => {
36296
+ if (!node.clip) return;
36297
+ st.clipPaths = [...st.clipPaths ?? [], {
36298
+ segments: transformedSegments,
36299
+ fillRule: node.clip === "W*" ? "evenodd" : "nonzero"
36300
+ }];
36286
36301
  let cMinX = Infinity, cMinY = Infinity, cMaxX = -Infinity, cMaxY = -Infinity;
36287
36302
  for (const s of transformedSegments) {
36288
36303
  if (s.kind === "move" || s.kind === "line") {
@@ -36310,8 +36325,11 @@ function evaluateContentStreamToDisplayList(params) {
36310
36325
  Math.min(st.clipRect[3], cMaxY)
36311
36326
  ] : [cMinX, cMinY, cMaxX, cMaxY];
36312
36327
  }
36328
+ };
36329
+ if (node.paint === "n") {
36330
+ applyClip();
36331
+ break;
36313
36332
  }
36314
- if (node.paint === "n") break;
36315
36333
  const isFill = ["f", "F", "f*", "B", "B*", "b", "b*"].includes(node.paint);
36316
36334
  const isStroke = ["S", "s", "B", "B*", "b", "b*"].includes(node.paint);
36317
36335
  let evaluatedFillPattern = false;
@@ -36444,7 +36462,10 @@ function evaluateContentStreamToDisplayList(params) {
36444
36462
  }
36445
36463
  }
36446
36464
  }
36447
- if (evaluatedFillPattern && !isStroke) break;
36465
+ if (evaluatedFillPattern && !isStroke) {
36466
+ applyClip();
36467
+ break;
36468
+ }
36448
36469
  const fillRule = node.paint.includes("*") ? "evenodd" : "nonzero";
36449
36470
  const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36450
36471
  const scaledStrokeWidth = st.strokeWidth === 0 ? 0 : st.strokeWidth * ctmScale;
@@ -36464,6 +36485,7 @@ function evaluateContentStreamToDisplayList(params) {
36464
36485
  ...st.dashPhase !== void 0 ? { dashPhase: st.dashPhase } : {},
36465
36486
  ...st.clipRect ? { clipRect: [...st.clipRect] } : {}
36466
36487
  } });
36488
+ applyClip();
36467
36489
  break;
36468
36490
  }
36469
36491
  case "xobject": {
@@ -40014,8 +40036,8 @@ function segmentsToScreenEdges(segments, pageHeight, scale, closeSubpaths = fals
40014
40036
  if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40015
40037
  edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40016
40038
  }
40017
- const [rx0, ry0] = toScreen(seg.x, seg.y + seg.height);
40018
- const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y);
40039
+ const [rx0, ry0] = toScreen(seg.x, seg.y);
40040
+ const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y + seg.height);
40019
40041
  edges.push(
40020
40042
  { x0: rx0, y0: ry0, x1: rx1, y1: ry0 },
40021
40043
  { x0: rx1, y0: ry0, x1: rx1, y1: ry1 },
@@ -40158,21 +40180,24 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40158
40180
  }
40159
40181
  const aaVec = options.antialiasVector !== false;
40160
40182
  const aaTxt = options.antialiasText !== false;
40161
- const clipMasks = /* @__PURE__ */ new Map();
40183
+ let cachedClips;
40184
+ let cachedClipMask;
40162
40185
  const imageClipMasks = /* @__PURE__ */ new Map();
40163
40186
  for (const original of paintOperations(displayList)) {
40164
40187
  if (original.kind === "glyph" && (original.value.renderMode === 3 || !original.value.outline && !original.value.unicode.trim())) continue;
40165
40188
  const operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
40166
40189
  const clips = original.value.clipPaths;
40167
- let clipMask = clips ? clipMasks.get(clips) : void 0;
40190
+ let clipMask = clips && clips === cachedClips ? cachedClipMask : void 0;
40168
40191
  if (clips && !clipMask) {
40169
40192
  clipMask = new Uint8Array(width * height * 4).fill(255);
40170
- for (const segments of clips) {
40193
+ for (const clip of clips) {
40194
+ const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40171
40195
  const layer = new Uint8Array(width * height * 4);
40172
- fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 });
40196
+ fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40173
40197
  for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
40174
40198
  }
40175
- clipMasks.set(clips, clipMask);
40199
+ cachedClips = clips;
40200
+ cachedClipMask = clipMask;
40176
40201
  }
40177
40202
  const imageClips = original.value.clipImages;
40178
40203
  if (imageClips) {