mirui 0.13.0

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
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
use crate::types::{Color, Fixed, Fixed64, Point, Rect};

use crate::draw::texture::Texture;

#[cfg(feature = "perf")]
use super::perf::quad_perf;

pub fn quad_bbox(q: &[Point; 4]) -> Rect {
    Rect::bounding_quad(q)
}

pub fn blit_quad(dst: &mut Texture, src: &Texture, q: &[Point; 4], phys_clip: Rect) {
    use super::quad_aa::{
        EdgeRowState, prepare_quad_edges, quad_pixel_coverage_row, shoelace_is_cw,
    };
    use crate::types::Transform3D;
    let cw = shoelace_is_cw(q);
    let edges = prepare_quad_edges(q, cw);
    let src_rect = Rect::new(0, 0, src.width, src.height);
    let Some(forward) = Transform3D::from_quad(src_rect, q) else {
        return;
    };
    let Some(inverse) = forward.inverse() else {
        return;
    };
    let bbox = quad_bbox(q);
    let Some(area) = bbox.intersect(&phys_clip) else {
        return;
    };
    let screen = Rect::new(0, 0, dst.width, dst.height);
    let Some(area) = area.intersect(&screen) else {
        return;
    };
    let (px_x0, px_y0, px_x1, px_y1) = area.pixel_bounds();
    let sw = src.width as i32;
    let sh = src.height as i32;
    let half = Fixed64::from_raw(Fixed64::ONE.raw() >> 1);
    for py in px_y0..px_y1 {
        let py_f = Fixed::from_int(py) + Fixed::HALF;
        let Some((x_l, x_r)) = quad_row_span(q, py_f) else {
            continue;
        };
        let x_l_px = x_l.to_int().max(px_x0);
        let x_r_px = x_r.ceil().to_int().min(px_x1);
        if x_r_px <= x_l_px {
            continue;
        }
        let x0_f = Fixed64::from_fixed(Fixed::from_int(x_l_px)) + half;
        let y0_f = Fixed64::from_fixed(py_f);
        let mut big_x = inverse.m00 * x0_f + inverse.m01 * y0_f + inverse.m02;
        let mut big_y = inverse.m10 * x0_f + inverse.m11 * y0_f + inverse.m12;
        let mut w = inverse.m20 * x0_f + inverse.m21 * y0_f + inverse.m22;
        #[cfg(feature = "perf")]
        unsafe {
            quad_perf::BLIT_PIXELS_SCANNED += (x_r_px - x_l_px) as u64;
        }
        let mut cx = Fixed::from_int(x_l_px) + Fixed::HALF;
        let one = Fixed::ONE;
        let mut row = EdgeRowState::new(&edges, cx, py_f);
        for px in x_l_px..x_r_px {
            let edge_cx = cx;
            cx += one;
            if w.raw() > 0 {
                let edge_cov = quad_pixel_coverage_row(&edges, None, edge_cx, py_f, &row);
                if edge_cov != Fixed::ZERO {
                    let inv_w = Fixed64::ONE / w;
                    let sx = (big_x * inv_w).to_fixed().to_int();
                    let sy = (big_y * inv_w).to_fixed().to_int();
                    if sx >= 0 && sx < sw && sy >= 0 && sy < sh {
                        let c = src.get_pixel(sx, sy);
                        if c.a != 0 {
                            #[cfg(feature = "perf")]
                            unsafe {
                                quad_perf::BLIT_PIXELS_DRAWN += 1;
                            }
                            let src_alpha = if edge_cov == Fixed::ONE {
                                c.a
                            } else {
                                (Fixed::from_int(c.a as i32) * edge_cov).to_int() as u8
                            };
                            if src_alpha > 0 {
                                if src_alpha == 255 {
                                    dst.set_pixel(px, py, &c);
                                } else {
                                    dst.blend_pixel_int(px, py, &c, src_alpha);
                                }
                            }
                        }
                    }
                }
            }
            row.step(&edges);
            big_x += inverse.m00;
            big_y += inverse.m10;
            w += inverse.m20;
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn fill_rect_quad(
    dst: &mut Texture,
    q: &[Point; 4],
    phys_clip: Rect,
    color: &Color,
    radius: Fixed,
    local_w: Fixed,
    local_h: Fixed,
    opa: u8,
) {
    let bbox = quad_bbox(q);
    let Some(area) = bbox.intersect(&phys_clip) else {
        return;
    };
    let screen = Rect::new(0, 0, dst.width, dst.height);
    let Some(area) = area.intersect(&screen) else {
        return;
    };
    let (px_x0, px_y0, px_x1, px_y1) = area.pixel_bounds();

    if radius == Fixed::ZERO {
        fill_rect_quad_no_corner(dst, q, px_x0, px_y0, px_x1, px_y1, color, opa);
        return;
    }

    let _ = (local_w, local_h);
    use super::quad_aa::{
        EdgeRowState, prepare_quad_edges, quad_pixel_coverage_row, shoelace_is_cw,
    };
    let cw = shoelace_is_cw(q);
    let edges = prepare_quad_edges(q, cw);
    let corners = prepare_corners(q, radius);
    let one = Fixed::ONE;
    for py in px_y0..px_y1 {
        let py_f = Fixed::from_int(py) + Fixed::HALF;
        let Some((x_l, x_r)) = quad_row_span(q, py_f) else {
            continue;
        };
        let x_l_px = x_l.to_int().max(px_x0);
        let x_r_px = x_r.ceil().to_int().min(px_x1);
        if x_r_px <= x_l_px {
            continue;
        }
        #[cfg(feature = "perf")]
        unsafe {
            quad_perf::FILL_PIXELS_SCANNED += (x_r_px - x_l_px) as u64;
        }
        let mut cx = Fixed::from_int(x_l_px) + Fixed::HALF;
        let mut row = EdgeRowState::new(&edges, cx, py_f);
        for px in x_l_px..x_r_px {
            let cov = quad_pixel_coverage_row(&edges, Some(&corners), cx, py_f, &row);
            row.step(&edges);
            cx += one;
            if cov == Fixed::ZERO {
                continue;
            }
            let final_opa = if cov == Fixed::ONE {
                opa
            } else {
                let c = (cov * Fixed::from_int(opa as i32)).to_int() as u8;
                if c == 0 {
                    continue;
                }
                c
            };
            #[cfg(feature = "perf")]
            unsafe {
                quad_perf::FILL_PIXELS_DRAWN += 1;
            }
            if final_opa == 255 {
                dst.set_pixel(px, py, color);
            } else {
                dst.blend_pixel_int(px, py, color, final_opa);
            }
        }
    }
}

pub fn stroke_rect_quad(
    dst: &mut Texture,
    q: &[Point; 4],
    phys_clip: Rect,
    color: &Color,
    width: Fixed,
    radius: Fixed,
    opa: u8,
) {
    if width <= Fixed::ZERO {
        return;
    }
    let bbox = quad_bbox(q);
    let Some(area) = bbox.intersect(&phys_clip) else {
        return;
    };
    let screen = Rect::new(0, 0, dst.width, dst.height);
    let Some(area) = area.intersect(&screen) else {
        return;
    };
    let (px_x0, px_y0, px_x1, px_y1) = area.pixel_bounds();

    use super::quad_aa::{
        EdgeRowState, prepare_quad_edges, quad_pixel_coverage_row, shoelace_is_cw,
    };
    let cw_outer = shoelace_is_cw(q);
    let outer_edges = prepare_quad_edges(q, cw_outer);
    let inner_radius = (radius - width).max(Fixed::ZERO);
    let shapes = build_corner_shapes(q);
    let mut inner = [Point::ZERO; 4];
    let outer_corners: [super::quad_aa::PreparedCorner; 4] =
        core::array::from_fn(|i| super::quad_aa::PreparedCorner {
            center: shapes[i].inset_center(radius),
            ua: shapes[i].ua,
            ub: shapes[i].ub,
            radius,
        });
    let inner_corners: [super::quad_aa::PreparedCorner; 4] = core::array::from_fn(|i| {
        inner[i] = shapes[i].inset_center(width);
        super::quad_aa::PreparedCorner {
            center: shapes[i].inset_center(inner_radius),
            ua: shapes[i].ua,
            ub: shapes[i].ub,
            radius: inner_radius,
        }
    });
    let degenerate_inner = inner_quad_is_degenerate(&inner);
    let inner_edges = if degenerate_inner {
        None
    } else {
        Some(prepare_quad_edges(&inner, cw_outer))
    };

    for py in px_y0..px_y1 {
        let py_f = Fixed::from_int(py) + Fixed::HALF;
        let Some((x_lo, x_ro)) = quad_row_span(q, py_f) else {
            continue;
        };
        let xlo_px = x_lo.to_int().max(px_x0);
        let xro_px = x_ro.ceil().to_int().min(px_x1);
        if xro_px <= xlo_px {
            continue;
        }
        let mut cx = Fixed::from_int(xlo_px) + Fixed::HALF;
        let one = Fixed::ONE;
        let mut outer_row = EdgeRowState::new(&outer_edges, cx, py_f);
        let mut inner_row = inner_edges
            .as_ref()
            .map(|ie| EdgeRowState::new(ie, cx, py_f));
        for px in xlo_px..xro_px {
            let edge_cx = cx;
            cx += one;
            let outer_cov = quad_pixel_coverage_row(
                &outer_edges,
                Some(&outer_corners),
                edge_cx,
                py_f,
                &outer_row,
            );
            outer_row.step(&outer_edges);
            if outer_cov == Fixed::ZERO {
                if let (Some(ie), Some(ir)) = (inner_edges.as_ref(), inner_row.as_mut()) {
                    ir.step(ie);
                }
                continue;
            }
            let inner_cov = if let (Some(ie), Some(ir)) = (inner_edges.as_ref(), inner_row.as_mut())
            {
                let cov = quad_pixel_coverage_row(ie, Some(&inner_corners), edge_cx, py_f, ir);
                ir.step(ie);
                cov
            } else {
                Fixed::ZERO
            };
            let stroke_cov = (outer_cov - inner_cov).max(Fixed::ZERO);
            if stroke_cov == Fixed::ZERO {
                continue;
            }
            let final_opa = if stroke_cov == Fixed::ONE {
                opa
            } else {
                let c = (stroke_cov * Fixed::from_int(opa as i32)).to_int() as u8;
                if c == 0 {
                    continue;
                }
                c
            };
            if final_opa == 255 {
                dst.set_pixel(px, py, color);
            } else {
                dst.blend_pixel_int(px, py, color, final_opa);
            }
        }
    }
}

fn inner_quad_is_degenerate(inner: &[Point; 4]) -> bool {
    // Detect width >= half-extent: opposite inner vertices cross over,
    // collapsing both diagonals.
    let d1x = inner[2].x - inner[0].x;
    let d1y = inner[2].y - inner[0].y;
    let d2x = inner[3].x - inner[1].x;
    let d2y = inner[3].y - inner[1].y;
    let d1_sq = d1x * d1x + d1y * d1y;
    let d2_sq = d2x * d2x + d2y * d2y;
    d1_sq < Fixed::ONE || d2_sq < Fixed::ONE
}

/// Shape of the quad's four corners: vertex + two inward unit vectors.
/// Depends only on the quad geometry, not the inset radius, so can be
/// reused to derive multiple CornerInfo sets (outer, inner, offset).
struct CornerShape {
    vertex: Point,
    ua: Point,
    ub: Point,
}

impl CornerShape {
    /// Inset center: where a rounded corner of radius `r` has its
    /// circle center, measured as `vertex + r·ua + r·ub`.
    fn inset_center(&self, r: Fixed) -> Point {
        Point {
            x: self.vertex.x + self.ua.x * r + self.ub.x * r,
            y: self.vertex.y + self.ua.y * r + self.ub.y * r,
        }
    }
}

fn build_corner_shapes(q: &[Point; 4]) -> [CornerShape; 4] {
    core::array::from_fn(|i| {
        let vertex = q[i];
        let next = q[(i + 1) % 4];
        let prev = q[(i + 3) % 4];
        let ua = unit_vec(next.x - vertex.x, next.y - vertex.y);
        let ub = unit_vec(prev.x - vertex.x, prev.y - vertex.y);
        CornerShape { vertex, ua, ub }
    })
}

fn unit_vec(dx: Fixed, dy: Fixed) -> Point {
    let len = (dx * dx + dy * dy).sqrt();
    if len > Fixed::ZERO {
        Point {
            x: dx / len,
            y: dy / len,
        }
    } else {
        Point::ZERO
    }
}

/// Bundle each corner's shape + radius into the form
/// `quad_pixel_coverage_row` expects. If `radius <= 0` the corners don't
/// carve anything; callers should skip this and pass `None` instead.
fn prepare_corners(q: &[Point; 4], radius: Fixed) -> [super::quad_aa::PreparedCorner; 4] {
    let shapes = build_corner_shapes(q);
    core::array::from_fn(|i| super::quad_aa::PreparedCorner {
        center: shapes[i].inset_center(radius),
        ua: shapes[i].ua,
        ub: shapes[i].ub,
        radius,
    })
}

/// Fill a quad with no rounded corners. Each pixel covered by the quad
/// gets analytic coverage from the 4 edges (see `quad_aa`) so sub-pixel
/// translation produces smoothly varying alpha, not step aliasing.
///
/// Hot path: rows entirely outside the quad return None from
/// `quad_row_span` and skip; inside-a-row cost is one `quad_pixel_coverage`
/// call per pixel (4 × `edge_clip_area`).
#[allow(clippy::too_many_arguments)]
fn fill_rect_quad_no_corner(
    dst: &mut Texture,
    q: &[Point; 4],
    px_x0: i32,
    px_y0: i32,
    px_x1: i32,
    px_y1: i32,
    color: &Color,
    opa: u8,
) {
    use super::quad_aa::{
        EdgeRowState, prepare_quad_edges, quad_pixel_coverage_row, shoelace_is_cw,
    };
    let cw = shoelace_is_cw(q);
    let edges = prepare_quad_edges(q, cw);
    let one = Fixed::ONE;
    for py in px_y0..px_y1 {
        let py_f = Fixed::from_int(py) + Fixed::HALF;
        let Some((x_l, x_r)) = quad_row_span(q, py_f) else {
            continue;
        };
        let xlo_px = x_l.to_int().max(px_x0);
        let xhi_px = x_r.ceil().to_int().min(px_x1);
        if xhi_px <= xlo_px {
            continue;
        }
        #[cfg(feature = "perf")]
        unsafe {
            quad_perf::FILL_PIXELS_SCANNED += (xhi_px - xlo_px) as u64;
        }
        let mut cx = Fixed::from_int(xlo_px) + Fixed::HALF;
        let mut row = EdgeRowState::new(&edges, cx, py_f);
        for px in xlo_px..xhi_px {
            let edge_cx = cx;
            cx += one;
            let cov = quad_pixel_coverage_row(&edges, None, edge_cx, py_f, &row);
            row.step(&edges);
            if cov == Fixed::ZERO {
                continue;
            }
            let final_opa = if cov == Fixed::ONE {
                opa
            } else {
                // cov is in [0, 1] Q24.8; map to 0..=255 and combine with opa.
                let c = (cov * Fixed::from_int(opa as i32)).to_int() as u8;
                if c == 0 {
                    continue;
                }
                c
            };
            if final_opa == 255 {
                dst.set_pixel(px, py, color);
            } else {
                dst.blend_pixel_int(px, py, color, final_opa);
            }
        }
    }
}

/// Intersect horizontal line y=py with convex quad q; return leftmost and
/// rightmost x of the two intersections. None if row is fully outside.
fn quad_row_span(q: &[Point; 4], py: Fixed) -> Option<(Fixed, Fixed)> {
    let mut x_l = Fixed::MAX;
    let mut x_r = Fixed::MIN;
    let mut hit = false;
    for i in 0..4 {
        let a = q[i];
        let b = q[(i + 1) % 4];
        let dy = b.y - a.y;
        if dy.raw() == 0 {
            continue;
        }
        let (y0, y1) = if dy.raw() > 0 { (a.y, b.y) } else { (b.y, a.y) };
        if py < y0 || py >= y1 {
            continue;
        }
        // Linear interp: x = a.x + (py - a.y) / (b.y - a.y) * (b.x - a.x).
        // Fixed64 intermediates avoid Q24.8 mul overflow on large widgets.
        let dx = Fixed64::from_fixed(b.x - a.x);
        let t_num = Fixed64::from_fixed(py - a.y);
        let t_den = Fixed64::from_fixed(dy);
        let x = Fixed64::from_fixed(a.x) + t_num * dx / t_den;
        let x = x.to_fixed();
        if x < x_l {
            x_l = x;
        }
        if x > x_r {
            x_r = x;
        }
        hit = true;
    }
    if hit { Some((x_l, x_r)) } else { None }
}