@poe-platform/safe-bash 0.1.763 → 0.1.765

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 (32) hide show
  1. package/dist/image-ast/index.browser.js +321 -162
  2. package/dist/image-ast/index.js +321 -162
  3. package/dist/pdf-ast/ast.d.ts +2 -0
  4. package/dist/pdf-ast/index.js +340 -173
  5. package/dist/safe-bash/chunks/{chunk-CRQDMBQA.js → chunk-FSH7QRIC.js} +10 -1
  6. package/dist/safe-bash/command-chunks/{chunk-4XHONGIU.js → chunk-DT4MFWV4.js} +2 -2
  7. package/dist/safe-bash/command-chunks/{chunk-S3G2B4OX.js → chunk-KO4PXCNL.js} +324 -162
  8. package/dist/safe-bash/command-chunks/{chunk-S5EX6UP5.js → chunk-SVL6HYQL.js} +1 -1
  9. package/dist/safe-bash/commands/python/index.browser.js +1 -1
  10. package/dist/safe-bash/commands/python/llm-module.js +3 -1
  11. package/dist/safe-bash/commands/python/shell-module.js +7 -0
  12. package/dist/safe-bash/core.browser.js +32 -12
  13. package/dist/safe-bash/shell/runtime.js +43 -11
  14. package/dist/safe-bash-command-ffmpeg/index.js +5800 -5693
  15. package/dist/safe-bash-command-imagemagick/index.js +2 -2
  16. package/dist/safe-bash-command-pandoc/command.js +3 -3
  17. package/dist/safe-bash-command-pandoc/index.js +3 -3
  18. package/dist/safe-bash-command-pdfimages/index.js +321 -162
  19. package/dist/safe-bash-command-pdfinfo/index.js +338 -173
  20. package/dist/safe-bash-command-pdftk/index.js +321 -162
  21. package/dist/safe-bash-command-pdftoppm/index.js +338 -173
  22. package/dist/safe-bash-command-pdftotext/index.js +321 -162
  23. package/dist/safe-bash-command-qpdf/index.js +321 -162
  24. package/dist/safe-bash-command-sips/index.js +2 -2
  25. package/dist/safe-bash-command-soffice/index.js +1 -1
  26. package/dist/safe-bash-command-ssconvert/index.js +373 -169
  27. package/dist/safe-bash-command-wkhtmltopdf/index.js +322 -162
  28. package/dist/safe-playwright-cloudflare/browser-snapshot-labels.d.ts +14 -0
  29. package/dist/safe-playwright-cloudflare/browser-snapshot-labels.js +62 -0
  30. package/dist/safe-playwright-cloudflare/browser-snapshot-scheduler.js +8 -1
  31. package/package.json +2 -2
  32. package/third-party/pdf-ast/THIRD_PARTY_NOTICES.md +24 -0
@@ -28,8 +28,8 @@ import {
28
28
  thresholdImage,
29
29
  tintImage,
30
30
  trimImage
31
- } from "../safe-bash/command-chunks/chunk-S5EX6UP5.js";
32
- import "../safe-bash/command-chunks/chunk-S3G2B4OX.js";
31
+ } from "../safe-bash/command-chunks/chunk-SVL6HYQL.js";
32
+ import "../safe-bash/command-chunks/chunk-KO4PXCNL.js";
33
33
  import {
34
34
  init_buffer
35
35
  } from "../safe-bash/command-chunks/chunk-HX3SHTCE.js";
@@ -2,11 +2,11 @@ import {
2
2
  createPandocCommand,
3
3
  createPandocCommands,
4
4
  pandocCommands
5
- } from "../safe-bash/command-chunks/chunk-4XHONGIU.js";
5
+ } from "../safe-bash/command-chunks/chunk-DT4MFWV4.js";
6
6
  import "../safe-bash/command-chunks/chunk-MKQVQMXO.js";
7
7
  import "../safe-bash/command-chunks/chunk-RDV7UCYZ.js";
8
- import "../safe-bash/command-chunks/chunk-S5EX6UP5.js";
9
- import "../safe-bash/command-chunks/chunk-S3G2B4OX.js";
8
+ import "../safe-bash/command-chunks/chunk-SVL6HYQL.js";
9
+ import "../safe-bash/command-chunks/chunk-KO4PXCNL.js";
10
10
  import "../safe-bash/command-chunks/chunk-Y2KHC6TZ.js";
11
11
  import "../safe-bash/command-chunks/chunk-YGYOVXTO.js";
12
12
  import "../safe-bash/command-chunks/chunk-I5GLTNVH.js";
@@ -21,11 +21,11 @@ import {
21
21
  readDocument,
22
22
  resolveConversionArgs,
23
23
  writeDocument
24
- } from "../safe-bash/command-chunks/chunk-4XHONGIU.js";
24
+ } from "../safe-bash/command-chunks/chunk-DT4MFWV4.js";
25
25
  import "../safe-bash/command-chunks/chunk-MKQVQMXO.js";
26
26
  import "../safe-bash/command-chunks/chunk-RDV7UCYZ.js";
27
- import "../safe-bash/command-chunks/chunk-S5EX6UP5.js";
28
- import "../safe-bash/command-chunks/chunk-S3G2B4OX.js";
27
+ import "../safe-bash/command-chunks/chunk-SVL6HYQL.js";
28
+ import "../safe-bash/command-chunks/chunk-KO4PXCNL.js";
29
29
  import {
30
30
  createLuaFilterCapability
31
31
  } from "../safe-bash/command-chunks/chunk-Y2KHC6TZ.js";
@@ -36049,11 +36049,10 @@ function evaluateContentStreamToDisplayList(params) {
36049
36049
  } else if (operator === "d") {
36050
36050
  const arrNode = ops[0]?.kind === "array" ? ops[0] : void 0;
36051
36051
  if (arrNode) {
36052
- const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36053
- const nums = arrNode.items.map((it) => it.kind === "number" ? it.value * ctmScale : 0).filter((v) => v >= 0);
36052
+ const nums = arrNode.items.map((it) => it.kind === "number" ? it.value : 0).filter((v) => v >= 0);
36054
36053
  const sum = nums.reduce((a, b) => a + b, 0);
36055
36054
  st.dashArray = sum > 0 ? nums : void 0;
36056
- st.dashPhase = num(1, 0) * ctmScale;
36055
+ st.dashPhase = num(1, 0);
36057
36056
  } else {
36058
36057
  st.dashArray = void 0;
36059
36058
  st.dashPhase = void 0;
@@ -36589,15 +36588,14 @@ function evaluateContentStreamToDisplayList(params) {
36589
36588
  break;
36590
36589
  }
36591
36590
  const fillRule = node.paint.includes("*") ? "evenodd" : "nonzero";
36592
- const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36593
- const scaledStrokeWidth = st.strokeWidth === 0 ? 0 : st.strokeWidth * ctmScale;
36594
36591
  emit({ kind: "path", value: {
36595
36592
  segments: transformedSegments,
36596
36593
  fillColor: isFill && !evaluatedFillPattern ? st.fillColor : void 0,
36597
36594
  fillAlpha: isFill && !evaluatedFillPattern ? st.fillAlpha : void 0,
36598
36595
  strokeColor: isStroke ? st.strokeColor : void 0,
36599
36596
  strokeAlpha: isStroke ? st.strokeAlpha : void 0,
36600
- strokeWidth: scaledStrokeWidth,
36597
+ strokeWidth: st.strokeWidth,
36598
+ ...isStroke ? { strokeMatrix: [...st.ctm] } : {},
36601
36599
  ...st.lineCap !== 0 ? { lineCap: st.lineCap } : {},
36602
36600
  ...st.lineJoin !== 0 ? { lineJoin: st.lineJoin } : {},
36603
36601
  ...st.miterLimit !== 10 ? { miterLimit: st.miterLimit } : {},
@@ -36785,14 +36783,14 @@ function evaluateContentStreamToDisplayList(params) {
36785
36783
  if (st.textRenderMode >= 4 && st.textRenderMode <= 7) pendingTextClip.push(...transformedGlyphSegs);
36786
36784
  const isFillGlyph = st.textRenderMode === 0 || st.textRenderMode === 2 || st.textRenderMode === 4 || st.textRenderMode === 6;
36787
36785
  const isStrokeGlyph = st.textRenderMode === 1 || st.textRenderMode === 2 || st.textRenderMode === 5 || st.textRenderMode === 6;
36788
- const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36789
36786
  const paint = {
36790
36787
  segments: transformedGlyphSegs,
36791
36788
  fillColor: isFillGlyph ? st.fillColor : void 0,
36792
36789
  fillAlpha: isFillGlyph ? st.fillAlpha : void 0,
36793
36790
  strokeColor: isStrokeGlyph ? st.strokeColor : void 0,
36794
36791
  strokeAlpha: isStrokeGlyph ? st.strokeAlpha : void 0,
36795
- strokeWidth: isStrokeGlyph ? st.strokeWidth * ctmScale : 0,
36792
+ strokeWidth: isStrokeGlyph ? st.strokeWidth : 0,
36793
+ ...isStrokeGlyph ? { strokeMatrix: [...st.ctm] } : {},
36796
36794
  fillRule: "nonzero",
36797
36795
  ...st.blendMode && st.blendMode !== "Normal" ? { blendMode: st.blendMode } : {},
36798
36796
  ...st.clipRect ? { clipRect: [...st.clipRect] } : {}
@@ -39170,6 +39168,184 @@ function downscaleImage(image, widthScale, heightScale) {
39170
39168
  }
39171
39169
  return image;
39172
39170
  }
39171
+ var MAX_STROKE_VERTICES = 1e6;
39172
+ function dashSubpath(path, pattern, phase) {
39173
+ const cycle = pattern.reduce((sum, value) => sum + value, 0);
39174
+ if (!(cycle > 0)) return [path];
39175
+ let offset = (phase % cycle + cycle) % cycle, index = 0;
39176
+ while (offset > 0) {
39177
+ if (offset < pattern[index]) break;
39178
+ offset -= pattern[index];
39179
+ index = (index + 1) % pattern.length;
39180
+ }
39181
+ let remaining = pattern[index] - offset;
39182
+ const points = path.closed ? [...path.points, path.points[0]] : path.points;
39183
+ const result = [];
39184
+ let current = [];
39185
+ let steps = 0;
39186
+ const flush = () => {
39187
+ if (current.length) result.push({ points: current, closed: false });
39188
+ current = [];
39189
+ };
39190
+ const advance = (point) => {
39191
+ while (remaining <= 0) {
39192
+ if (++steps > MAX_STROKE_VERTICES) throw new PdfError("E_LIMIT", "Stroke dash expansion exceeds the vertex limit");
39193
+ if (index % 2 === 0) {
39194
+ if (pattern[index] === 0) result.push({ points: [point, point], closed: false });
39195
+ flush();
39196
+ }
39197
+ index = (index + 1) % pattern.length;
39198
+ remaining = pattern[index];
39199
+ }
39200
+ };
39201
+ for (let i = 1; i < points.length; i++) {
39202
+ const start = points[i - 1], end = points[i];
39203
+ const dx = end[0] - start[0], dy = end[1] - start[1];
39204
+ const length = Math.hypot(dx, dy);
39205
+ if (!Number.isFinite(length) || length / cycle * pattern.length > MAX_STROKE_VERTICES) {
39206
+ throw new PdfError("E_LIMIT", "Stroke dash expansion exceeds the vertex limit");
39207
+ }
39208
+ let distance = 0;
39209
+ while (distance < length) {
39210
+ const point = [start[0] + dx * distance / length, start[1] + dy * distance / length];
39211
+ advance(point);
39212
+ const step = Math.min(remaining, length - distance);
39213
+ if (distance + step === distance || ++steps > MAX_STROKE_VERTICES) throw new PdfError("E_LIMIT", "Stroke dash expansion exceeds the vertex limit");
39214
+ distance += step;
39215
+ remaining -= step;
39216
+ if (index % 2 === 0) {
39217
+ if (!current.length) current.push(point);
39218
+ current.push([start[0] + dx * distance / length, start[1] + dy * distance / length]);
39219
+ }
39220
+ }
39221
+ }
39222
+ flush();
39223
+ if (path.closed && result.length) {
39224
+ const first = result[0], last = result[result.length - 1];
39225
+ const start = path.points[0];
39226
+ const firstPoint = first.points[0], lastPoint = last.points[last.points.length - 1];
39227
+ if (firstPoint[0] === start[0] && firstPoint[1] === start[1] && lastPoint[0] === start[0] && lastPoint[1] === start[1]) {
39228
+ if (first === last) result[0] = { points: first.points, closed: true };
39229
+ else {
39230
+ result[0] = { points: [...last.points, ...first.points.slice(1)], closed: false };
39231
+ result.pop();
39232
+ }
39233
+ }
39234
+ }
39235
+ return result;
39236
+ }
39237
+ function strokeOutlines(paths, width, cap, join, miterLimit, dashArray = [], dashPhase = 0) {
39238
+ const half = width / 2;
39239
+ const contours = [];
39240
+ const pattern = dashArray.length % 2 ? [...dashArray, ...dashArray] : dashArray;
39241
+ let count = 0;
39242
+ let contour = [];
39243
+ const add = (x, y) => {
39244
+ if (++count > MAX_STROKE_VERTICES) throw new PdfError("E_LIMIT", "Stroke outline exceeds the vertex limit");
39245
+ contour.push([x, y]);
39246
+ };
39247
+ const arc = (point, dx1, dy1, dx2, dy2) => {
39248
+ let a1 = Math.atan2(dy1, dx1), a2 = Math.atan2(dy2, dx2);
39249
+ const ccw = a1 - a2 > 0 && a1 - a2 < Math.PI;
39250
+ const step = Math.acos(half / (half + 0.125)) * 2;
39251
+ add(point[0] + dx1, point[1] + dy1);
39252
+ if (step > 0) {
39253
+ if (!ccw) {
39254
+ if (a1 > a2) a2 += 2 * Math.PI;
39255
+ a2 -= step / 4;
39256
+ if ((a2 - a1) / step > MAX_STROKE_VERTICES - count) throw new PdfError("E_LIMIT", "Stroke arc exceeds the vertex limit");
39257
+ for (a1 += step; a1 < a2; a1 += step) add(point[0] + half * Math.cos(a1), point[1] + half * Math.sin(a1));
39258
+ } else {
39259
+ if (a1 < a2) a2 -= 2 * Math.PI;
39260
+ a2 += step / 4;
39261
+ if ((a1 - a2) / step > MAX_STROKE_VERTICES - count) throw new PdfError("E_LIMIT", "Stroke arc exceeds the vertex limit");
39262
+ for (a1 -= step; a1 > a2; a1 -= step) add(point[0] + half * Math.cos(a1), point[1] + half * Math.sin(a1));
39263
+ }
39264
+ }
39265
+ add(point[0] + dx2, point[1] + dy2);
39266
+ };
39267
+ const addCap = (point, next) => {
39268
+ const length = Math.hypot(next[0] - point[0], next[1] - point[1]);
39269
+ const dx = half * (next[1] - point[1]) / length;
39270
+ const dy = half * (next[0] - point[0]) / length;
39271
+ if (cap !== 1) {
39272
+ const sx = cap === 2 ? dy : 0, sy = cap === 2 ? dx : 0;
39273
+ add(point[0] - dx - sx, point[1] + dy - sy);
39274
+ add(point[0] + dx - sx, point[1] - dy - sy);
39275
+ } else {
39276
+ const angle = Math.atan2(dy, -dx);
39277
+ const step = Math.max(1e-3, Math.acos(half / (half + 0.125)) * 2);
39278
+ add(point[0] - dx, point[1] + dy);
39279
+ for (let a = angle + step; a < angle + Math.PI - step / 4; a += step) add(point[0] + half * Math.cos(a), point[1] + half * Math.sin(a));
39280
+ add(point[0] + dx, point[1] - dy);
39281
+ }
39282
+ };
39283
+ const addJoin = (prev, point, next) => {
39284
+ const len1 = Math.hypot(point[0] - prev[0], point[1] - prev[1]);
39285
+ const len2 = Math.hypot(next[0] - point[0], next[1] - point[1]);
39286
+ const dx1 = half * (point[1] - prev[1]) / len1, dy1 = half * (point[0] - prev[0]) / len1;
39287
+ const dx2 = half * (next[1] - point[1]) / len2, dy2 = half * (next[0] - point[0]) / len2;
39288
+ const inner = (next[0] - point[0]) * (point[1] - prev[1]) - (next[1] - point[1]) * (point[0] - prev[0]) > 0;
39289
+ if (!inner && join === 1) {
39290
+ arc(point, dx1, -dy1, dx2, -dy2);
39291
+ return;
39292
+ }
39293
+ if (inner || join === 0) {
39294
+ const ax = prev[0] + dx1, ay = prev[1] - dy1;
39295
+ const bx = point[0] + dx1, by = point[1] - dy1;
39296
+ const cx = point[0] + dx2, cy = point[1] - dy2;
39297
+ const dx = next[0] + dx2, dy = next[1] - dy2;
39298
+ const den = (bx - ax) * (dy - cy) - (by - ay) * (dx - cx);
39299
+ if (Math.abs(den) >= 1e-30) {
39300
+ const t = ((ay - cy) * (dx - cx) - (ax - cx) * (dy - cy)) / den;
39301
+ const x = ax + (bx - ax) * t, y = ay + (by - ay) * t;
39302
+ if (Math.hypot(x - point[0], y - point[1]) <= half * (inner ? 1.01 : miterLimit)) {
39303
+ add(x, y);
39304
+ return;
39305
+ }
39306
+ } else if ((bx - prev[0]) * dy1 - (prev[1] - by) * dx1 < 0 !== (bx - next[0]) * dy1 - (next[1] - by) * dx1 < 0) {
39307
+ add(bx, by);
39308
+ return;
39309
+ }
39310
+ }
39311
+ add(point[0] + dx1, point[1] - dy1);
39312
+ add(point[0] + dx2, point[1] - dy2);
39313
+ };
39314
+ for (const path of paths) {
39315
+ for (const subpath of pattern.length ? dashSubpath(path, pattern, dashPhase) : [path]) {
39316
+ const points = [];
39317
+ for (const point of subpath.points) {
39318
+ const prev = points[points.length - 1];
39319
+ if (!prev || Math.hypot(prev[0] - point[0], prev[1] - point[1]) > 1e-14) points.push(point);
39320
+ }
39321
+ let closed = subpath.closed;
39322
+ if (closed && points.length > 1 && Math.hypot(points[0][0] - points[points.length - 1][0], points[0][1] - points[points.length - 1][1]) <= 1e-14) points.pop();
39323
+ if (points.length < 3) closed = false;
39324
+ if (points.length === 1 && subpath.points.length > 1 && cap === 1) {
39325
+ contour = [];
39326
+ const [x, y] = points[0];
39327
+ addCap([x, y], [x + 1, y]);
39328
+ addCap([x, y], [x - 1, y]);
39329
+ contours.push(contour);
39330
+ } else if (points.length >= 2) {
39331
+ contour = [];
39332
+ if (!closed) addCap(points[0], points[1]);
39333
+ for (let i = closed ? 0 : 1; i < points.length - (closed ? 0 : 1); i++) {
39334
+ addJoin(points[(i + points.length - 1) % points.length], points[i], points[(i + 1) % points.length]);
39335
+ }
39336
+ if (closed) {
39337
+ contours.push(contour);
39338
+ contour = [];
39339
+ } else addCap(points[points.length - 1], points[points.length - 2]);
39340
+ for (let i = points.length - (closed ? 1 : 2); i >= (closed ? 0 : 1); i--) {
39341
+ addJoin(points[(i + 1) % points.length], points[i], points[(i + points.length - 1) % points.length]);
39342
+ }
39343
+ contours.push(contour);
39344
+ }
39345
+ }
39346
+ }
39347
+ return contours;
39348
+ }
39173
39349
  function encodePng(bitmap) {
39174
39350
  return encodeRgbaToPng(bitmap.width, bitmap.height, bitmap.data);
39175
39351
  }
@@ -40018,111 +40194,116 @@ function blendPixel(rgba, width, height, px, py, color, alpha, blendMode, clipMa
40018
40194
  rgba[idx + 2] = Math.round((effB * a + cbB * dstA * (1 - a)) / outA * 255);
40019
40195
  rgba[idx + 3] = Math.round(outA * 255);
40020
40196
  }
40021
- function drawAntiAliasedSegment(rgba, width, height, x0, y0, x1, y1, strokeWidth, color, alpha = 1, lineCap = 0, antialias = true, blendMode, clipMask) {
40022
- const halfW = Math.max(0.6, strokeWidth * 0.5);
40023
- const dx = x1 - x0;
40024
- const dy = y1 - y0;
40025
- const lenSq = dx * dx + dy * dy;
40026
- const segLen = Math.sqrt(lenSq);
40027
- const pad = lineCap === 2 ? halfW + 2 : halfW + 1;
40028
- const minX = Math.max(0, Math.floor(Math.min(x0, x1) - pad));
40029
- const maxX = Math.min(width - 1, Math.ceil(Math.max(x0, x1) + pad));
40030
- const minY = Math.max(0, Math.floor(Math.min(y0, y1) - pad));
40031
- const maxY = Math.min(height - 1, Math.ceil(Math.max(y0, y1) + pad));
40032
- for (let py = minY; py <= maxY; py++) {
40033
- for (let px = minX; px <= maxX; px++) {
40034
- const cx = px + 0.5;
40035
- const cy = py + 0.5;
40036
- if (segLen > 1e-6 && (lineCap === 0 || lineCap === 2)) {
40037
- const ux = dx / segLen;
40038
- const uy = dy / segLen;
40039
- const along = (cx - x0) * ux + (cy - y0) * uy;
40040
- const perp = Math.abs(-(cx - x0) * uy + (cy - y0) * ux);
40041
- const minAlong = lineCap === 2 ? -halfW : 0;
40042
- const maxAlong = lineCap === 2 ? segLen + halfW : segLen;
40043
- const overAlong = along < minAlong ? minAlong - along : along > maxAlong ? along - maxAlong : 0;
40044
- const dist2 = overAlong > 0 ? Math.max(perp, halfW + overAlong) : perp;
40045
- if (dist2 <= halfW + 0.75) {
40046
- const cov = antialias ? Math.max(0, Math.min(1, halfW + 0.5 - dist2)) : dist2 <= halfW + 0.25 ? 1 : 0;
40047
- blendPixel(rgba, width, height, px, py, color, cov * alpha, blendMode, clipMask);
40048
- }
40049
- continue;
40050
- }
40051
- let t = lenSq > 1e-6 ? ((cx - x0) * dx + (cy - y0) * dy) / lenSq : 0;
40052
- t = Math.max(0, Math.min(1, t));
40053
- const projX = x0 + t * dx;
40054
- const projY = y0 + t * dy;
40055
- const dist = Math.hypot(cx - projX, cy - projY);
40056
- if (dist <= halfW + 0.75) {
40057
- const cov = antialias ? Math.max(0, Math.min(1, halfW + 0.5 - dist)) : dist <= halfW + 0.25 ? 1 : 0;
40058
- blendPixel(rgba, width, height, px, py, color, cov * alpha, blendMode, clipMask);
40059
- }
40060
- }
40061
- }
40197
+ function segmentsToScreenPaths(segments, pageHeight, scale, toScreen = (x, y) => [x * scale, (pageHeight - y) * scale]) {
40198
+ const paths = [];
40199
+ let points = [];
40200
+ let current = [0, 0];
40201
+ for (const segment of segments) {
40202
+ if (segment.kind === "move") {
40203
+ if (points.length) paths.push({ points, closed: false });
40204
+ current = toScreen(segment.x, segment.y);
40205
+ points = [current];
40206
+ } else if (segment.kind === "line") {
40207
+ if (!points.length) points = [current];
40208
+ current = toScreen(segment.x, segment.y);
40209
+ points.push(current);
40210
+ } else if (segment.kind === "cubic") {
40211
+ if (!points.length) points = [current];
40212
+ const a = toScreen(segment.x1, segment.y1), b = toScreen(segment.x2, segment.y2);
40213
+ const end = toScreen(segment.x, segment.y);
40214
+ const curve = flattenCubic(current[0], current[1], a[0], a[1], b[0], b[1], end[0], end[1]);
40215
+ for (let i = 1; i < curve.length; i++) points.push(curve[i]);
40216
+ current = end;
40217
+ } else if (segment.kind === "close") {
40218
+ if (points.length) {
40219
+ paths.push({ points, closed: true });
40220
+ current = points[0];
40221
+ points = [];
40222
+ }
40223
+ } else if (segment.kind === "rect") {
40224
+ if (points.length) paths.push({ points, closed: false });
40225
+ const first = toScreen(segment.x, segment.y);
40226
+ paths.push({ points: [
40227
+ first,
40228
+ toScreen(segment.x + segment.width, segment.y),
40229
+ toScreen(segment.x + segment.width, segment.y + segment.height),
40230
+ toScreen(segment.x, segment.y + segment.height)
40231
+ ], closed: true });
40232
+ current = first;
40233
+ points = [];
40234
+ }
40235
+ }
40236
+ if (points.length) paths.push({ points, closed: false });
40237
+ return paths;
40238
+ }
40239
+ function inverseStrokeMatrix(matrix) {
40240
+ const [a, b, c, d, e, f] = matrix;
40241
+ const det = a * d - b * c;
40242
+ if (!Number.isFinite(det) || det === 0) return void 0;
40243
+ return [d / det, -b / det, -c / det, a / det, (c * f - d * e) / det, (b * e - a * f) / det];
40244
+ }
40245
+ function prepareStroke(path, scale) {
40246
+ const matrix = path.strokeMatrix ? [...path.strokeMatrix] : [1, 0, 0, 1, 0, 0];
40247
+ const [a, b, c, d] = matrix;
40248
+ const area = Math.abs(a * d - b * c) * scale * scale;
40249
+ if (!(area > 0) || !Number.isFinite(area)) return void 0;
40250
+ const normX = Math.hypot(a, b) * scale, normY = Math.hypot(c, d) * scale;
40251
+ let width = path.strokeWidth || 1;
40252
+ const scaleX = path.strokeWidth === 0 ? normY / area : Math.max(1, normY / (width * area));
40253
+ const scaleY = path.strokeWidth === 0 ? normX / area : Math.max(1, normX / (width * area));
40254
+ let dashArray = path.dashArray, dashPhase = path.dashPhase ?? 0;
40255
+ if (scaleX === scaleY) {
40256
+ width *= scaleX;
40257
+ } else {
40258
+ matrix[0] *= scaleX;
40259
+ matrix[1] *= scaleX;
40260
+ matrix[2] *= scaleY;
40261
+ matrix[3] *= scaleY;
40262
+ if (dashArray?.length) {
40263
+ const dashScale = Math.max(scaleX, scaleY);
40264
+ dashArray = dashArray.map((value) => value / dashScale);
40265
+ dashPhase /= dashScale;
40266
+ }
40267
+ }
40268
+ return { matrix, width, dashArray, dashPhase };
40269
+ }
40270
+ function strokeEdges(path, pageHeight, scale) {
40271
+ const stroke = prepareStroke(path, scale);
40272
+ if (!stroke) return [];
40273
+ const inverse = inverseStrokeMatrix(stroke.matrix);
40274
+ if (!inverse) return [];
40275
+ const [a, b, c, d, e, f] = stroke.matrix;
40276
+ const strokeScale = scale * Math.hypot(a, b, c, d);
40277
+ const project = (x, y) => [
40278
+ (inverse[0] * x + inverse[2] * y + inverse[4]) * strokeScale,
40279
+ (inverse[1] * x + inverse[3] * y + inverse[5]) * strokeScale
40280
+ ];
40281
+ const toScreen = ([x, y]) => [
40282
+ (a * x / strokeScale + c * y / strokeScale + e) * scale,
40283
+ (pageHeight - b * x / strokeScale - d * y / strokeScale - f) * scale
40284
+ ];
40285
+ const contours = strokeOutlines(
40286
+ segmentsToScreenPaths(path.segments, pageHeight, scale, project),
40287
+ stroke.width * strokeScale,
40288
+ path.lineCap ?? 0,
40289
+ path.lineJoin ?? 0,
40290
+ path.miterLimit ?? 10,
40291
+ stroke.dashArray?.map((value) => Math.max(0, value * strokeScale)),
40292
+ stroke.dashPhase * strokeScale
40293
+ );
40294
+ return pathsToEdges(contours.map((points) => ({ points: points.map(toScreen), closed: true })));
40062
40295
  }
40063
- function segmentsToScreenEdges(segments, pageHeight, scale, closeSubpaths = false) {
40296
+ function pathsToEdges(paths, closeSubpaths = false) {
40064
40297
  const edges = [];
40065
- let curX = 0;
40066
- let curY = 0;
40067
- let startX = 0;
40068
- let startY = 0;
40069
- let hasOpenSubpath = false;
40070
- const toScreen = (x, y) => [x * scale, (pageHeight - y) * scale];
40071
- for (const seg of segments) {
40072
- if (seg.kind === "move") {
40073
- if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40074
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40075
- }
40076
- [curX, curY] = toScreen(seg.x, seg.y);
40077
- startX = curX;
40078
- startY = curY;
40079
- hasOpenSubpath = false;
40080
- } else if (seg.kind === "line") {
40081
- const [nx, ny] = toScreen(seg.x, seg.y);
40082
- edges.push({ x0: curX, y0: curY, x1: nx, y1: ny });
40083
- curX = nx;
40084
- curY = ny;
40085
- hasOpenSubpath = true;
40086
- } else if (seg.kind === "cubic") {
40087
- const [p1x, p1y] = toScreen(seg.x1, seg.y1);
40088
- const [p2x, p2y] = toScreen(seg.x2, seg.y2);
40089
- const [p3x, p3y] = toScreen(seg.x, seg.y);
40090
- const points = flattenCubic(curX, curY, p1x, p1y, p2x, p2y, p3x, p3y);
40091
- let px = curX;
40092
- let py = curY;
40093
- for (let i = 1; i < points.length; i++) {
40094
- const [qx, qy] = points[i];
40095
- edges.push({ x0: px, y0: py, x1: qx, y1: qy });
40096
- px = qx;
40097
- py = qy;
40098
- }
40099
- curX = p3x;
40100
- curY = p3y;
40101
- hasOpenSubpath = true;
40102
- } else if (seg.kind === "close") {
40103
- if (curX !== startX || curY !== startY) {
40104
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40105
- curX = startX;
40106
- curY = startY;
40107
- }
40108
- hasOpenSubpath = false;
40109
- } else if (seg.kind === "rect") {
40110
- if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40111
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40112
- }
40113
- const [rx0, ry0] = toScreen(seg.x, seg.y);
40114
- const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y + seg.height);
40115
- edges.push(
40116
- { x0: rx0, y0: ry0, x1: rx1, y1: ry0 },
40117
- { x0: rx1, y0: ry0, x1: rx1, y1: ry1 },
40118
- { x0: rx1, y0: ry1, x1: rx0, y1: ry1 },
40119
- { x0: rx0, y0: ry1, x1: rx0, y1: ry0 }
40120
- );
40121
- hasOpenSubpath = false;
40298
+ for (const { points, closed } of paths) {
40299
+ for (let i = 1; i < points.length; i++) {
40300
+ const a = points[i - 1], b = points[i];
40301
+ edges.push({ x0: a[0], y0: a[1], x1: b[0], y1: b[1] });
40302
+ }
40303
+ if ((closed || closeSubpaths) && points.length > 1) {
40304
+ const a = points[points.length - 1], b = points[0];
40305
+ edges.push({ x0: a[0], y0: a[1], x1: b[0], y1: b[1] });
40122
40306
  }
40123
- }
40124
- if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40125
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40126
40307
  }
40127
40308
  return edges;
40128
40309
  }
@@ -40309,7 +40490,7 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40309
40490
  for (const clip of clips) {
40310
40491
  const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40311
40492
  const layer = new Uint8Array(width * height * 4);
40312
- fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40493
+ fillEdgesScanline4x4(layer, width, height, pathsToEdges(segmentsToScreenPaths(segments, displayList.height, scale), true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40313
40494
  for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
40314
40495
  }
40315
40496
  cachedClips = clips;
@@ -40356,62 +40537,28 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40356
40537
  if (operation.kind === "path") {
40357
40538
  const path = operation.value;
40358
40539
  if (path.fillColor) {
40359
- const edges = segmentsToScreenEdges(path.segments, displayList.height, scale, true);
40540
+ const edges = pathsToEdges(segmentsToScreenPaths(path.segments, displayList.height, scale), true);
40360
40541
  const clipScreen = path.clipRect ? [path.clipRect[0] * scale, (displayList.height - path.clipRect[3]) * scale, path.clipRect[2] * scale, (displayList.height - path.clipRect[1]) * scale] : void 0;
40361
40542
  fillEdgesScanline4x4(rgba, width, height, edges, path.fillColor, path.fillAlpha ?? 1, path.fillRule ?? "nonzero", clipScreen, original.kind === "glyph" ? aaTxt : aaVec, path.blendMode, clipMask);
40362
40543
  }
40363
40544
  if (path.strokeColor) {
40364
- const edges = segmentsToScreenEdges(path.segments, displayList.height, scale, false);
40365
- const rawSw = path.strokeWidth * scale;
40366
- const sw = Math.max(1, rawSw);
40545
+ const rawSw = path.strokeWidth * scale * (path.strokeMatrix ? Math.hypot(path.strokeMatrix[0], path.strokeMatrix[1]) : 1);
40367
40546
  const strokeAlpha = options.thinLineMode === "shape" && rawSw < 1 ? (path.strokeAlpha ?? 1) * Math.max(0.25, rawSw) : path.strokeAlpha ?? 1;
40368
- const dashArr = path.dashArray && path.dashArray.length > 0 ? path.dashArray.map((d) => Math.max(0, d * scale)) : void 0;
40369
- const dashCycle = dashArr ? dashArr.reduce((a, b) => a + b, 0) : 0;
40370
- let dashDist = (path.dashPhase ?? 0) * scale % (dashCycle || 1);
40371
- if (dashDist < 0) dashDist += dashCycle;
40372
- for (const e of edges) {
40373
- if (!dashArr || dashCycle <= 1e-4) {
40374
- drawAntiAliasedSegment(rgba, width, height, e.x0, e.y0, e.x1, e.y1, sw, path.strokeColor, strokeAlpha, path.lineCap ?? 0, original.kind === "glyph" ? aaTxt : aaVec, path.blendMode, clipMask);
40375
- } else {
40376
- const dx = e.x1 - e.x0;
40377
- const dy = e.y1 - e.y0;
40378
- const edgeLen = Math.hypot(dx, dy);
40379
- if (edgeLen <= 1e-4) continue;
40380
- let pos = 0;
40381
- while (pos < edgeLen) {
40382
- let cycleRem = dashDist % dashCycle;
40383
- let dIdx = 0;
40384
- while (dIdx < dashArr.length - 1 && cycleRem >= dashArr[dIdx]) {
40385
- cycleRem -= dashArr[dIdx];
40386
- dIdx++;
40387
- }
40388
- const segRemainInDash = Math.max(1e-4, dashArr[dIdx] - cycleRem);
40389
- const step = Math.min(edgeLen - pos, segRemainInDash);
40390
- if (dIdx % 2 === 0) {
40391
- const t0 = pos / edgeLen;
40392
- const t1 = (pos + step) / edgeLen;
40393
- drawAntiAliasedSegment(
40394
- rgba,
40395
- width,
40396
- height,
40397
- e.x0 + dx * t0,
40398
- e.y0 + dy * t0,
40399
- e.x0 + dx * t1,
40400
- e.y0 + dy * t1,
40401
- sw,
40402
- path.strokeColor,
40403
- strokeAlpha,
40404
- path.lineCap ?? 0,
40405
- original.kind === "glyph" ? aaTxt : aaVec,
40406
- path.blendMode,
40407
- clipMask
40408
- );
40409
- }
40410
- pos += step;
40411
- dashDist = (dashDist + step) % dashCycle;
40412
- }
40413
- }
40414
- }
40547
+ const edges = strokeEdges(path, displayList.height, scale);
40548
+ const clipScreen = path.clipRect ? [path.clipRect[0] * scale, (displayList.height - path.clipRect[3]) * scale, path.clipRect[2] * scale, (displayList.height - path.clipRect[1]) * scale] : void 0;
40549
+ fillEdgesScanline4x4(
40550
+ rgba,
40551
+ width,
40552
+ height,
40553
+ edges,
40554
+ path.strokeColor,
40555
+ strokeAlpha,
40556
+ "nonzero",
40557
+ clipScreen,
40558
+ original.kind === "glyph" ? aaTxt : aaVec,
40559
+ path.blendMode,
40560
+ clipMask
40561
+ );
40415
40562
  }
40416
40563
  } else if (operation.kind === "image") {
40417
40564
  const img = operation.value;
@@ -41508,3 +41655,15 @@ export {
41508
41655
  * Copyright 2012 Mozilla Foundation. Licensed under Apache-2.0.
41509
41656
  * See THIRD_PARTY_NOTICES.md for the source revision and adaptations.
41510
41657
  */
41658
+ /*!
41659
+ * Anti-Grain Geometry - Version 2.3
41660
+ * Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
41661
+ *
41662
+ * Permission to copy, use, modify, sell and distribute this software
41663
+ * is granted provided this copyright notice appears in all copies.
41664
+ * This software is provided "as is" without express or implied
41665
+ * warranty, and with no claim as to its suitability for any purpose.
41666
+ *
41667
+ * Adapted from PDFium agg_math_stroke.h and agg_vcgen_stroke.cpp.
41668
+ * See THIRD_PARTY_NOTICES.md for the source revision and adaptations.
41669
+ */