velox-engine 0.4.0

Velox core UI engine: arena, pod state, 1D fixed-point layout solver, damage tracker, SDF scanline rasterizer, and cross-platform GPU/software backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
//! 原生 2D 矢量画布原语(`VectorCanvas` / `Polyline` / `Polygon` / `Circle` / `Sector`)。
//!
//! 面向实时波形图、折线图、仪表盘、雷达/海图矢量轨迹等高频动态矢量绘制场景:
//! - 100% 无堆兼容,数据全部驻留当前帧 `ScratchArena`,通过 8B 引用挂入 `DrawPayload::Vector`;
//! - 彻底规避每帧拼接 SVG 字符串的开销,软件端与 GPU 端均提供原生抗锯齿绘制。

use crate::display::PixelFormat;
use crate::element::{Element, IntoElement};
use crate::pod::FrameScope;
use crate::solver::Region;
use crate::style::Color;
use crate::svg::math::{atan2_f32, floor_f32, sqrt_f32};

/// 二维局部浮点坐标点(相对于 `VectorCanvas` 盒子左上角,单位逻辑像素)。
#[derive(Copy, Clone, Debug, Default)]
pub struct VecPoint {
    pub x: f32,
    pub y: f32,
}

impl VecPoint {
    #[inline(always)]
    pub const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }
}

#[inline(always)]
fn canon_f32(v: f32) -> u32 {
    if v == 0.0 { 0 } else { v.to_bits() }
}

impl PartialEq for VecPoint {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        canon_f32(self.x) == canon_f32(other.x) && canon_f32(self.y) == canon_f32(other.y)
    }
}

impl Eq for VecPoint {}

/// 单个矢量画布绘制图元。
#[derive(Copy, Clone, Debug)]
pub enum VectorPrimitive<'a> {
    /// 抗锯齿折线 / 虚线 / 闭合折线环(支持自定义线宽、圆头与虚线样式)。
    Polyline {
        points: &'a [VecPoint],
        color: Color,
        width: f32,
        dash_len: f32,
        gap_len: f32,
        closed: bool,
    },
    /// 任意多边形填充与可选描边(扫描线奇偶规则抗锯齿填充)。
    Polygon {
        points: &'a [VecPoint],
        fill: Color,
        stroke: Color,
        stroke_width: f32,
    },
    /// 解析 SDF 抗锯齿实心圆 / 圆环。
    Circle {
        cx: f32,
        cy: f32,
        radius: f32,
        fill: Color,
        stroke: Color,
        stroke_width: f32,
    },
    /// 解析 SDF 扇形 / 环扇(角度单位度,0° 为 12 点钟方向顺时针递增)。
    Sector {
        cx: f32,
        cy: f32,
        inner_r: f32,
        outer_r: f32,
        start_deg: f32,
        sweep_deg: f32,
        fill: Color,
    },
    /// 光滑三次贝塞尔曲线(24 段解析细分抗锯齿描边 + 虚线支持)。
    CubicBezier {
        p0: VecPoint,
        p1: VecPoint,
        p2: VecPoint,
        p3: VecPoint,
        color: Color,
        width: f32,
        dash_len: f32,
        gap_len: f32,
    },
    /// 任意坐标系完整 W3C SVG Path `d` 矢量路径(突破 24×24 限制,支持 20 条贝塞尔/椭圆弧指令、`NonZero`/`EvenOdd` 4× AA 填充与任意宽度圆/斜/平头描边)。
    SvgPath {
        d: &'a str,
        fill: Color,
        fill_rule: crate::svg::SvgFillRule,
        stroke: Color,
        stroke_width: f32,
        line_cap: crate::svg::SvgLineCap,
        line_join: crate::svg::SvgLineJoin,
    },
}

impl<'a> PartialEq for VectorPrimitive<'a> {
    fn eq(&self, other: &Self) -> bool {
        match (*self, *other) {
            (
                Self::Polyline {
                    points: p1,
                    color: c1,
                    width: w1,
                    dash_len: d1,
                    gap_len: g1,
                    closed: cl1,
                },
                Self::Polyline {
                    points: p2,
                    color: c2,
                    width: w2,
                    dash_len: d2,
                    gap_len: g2,
                    closed: cl2,
                },
            ) => {
                p1 == p2
                    && c1 == c2
                    && canon_f32(w1) == canon_f32(w2)
                    && canon_f32(d1) == canon_f32(d2)
                    && canon_f32(g1) == canon_f32(g2)
                    && cl1 == cl2
            }
            (
                Self::Polygon {
                    points: p1,
                    fill: f1,
                    stroke: s1,
                    stroke_width: sw1,
                },
                Self::Polygon {
                    points: p2,
                    fill: f2,
                    stroke: s2,
                    stroke_width: sw2,
                },
            ) => p1 == p2 && f1 == f2 && s1 == s2 && canon_f32(sw1) == canon_f32(sw2),
            (
                Self::Circle {
                    cx: x1,
                    cy: y1,
                    radius: r1,
                    fill: f1,
                    stroke: s1,
                    stroke_width: sw1,
                },
                Self::Circle {
                    cx: x2,
                    cy: y2,
                    radius: r2,
                    fill: f2,
                    stroke: s2,
                    stroke_width: sw2,
                },
            ) => {
                canon_f32(x1) == canon_f32(x2)
                    && canon_f32(y1) == canon_f32(y2)
                    && canon_f32(r1) == canon_f32(r2)
                    && f1 == f2
                    && s1 == s2
                    && canon_f32(sw1) == canon_f32(sw2)
            }
            (
                Self::Sector {
                    cx: x1,
                    cy: y1,
                    inner_r: ir1,
                    outer_r: or1,
                    start_deg: sd1,
                    sweep_deg: sw1,
                    fill: f1,
                },
                Self::Sector {
                    cx: x2,
                    cy: y2,
                    inner_r: ir2,
                    outer_r: or2,
                    start_deg: sd2,
                    sweep_deg: sw2,
                    fill: f2,
                },
            ) => {
                canon_f32(x1) == canon_f32(x2)
                    && canon_f32(y1) == canon_f32(y2)
                    && canon_f32(ir1) == canon_f32(ir2)
                    && canon_f32(or1) == canon_f32(or2)
                    && canon_f32(sd1) == canon_f32(sd2)
                    && canon_f32(sw1) == canon_f32(sw2)
                    && f1 == f2
            }
            (
                Self::CubicBezier {
                    p0: a0,
                    p1: a1,
                    p2: a2,
                    p3: a3,
                    color: c1,
                    width: w1,
                    dash_len: d1,
                    gap_len: g1,
                },
                Self::CubicBezier {
                    p0: b0,
                    p1: b1,
                    p2: b2,
                    p3: b3,
                    color: c2,
                    width: w2,
                    dash_len: d2,
                    gap_len: g2,
                },
            ) => {
                a0 == b0
                    && a1 == b1
                    && a2 == b2
                    && a3 == b3
                    && c1 == c2
                    && canon_f32(w1) == canon_f32(w2)
                    && canon_f32(d1) == canon_f32(d2)
                    && canon_f32(g1) == canon_f32(g2)
            }
            (
                Self::SvgPath {
                    d: d1,
                    fill: f1,
                    fill_rule: r1,
                    stroke: s1,
                    stroke_width: sw1,
                    line_cap: lc1,
                    line_join: lj1,
                },
                Self::SvgPath {
                    d: d2,
                    fill: f2,
                    fill_rule: r2,
                    stroke: s2,
                    stroke_width: sw2,
                    line_cap: lc2,
                    line_join: lj2,
                },
            ) => {
                d1 == d2
                    && f1 == f2
                    && r1 == r2
                    && s1 == s2
                    && canon_f32(sw1) == canon_f32(sw2)
                    && lc1 == lc2
                    && lj1 == lj2
            }
            _ => false,
        }
    }
}

impl<'a> Eq for VectorPrimitive<'a> {}

/// 矢量画布绘制载荷(分配于 `ScratchArena`,通过 8B 引用挂入 `DrawPayload::Vector`)。
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct VectorDrawSpec<'a> {
    pub primitives: &'a [VectorPrimitive<'a>],
    pub width: i16,
    pub height: i16,
    pub opacity: u8,
}

impl<'a> VectorDrawSpec<'a> {
    pub fn mix_hash(&self, h: &mut u32) {
        #[inline(always)]
        fn mix_u8(h: &mut u32, b: u8) {
            *h ^= b as u32;
            *h = h.wrapping_mul(0x0100_0193);
        }
        #[inline(always)]
        fn mix_u32(h: &mut u32, v: u32) {
            for b in v.to_le_bytes() {
                mix_u8(h, b);
            }
        }
        mix_u32(
            h,
            ((self.width as u16 as u32) << 16) | (self.height as u16 as u32),
        );
        mix_u8(h, self.opacity);
        for p in self.primitives {
            match *p {
                VectorPrimitive::Polyline {
                    points,
                    color,
                    width,
                    dash_len,
                    gap_len,
                    closed,
                } => {
                    mix_u8(h, 1);
                    mix_u32(h, color.0);
                    mix_u32(h, canon_f32(width));
                    mix_u32(h, canon_f32(dash_len));
                    mix_u32(h, canon_f32(gap_len));
                    mix_u8(h, if closed { 1 } else { 0 });
                    for pt in points {
                        mix_u32(h, canon_f32(pt.x));
                        mix_u32(h, canon_f32(pt.y));
                    }
                }
                VectorPrimitive::Polygon {
                    points,
                    fill,
                    stroke,
                    stroke_width,
                } => {
                    mix_u8(h, 2);
                    mix_u32(h, fill.0);
                    mix_u32(h, stroke.0);
                    mix_u32(h, canon_f32(stroke_width));
                    for pt in points {
                        mix_u32(h, canon_f32(pt.x));
                        mix_u32(h, canon_f32(pt.y));
                    }
                }
                VectorPrimitive::Circle {
                    cx,
                    cy,
                    radius,
                    fill,
                    stroke,
                    stroke_width,
                } => {
                    mix_u8(h, 3);
                    mix_u32(h, canon_f32(cx));
                    mix_u32(h, canon_f32(cy));
                    mix_u32(h, canon_f32(radius));
                    mix_u32(h, fill.0);
                    mix_u32(h, stroke.0);
                    mix_u32(h, canon_f32(stroke_width));
                }
                VectorPrimitive::Sector {
                    cx,
                    cy,
                    inner_r,
                    outer_r,
                    start_deg,
                    sweep_deg,
                    fill,
                } => {
                    mix_u8(h, 4);
                    mix_u32(h, canon_f32(cx));
                    mix_u32(h, canon_f32(cy));
                    mix_u32(h, canon_f32(inner_r));
                    mix_u32(h, canon_f32(outer_r));
                    mix_u32(h, canon_f32(start_deg));
                    mix_u32(h, canon_f32(sweep_deg));
                    mix_u32(h, fill.0);
                }
                VectorPrimitive::CubicBezier {
                    p0,
                    p1,
                    p2,
                    p3,
                    color,
                    width,
                    dash_len,
                    gap_len,
                } => {
                    mix_u8(h, 5);
                    for pt in [p0, p1, p2, p3] {
                        mix_u32(h, canon_f32(pt.x));
                        mix_u32(h, canon_f32(pt.y));
                    }
                    mix_u32(h, color.0);
                    mix_u32(h, canon_f32(width));
                    mix_u32(h, canon_f32(dash_len));
                    mix_u32(h, canon_f32(gap_len));
                }
                VectorPrimitive::SvgPath {
                    d,
                    fill,
                    fill_rule,
                    stroke,
                    stroke_width,
                    line_cap,
                    line_join,
                } => {
                    mix_u8(h, 6);
                    mix_u32(h, fill.0);
                    mix_u8(h, fill_rule as u8);
                    mix_u32(h, stroke.0);
                    mix_u32(h, canon_f32(stroke_width));
                    mix_u8(h, line_cap as u8);
                    mix_u8(h, line_join as u8);
                    for &b in d.as_bytes() {
                        mix_u8(h, b);
                    }
                }
            }
        }
    }
}

/// 声明式原生 2D 矢量画布元素。
#[derive(Copy, Clone, Debug)]
pub struct VectorCanvas<'a> {
    pub spec: VectorDrawSpec<'a>,
}

/// 构造一个 2D 矢量画布元素(图元切片自动分配于当前帧 `ScratchArena`)。
#[inline]
pub fn vector_canvas<'a>(
    cx: &'a FrameScope,
    width: i16,
    height: i16,
    primitives: &[VectorPrimitive<'a>],
) -> VectorCanvas<'a> {
    let alloc = cx.arena().alloc_iter(primitives.iter().copied());
    VectorCanvas {
        spec: VectorDrawSpec {
            primitives: alloc,
            width,
            height,
            opacity: 255,
        },
    }
}

impl<'a> VectorCanvas<'a> {
    #[inline(always)]
    pub const fn opacity(mut self, opacity: u8) -> Self {
        self.spec.opacity = opacity;
        self
    }
}

impl<'a> IntoElement<'a> for VectorCanvas<'a> {
    #[inline]
    fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
        let spec_ref = cx.arena().alloc_temp(self.spec);
        Element::Vector(spec_ref)
    }
}

// ============================================================================
// 跨平台抗锯齿软件光栅器(纯数学 SDF + 奇偶填充,100% 无堆)
// ============================================================================

#[inline]
fn blend_cov_pixel<P: PixelFormat>(target: &mut [P], idx: usize, color: Color, cov: f32) {
    let a = ((color.a() as f32) * cov.clamp(0.0, 1.0) + 0.5) as u8;
    if a == 0 {
        return;
    }
    if let Some(dst) = target.get_mut(idx) {
        let blended = crate::sdf::blend_color(dst.to_color(), color, a);
        *dst = P::from_color(blended);
    }
}

#[inline]
fn dist_point_to_segment(px: f32, py: f32, ax: f32, ay: f32, bx: f32, by: f32) -> (f32, f32) {
    let vx = bx - ax;
    let vy = by - ay;
    let len2 = vx * vx + vy * vy;
    if len2 <= 1e-6 {
        let dx = px - ax;
        let dy = py - ay;
        return (sqrt_f32(dx * dx + dy * dy), 0.0);
    }
    let t = (((px - ax) * vx + (py - ay) * vy) / len2).clamp(0.0, 1.0);
    let dx = px - (ax + vx * t);
    let dy = py - (ay + vy * t);
    (sqrt_f32(dx * dx + dy * dy), t * sqrt_f32(len2))
}

#[allow(clippy::too_many_arguments)]
fn rasterize_polyline<P: PixelFormat>(
    points: &[VecPoint],
    color: Color,
    width: f32,
    dash_len: f32,
    gap_len: f32,
    closed: bool,
    bounds: Region,
    vis: Region,
    sf: f32,
    origin_x: i16,
    origin_y: i16,
    stride: usize,
    target: &mut [P],
) {
    if points.len() < 2 || color.a() == 0 || width <= 0.0 {
        return;
    }
    let half_w = (width * sf * 0.5).max(0.5);
    let bx0 = bounds.x as f32;
    let by0 = bounds.y as f32;
    let seg_count = if closed { points.len() } else { points.len() - 1 };
    let dash_period = if dash_len > 0.0 && gap_len > 0.0 {
        (dash_len + gap_len) * sf
    } else {
        0.0
    };
    let dash_on = dash_len * sf;
    let mut accum_len = 0.0f32;

    for s in 0..seg_count {
        let p0 = points[s];
        let p1 = points[(s + 1) % points.len()];
        let ax = bx0 + p0.x * sf;
        let ay = by0 + p0.y * sf;
        let bx = bx0 + p1.x * sf;
        let by = by0 + p1.y * sf;
        let seg_len = sqrt_f32((bx - ax) * (bx - ax) + (by - ay) * (by - ay));

        let pad = half_w + 1.5;
        let min_x = (floor_f32(ax.min(bx) - pad) as i16).max(vis.x);
        let max_x = ((ax.max(bx) + pad) as i16 + 1).min(vis.right());
        let min_y = (floor_f32(ay.min(by) - pad) as i16).max(vis.y);
        let max_y = ((ay.max(by) + pad) as i16 + 1).min(vis.bottom());

        for py in min_y..max_y {
            let row_off = ((py - origin_y) as usize) * stride;
            let cy = py as f32 + 0.5;
            for px in min_x..max_x {
                let cx = px as f32 + 0.5;
                let (d, along) = dist_point_to_segment(cx, cy, ax, ay, bx, by);
                let cov = (half_w + 0.5 - d).clamp(0.0, 1.0);
                if cov <= 0.0 {
                    continue;
                }
                if dash_period > 0.0 {
                    let phase = (accum_len + along) % dash_period;
                    if phase > dash_on {
                        continue;
                    }
                }
                blend_cov_pixel(target, row_off + ((px - origin_x) as usize), color, cov);
            }
        }
        accum_len += seg_len;
    }
}

#[allow(clippy::too_many_arguments)]
fn rasterize_polygon<P: PixelFormat>(
    points: &[VecPoint],
    fill: Color,
    stroke: Color,
    stroke_width: f32,
    bounds: Region,
    vis: Region,
    sf: f32,
    origin_x: i16,
    origin_y: i16,
    stride: usize,
    target: &mut [P],
) {
    if points.len() < 3 {
        return;
    }
    let bx0 = bounds.x as f32;
    let by0 = bounds.y as f32;
    if fill.a() > 0 {
        let mut min_x = f32::INFINITY;
        let mut max_x = f32::NEG_INFINITY;
        let mut min_y = f32::INFINITY;
        let mut max_y = f32::NEG_INFINITY;
        for pt in points {
            let x = bx0 + pt.x * sf;
            let y = by0 + pt.y * sf;
            min_x = min_x.min(x);
            max_x = max_x.max(x);
            min_y = min_y.min(y);
            max_y = max_y.max(y);
        }
        let x0 = (floor_f32(min_x) as i16).max(vis.x);
        let x1 = (max_x as i16 + 1).min(vis.right());
        let y0 = (floor_f32(min_y) as i16).max(vis.y);
        let y1 = (max_y as i16 + 1).min(vis.bottom());

        for py in y0..y1 {
            let row_off = ((py - origin_y) as usize) * stride;
            let cy = py as f32 + 0.5;
            for px in x0..x1 {
                let cx = px as f32 + 0.5;
                // 奇偶射线法
                let mut inside = false;
                let mut j = points.len() - 1;
                for i in 0..points.len() {
                    let xi = bx0 + points[i].x * sf;
                    let yi = by0 + points[i].y * sf;
                    let xj = bx0 + points[j].x * sf;
                    let yj = by0 + points[j].y * sf;
                    let cond = ((yi > cy) != (yj > cy))
                        && (cx < (xj - xi) * (cy - yi) / ((yj - yi) + 1e-7) + xi);
                    if cond {
                        inside = !inside;
                    }
                    j = i;
                }
                if inside {
                    blend_cov_pixel(target, row_off + ((px - origin_x) as usize), fill, 1.0);
                }
            }
        }
    }
    if stroke.a() > 0 && stroke_width > 0.0 {
        rasterize_polyline(
            points,
            stroke,
            stroke_width,
            0.0,
            0.0,
            true,
            bounds,
            vis,
            sf,
            origin_x,
            origin_y,
            stride,
            target,
        );
    }
}

/// 将 [`VectorDrawSpec`] 光栅化至目标像素缓冲(支持任意 HiDPI 缩放与裁剪框)。
#[allow(clippy::too_many_arguments)]
pub fn rasterize_vector_spec<P: PixelFormat>(
    spec: &VectorDrawSpec<'_>,
    bounds: Region,
    clip: Region,
    sf: f32,
    origin_x: i16,
    origin_y: i16,
    stride: usize,
    target: &mut [P],
) {
    let vis = bounds.intersect(clip);
    if vis.is_empty() || spec.opacity == 0 {
        return;
    }
    let scale_c = |c: Color| c.scale_alpha(spec.opacity);
    let bx0 = bounds.x as f32;
    let by0 = bounds.y as f32;

    for prim in spec.primitives {
        match *prim {
            VectorPrimitive::Polyline {
                points,
                color,
                width,
                dash_len,
                gap_len,
                closed,
            } => rasterize_polyline(
                points,
                scale_c(color),
                width,
                dash_len,
                gap_len,
                closed,
                bounds,
                vis,
                sf,
                origin_x,
                origin_y,
                stride,
                target,
            ),
            VectorPrimitive::Polygon {
                points,
                fill,
                stroke,
                stroke_width,
            } => rasterize_polygon(
                points,
                scale_c(fill),
                scale_c(stroke),
                stroke_width,
                bounds,
                vis,
                sf,
                origin_x,
                origin_y,
                stride,
                target,
            ),
            VectorPrimitive::Circle {
                cx,
                cy,
                radius,
                fill,
                stroke,
                stroke_width,
            } => {
                let ccx = bx0 + cx * sf;
                let ccy = by0 + cy * sf;
                let r = radius * sf;
                let sw = stroke_width * sf;
                let outer = r + sw * 0.5 + 1.5;
                let x0 = (floor_f32(ccx - outer) as i16).max(vis.x);
                let x1 = ((ccx + outer) as i16 + 1).min(vis.right());
                let y0 = (floor_f32(ccy - outer) as i16).max(vis.y);
                let y1 = ((ccy + outer) as i16 + 1).min(vis.bottom());
                let fcol = scale_c(fill);
                let scol = scale_c(stroke);

                for py in y0..y1 {
                    let row_off = ((py - origin_y) as usize) * stride;
                    let dy = py as f32 + 0.5 - ccy;
                    for px in x0..x1 {
                        let dx = px as f32 + 0.5 - ccx;
                        let dist = sqrt_f32(dx * dx + dy * dy);
                        let idx = row_off + ((px - origin_x) as usize);
                        if fcol.a() > 0 {
                            let cov = (r + 0.5 - dist).clamp(0.0, 1.0);
                            if cov > 0.0 {
                                blend_cov_pixel(target, idx, fcol, cov);
                            }
                        }
                        if scol.a() > 0 && sw > 0.0 {
                            let sd = crate::svg::math::abs_f32(dist - r);
                            let cov = (sw * 0.5 + 0.5 - sd).clamp(0.0, 1.0);
                            if cov > 0.0 {
                                blend_cov_pixel(target, idx, scol, cov);
                            }
                        }
                    }
                }
            }
            VectorPrimitive::Sector {
                cx,
                cy,
                inner_r,
                outer_r,
                start_deg,
                sweep_deg,
                fill,
            } => {
                let fcol = scale_c(fill);
                if fcol.a() == 0 || outer_r <= inner_r || sweep_deg <= 0.0 {
                    continue;
                }
                let ccx = bx0 + cx * sf;
                let ccy = by0 + cy * sf;
                let ir = inner_r.max(0.0) * sf;
                let or = outer_r * sf;
                let pad = or + 1.5;
                let x0 = (floor_f32(ccx - pad) as i16).max(vis.x);
                let x1 = ((ccx + pad) as i16 + 1).min(vis.right());
                let y0 = (floor_f32(ccy - pad) as i16).max(vis.y);
                let y1 = ((ccy + pad) as i16 + 1).min(vis.bottom());
                let start_n = ((start_deg % 360.0) + 360.0) % 360.0;
                let sweep_c = sweep_deg.clamp(0.0, 360.0);

                for py in y0..y1 {
                    let row_off = ((py - origin_y) as usize) * stride;
                    let dy = py as f32 + 0.5 - ccy;
                    for px in x0..x1 {
                        let dx = px as f32 + 0.5 - ccx;
                        let dist = sqrt_f32(dx * dx + dy * dy);
                        let cov_r = (or + 0.5 - dist).min(dist - ir + 0.5).clamp(0.0, 1.0);
                        if cov_r <= 0.0 {
                            continue;
                        }
                        if sweep_c < 359.9 {
                            // 0° 为 12 点钟方向顺时针
                            let mut ang = atan2_f32(dx, -dy) * (180.0 / core::f32::consts::PI);
                            if ang < 0.0 {
                                ang += 360.0;
                            }
                            let rel = ((ang - start_n) + 360.0) % 360.0;
                            if rel > sweep_c {
                                continue;
                            }
                        }
                        blend_cov_pixel(
                            target,
                            row_off + ((px - origin_x) as usize),
                            fcol,
                            cov_r,
                        );
                    }
                }
            }
            VectorPrimitive::CubicBezier {
                p0,
                p1,
                p2,
                p3,
                color,
                width,
                dash_len,
                gap_len,
            } => {
                let col = scale_c(color);
                if col.a() == 0 || width <= 0.0 {
                    continue;
                }
                let mut pts = [VecPoint::default(); 25];
                for i in 0..=24 {
                    let t = i as f32 / 24.0;
                    let u = 1.0 - t;
                    let uu = u * u;
                    let tt = t * t;
                    let uuu = uu * u;
                    let ttt = tt * t;
                    pts[i] = VecPoint::new(
                        uuu * p0.x + 3.0 * uu * t * p1.x + 3.0 * u * tt * p2.x + ttt * p3.x,
                        uuu * p0.y + 3.0 * uu * t * p1.y + 3.0 * u * tt * p2.y + ttt * p3.y,
                    );
                }
                rasterize_polyline(
                    &pts,
                    col,
                    width,
                    dash_len,
                    gap_len,
                    false,
                    bounds,
                    vis,
                    sf,
                    origin_x,
                    origin_y,
                    stride,
                    target,
                );
            }
            VectorPrimitive::SvgPath {
                d,
                fill,
                fill_rule,
                stroke,
                stroke_width,
                line_cap,
                line_join,
            } => {
                if d.is_empty() {
                    continue;
                }
                let xf = crate::svg::Transform2D::new(sf, 0.0, 0.0, sf, bx0, by0);
                let mut path = crate::svg::FlattenedPath::new();
                path.parse_svg_path_d(d, xf);
                if path.is_empty() {
                    continue;
                }
                let fcol = scale_c(fill);
                if fcol.a() > 0 {
                    crate::svg::fill_path(
                        &path, fcol, fill_rule, vis, origin_x, origin_y, stride, target,
                    );
                }
                let scol = scale_c(stroke);
                if scol.a() > 0 && stroke_width > 0.0 {
                    crate::svg::stroke_path(
                        &path,
                        scol,
                        stroke_width * sf,
                        line_cap,
                        line_join,
                        4.0,
                        vis,
                        origin_x,
                        origin_y,
                        stride,
                        target,
                    );
                }
            }
        }
    }
}