Skip to content

Commit abc450a

Browse files
Merge pull request #431 from SixLabors/js/polygonclipper-v2
Integrate PolygonClipper V2
2 parents 43cd8a2 + a46136b commit abc450a

53 files changed

Lines changed: 621 additions & 593 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/ImageSharp.Drawing.WebGPU/Shaders/WgslSource/path_lowering.wgsl‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn stroke_normalize_positive_angle(angle: f32) -> f32 {
8787

8888
// Port of the CPU stroker's GetArcSubdivisionCount (round join and cap tessellation): returns the
8989
// number of interior vertices needed to keep the arc's chordal error within the arc detail scale.
90-
fn stroke_arc_subdivision_count(radius: f32, sweep: f32, arc_detail_scale: f32) -> u32 {
90+
fn stroke_arc_subdivision_count(radius: f32, sweep: f32, arc_detail_scale: f32, limit: u32) -> u32 {
9191
let safe_radius = max(radius, TANGENT_THRESH);
9292
let safe_scale = max(arc_detail_scale, 0.01);
9393
let ratio = clamp(safe_radius / (safe_radius + (0.125 / safe_scale)), -1.0, 1.0);
@@ -97,7 +97,7 @@ fn stroke_arc_subdivision_count(radius: f32, sweep: f32, arc_detail_scale: f32)
9797
}
9898
// The f32 radius and acos can give more vertices than the CPU count. The CPU line estimate
9999
// allows the arc limit, so the count is clamped to it.
100-
return min(u32(max(sweep / theta, 0.0)), arc_limit);
100+
return min(u32(max(sweep / theta, 0.0)), limit);
101101
}
102102

103103
// Port of the CPU stroker's AppendDirectedArcContour (round caps): steps through the positive
@@ -118,7 +118,7 @@ fn stroke_chain_arc(
118118
// must rotate the start offset; absolute angles of the offsets are not computable here.
119119
let cross_ft = (from_offset.x * to_offset.y) - (from_offset.y * to_offset.x);
120120
let sweep = stroke_normalize_positive_angle(atan2(cross_ft, dot(from_offset, to_offset)));
121-
let n = stroke_arc_subdivision_count(radius, sweep, arc_detail_scale);
121+
let n = stroke_arc_subdivision_count(radius, sweep, arc_detail_scale, arc_limit);
122122
let step = sweep / f32(n + 1u);
123123
let rot_c = cos(step);
124124
let rot_s = sin(step);
@@ -144,7 +144,7 @@ fn stroke_calc_arc(
144144
let cross_oo = (o1.x * o2.y) - (o1.y * o2.x);
145145
let sweep = stroke_normalize_positive_angle(atan2(cross_oo, dot(o1, o2)));
146146
stroke_chain_point(path_ix, last, v1 + o1, transform);
147-
let n = stroke_arc_subdivision_count(half_width, sweep, arc_detail_scale);
147+
let n = stroke_arc_subdivision_count(half_width, sweep, arc_detail_scale, arc_limit);
148148
let step = sweep / f32(n + 1u);
149149
let rot_c = cos(step);
150150
let rot_s = sin(step);
@@ -657,21 +657,45 @@ fn main(
657657
if is_open {
658658
// Start cap. The marker carries the first segment's tangent; the CPU stroker's
659659
// caps chain from cap0 to cap1 so they splice into the offset edges.
660-
var tangent = pts.p1 - pts.p0;
661-
if dot(tangent, tangent) < TANGENT_THRESH * TANGENT_THRESH {
662-
tangent = vec2(TANGENT_THRESH, 0.);
660+
let tangent = pts.p1 - pts.p0;
661+
let start_cap = (style_flags & STYLE_FLAGS_START_CAP_MASK) >> 2u;
662+
if all(tangent == vec2f(0.)) {
663+
// Equal marker points denote a zero-length segment. Emit its entire
664+
// outline here because the body has no direction and emits no lines.
665+
if start_cap == STYLE_FLAGS_CAP_ROUND {
666+
// Use one full turn as PolygonClipper does. Two cap budgets
667+
// cover the full circle because each includes its final edge.
668+
let full_turn = 6.283185307179586;
669+
let count = max(4u, stroke_arc_subdivision_count(half_width, full_turn, arc_detail_scale, 2u * (arc_limit + 1u)));
670+
let step = full_turn / f32(count);
671+
let first = pts.p0 + vec2f(half_width, 0.);
672+
var last = first;
673+
for (var i = 1u; i < count; i += 1u) {
674+
let angle = step * f32(i);
675+
let next = pts.p0 + half_width * vec2f(cos(angle), sin(angle));
676+
stroke_chain_point(path_ix, &last, next, transform);
677+
}
678+
stroke_chain_point(path_ix, &last, first, transform);
679+
} else {
680+
let p0 = pts.p0 + vec2f(-half_width, -half_width);
681+
let p1 = pts.p0 + vec2f(half_width, -half_width);
682+
let p2 = pts.p0 + vec2f(half_width, half_width);
683+
let p3 = pts.p0 + vec2f(-half_width, half_width);
684+
output_two_lines_with_transform(path_ix, p0, p1, p1, p2, transform);
685+
output_two_lines_with_transform(path_ix, p2, p3, p3, p0, transform);
686+
}
687+
} else {
688+
let offset_tangent = half_width * normalize(tangent);
689+
let n = vec2(offset_tangent.y, -offset_tangent.x);
690+
draw_cap(path_ix, start_cap, arc_detail_scale,
691+
pts.p0, pts.p0 - n, pts.p0 + n, -offset_tangent, transform);
663692
}
664-
let offset_tangent = half_width * normalize(tangent);
665-
let n = vec2(offset_tangent.y, -offset_tangent.x);
666-
draw_cap(path_ix, (style_flags & STYLE_FLAGS_START_CAP_MASK) >> 2u, arc_detail_scale,
667-
pts.p0, pts.p0 - n, pts.p0 + n, -offset_tangent, transform);
668693
} else {
669694
// Don't draw anything if the path is closed.
670695
}
671696
} else {
672697
// CPU stroker port: stroke centerline segments are always straight lines, so the
673698
// offset edges are straight offset lines; joins and caps carry all the curvature.
674-
// The encoder collapses micro-segments (1/64 px) exactly like the CPU stroker.
675699
let p0 = pts.p0;
676700
let p1 = pts.p1;
677701
let seg = p1 - p0;

0 commit comments

Comments
 (0)