@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
@@ -36243,11 +36243,10 @@ function evaluateContentStreamToDisplayList(params) {
36243
36243
  } else if (operator === "d") {
36244
36244
  const arrNode = ops[0]?.kind === "array" ? ops[0] : void 0;
36245
36245
  if (arrNode) {
36246
- const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36247
- const nums = arrNode.items.map((it) => it.kind === "number" ? it.value * ctmScale : 0).filter((v) => v >= 0);
36246
+ const nums = arrNode.items.map((it) => it.kind === "number" ? it.value : 0).filter((v) => v >= 0);
36248
36247
  const sum = nums.reduce((a, b) => a + b, 0);
36249
36248
  st.dashArray = sum > 0 ? nums : void 0;
36250
- st.dashPhase = num(1, 0) * ctmScale;
36249
+ st.dashPhase = num(1, 0);
36251
36250
  } else {
36252
36251
  st.dashArray = void 0;
36253
36252
  st.dashPhase = void 0;
@@ -36783,15 +36782,14 @@ function evaluateContentStreamToDisplayList(params) {
36783
36782
  break;
36784
36783
  }
36785
36784
  const fillRule = node.paint.includes("*") ? "evenodd" : "nonzero";
36786
- const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36787
- const scaledStrokeWidth = st.strokeWidth === 0 ? 0 : st.strokeWidth * ctmScale;
36788
36785
  emit({ kind: "path", value: {
36789
36786
  segments: transformedSegments,
36790
36787
  fillColor: isFill && !evaluatedFillPattern ? st.fillColor : void 0,
36791
36788
  fillAlpha: isFill && !evaluatedFillPattern ? st.fillAlpha : void 0,
36792
36789
  strokeColor: isStroke ? st.strokeColor : void 0,
36793
36790
  strokeAlpha: isStroke ? st.strokeAlpha : void 0,
36794
- strokeWidth: scaledStrokeWidth,
36791
+ strokeWidth: st.strokeWidth,
36792
+ ...isStroke ? { strokeMatrix: [...st.ctm] } : {},
36795
36793
  ...st.lineCap !== 0 ? { lineCap: st.lineCap } : {},
36796
36794
  ...st.lineJoin !== 0 ? { lineJoin: st.lineJoin } : {},
36797
36795
  ...st.miterLimit !== 10 ? { miterLimit: st.miterLimit } : {},
@@ -36979,14 +36977,14 @@ function evaluateContentStreamToDisplayList(params) {
36979
36977
  if (st.textRenderMode >= 4 && st.textRenderMode <= 7) pendingTextClip.push(...transformedGlyphSegs);
36980
36978
  const isFillGlyph = st.textRenderMode === 0 || st.textRenderMode === 2 || st.textRenderMode === 4 || st.textRenderMode === 6;
36981
36979
  const isStrokeGlyph = st.textRenderMode === 1 || st.textRenderMode === 2 || st.textRenderMode === 5 || st.textRenderMode === 6;
36982
- const ctmScale = Math.max(1e-6, Math.hypot(st.ctm[0], st.ctm[1]));
36983
36980
  const paint = {
36984
36981
  segments: transformedGlyphSegs,
36985
36982
  fillColor: isFillGlyph ? st.fillColor : void 0,
36986
36983
  fillAlpha: isFillGlyph ? st.fillAlpha : void 0,
36987
36984
  strokeColor: isStrokeGlyph ? st.strokeColor : void 0,
36988
36985
  strokeAlpha: isStrokeGlyph ? st.strokeAlpha : void 0,
36989
- strokeWidth: isStrokeGlyph ? st.strokeWidth * ctmScale : 0,
36986
+ strokeWidth: isStrokeGlyph ? st.strokeWidth : 0,
36987
+ ...isStrokeGlyph ? { strokeMatrix: [...st.ctm] } : {},
36990
36988
  fillRule: "nonzero",
36991
36989
  ...st.blendMode && st.blendMode !== "Normal" ? { blendMode: st.blendMode } : {},
36992
36990
  ...st.clipRect ? { clipRect: [...st.clipRect] } : {}
@@ -39364,6 +39362,184 @@ function downscaleImage(image, widthScale, heightScale) {
39364
39362
  }
39365
39363
  return image;
39366
39364
  }
39365
+ var MAX_STROKE_VERTICES = 1e6;
39366
+ function dashSubpath(path, pattern, phase) {
39367
+ const cycle = pattern.reduce((sum, value) => sum + value, 0);
39368
+ if (!(cycle > 0)) return [path];
39369
+ let offset = (phase % cycle + cycle) % cycle, index = 0;
39370
+ while (offset > 0) {
39371
+ if (offset < pattern[index]) break;
39372
+ offset -= pattern[index];
39373
+ index = (index + 1) % pattern.length;
39374
+ }
39375
+ let remaining = pattern[index] - offset;
39376
+ const points = path.closed ? [...path.points, path.points[0]] : path.points;
39377
+ const result = [];
39378
+ let current = [];
39379
+ let steps = 0;
39380
+ const flush = () => {
39381
+ if (current.length) result.push({ points: current, closed: false });
39382
+ current = [];
39383
+ };
39384
+ const advance = (point) => {
39385
+ while (remaining <= 0) {
39386
+ if (++steps > MAX_STROKE_VERTICES) throw new PdfError("E_LIMIT", "Stroke dash expansion exceeds the vertex limit");
39387
+ if (index % 2 === 0) {
39388
+ if (pattern[index] === 0) result.push({ points: [point, point], closed: false });
39389
+ flush();
39390
+ }
39391
+ index = (index + 1) % pattern.length;
39392
+ remaining = pattern[index];
39393
+ }
39394
+ };
39395
+ for (let i = 1; i < points.length; i++) {
39396
+ const start = points[i - 1], end = points[i];
39397
+ const dx = end[0] - start[0], dy = end[1] - start[1];
39398
+ const length = Math.hypot(dx, dy);
39399
+ if (!Number.isFinite(length) || length / cycle * pattern.length > MAX_STROKE_VERTICES) {
39400
+ throw new PdfError("E_LIMIT", "Stroke dash expansion exceeds the vertex limit");
39401
+ }
39402
+ let distance = 0;
39403
+ while (distance < length) {
39404
+ const point = [start[0] + dx * distance / length, start[1] + dy * distance / length];
39405
+ advance(point);
39406
+ const step = Math.min(remaining, length - distance);
39407
+ if (distance + step === distance || ++steps > MAX_STROKE_VERTICES) throw new PdfError("E_LIMIT", "Stroke dash expansion exceeds the vertex limit");
39408
+ distance += step;
39409
+ remaining -= step;
39410
+ if (index % 2 === 0) {
39411
+ if (!current.length) current.push(point);
39412
+ current.push([start[0] + dx * distance / length, start[1] + dy * distance / length]);
39413
+ }
39414
+ }
39415
+ }
39416
+ flush();
39417
+ if (path.closed && result.length) {
39418
+ const first = result[0], last = result[result.length - 1];
39419
+ const start = path.points[0];
39420
+ const firstPoint = first.points[0], lastPoint = last.points[last.points.length - 1];
39421
+ if (firstPoint[0] === start[0] && firstPoint[1] === start[1] && lastPoint[0] === start[0] && lastPoint[1] === start[1]) {
39422
+ if (first === last) result[0] = { points: first.points, closed: true };
39423
+ else {
39424
+ result[0] = { points: [...last.points, ...first.points.slice(1)], closed: false };
39425
+ result.pop();
39426
+ }
39427
+ }
39428
+ }
39429
+ return result;
39430
+ }
39431
+ function strokeOutlines(paths, width, cap, join, miterLimit, dashArray = [], dashPhase = 0) {
39432
+ const half = width / 2;
39433
+ const contours = [];
39434
+ const pattern = dashArray.length % 2 ? [...dashArray, ...dashArray] : dashArray;
39435
+ let count = 0;
39436
+ let contour = [];
39437
+ const add = (x, y) => {
39438
+ if (++count > MAX_STROKE_VERTICES) throw new PdfError("E_LIMIT", "Stroke outline exceeds the vertex limit");
39439
+ contour.push([x, y]);
39440
+ };
39441
+ const arc = (point, dx1, dy1, dx2, dy2) => {
39442
+ let a1 = Math.atan2(dy1, dx1), a2 = Math.atan2(dy2, dx2);
39443
+ const ccw = a1 - a2 > 0 && a1 - a2 < Math.PI;
39444
+ const step = Math.acos(half / (half + 0.125)) * 2;
39445
+ add(point[0] + dx1, point[1] + dy1);
39446
+ if (step > 0) {
39447
+ if (!ccw) {
39448
+ if (a1 > a2) a2 += 2 * Math.PI;
39449
+ a2 -= step / 4;
39450
+ if ((a2 - a1) / step > MAX_STROKE_VERTICES - count) throw new PdfError("E_LIMIT", "Stroke arc exceeds the vertex limit");
39451
+ for (a1 += step; a1 < a2; a1 += step) add(point[0] + half * Math.cos(a1), point[1] + half * Math.sin(a1));
39452
+ } else {
39453
+ if (a1 < a2) a2 -= 2 * Math.PI;
39454
+ a2 += step / 4;
39455
+ if ((a1 - a2) / step > MAX_STROKE_VERTICES - count) throw new PdfError("E_LIMIT", "Stroke arc exceeds the vertex limit");
39456
+ for (a1 -= step; a1 > a2; a1 -= step) add(point[0] + half * Math.cos(a1), point[1] + half * Math.sin(a1));
39457
+ }
39458
+ }
39459
+ add(point[0] + dx2, point[1] + dy2);
39460
+ };
39461
+ const addCap = (point, next) => {
39462
+ const length = Math.hypot(next[0] - point[0], next[1] - point[1]);
39463
+ const dx = half * (next[1] - point[1]) / length;
39464
+ const dy = half * (next[0] - point[0]) / length;
39465
+ if (cap !== 1) {
39466
+ const sx = cap === 2 ? dy : 0, sy = cap === 2 ? dx : 0;
39467
+ add(point[0] - dx - sx, point[1] + dy - sy);
39468
+ add(point[0] + dx - sx, point[1] - dy - sy);
39469
+ } else {
39470
+ const angle = Math.atan2(dy, -dx);
39471
+ const step = Math.max(1e-3, Math.acos(half / (half + 0.125)) * 2);
39472
+ add(point[0] - dx, point[1] + dy);
39473
+ 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));
39474
+ add(point[0] + dx, point[1] - dy);
39475
+ }
39476
+ };
39477
+ const addJoin = (prev, point, next) => {
39478
+ const len1 = Math.hypot(point[0] - prev[0], point[1] - prev[1]);
39479
+ const len2 = Math.hypot(next[0] - point[0], next[1] - point[1]);
39480
+ const dx1 = half * (point[1] - prev[1]) / len1, dy1 = half * (point[0] - prev[0]) / len1;
39481
+ const dx2 = half * (next[1] - point[1]) / len2, dy2 = half * (next[0] - point[0]) / len2;
39482
+ const inner = (next[0] - point[0]) * (point[1] - prev[1]) - (next[1] - point[1]) * (point[0] - prev[0]) > 0;
39483
+ if (!inner && join === 1) {
39484
+ arc(point, dx1, -dy1, dx2, -dy2);
39485
+ return;
39486
+ }
39487
+ if (inner || join === 0) {
39488
+ const ax = prev[0] + dx1, ay = prev[1] - dy1;
39489
+ const bx = point[0] + dx1, by = point[1] - dy1;
39490
+ const cx = point[0] + dx2, cy = point[1] - dy2;
39491
+ const dx = next[0] + dx2, dy = next[1] - dy2;
39492
+ const den = (bx - ax) * (dy - cy) - (by - ay) * (dx - cx);
39493
+ if (Math.abs(den) >= 1e-30) {
39494
+ const t = ((ay - cy) * (dx - cx) - (ax - cx) * (dy - cy)) / den;
39495
+ const x = ax + (bx - ax) * t, y = ay + (by - ay) * t;
39496
+ if (Math.hypot(x - point[0], y - point[1]) <= half * (inner ? 1.01 : miterLimit)) {
39497
+ add(x, y);
39498
+ return;
39499
+ }
39500
+ } else if ((bx - prev[0]) * dy1 - (prev[1] - by) * dx1 < 0 !== (bx - next[0]) * dy1 - (next[1] - by) * dx1 < 0) {
39501
+ add(bx, by);
39502
+ return;
39503
+ }
39504
+ }
39505
+ add(point[0] + dx1, point[1] - dy1);
39506
+ add(point[0] + dx2, point[1] - dy2);
39507
+ };
39508
+ for (const path of paths) {
39509
+ for (const subpath of pattern.length ? dashSubpath(path, pattern, dashPhase) : [path]) {
39510
+ const points = [];
39511
+ for (const point of subpath.points) {
39512
+ const prev = points[points.length - 1];
39513
+ if (!prev || Math.hypot(prev[0] - point[0], prev[1] - point[1]) > 1e-14) points.push(point);
39514
+ }
39515
+ let closed = subpath.closed;
39516
+ 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();
39517
+ if (points.length < 3) closed = false;
39518
+ if (points.length === 1 && subpath.points.length > 1 && cap === 1) {
39519
+ contour = [];
39520
+ const [x, y] = points[0];
39521
+ addCap([x, y], [x + 1, y]);
39522
+ addCap([x, y], [x - 1, y]);
39523
+ contours.push(contour);
39524
+ } else if (points.length >= 2) {
39525
+ contour = [];
39526
+ if (!closed) addCap(points[0], points[1]);
39527
+ for (let i = closed ? 0 : 1; i < points.length - (closed ? 0 : 1); i++) {
39528
+ addJoin(points[(i + points.length - 1) % points.length], points[i], points[(i + 1) % points.length]);
39529
+ }
39530
+ if (closed) {
39531
+ contours.push(contour);
39532
+ contour = [];
39533
+ } else addCap(points[points.length - 1], points[points.length - 2]);
39534
+ for (let i = points.length - (closed ? 1 : 2); i >= (closed ? 0 : 1); i--) {
39535
+ addJoin(points[(i + 1) % points.length], points[i], points[(i + points.length - 1) % points.length]);
39536
+ }
39537
+ contours.push(contour);
39538
+ }
39539
+ }
39540
+ }
39541
+ return contours;
39542
+ }
39367
39543
  function encodePng(bitmap) {
39368
39544
  return encodeRgbaToPng(bitmap.width, bitmap.height, bitmap.data);
39369
39545
  }
@@ -40454,111 +40630,116 @@ function blendPixel(rgba, width, height, px, py, color, alpha, blendMode, clipMa
40454
40630
  rgba[idx + 2] = Math.round((effB * a + cbB * dstA * (1 - a)) / outA * 255);
40455
40631
  rgba[idx + 3] = Math.round(outA * 255);
40456
40632
  }
40457
- function drawAntiAliasedSegment(rgba, width, height, x0, y0, x1, y1, strokeWidth, color, alpha = 1, lineCap = 0, antialias = true, blendMode, clipMask) {
40458
- const halfW = Math.max(0.6, strokeWidth * 0.5);
40459
- const dx = x1 - x0;
40460
- const dy = y1 - y0;
40461
- const lenSq = dx * dx + dy * dy;
40462
- const segLen = Math.sqrt(lenSq);
40463
- const pad = lineCap === 2 ? halfW + 2 : halfW + 1;
40464
- const minX = Math.max(0, Math.floor(Math.min(x0, x1) - pad));
40465
- const maxX = Math.min(width - 1, Math.ceil(Math.max(x0, x1) + pad));
40466
- const minY = Math.max(0, Math.floor(Math.min(y0, y1) - pad));
40467
- const maxY = Math.min(height - 1, Math.ceil(Math.max(y0, y1) + pad));
40468
- for (let py = minY; py <= maxY; py++) {
40469
- for (let px = minX; px <= maxX; px++) {
40470
- const cx = px + 0.5;
40471
- const cy = py + 0.5;
40472
- if (segLen > 1e-6 && (lineCap === 0 || lineCap === 2)) {
40473
- const ux = dx / segLen;
40474
- const uy = dy / segLen;
40475
- const along = (cx - x0) * ux + (cy - y0) * uy;
40476
- const perp = Math.abs(-(cx - x0) * uy + (cy - y0) * ux);
40477
- const minAlong = lineCap === 2 ? -halfW : 0;
40478
- const maxAlong = lineCap === 2 ? segLen + halfW : segLen;
40479
- const overAlong = along < minAlong ? minAlong - along : along > maxAlong ? along - maxAlong : 0;
40480
- const dist2 = overAlong > 0 ? Math.max(perp, halfW + overAlong) : perp;
40481
- if (dist2 <= halfW + 0.75) {
40482
- const cov = antialias ? Math.max(0, Math.min(1, halfW + 0.5 - dist2)) : dist2 <= halfW + 0.25 ? 1 : 0;
40483
- blendPixel(rgba, width, height, px, py, color, cov * alpha, blendMode, clipMask);
40484
- }
40485
- continue;
40486
- }
40487
- let t = lenSq > 1e-6 ? ((cx - x0) * dx + (cy - y0) * dy) / lenSq : 0;
40488
- t = Math.max(0, Math.min(1, t));
40489
- const projX = x0 + t * dx;
40490
- const projY = y0 + t * dy;
40491
- const dist = Math.hypot(cx - projX, cy - projY);
40492
- if (dist <= halfW + 0.75) {
40493
- const cov = antialias ? Math.max(0, Math.min(1, halfW + 0.5 - dist)) : dist <= halfW + 0.25 ? 1 : 0;
40494
- blendPixel(rgba, width, height, px, py, color, cov * alpha, blendMode, clipMask);
40495
- }
40496
- }
40497
- }
40633
+ function segmentsToScreenPaths(segments, pageHeight, scale, toScreen = (x, y) => [x * scale, (pageHeight - y) * scale]) {
40634
+ const paths = [];
40635
+ let points = [];
40636
+ let current = [0, 0];
40637
+ for (const segment of segments) {
40638
+ if (segment.kind === "move") {
40639
+ if (points.length) paths.push({ points, closed: false });
40640
+ current = toScreen(segment.x, segment.y);
40641
+ points = [current];
40642
+ } else if (segment.kind === "line") {
40643
+ if (!points.length) points = [current];
40644
+ current = toScreen(segment.x, segment.y);
40645
+ points.push(current);
40646
+ } else if (segment.kind === "cubic") {
40647
+ if (!points.length) points = [current];
40648
+ const a = toScreen(segment.x1, segment.y1), b = toScreen(segment.x2, segment.y2);
40649
+ const end = toScreen(segment.x, segment.y);
40650
+ const curve = flattenCubic(current[0], current[1], a[0], a[1], b[0], b[1], end[0], end[1]);
40651
+ for (let i = 1; i < curve.length; i++) points.push(curve[i]);
40652
+ current = end;
40653
+ } else if (segment.kind === "close") {
40654
+ if (points.length) {
40655
+ paths.push({ points, closed: true });
40656
+ current = points[0];
40657
+ points = [];
40658
+ }
40659
+ } else if (segment.kind === "rect") {
40660
+ if (points.length) paths.push({ points, closed: false });
40661
+ const first = toScreen(segment.x, segment.y);
40662
+ paths.push({ points: [
40663
+ first,
40664
+ toScreen(segment.x + segment.width, segment.y),
40665
+ toScreen(segment.x + segment.width, segment.y + segment.height),
40666
+ toScreen(segment.x, segment.y + segment.height)
40667
+ ], closed: true });
40668
+ current = first;
40669
+ points = [];
40670
+ }
40671
+ }
40672
+ if (points.length) paths.push({ points, closed: false });
40673
+ return paths;
40674
+ }
40675
+ function inverseStrokeMatrix(matrix) {
40676
+ const [a, b, c, d, e, f] = matrix;
40677
+ const det = a * d - b * c;
40678
+ if (!Number.isFinite(det) || det === 0) return void 0;
40679
+ return [d / det, -b / det, -c / det, a / det, (c * f - d * e) / det, (b * e - a * f) / det];
40680
+ }
40681
+ function prepareStroke(path, scale) {
40682
+ const matrix = path.strokeMatrix ? [...path.strokeMatrix] : [1, 0, 0, 1, 0, 0];
40683
+ const [a, b, c, d] = matrix;
40684
+ const area = Math.abs(a * d - b * c) * scale * scale;
40685
+ if (!(area > 0) || !Number.isFinite(area)) return void 0;
40686
+ const normX = Math.hypot(a, b) * scale, normY = Math.hypot(c, d) * scale;
40687
+ let width = path.strokeWidth || 1;
40688
+ const scaleX = path.strokeWidth === 0 ? normY / area : Math.max(1, normY / (width * area));
40689
+ const scaleY = path.strokeWidth === 0 ? normX / area : Math.max(1, normX / (width * area));
40690
+ let dashArray = path.dashArray, dashPhase = path.dashPhase ?? 0;
40691
+ if (scaleX === scaleY) {
40692
+ width *= scaleX;
40693
+ } else {
40694
+ matrix[0] *= scaleX;
40695
+ matrix[1] *= scaleX;
40696
+ matrix[2] *= scaleY;
40697
+ matrix[3] *= scaleY;
40698
+ if (dashArray?.length) {
40699
+ const dashScale = Math.max(scaleX, scaleY);
40700
+ dashArray = dashArray.map((value) => value / dashScale);
40701
+ dashPhase /= dashScale;
40702
+ }
40703
+ }
40704
+ return { matrix, width, dashArray, dashPhase };
40705
+ }
40706
+ function strokeEdges(path, pageHeight, scale) {
40707
+ const stroke = prepareStroke(path, scale);
40708
+ if (!stroke) return [];
40709
+ const inverse = inverseStrokeMatrix(stroke.matrix);
40710
+ if (!inverse) return [];
40711
+ const [a, b, c, d, e, f] = stroke.matrix;
40712
+ const strokeScale = scale * Math.hypot(a, b, c, d);
40713
+ const project = (x, y) => [
40714
+ (inverse[0] * x + inverse[2] * y + inverse[4]) * strokeScale,
40715
+ (inverse[1] * x + inverse[3] * y + inverse[5]) * strokeScale
40716
+ ];
40717
+ const toScreen = ([x, y]) => [
40718
+ (a * x / strokeScale + c * y / strokeScale + e) * scale,
40719
+ (pageHeight - b * x / strokeScale - d * y / strokeScale - f) * scale
40720
+ ];
40721
+ const contours = strokeOutlines(
40722
+ segmentsToScreenPaths(path.segments, pageHeight, scale, project),
40723
+ stroke.width * strokeScale,
40724
+ path.lineCap ?? 0,
40725
+ path.lineJoin ?? 0,
40726
+ path.miterLimit ?? 10,
40727
+ stroke.dashArray?.map((value) => Math.max(0, value * strokeScale)),
40728
+ stroke.dashPhase * strokeScale
40729
+ );
40730
+ return pathsToEdges(contours.map((points) => ({ points: points.map(toScreen), closed: true })));
40498
40731
  }
40499
- function segmentsToScreenEdges(segments, pageHeight, scale, closeSubpaths = false) {
40732
+ function pathsToEdges(paths, closeSubpaths = false) {
40500
40733
  const edges = [];
40501
- let curX = 0;
40502
- let curY = 0;
40503
- let startX = 0;
40504
- let startY = 0;
40505
- let hasOpenSubpath = false;
40506
- const toScreen = (x, y) => [x * scale, (pageHeight - y) * scale];
40507
- for (const seg of segments) {
40508
- if (seg.kind === "move") {
40509
- if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40510
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40511
- }
40512
- [curX, curY] = toScreen(seg.x, seg.y);
40513
- startX = curX;
40514
- startY = curY;
40515
- hasOpenSubpath = false;
40516
- } else if (seg.kind === "line") {
40517
- const [nx, ny] = toScreen(seg.x, seg.y);
40518
- edges.push({ x0: curX, y0: curY, x1: nx, y1: ny });
40519
- curX = nx;
40520
- curY = ny;
40521
- hasOpenSubpath = true;
40522
- } else if (seg.kind === "cubic") {
40523
- const [p1x, p1y] = toScreen(seg.x1, seg.y1);
40524
- const [p2x, p2y] = toScreen(seg.x2, seg.y2);
40525
- const [p3x, p3y] = toScreen(seg.x, seg.y);
40526
- const points = flattenCubic(curX, curY, p1x, p1y, p2x, p2y, p3x, p3y);
40527
- let px = curX;
40528
- let py = curY;
40529
- for (let i = 1; i < points.length; i++) {
40530
- const [qx, qy] = points[i];
40531
- edges.push({ x0: px, y0: py, x1: qx, y1: qy });
40532
- px = qx;
40533
- py = qy;
40534
- }
40535
- curX = p3x;
40536
- curY = p3y;
40537
- hasOpenSubpath = true;
40538
- } else if (seg.kind === "close") {
40539
- if (curX !== startX || curY !== startY) {
40540
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40541
- curX = startX;
40542
- curY = startY;
40543
- }
40544
- hasOpenSubpath = false;
40545
- } else if (seg.kind === "rect") {
40546
- if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40547
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40548
- }
40549
- const [rx0, ry0] = toScreen(seg.x, seg.y);
40550
- const [rx1, ry1] = toScreen(seg.x + seg.width, seg.y + seg.height);
40551
- edges.push(
40552
- { x0: rx0, y0: ry0, x1: rx1, y1: ry0 },
40553
- { x0: rx1, y0: ry0, x1: rx1, y1: ry1 },
40554
- { x0: rx1, y0: ry1, x1: rx0, y1: ry1 },
40555
- { x0: rx0, y0: ry1, x1: rx0, y1: ry0 }
40556
- );
40557
- hasOpenSubpath = false;
40734
+ for (const { points, closed } of paths) {
40735
+ for (let i = 1; i < points.length; i++) {
40736
+ const a = points[i - 1], b = points[i];
40737
+ edges.push({ x0: a[0], y0: a[1], x1: b[0], y1: b[1] });
40738
+ }
40739
+ if ((closed || closeSubpaths) && points.length > 1) {
40740
+ const a = points[points.length - 1], b = points[0];
40741
+ edges.push({ x0: a[0], y0: a[1], x1: b[0], y1: b[1] });
40558
40742
  }
40559
- }
40560
- if (closeSubpaths && hasOpenSubpath && (curX !== startX || curY !== startY)) {
40561
- edges.push({ x0: curX, y0: curY, x1: startX, y1: startY });
40562
40743
  }
40563
40744
  return edges;
40564
40745
  }
@@ -40745,7 +40926,7 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40745
40926
  for (const clip of clips) {
40746
40927
  const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
40747
40928
  const layer = new Uint8Array(width * height * 4);
40748
- fillEdgesScanline4x4(layer, width, height, segmentsToScreenEdges(segments, displayList.height, scale, true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40929
+ fillEdgesScanline4x4(layer, width, height, pathsToEdges(segmentsToScreenPaths(segments, displayList.height, scale), true), { r: 1, g: 1, b: 1 }, 1, fillRule);
40749
40930
  for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
40750
40931
  }
40751
40932
  cachedClips = clips;
@@ -40792,62 +40973,28 @@ function renderDisplayListToBitmap(displayList, options = {}) {
40792
40973
  if (operation.kind === "path") {
40793
40974
  const path = operation.value;
40794
40975
  if (path.fillColor) {
40795
- const edges = segmentsToScreenEdges(path.segments, displayList.height, scale, true);
40976
+ const edges = pathsToEdges(segmentsToScreenPaths(path.segments, displayList.height, scale), true);
40796
40977
  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;
40797
40978
  fillEdgesScanline4x4(rgba, width, height, edges, path.fillColor, path.fillAlpha ?? 1, path.fillRule ?? "nonzero", clipScreen, original.kind === "glyph" ? aaTxt : aaVec, path.blendMode, clipMask);
40798
40979
  }
40799
40980
  if (path.strokeColor) {
40800
- const edges = segmentsToScreenEdges(path.segments, displayList.height, scale, false);
40801
- const rawSw = path.strokeWidth * scale;
40802
- const sw = Math.max(1, rawSw);
40981
+ const rawSw = path.strokeWidth * scale * (path.strokeMatrix ? Math.hypot(path.strokeMatrix[0], path.strokeMatrix[1]) : 1);
40803
40982
  const strokeAlpha = options.thinLineMode === "shape" && rawSw < 1 ? (path.strokeAlpha ?? 1) * Math.max(0.25, rawSw) : path.strokeAlpha ?? 1;
40804
- const dashArr = path.dashArray && path.dashArray.length > 0 ? path.dashArray.map((d) => Math.max(0, d * scale)) : void 0;
40805
- const dashCycle = dashArr ? dashArr.reduce((a, b) => a + b, 0) : 0;
40806
- let dashDist = (path.dashPhase ?? 0) * scale % (dashCycle || 1);
40807
- if (dashDist < 0) dashDist += dashCycle;
40808
- for (const e of edges) {
40809
- if (!dashArr || dashCycle <= 1e-4) {
40810
- 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);
40811
- } else {
40812
- const dx = e.x1 - e.x0;
40813
- const dy = e.y1 - e.y0;
40814
- const edgeLen = Math.hypot(dx, dy);
40815
- if (edgeLen <= 1e-4) continue;
40816
- let pos = 0;
40817
- while (pos < edgeLen) {
40818
- let cycleRem = dashDist % dashCycle;
40819
- let dIdx = 0;
40820
- while (dIdx < dashArr.length - 1 && cycleRem >= dashArr[dIdx]) {
40821
- cycleRem -= dashArr[dIdx];
40822
- dIdx++;
40823
- }
40824
- const segRemainInDash = Math.max(1e-4, dashArr[dIdx] - cycleRem);
40825
- const step = Math.min(edgeLen - pos, segRemainInDash);
40826
- if (dIdx % 2 === 0) {
40827
- const t0 = pos / edgeLen;
40828
- const t1 = (pos + step) / edgeLen;
40829
- drawAntiAliasedSegment(
40830
- rgba,
40831
- width,
40832
- height,
40833
- e.x0 + dx * t0,
40834
- e.y0 + dy * t0,
40835
- e.x0 + dx * t1,
40836
- e.y0 + dy * t1,
40837
- sw,
40838
- path.strokeColor,
40839
- strokeAlpha,
40840
- path.lineCap ?? 0,
40841
- original.kind === "glyph" ? aaTxt : aaVec,
40842
- path.blendMode,
40843
- clipMask
40844
- );
40845
- }
40846
- pos += step;
40847
- dashDist = (dashDist + step) % dashCycle;
40848
- }
40849
- }
40850
- }
40983
+ const edges = strokeEdges(path, displayList.height, scale);
40984
+ 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;
40985
+ fillEdgesScanline4x4(
40986
+ rgba,
40987
+ width,
40988
+ height,
40989
+ edges,
40990
+ path.strokeColor,
40991
+ strokeAlpha,
40992
+ "nonzero",
40993
+ clipScreen,
40994
+ original.kind === "glyph" ? aaTxt : aaVec,
40995
+ path.blendMode,
40996
+ clipMask
40997
+ );
40851
40998
  }
40852
40999
  } else if (operation.kind === "image") {
40853
41000
  const img = operation.value;
@@ -40983,17 +41130,19 @@ function svgImage(image, pageHeight) {
40983
41130
  const [a, b, c, d, e, f] = image.matrix;
40984
41131
  return `<image width="1" height="1" preserveAspectRatio="none" transform="matrix(${a} ${-b} ${-c} ${d} ${e + c} ${pageHeight - f - d})" href="data:image/png;base64,${btoa(chunks.join(""))}"/>`;
40985
41132
  }
40986
- function svgPathData(segments, height) {
41133
+ function svgPathData(segments, height, matrix) {
40987
41134
  const dParts = [];
41135
+ const point = (x, y) => matrix ? `${matrix[0] * x + matrix[2] * y + matrix[4]} ${height - matrix[1] * x - matrix[3] * y - matrix[5]}` : `${x} ${height - y}`;
40988
41136
  for (const seg of segments) {
40989
41137
  if (seg.kind === "move") {
40990
- dParts.push(`M ${seg.x} ${height - seg.y}`);
41138
+ dParts.push(`M ${point(seg.x, seg.y)}`);
40991
41139
  } else if (seg.kind === "line") {
40992
- dParts.push(`L ${seg.x} ${height - seg.y}`);
41140
+ dParts.push(`L ${point(seg.x, seg.y)}`);
40993
41141
  } else if (seg.kind === "cubic") {
40994
- dParts.push(`C ${seg.x1} ${height - seg.y1} ${seg.x2} ${height - seg.y2} ${seg.x} ${height - seg.y}`);
41142
+ dParts.push(`C ${point(seg.x1, seg.y1)} ${point(seg.x2, seg.y2)} ${point(seg.x, seg.y)}`);
40995
41143
  } else if (seg.kind === "rect") {
40996
- dParts.push(`M ${seg.x} ${height - seg.y} h ${seg.width} v ${-seg.height} h ${-seg.width} Z`);
41144
+ if (matrix) dParts.push(`M ${point(seg.x, seg.y)} L ${point(seg.x + seg.width, seg.y)} L ${point(seg.x + seg.width, seg.y + seg.height)} L ${point(seg.x, seg.y + seg.height)} Z`);
41145
+ else dParts.push(`M ${seg.x} ${height - seg.y} h ${seg.width} v ${-seg.height} h ${-seg.width} Z`);
40997
41146
  } else if (seg.kind === "close") {
40998
41147
  dParts.push("Z");
40999
41148
  }
@@ -41072,21 +41221,25 @@ function renderDisplayListToSvg(displayList, options = {}) {
41072
41221
  parts.push("</g>");
41073
41222
  } else if (operation.kind === "path") {
41074
41223
  const p = operation.value;
41075
- const pathData = svgPathData(p.segments, displayList.height);
41224
+ const prepared = p.strokeColor ? prepareStroke(p, Math.max(scaleX, scaleY)) : void 0;
41225
+ const matrix = prepared && (prepared.matrix[0] !== 1 || prepared.matrix[1] !== 0 || prepared.matrix[2] !== 0 || prepared.matrix[3] !== 1) ? prepared.matrix : void 0;
41226
+ const inverse = matrix ? inverseStrokeMatrix(matrix) : void 0;
41227
+ const pathData = svgPathData(p.segments, inverse ? 0 : displayList.height, inverse);
41228
+ const transformAttr = inverse && matrix ? ` transform="matrix(${matrix[0]} ${-matrix[1]} ${-matrix[2]} ${matrix[3]} ${matrix[4]} ${displayList.height - matrix[5]})"` : "";
41076
41229
  if (pathData) {
41077
41230
  const fill = p.fillColor ? `rgb(${Math.round(p.fillColor.r * 255)},${Math.round(p.fillColor.g * 255)},${Math.round(p.fillColor.b * 255)})` : "none";
41078
- const stroke = p.strokeColor ? `rgb(${Math.round(p.strokeColor.r * 255)},${Math.round(p.strokeColor.g * 255)},${Math.round(p.strokeColor.b * 255)})` : "none";
41231
+ const stroke = p.strokeColor && prepared ? `rgb(${Math.round(p.strokeColor.r * 255)},${Math.round(p.strokeColor.g * 255)},${Math.round(p.strokeColor.b * 255)})` : "none";
41079
41232
  const fillRuleAttr = p.fillRule === "evenodd" ? ` fill-rule="evenodd"` : "";
41080
41233
  const fillOpacityAttr = p.fillAlpha !== void 0 && p.fillAlpha < 1 ? ` fill-opacity="${p.fillAlpha}"` : "";
41081
41234
  const strokeOpacityAttr = p.strokeAlpha !== void 0 && p.strokeAlpha < 1 ? ` stroke-opacity="${p.strokeAlpha}"` : "";
41082
- const strokeWidthAttr = p.strokeWidth <= 0 ? ` stroke-width="1" vector-effect="non-scaling-stroke"` : ` stroke-width="${p.strokeWidth}"`;
41235
+ const strokeWidthAttr = p.strokeWidth <= 0 && !matrix && !p.dashArray?.length ? ` stroke-width="1" vector-effect="non-scaling-stroke"` : ` stroke-width="${prepared?.width ?? p.strokeWidth}"`;
41083
41236
  const lineCapAttr = p.lineCap === 1 ? ` stroke-linecap="round"` : p.lineCap === 2 ? ` stroke-linecap="square"` : "";
41084
41237
  const lineJoinAttr = p.lineJoin === 1 ? ` stroke-linejoin="round"` : p.lineJoin === 2 ? ` stroke-linejoin="bevel"` : "";
41085
41238
  const miterLimitAttr = p.miterLimit !== void 0 && p.miterLimit !== 10 ? ` stroke-miterlimit="${p.miterLimit}"` : "";
41086
- const dashArrayAttr = p.dashArray && p.dashArray.length > 0 ? ` stroke-dasharray="${p.dashArray.join(" ")}"` : "";
41087
- const dashOffsetAttr = p.dashPhase ? ` stroke-dashoffset="${p.dashPhase}"` : "";
41239
+ const dashArrayAttr = prepared?.dashArray?.length ? ` stroke-dasharray="${prepared.dashArray.join(" ")}"` : "";
41240
+ const dashOffsetAttr = prepared?.dashPhase ? ` stroke-dashoffset="${prepared.dashPhase}"` : "";
41088
41241
  const labelAttr = original.kind === "glyph" ? ` aria-label="${escapeXmlText(original.value.unicode)}"` : "";
41089
- parts.push(` <path${labelAttr} d="${pathData}" fill="${fill}"${fillRuleAttr}${fillOpacityAttr} stroke="${stroke}"${strokeOpacityAttr}${strokeWidthAttr}${lineCapAttr}${lineJoinAttr}${miterLimitAttr}${dashArrayAttr}${dashOffsetAttr}/>`);
41242
+ parts.push(` <path${labelAttr}${transformAttr} d="${pathData}" fill="${fill}"${fillRuleAttr}${fillOpacityAttr} stroke="${stroke}"${strokeOpacityAttr}${strokeWidthAttr}${lineCapAttr}${lineJoinAttr}${miterLimitAttr}${dashArrayAttr}${dashOffsetAttr}/>`);
41090
41243
  }
41091
41244
  } else if (operation.kind === "image") {
41092
41245
  parts.push(` ${svgImage(operation.value, displayList.height)}`);
@@ -44512,3 +44665,15 @@ export {
44512
44665
  * Copyright 2012 Mozilla Foundation. Licensed under Apache-2.0.
44513
44666
  * See THIRD_PARTY_NOTICES.md for the source revision and adaptations.
44514
44667
  */
44668
+ /*!
44669
+ * Anti-Grain Geometry - Version 2.3
44670
+ * Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
44671
+ *
44672
+ * Permission to copy, use, modify, sell and distribute this software
44673
+ * is granted provided this copyright notice appears in all copies.
44674
+ * This software is provided "as is" without express or implied
44675
+ * warranty, and with no claim as to its suitability for any purpose.
44676
+ *
44677
+ * Adapted from PDFium agg_math_stroke.h and agg_vcgen_stroke.cpp.
44678
+ * See THIRD_PARTY_NOTICES.md for the source revision and adaptations.
44679
+ */