Skip to main content

velox_engine/
vector_canvas.rs

1//! 原生 2D 矢量画布原语(`VectorCanvas` / `Polyline` / `Polygon` / `Circle` / `Sector`)。
2//!
3//! 面向实时波形图、折线图、仪表盘、雷达/海图矢量轨迹等高频动态矢量绘制场景:
4//! - 100% 无堆兼容,数据全部驻留当前帧 `ScratchArena`,通过 8B 引用挂入 `DrawPayload::Vector`;
5//! - 彻底规避每帧拼接 SVG 字符串的开销,软件端与 GPU 端均提供原生抗锯齿绘制。
6
7use crate::display::PixelFormat;
8use crate::element::{Element, IntoElement};
9use crate::pod::FrameScope;
10use crate::solver::Region;
11use crate::style::Color;
12use crate::svg::math::{atan2_f32, floor_f32, sqrt_f32};
13
14/// 二维局部浮点坐标点(相对于 `VectorCanvas` 盒子左上角,单位逻辑像素)。
15#[derive(Copy, Clone, Debug, Default)]
16pub struct VecPoint {
17    pub x: f32,
18    pub y: f32,
19}
20
21impl VecPoint {
22    #[inline(always)]
23    pub const fn new(x: f32, y: f32) -> Self {
24        Self { x, y }
25    }
26}
27
28#[inline(always)]
29fn canon_f32(v: f32) -> u32 {
30    if v == 0.0 { 0 } else { v.to_bits() }
31}
32
33impl PartialEq for VecPoint {
34    #[inline(always)]
35    fn eq(&self, other: &Self) -> bool {
36        canon_f32(self.x) == canon_f32(other.x) && canon_f32(self.y) == canon_f32(other.y)
37    }
38}
39
40impl Eq for VecPoint {}
41
42/// 单个矢量画布绘制图元。
43#[derive(Copy, Clone, Debug)]
44pub enum VectorPrimitive<'a> {
45    /// 抗锯齿折线 / 虚线 / 闭合折线环(支持自定义线宽、圆头与虚线样式)。
46    Polyline {
47        points: &'a [VecPoint],
48        color: Color,
49        width: f32,
50        dash_len: f32,
51        gap_len: f32,
52        closed: bool,
53    },
54    /// 任意多边形填充与可选描边(扫描线奇偶规则抗锯齿填充)。
55    Polygon {
56        points: &'a [VecPoint],
57        fill: Color,
58        stroke: Color,
59        stroke_width: f32,
60    },
61    /// 解析 SDF 抗锯齿实心圆 / 圆环。
62    Circle {
63        cx: f32,
64        cy: f32,
65        radius: f32,
66        fill: Color,
67        stroke: Color,
68        stroke_width: f32,
69    },
70    /// 解析 SDF 扇形 / 环扇(角度单位度,0° 为 12 点钟方向顺时针递增)。
71    Sector {
72        cx: f32,
73        cy: f32,
74        inner_r: f32,
75        outer_r: f32,
76        start_deg: f32,
77        sweep_deg: f32,
78        fill: Color,
79    },
80    /// 光滑三次贝塞尔曲线(24 段解析细分抗锯齿描边 + 虚线支持)。
81    CubicBezier {
82        p0: VecPoint,
83        p1: VecPoint,
84        p2: VecPoint,
85        p3: VecPoint,
86        color: Color,
87        width: f32,
88        dash_len: f32,
89        gap_len: f32,
90    },
91    /// 任意坐标系完整 W3C SVG Path `d` 矢量路径(突破 24×24 限制,支持 20 条贝塞尔/椭圆弧指令、`NonZero`/`EvenOdd` 4× AA 填充与任意宽度圆/斜/平头描边)。
92    SvgPath {
93        d: &'a str,
94        fill: Color,
95        fill_rule: crate::svg::SvgFillRule,
96        stroke: Color,
97        stroke_width: f32,
98        line_cap: crate::svg::SvgLineCap,
99        line_join: crate::svg::SvgLineJoin,
100    },
101}
102
103impl<'a> PartialEq for VectorPrimitive<'a> {
104    fn eq(&self, other: &Self) -> bool {
105        match (*self, *other) {
106            (
107                Self::Polyline {
108                    points: p1,
109                    color: c1,
110                    width: w1,
111                    dash_len: d1,
112                    gap_len: g1,
113                    closed: cl1,
114                },
115                Self::Polyline {
116                    points: p2,
117                    color: c2,
118                    width: w2,
119                    dash_len: d2,
120                    gap_len: g2,
121                    closed: cl2,
122                },
123            ) => {
124                p1 == p2
125                    && c1 == c2
126                    && canon_f32(w1) == canon_f32(w2)
127                    && canon_f32(d1) == canon_f32(d2)
128                    && canon_f32(g1) == canon_f32(g2)
129                    && cl1 == cl2
130            }
131            (
132                Self::Polygon {
133                    points: p1,
134                    fill: f1,
135                    stroke: s1,
136                    stroke_width: sw1,
137                },
138                Self::Polygon {
139                    points: p2,
140                    fill: f2,
141                    stroke: s2,
142                    stroke_width: sw2,
143                },
144            ) => p1 == p2 && f1 == f2 && s1 == s2 && canon_f32(sw1) == canon_f32(sw2),
145            (
146                Self::Circle {
147                    cx: x1,
148                    cy: y1,
149                    radius: r1,
150                    fill: f1,
151                    stroke: s1,
152                    stroke_width: sw1,
153                },
154                Self::Circle {
155                    cx: x2,
156                    cy: y2,
157                    radius: r2,
158                    fill: f2,
159                    stroke: s2,
160                    stroke_width: sw2,
161                },
162            ) => {
163                canon_f32(x1) == canon_f32(x2)
164                    && canon_f32(y1) == canon_f32(y2)
165                    && canon_f32(r1) == canon_f32(r2)
166                    && f1 == f2
167                    && s1 == s2
168                    && canon_f32(sw1) == canon_f32(sw2)
169            }
170            (
171                Self::Sector {
172                    cx: x1,
173                    cy: y1,
174                    inner_r: ir1,
175                    outer_r: or1,
176                    start_deg: sd1,
177                    sweep_deg: sw1,
178                    fill: f1,
179                },
180                Self::Sector {
181                    cx: x2,
182                    cy: y2,
183                    inner_r: ir2,
184                    outer_r: or2,
185                    start_deg: sd2,
186                    sweep_deg: sw2,
187                    fill: f2,
188                },
189            ) => {
190                canon_f32(x1) == canon_f32(x2)
191                    && canon_f32(y1) == canon_f32(y2)
192                    && canon_f32(ir1) == canon_f32(ir2)
193                    && canon_f32(or1) == canon_f32(or2)
194                    && canon_f32(sd1) == canon_f32(sd2)
195                    && canon_f32(sw1) == canon_f32(sw2)
196                    && f1 == f2
197            }
198            (
199                Self::CubicBezier {
200                    p0: a0,
201                    p1: a1,
202                    p2: a2,
203                    p3: a3,
204                    color: c1,
205                    width: w1,
206                    dash_len: d1,
207                    gap_len: g1,
208                },
209                Self::CubicBezier {
210                    p0: b0,
211                    p1: b1,
212                    p2: b2,
213                    p3: b3,
214                    color: c2,
215                    width: w2,
216                    dash_len: d2,
217                    gap_len: g2,
218                },
219            ) => {
220                a0 == b0
221                    && a1 == b1
222                    && a2 == b2
223                    && a3 == b3
224                    && c1 == c2
225                    && canon_f32(w1) == canon_f32(w2)
226                    && canon_f32(d1) == canon_f32(d2)
227                    && canon_f32(g1) == canon_f32(g2)
228            }
229            (
230                Self::SvgPath {
231                    d: d1,
232                    fill: f1,
233                    fill_rule: r1,
234                    stroke: s1,
235                    stroke_width: sw1,
236                    line_cap: lc1,
237                    line_join: lj1,
238                },
239                Self::SvgPath {
240                    d: d2,
241                    fill: f2,
242                    fill_rule: r2,
243                    stroke: s2,
244                    stroke_width: sw2,
245                    line_cap: lc2,
246                    line_join: lj2,
247                },
248            ) => {
249                d1 == d2
250                    && f1 == f2
251                    && r1 == r2
252                    && s1 == s2
253                    && canon_f32(sw1) == canon_f32(sw2)
254                    && lc1 == lc2
255                    && lj1 == lj2
256            }
257            _ => false,
258        }
259    }
260}
261
262impl<'a> Eq for VectorPrimitive<'a> {}
263
264/// 矢量画布绘制载荷(分配于 `ScratchArena`,通过 8B 引用挂入 `DrawPayload::Vector`)。
265#[derive(Copy, Clone, PartialEq, Eq, Debug)]
266pub struct VectorDrawSpec<'a> {
267    pub primitives: &'a [VectorPrimitive<'a>],
268    pub width: i16,
269    pub height: i16,
270    pub opacity: u8,
271}
272
273impl<'a> VectorDrawSpec<'a> {
274    pub fn mix_hash(&self, h: &mut u32) {
275        #[inline(always)]
276        fn mix_u8(h: &mut u32, b: u8) {
277            *h ^= b as u32;
278            *h = h.wrapping_mul(0x0100_0193);
279        }
280        #[inline(always)]
281        fn mix_u32(h: &mut u32, v: u32) {
282            for b in v.to_le_bytes() {
283                mix_u8(h, b);
284            }
285        }
286        mix_u32(
287            h,
288            ((self.width as u16 as u32) << 16) | (self.height as u16 as u32),
289        );
290        mix_u8(h, self.opacity);
291        for p in self.primitives {
292            match *p {
293                VectorPrimitive::Polyline {
294                    points,
295                    color,
296                    width,
297                    dash_len,
298                    gap_len,
299                    closed,
300                } => {
301                    mix_u8(h, 1);
302                    mix_u32(h, color.0);
303                    mix_u32(h, canon_f32(width));
304                    mix_u32(h, canon_f32(dash_len));
305                    mix_u32(h, canon_f32(gap_len));
306                    mix_u8(h, if closed { 1 } else { 0 });
307                    for pt in points {
308                        mix_u32(h, canon_f32(pt.x));
309                        mix_u32(h, canon_f32(pt.y));
310                    }
311                }
312                VectorPrimitive::Polygon {
313                    points,
314                    fill,
315                    stroke,
316                    stroke_width,
317                } => {
318                    mix_u8(h, 2);
319                    mix_u32(h, fill.0);
320                    mix_u32(h, stroke.0);
321                    mix_u32(h, canon_f32(stroke_width));
322                    for pt in points {
323                        mix_u32(h, canon_f32(pt.x));
324                        mix_u32(h, canon_f32(pt.y));
325                    }
326                }
327                VectorPrimitive::Circle {
328                    cx,
329                    cy,
330                    radius,
331                    fill,
332                    stroke,
333                    stroke_width,
334                } => {
335                    mix_u8(h, 3);
336                    mix_u32(h, canon_f32(cx));
337                    mix_u32(h, canon_f32(cy));
338                    mix_u32(h, canon_f32(radius));
339                    mix_u32(h, fill.0);
340                    mix_u32(h, stroke.0);
341                    mix_u32(h, canon_f32(stroke_width));
342                }
343                VectorPrimitive::Sector {
344                    cx,
345                    cy,
346                    inner_r,
347                    outer_r,
348                    start_deg,
349                    sweep_deg,
350                    fill,
351                } => {
352                    mix_u8(h, 4);
353                    mix_u32(h, canon_f32(cx));
354                    mix_u32(h, canon_f32(cy));
355                    mix_u32(h, canon_f32(inner_r));
356                    mix_u32(h, canon_f32(outer_r));
357                    mix_u32(h, canon_f32(start_deg));
358                    mix_u32(h, canon_f32(sweep_deg));
359                    mix_u32(h, fill.0);
360                }
361                VectorPrimitive::CubicBezier {
362                    p0,
363                    p1,
364                    p2,
365                    p3,
366                    color,
367                    width,
368                    dash_len,
369                    gap_len,
370                } => {
371                    mix_u8(h, 5);
372                    for pt in [p0, p1, p2, p3] {
373                        mix_u32(h, canon_f32(pt.x));
374                        mix_u32(h, canon_f32(pt.y));
375                    }
376                    mix_u32(h, color.0);
377                    mix_u32(h, canon_f32(width));
378                    mix_u32(h, canon_f32(dash_len));
379                    mix_u32(h, canon_f32(gap_len));
380                }
381                VectorPrimitive::SvgPath {
382                    d,
383                    fill,
384                    fill_rule,
385                    stroke,
386                    stroke_width,
387                    line_cap,
388                    line_join,
389                } => {
390                    mix_u8(h, 6);
391                    mix_u32(h, fill.0);
392                    mix_u8(h, fill_rule as u8);
393                    mix_u32(h, stroke.0);
394                    mix_u32(h, canon_f32(stroke_width));
395                    mix_u8(h, line_cap as u8);
396                    mix_u8(h, line_join as u8);
397                    for &b in d.as_bytes() {
398                        mix_u8(h, b);
399                    }
400                }
401            }
402        }
403    }
404}
405
406/// 声明式原生 2D 矢量画布元素。
407#[derive(Copy, Clone, Debug)]
408pub struct VectorCanvas<'a> {
409    pub spec: VectorDrawSpec<'a>,
410}
411
412/// 构造一个 2D 矢量画布元素(图元切片自动分配于当前帧 `ScratchArena`)。
413#[inline]
414pub fn vector_canvas<'a>(
415    cx: &'a FrameScope,
416    width: i16,
417    height: i16,
418    primitives: &[VectorPrimitive<'a>],
419) -> VectorCanvas<'a> {
420    let alloc = cx.arena().alloc_iter(primitives.iter().copied());
421    VectorCanvas {
422        spec: VectorDrawSpec {
423            primitives: alloc,
424            width,
425            height,
426            opacity: 255,
427        },
428    }
429}
430
431impl<'a> VectorCanvas<'a> {
432    #[inline(always)]
433    pub const fn opacity(mut self, opacity: u8) -> Self {
434        self.spec.opacity = opacity;
435        self
436    }
437}
438
439impl<'a> IntoElement<'a> for VectorCanvas<'a> {
440    #[inline]
441    fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
442        let spec_ref = cx.arena().alloc_temp(self.spec);
443        Element::Vector(spec_ref)
444    }
445}
446
447// ============================================================================
448// 跨平台抗锯齿软件光栅器(纯数学 SDF + 奇偶填充,100% 无堆)
449// ============================================================================
450
451#[inline]
452fn blend_cov_pixel<P: PixelFormat>(target: &mut [P], idx: usize, color: Color, cov: f32) {
453    let a = ((color.a() as f32) * cov.clamp(0.0, 1.0) + 0.5) as u8;
454    if a == 0 {
455        return;
456    }
457    if let Some(dst) = target.get_mut(idx) {
458        let blended = crate::sdf::blend_color(dst.to_color(), color, a);
459        *dst = P::from_color(blended);
460    }
461}
462
463#[inline]
464fn dist_point_to_segment(px: f32, py: f32, ax: f32, ay: f32, bx: f32, by: f32) -> (f32, f32) {
465    let vx = bx - ax;
466    let vy = by - ay;
467    let len2 = vx * vx + vy * vy;
468    if len2 <= 1e-6 {
469        let dx = px - ax;
470        let dy = py - ay;
471        return (sqrt_f32(dx * dx + dy * dy), 0.0);
472    }
473    let t = (((px - ax) * vx + (py - ay) * vy) / len2).clamp(0.0, 1.0);
474    let dx = px - (ax + vx * t);
475    let dy = py - (ay + vy * t);
476    (sqrt_f32(dx * dx + dy * dy), t * sqrt_f32(len2))
477}
478
479#[allow(clippy::too_many_arguments)]
480fn rasterize_polyline<P: PixelFormat>(
481    points: &[VecPoint],
482    color: Color,
483    width: f32,
484    dash_len: f32,
485    gap_len: f32,
486    closed: bool,
487    bounds: Region,
488    vis: Region,
489    sf: f32,
490    origin_x: i16,
491    origin_y: i16,
492    stride: usize,
493    target: &mut [P],
494) {
495    if points.len() < 2 || color.a() == 0 || width <= 0.0 {
496        return;
497    }
498    let half_w = (width * sf * 0.5).max(0.5);
499    let bx0 = bounds.x as f32;
500    let by0 = bounds.y as f32;
501    let seg_count = if closed { points.len() } else { points.len() - 1 };
502    let dash_period = if dash_len > 0.0 && gap_len > 0.0 {
503        (dash_len + gap_len) * sf
504    } else {
505        0.0
506    };
507    let dash_on = dash_len * sf;
508    let mut accum_len = 0.0f32;
509
510    for s in 0..seg_count {
511        let p0 = points[s];
512        let p1 = points[(s + 1) % points.len()];
513        let ax = bx0 + p0.x * sf;
514        let ay = by0 + p0.y * sf;
515        let bx = bx0 + p1.x * sf;
516        let by = by0 + p1.y * sf;
517        let seg_len = sqrt_f32((bx - ax) * (bx - ax) + (by - ay) * (by - ay));
518
519        let pad = half_w + 1.5;
520        let min_x = (floor_f32(ax.min(bx) - pad) as i16).max(vis.x);
521        let max_x = ((ax.max(bx) + pad) as i16 + 1).min(vis.right());
522        let min_y = (floor_f32(ay.min(by) - pad) as i16).max(vis.y);
523        let max_y = ((ay.max(by) + pad) as i16 + 1).min(vis.bottom());
524
525        for py in min_y..max_y {
526            let row_off = ((py - origin_y) as usize) * stride;
527            let cy = py as f32 + 0.5;
528            for px in min_x..max_x {
529                let cx = px as f32 + 0.5;
530                let (d, along) = dist_point_to_segment(cx, cy, ax, ay, bx, by);
531                let cov = (half_w + 0.5 - d).clamp(0.0, 1.0);
532                if cov <= 0.0 {
533                    continue;
534                }
535                if dash_period > 0.0 {
536                    let phase = (accum_len + along) % dash_period;
537                    if phase > dash_on {
538                        continue;
539                    }
540                }
541                blend_cov_pixel(target, row_off + ((px - origin_x) as usize), color, cov);
542            }
543        }
544        accum_len += seg_len;
545    }
546}
547
548#[allow(clippy::too_many_arguments)]
549fn rasterize_polygon<P: PixelFormat>(
550    points: &[VecPoint],
551    fill: Color,
552    stroke: Color,
553    stroke_width: f32,
554    bounds: Region,
555    vis: Region,
556    sf: f32,
557    origin_x: i16,
558    origin_y: i16,
559    stride: usize,
560    target: &mut [P],
561) {
562    if points.len() < 3 {
563        return;
564    }
565    let bx0 = bounds.x as f32;
566    let by0 = bounds.y as f32;
567    if fill.a() > 0 {
568        let mut min_x = f32::INFINITY;
569        let mut max_x = f32::NEG_INFINITY;
570        let mut min_y = f32::INFINITY;
571        let mut max_y = f32::NEG_INFINITY;
572        for pt in points {
573            let x = bx0 + pt.x * sf;
574            let y = by0 + pt.y * sf;
575            min_x = min_x.min(x);
576            max_x = max_x.max(x);
577            min_y = min_y.min(y);
578            max_y = max_y.max(y);
579        }
580        let x0 = (floor_f32(min_x) as i16).max(vis.x);
581        let x1 = (max_x as i16 + 1).min(vis.right());
582        let y0 = (floor_f32(min_y) as i16).max(vis.y);
583        let y1 = (max_y as i16 + 1).min(vis.bottom());
584
585        for py in y0..y1 {
586            let row_off = ((py - origin_y) as usize) * stride;
587            let cy = py as f32 + 0.5;
588            for px in x0..x1 {
589                let cx = px as f32 + 0.5;
590                // 奇偶射线法
591                let mut inside = false;
592                let mut j = points.len() - 1;
593                for i in 0..points.len() {
594                    let xi = bx0 + points[i].x * sf;
595                    let yi = by0 + points[i].y * sf;
596                    let xj = bx0 + points[j].x * sf;
597                    let yj = by0 + points[j].y * sf;
598                    let cond = ((yi > cy) != (yj > cy))
599                        && (cx < (xj - xi) * (cy - yi) / ((yj - yi) + 1e-7) + xi);
600                    if cond {
601                        inside = !inside;
602                    }
603                    j = i;
604                }
605                if inside {
606                    blend_cov_pixel(target, row_off + ((px - origin_x) as usize), fill, 1.0);
607                }
608            }
609        }
610    }
611    if stroke.a() > 0 && stroke_width > 0.0 {
612        rasterize_polyline(
613            points,
614            stroke,
615            stroke_width,
616            0.0,
617            0.0,
618            true,
619            bounds,
620            vis,
621            sf,
622            origin_x,
623            origin_y,
624            stride,
625            target,
626        );
627    }
628}
629
630/// 将 [`VectorDrawSpec`] 光栅化至目标像素缓冲(支持任意 HiDPI 缩放与裁剪框)。
631#[allow(clippy::too_many_arguments)]
632pub fn rasterize_vector_spec<P: PixelFormat>(
633    spec: &VectorDrawSpec<'_>,
634    bounds: Region,
635    clip: Region,
636    sf: f32,
637    origin_x: i16,
638    origin_y: i16,
639    stride: usize,
640    target: &mut [P],
641) {
642    let vis = bounds.intersect(clip);
643    if vis.is_empty() || spec.opacity == 0 {
644        return;
645    }
646    let scale_c = |c: Color| c.scale_alpha(spec.opacity);
647    let bx0 = bounds.x as f32;
648    let by0 = bounds.y as f32;
649
650    for prim in spec.primitives {
651        match *prim {
652            VectorPrimitive::Polyline {
653                points,
654                color,
655                width,
656                dash_len,
657                gap_len,
658                closed,
659            } => rasterize_polyline(
660                points,
661                scale_c(color),
662                width,
663                dash_len,
664                gap_len,
665                closed,
666                bounds,
667                vis,
668                sf,
669                origin_x,
670                origin_y,
671                stride,
672                target,
673            ),
674            VectorPrimitive::Polygon {
675                points,
676                fill,
677                stroke,
678                stroke_width,
679            } => rasterize_polygon(
680                points,
681                scale_c(fill),
682                scale_c(stroke),
683                stroke_width,
684                bounds,
685                vis,
686                sf,
687                origin_x,
688                origin_y,
689                stride,
690                target,
691            ),
692            VectorPrimitive::Circle {
693                cx,
694                cy,
695                radius,
696                fill,
697                stroke,
698                stroke_width,
699            } => {
700                let ccx = bx0 + cx * sf;
701                let ccy = by0 + cy * sf;
702                let r = radius * sf;
703                let sw = stroke_width * sf;
704                let outer = r + sw * 0.5 + 1.5;
705                let x0 = (floor_f32(ccx - outer) as i16).max(vis.x);
706                let x1 = ((ccx + outer) as i16 + 1).min(vis.right());
707                let y0 = (floor_f32(ccy - outer) as i16).max(vis.y);
708                let y1 = ((ccy + outer) as i16 + 1).min(vis.bottom());
709                let fcol = scale_c(fill);
710                let scol = scale_c(stroke);
711
712                for py in y0..y1 {
713                    let row_off = ((py - origin_y) as usize) * stride;
714                    let dy = py as f32 + 0.5 - ccy;
715                    for px in x0..x1 {
716                        let dx = px as f32 + 0.5 - ccx;
717                        let dist = sqrt_f32(dx * dx + dy * dy);
718                        let idx = row_off + ((px - origin_x) as usize);
719                        if fcol.a() > 0 {
720                            let cov = (r + 0.5 - dist).clamp(0.0, 1.0);
721                            if cov > 0.0 {
722                                blend_cov_pixel(target, idx, fcol, cov);
723                            }
724                        }
725                        if scol.a() > 0 && sw > 0.0 {
726                            let sd = crate::svg::math::abs_f32(dist - r);
727                            let cov = (sw * 0.5 + 0.5 - sd).clamp(0.0, 1.0);
728                            if cov > 0.0 {
729                                blend_cov_pixel(target, idx, scol, cov);
730                            }
731                        }
732                    }
733                }
734            }
735            VectorPrimitive::Sector {
736                cx,
737                cy,
738                inner_r,
739                outer_r,
740                start_deg,
741                sweep_deg,
742                fill,
743            } => {
744                let fcol = scale_c(fill);
745                if fcol.a() == 0 || outer_r <= inner_r || sweep_deg <= 0.0 {
746                    continue;
747                }
748                let ccx = bx0 + cx * sf;
749                let ccy = by0 + cy * sf;
750                let ir = inner_r.max(0.0) * sf;
751                let or = outer_r * sf;
752                let pad = or + 1.5;
753                let x0 = (floor_f32(ccx - pad) as i16).max(vis.x);
754                let x1 = ((ccx + pad) as i16 + 1).min(vis.right());
755                let y0 = (floor_f32(ccy - pad) as i16).max(vis.y);
756                let y1 = ((ccy + pad) as i16 + 1).min(vis.bottom());
757                let start_n = ((start_deg % 360.0) + 360.0) % 360.0;
758                let sweep_c = sweep_deg.clamp(0.0, 360.0);
759
760                for py in y0..y1 {
761                    let row_off = ((py - origin_y) as usize) * stride;
762                    let dy = py as f32 + 0.5 - ccy;
763                    for px in x0..x1 {
764                        let dx = px as f32 + 0.5 - ccx;
765                        let dist = sqrt_f32(dx * dx + dy * dy);
766                        let cov_r = (or + 0.5 - dist).min(dist - ir + 0.5).clamp(0.0, 1.0);
767                        if cov_r <= 0.0 {
768                            continue;
769                        }
770                        if sweep_c < 359.9 {
771                            // 0° 为 12 点钟方向顺时针
772                            let mut ang = atan2_f32(dx, -dy) * (180.0 / core::f32::consts::PI);
773                            if ang < 0.0 {
774                                ang += 360.0;
775                            }
776                            let rel = ((ang - start_n) + 360.0) % 360.0;
777                            if rel > sweep_c {
778                                continue;
779                            }
780                        }
781                        blend_cov_pixel(
782                            target,
783                            row_off + ((px - origin_x) as usize),
784                            fcol,
785                            cov_r,
786                        );
787                    }
788                }
789            }
790            VectorPrimitive::CubicBezier {
791                p0,
792                p1,
793                p2,
794                p3,
795                color,
796                width,
797                dash_len,
798                gap_len,
799            } => {
800                let col = scale_c(color);
801                if col.a() == 0 || width <= 0.0 {
802                    continue;
803                }
804                let mut pts = [VecPoint::default(); 25];
805                for i in 0..=24 {
806                    let t = i as f32 / 24.0;
807                    let u = 1.0 - t;
808                    let uu = u * u;
809                    let tt = t * t;
810                    let uuu = uu * u;
811                    let ttt = tt * t;
812                    pts[i] = VecPoint::new(
813                        uuu * p0.x + 3.0 * uu * t * p1.x + 3.0 * u * tt * p2.x + ttt * p3.x,
814                        uuu * p0.y + 3.0 * uu * t * p1.y + 3.0 * u * tt * p2.y + ttt * p3.y,
815                    );
816                }
817                rasterize_polyline(
818                    &pts,
819                    col,
820                    width,
821                    dash_len,
822                    gap_len,
823                    false,
824                    bounds,
825                    vis,
826                    sf,
827                    origin_x,
828                    origin_y,
829                    stride,
830                    target,
831                );
832            }
833            VectorPrimitive::SvgPath {
834                d,
835                fill,
836                fill_rule,
837                stroke,
838                stroke_width,
839                line_cap,
840                line_join,
841            } => {
842                if d.is_empty() {
843                    continue;
844                }
845                let xf = crate::svg::Transform2D::new(sf, 0.0, 0.0, sf, bx0, by0);
846                let mut path = crate::svg::FlattenedPath::new();
847                path.parse_svg_path_d(d, xf);
848                if path.is_empty() {
849                    continue;
850                }
851                let fcol = scale_c(fill);
852                if fcol.a() > 0 {
853                    crate::svg::fill_path(
854                        &path, fcol, fill_rule, vis, origin_x, origin_y, stride, target,
855                    );
856                }
857                let scol = scale_c(stroke);
858                if scol.a() > 0 && stroke_width > 0.0 {
859                    crate::svg::stroke_path(
860                        &path,
861                        scol,
862                        stroke_width * sf,
863                        line_cap,
864                        line_join,
865                        4.0,
866                        vis,
867                        origin_x,
868                        origin_y,
869                        stride,
870                        target,
871                    );
872                }
873            }
874        }
875    }
876}