velox-engine 0.1.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
//! 高精度抗锯齿矢量 SVG 扫描线填充器(4× 垂直 + 解析水平覆盖率)与逐行最大覆盖率 SDF 描边器。

use crate::display::PixelFormat;
use crate::solver::Region;
use crate::style::Color;

use super::math::{abs_f32, ceil_f32, floor_f32, round_f32, sqrt_f32};
use super::path::FlattenedPath;

use std::vec::Vec;


/// SVG 填充绕数规则(`fill-rule`)。
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum SvgFillRule {
    /// 非零绕数规则(默认)。
    #[default]
    NonZero,
    /// 奇偶规则(支持同向子路径镂空孔洞)。
    EvenOdd,
}

/// SVG 描边端点样式(`stroke-linecap`)。
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum SvgLineCap {
    /// 平头截断(默认)。
    #[default]
    Butt,
    /// 半圆端帽(Lucide 图标默认风格)。
    Round,
    /// 方形延伸端帽。
    Square,
}

/// SVG 描边拐角连接样式(`stroke-linejoin`)。
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum SvgLineJoin {
    /// 尖角连接(默认,受 `stroke-miterlimit` 限制)。
    #[default]
    Miter,
    /// 圆角连接(Lucide 图标默认风格)。
    Round,
    /// 斜切连接。
    Bevel,
}

#[derive(Copy, Clone)]
struct ScanEdge {
    y0: f32,
    y1: f32,
    x0: f32,
    dx_dy: f32,
    dir: i8,
}

#[derive(Copy, Clone)]
struct Crossing {
    x: f32,
    dir: i8,
}

/// 对已变换至物理像素空间的 `path` 执行 4× 垂直 + 解析水平覆盖率抗锯齿扫描线填充。
#[allow(clippy::too_many_arguments)]
pub fn fill_path<P: PixelFormat>(
    path: &FlattenedPath,
    color: Color,
    fill_rule: SvgFillRule,
    clip: Region,
    origin_x: i16,
    origin_y: i16,
    stride: usize,
    target: &mut [P],
) {
    if color.a() == 0 || clip.is_empty() || stride == 0 || path.is_empty() {
        return;
    }

    let mut edges: Vec<ScanEdge> = Vec::with_capacity(path.points().len());

    let pts = path.points();
    let mut global_min_y = f32::MAX;
    let mut global_max_y = f32::MIN;

    for sp in path.subpaths() {
        if sp.len < 2 {
            continue;
        }
        let end = (sp.start + sp.len).min(pts.len());
        if end <= sp.start + 1 {
            continue;
        }
        let sub = &pts[sp.start..end];
        let seg_count = sub.len();
        for i in 0..seg_count {
            let p0 = sub[i];
            let p1 = sub[(i + 1) % seg_count];
            let dy = p1.1 - p0.1;
            if abs_f32(dy) < 1e-6 {
                continue;
            }
            let edge = if dy > 0.0 {
                ScanEdge {
                    y0: p0.1,
                    y1: p1.1,
                    x0: p0.0,
                    dx_dy: (p1.0 - p0.0) / dy,
                    dir: 1,
                }
            } else {
                ScanEdge {
                    y0: p1.1,
                    y1: p0.1,
                    x0: p1.0,
                    dx_dy: (p0.0 - p1.0) / (-dy),
                    dir: -1,
                }
            };
            if edge.y0 < global_min_y {
                global_min_y = edge.y0;
            }
            if edge.y1 > global_max_y {
                global_max_y = edge.y1;
            }
            edges.push(edge);
        }
    }

    if edges.is_empty() {
        return;
    }

    let y_start = (floor_f32(global_min_y) as i32)
        .max(clip.y as i32)
        .min(clip.bottom() as i32);
    let y_end = (ceil_f32(global_max_y) as i32)
        .max(clip.y as i32)
        .min(clip.bottom() as i32);
    if y_start >= y_end {
        return;
    }

    let clip_x0 = clip.x as i32;
    let clip_x1 = clip.right() as i32;
    let clip_w = (clip_x1 - clip_x0).max(0) as usize;
    if clip_w == 0 {
        return;
    }

    const SUB_OFFSETS: [f32; 4] = [0.125, 0.375, 0.625, 0.875];

    let mut row_cov = std::vec![0.0f32; clip_w];
    let mut crossings: Vec<Crossing> = Vec::with_capacity(32);


    for py_i32 in y_start..y_end {
        let py = py_i32 as i16;
        let row_offset = ((py - origin_y) as usize) * stride;

        {
            row_cov.fill(0.0);
            for &sub_y in &SUB_OFFSETS {
                let sy = (py_i32 as f32) + sub_y;
                crossings.clear();
                for e in &edges {
                    if e.y0 <= sy && sy < e.y1 {
                        let x = e.x0 + (sy - e.y0) * e.dx_dy;
                        crossings.push(Crossing { x, dir: e.dir });
                    }
                }
                if crossings.len() < 2 {
                    continue;
                }
                sort_crossings(&mut crossings);
                accumulate_scanline_spans(&crossings, fill_rule, clip_x0, clip_x1, &mut row_cov);
            }
            flush_row_coverage(
                &row_cov,
                clip_x0,
                origin_x,
                row_offset,
                color,
                1.0,
                target,
            );
        }

    }
}

#[inline]
fn sort_crossings(list: &mut [Crossing]) {
    for i in 1..list.len() {
        let key = list[i];
        let mut j = i;
        while j > 0 && list[j - 1].x > key.x {
            list[j] = list[j - 1];
            j -= 1;
        }
        list[j] = key;
    }
}

#[inline]
fn accumulate_scanline_spans(
    crossings: &[Crossing],
    fill_rule: SvgFillRule,
    tile_x0: i32,
    tile_x1: i32,
    row_cov: &mut [f32],
) {
    let mut winding: i32 = 0;
    let mut span_start = 0.0f32;
    let mut inside = false;
    let tile_x0_f = tile_x0 as f32;
    let tile_x1_f = tile_x1 as f32;

    for c in crossings {
        winding += c.dir as i32;
        let now_inside = match fill_rule {
            SvgFillRule::NonZero => winding != 0,
            SvgFillRule::EvenOdd => (winding & 1) != 0,
        };
        if !inside && now_inside {
            span_start = c.x;
            inside = true;
        } else if inside && !now_inside {
            let x0 = span_start.max(tile_x0_f);
            let x1 = c.x.min(tile_x1_f);
            if x1 > x0 {
                let ix0 = floor_f32(x0) as i32;
                let ix1 = (ceil_f32(x1) as i32).min(tile_x1);
                for px in ix0..ix1 {
                    let px_f = px as f32;
                    let cov = (x1.min(px_f + 1.0) - x0.max(px_f)).clamp(0.0, 1.0);
                    let idx = (px - tile_x0) as usize;
                    if let Some(slot) = row_cov.get_mut(idx) {
                        *slot += cov * 0.25;
                    }
                }
            }
            inside = false;
        }
    }
}

#[derive(Copy, Clone)]
struct StrokeSeg {
    ax: f32,
    ay: f32,
    ux: f32,
    uy: f32,
    len: f32,
    min_x: f32,
    max_x: f32,
    min_y: f32,
    max_y: f32,
    cap_start: SvgLineCap,
    cap_end: SvgLineCap,
}

#[derive(Copy, Clone)]
struct MiterTri {
    ax: f32,
    ay: f32,
    bx: f32,
    by: f32,
    cx: f32,
    cy: f32,
    min_x: f32,
    max_x: f32,
    min_y: f32,
    max_y: f32,
}

/// 对已变换至物理像素空间的 `path` 执行逐行最大覆盖率解析 SDF 抗锯齿描边(彻底杜绝半透明折点双重叠加)。
#[allow(clippy::too_many_arguments)]
pub fn stroke_path<P: PixelFormat>(
    path: &FlattenedPath,
    color: Color,
    stroke_width_px: f32,
    line_cap: SvgLineCap,
    line_join: SvgLineJoin,
    miter_limit: f32,
    clip: Region,
    origin_x: i16,
    origin_y: i16,
    stride: usize,
    target: &mut [P],
) {
    if color.a() == 0 || stroke_width_px <= 1e-4 || clip.is_empty() || stride == 0 || path.is_empty()
    {
        return;
    }

    // 次像素细线能量守恒:宽度 < 1px 时保持 0.5px 最小解析半径并等比衰减不透明度,防止断线发虚
    let (half_w, alpha_scale) = if stroke_width_px < 1.0 {
        (0.5, stroke_width_px.clamp(0.0, 1.0))
    } else {
        (stroke_width_px * 0.5, 1.0)
    };
    let pad = half_w + 1.0;

    let mut segs: Vec<StrokeSeg> = Vec::with_capacity(path.points().len());

    let mut miters: Vec<MiterTri> = Vec::new();

    let pts = path.points();
    let mut global_min_y = f32::MAX;
    let mut global_max_y = f32::MIN;

    let internal_cap = match line_join {
        SvgLineJoin::Round => SvgLineCap::Round,
        SvgLineJoin::Bevel | SvgLineJoin::Miter => SvgLineCap::Butt,
    };

    for sp in path.subpaths() {
        let end = (sp.start + sp.len).min(pts.len());
        let sub = &pts[sp.start..end];
        if sub.is_empty() {
            continue;
        }
        if sub.len() == 1 {
            // 单点路径在 Round/Square 端帽下绘制一个点/方点
            if line_cap != SvgLineCap::Butt {
                let (x, y) = sub[0];
                let seg = StrokeSeg {
                    ax: x,
                    ay: y,
                    ux: 1.0,
                    uy: 0.0,
                    len: 0.0,
                    min_x: x - pad,
                    max_x: x + pad,
                    min_y: y - pad,
                    max_y: y + pad,
                    cap_start: line_cap,
                    cap_end: line_cap,
                };
                global_min_y = global_min_y.min(seg.min_y);
                global_max_y = global_max_y.max(seg.max_y);
                segs.push(seg);
            }
            continue;
        }

        let seg_count = if sp.closed { sub.len() } else { sub.len() - 1 };
        let sub_seg_base = segs.len();
        for i in 0..seg_count {
            let p0 = sub[i];
            let p1 = sub[(i + 1) % sub.len()];
            let dx = p1.0 - p0.0;
            let dy = p1.1 - p0.1;
            let len = sqrt_f32(dx * dx + dy * dy);
            if len < 1e-5 {
                continue;
            }
            let cap_start = if !sp.closed && i == 0 {
                line_cap
            } else {
                internal_cap
            };
            let cap_end = if !sp.closed && i + 1 == seg_count {
                line_cap
            } else {
                internal_cap
            };
            let ext = if cap_start == SvgLineCap::Square || cap_end == SvgLineCap::Square {
                pad + half_w
            } else {
                pad
            };
            let seg = StrokeSeg {
                ax: p0.0,
                ay: p0.1,
                ux: dx / len,
                uy: dy / len,
                len,
                min_x: p0.0.min(p1.0) - ext,
                max_x: p0.0.max(p1.0) + ext,
                min_y: p0.1.min(p1.1) - ext,
                max_y: p0.1.max(p1.1) + ext,
                cap_start,
                cap_end,
            };
            global_min_y = global_min_y.min(seg.min_y);
            global_max_y = global_max_y.max(seg.max_y);
            segs.push(seg);
        }

        // 为 Bevel / Miter 内部拐角补充外侧连接三角形,消除粗线折角缺口
        let added = segs.len() - sub_seg_base;
        if added >= 2 && line_join != SvgLineJoin::Round {
            let join_count = if sp.closed { added } else { added - 1 };
            for j in 0..join_count {
                let s0 = segs.as_slice()[sub_seg_base + j];
                let s1 = segs.as_slice()[sub_seg_base + ((j + 1) % added)];
                let vx = s1.ax;
                let vy = s1.ay;
                let cross = s0.ux * s1.uy - s0.uy * s1.ux;
                if abs_f32(cross) < 1e-4 {
                    continue;
                }
                // 外侧法向量指向拐角凸出侧
                let sign = if cross > 0.0 { 1.0 } else { -1.0 };
                let n0x = sign * s0.uy;
                let n0y = -sign * s0.ux;
                let n1x = sign * s1.uy;
                let n1y = -sign * s1.ux;
                let p0 = (vx + n0x * half_w, vy + n0y * half_w);
                let p1 = (vx + n1x * half_w, vy + n1y * half_w);
                // 斜切基座三角形 (V, P0, P1)
                push_tri(&mut miters, (vx, vy), p0, p1, &mut global_min_y, &mut global_max_y);

                if line_join == SvgLineJoin::Miter {
                    // 求两条外侧偏移直线的交点作为 Miter 尖角顶点
                    let mx = n0x + n1x;
                    let my = n0y + n1y;
                    let m_len_sq = mx * mx + my * my;
                    if m_len_sq > 1e-5 {
                        let inv_cos = 2.0 / sqrt_f32(m_len_sq);
                        if inv_cos <= miter_limit.max(1.0) {
                            let scale = half_w * inv_cos / sqrt_f32(m_len_sq);
                            let apex = (vx + mx * scale, vy + my * scale);
                            push_tri(
                                &mut miters,
                                p0,
                                apex,
                                p1,
                                &mut global_min_y,
                                &mut global_max_y,
                            );
                        }
                    }
                }
            }
        }
    }

    if segs.is_empty() {
        return;
    }

    let y_start = (floor_f32(global_min_y) as i32)
        .max(clip.y as i32)
        .min(clip.bottom() as i32);
    let y_end = (ceil_f32(global_max_y) as i32)
        .max(clip.y as i32)
        .min(clip.bottom() as i32);
    if y_start >= y_end {
        return;
    }

    let clip_x0 = clip.x as i32;
    let clip_x1 = clip.right() as i32;
    let clip_w = (clip_x1 - clip_x0).max(0) as usize;
    if clip_w == 0 {
        return;
    }

    let mut row_cov = std::vec![0.0f32; clip_w];

    for py_i32 in y_start..y_end {
        let py = py_i32 as i16;
        let py_c = (py_i32 as f32) + 0.5;
        let row_offset = ((py - origin_y) as usize) * stride;

        {
            row_cov.fill(0.0);
            raster_stroke_row(
                py_c,
                clip_x0,
                clip_x1,
                half_w,
                &segs,
                &miters,
                &mut row_cov,
            );
            flush_row_coverage(
                &row_cov,
                clip_x0,
                origin_x,
                row_offset,
                color,
                alpha_scale,
                target,
            );
        }

    }
}

fn push_tri(
    out: &mut Vec<MiterTri>,
    a: (f32, f32),
    b: (f32, f32),
    c: (f32, f32),
    g_min_y: &mut f32,
    g_max_y: &mut f32,
) {
    let min_x = a.0.min(b.0).min(c.0) - 1.0;
    let max_x = a.0.max(b.0).max(c.0) + 1.0;
    let min_y = a.1.min(b.1).min(c.1) - 1.0;
    let max_y = a.1.max(b.1).max(c.1) + 1.0;
    *g_min_y = g_min_y.min(min_y);
    *g_max_y = g_max_y.max(max_y);
    out.push(MiterTri {
        ax: a.0,
        ay: a.1,
        bx: b.0,
        by: b.1,
        cx: c.0,
        cy: c.1,
        min_x,
        max_x,
        min_y,
        max_y,
    });
}


fn raster_stroke_row(
    py_c: f32,
    tile_x0: i32,
    tile_x1: i32,
    half_w: f32,
    segs: &[StrokeSeg],
    miters: &[MiterTri],
    row_cov: &mut [f32],
) {
    for seg in segs {
        if py_c < seg.min_y || py_c > seg.max_y {
            continue;
        }
        let sx0 = (floor_f32(seg.min_x) as i32).max(tile_x0);
        let sx1 = (ceil_f32(seg.max_x) as i32).min(tile_x1);
        if sx0 >= sx1 {
            continue;
        }
        let dy = py_c - seg.ay;
        for px in sx0..sx1 {
            let dx = (px as f32 + 0.5) - seg.ax;
            let along = dx * seg.ux + dy * seg.uy;
            let perp = abs_f32(dx * (-seg.uy) + dy * seg.ux);

            let sd = if along < 0.0 {
                match seg.cap_start {
                    SvgLineCap::Round => sqrt_f32(dx * dx + dy * dy) - half_w,
                    SvgLineCap::Butt => box_outside_sd(-along, perp - half_w),
                    SvgLineCap::Square => box_outside_sd(-along - half_w, perp - half_w),
                }
            } else if along > seg.len {
                let over = along - seg.len;
                match seg.cap_end {
                    SvgLineCap::Round => {
                        let ex = dx - seg.ux * seg.len;
                        let ey = dy - seg.uy * seg.len;
                        sqrt_f32(ex * ex + ey * ey) - half_w
                    }
                    SvgLineCap::Butt => box_outside_sd(over, perp - half_w),
                    SvgLineCap::Square => box_outside_sd(over - half_w, perp - half_w),
                }
            } else {
                perp - half_w
            };

            let cov = (0.5 - sd).clamp(0.0, 1.0);
            if cov > 0.0 {
                let idx = (px - tile_x0) as usize;
                if let Some(slot) = row_cov.get_mut(idx) {
                    if cov > *slot {
                        *slot = cov;
                    }
                }
            }
        }
    }

    for tri in miters {
        if py_c < tri.min_y || py_c > tri.max_y {
            continue;
        }
        let sx0 = (floor_f32(tri.min_x) as i32).max(tile_x0);
        let sx1 = (ceil_f32(tri.max_x) as i32).min(tile_x1);
        if sx0 >= sx1 {
            continue;
        }
        for px in sx0..sx1 {
            let sd = sd_triangle(
                px as f32 + 0.5,
                py_c,
                tri.ax,
                tri.ay,
                tri.bx,
                tri.by,
                tri.cx,
                tri.cy,
            );
            let cov = (0.5 - sd).clamp(0.0, 1.0);
            if cov > 0.0 {
                let idx = (px - tile_x0) as usize;
                if let Some(slot) = row_cov.get_mut(idx) {
                    if cov > *slot {
                        *slot = cov;
                    }
                }
            }
        }
    }
}

/// 有向矩形外侧有符号距离(`dx_out` 为轴向外凸距离,`dy_out` 为法向外凸距离)。
#[inline(always)]
fn box_outside_sd(dx_out: f32, dy_out: f32) -> f32 {
    if dx_out > 0.0 && dy_out > 0.0 {
        sqrt_f32(dx_out * dx_out + dy_out * dy_out)
    } else {
        dx_out.max(dy_out)
    }
}

/// 点 `(px, py)` 到任意三角形 `(ax, ay)-(bx, by)-(cx, cy)` 的精确有符号距离(内部为负)。
#[allow(clippy::too_many_arguments)]
fn sd_triangle(
    px: f32,
    py: f32,
    ax: f32,
    ay: f32,
    bx: f32,
    by: f32,
    cx: f32,
    cy: f32,
) -> f32 {
    let e0x = bx - ax;
    let e0y = by - ay;
    let e1x = cx - bx;
    let e1y = cy - by;
    let e2x = ax - cx;
    let e2y = ay - cy;

    let v0x = px - ax;
    let v0y = py - ay;
    let v1x = px - bx;
    let v1y = py - by;
    let v2x = px - cx;
    let v2y = py - cy;

    let t0 = ((v0x * e0x + v0y * e0y) / (e0x * e0x + e0y * e0y).max(1e-10)).clamp(0.0, 1.0);
    let t1 = ((v1x * e1x + v1y * e1y) / (e1x * e1x + e1y * e1y).max(1e-10)).clamp(0.0, 1.0);
    let t2 = ((v2x * e2x + v2y * e2y) / (e2x * e2x + e2y * e2y).max(1e-10)).clamp(0.0, 1.0);

    let pq0x = v0x - e0x * t0;
    let pq0y = v0y - e0y * t0;
    let pq1x = v1x - e1x * t1;
    let pq1y = v1y - e1y * t1;
    let pq2x = v2x - e2x * t2;
    let pq2y = v2y - e2y * t2;

    let s = if e0x * e2y - e0y * e2x >= 0.0 {
        1.0
    } else {
        -1.0
    };
    let d0 = pq0x * pq0x + pq0y * pq0y;
    let d1 = pq1x * pq1x + pq1y * pq1y;
    let d2 = pq2x * pq2x + pq2y * pq2y;
    let min_d2 = d0.min(d1).min(d2);

    let c0 = s * (v0x * e0y - v0y * e0x);
    let c1 = s * (v1x * e1y - v1y * e1x);
    let c2 = s * (v2x * e2y - v2y * e2x);
    let min_c = c0.min(c1).min(c2);

    let dist = sqrt_f32(min_d2);
    if min_c >= 0.0 {
        -dist
    } else {
        dist
    }
}

#[inline]
fn flush_row_coverage<P: PixelFormat>(
    row_cov: &[f32],
    tile_x0: i32,
    origin_x: i16,
    row_offset: usize,
    color: Color,
    alpha_scale: f32,
    target: &mut [P],
) {
    let base_dst = tile_x0 - (origin_x as i32);
    for (i, &cov_raw) in row_cov.iter().enumerate() {
        if cov_raw <= 1e-4 {
            continue;
        }
        let cov_f = (cov_raw * alpha_scale).clamp(0.0, 1.0);
        let cov_u8 = round_f32(cov_f * 255.0) as u8;
        if cov_u8 == 0 {
            continue;
        }
        let dst_x = base_dst + (i as i32);
        if dst_x >= 0 {
            let idx = row_offset + (dst_x as usize);
            if let Some(dst) = target.get_mut(idx) {
                *dst = dst.blend(color, cov_u8);
            }
        }
    }
}