Skip to main content

felis_render_core/
display_list.rs

1//! What the cell painter emits: rectangles, glyph draws and line
2//! decorations in physical pixels, colors in linear RGBA. A backend
3//! draws `rects`, then `glyphs`, then `decos`, each in list order.
4
5/// Which glyph sheet a [`GlyphDraw`] samples.
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum AtlasKind {
8    /// Coverage masks, tinted by the draw's color.
9    Mono,
10    /// RGBA color bitmaps, drawn untinted.
11    Color,
12}
13
14#[derive(Copy, Clone, Debug, PartialEq)]
15pub struct Rect {
16    pub origin_px: [f32; 2],
17    pub size_px: [f32; 2],
18    pub color: [f32; 4],
19}
20
21#[derive(Copy, Clone, Debug, PartialEq)]
22pub struct GlyphDraw {
23    pub origin_px: [f32; 2],
24    pub size_px: [f32; 2],
25    pub atlas: AtlasKind,
26    /// Top-left of the sampled region, in pixels of the `atlas` sheet.
27    pub src_min_px: [f32; 2],
28    /// Bottom-right of the sampled region, in pixels of the `atlas` sheet.
29    pub src_max_px: [f32; 2],
30    pub color: [f32; 4],
31}
32
33impl GlyphDraw {
34    /// Narrows the source to the fractions `t0..t1` of its width, the
35    /// part a destination cut at the same fractions still shows.
36    pub fn crop_src_x(&mut self, t0: f32, t1: f32) {
37        let (min, max) = crop(self.src_min_px[0], self.src_max_px[0], t0, t1);
38        self.src_min_px[0] = min;
39        self.src_max_px[0] = max;
40    }
41
42    /// [`Self::crop_src_x`] along the height.
43    pub fn crop_src_y(&mut self, t0: f32, t1: f32) {
44        let (min, max) = crop(self.src_min_px[1], self.src_max_px[1], t0, t1);
45        self.src_min_px[1] = min;
46        self.src_max_px[1] = max;
47    }
48}
49
50fn crop(min: f32, max: f32, t0: f32, t1: f32) -> (f32, f32) {
51    let span = max - min;
52    (span.mul_add(t0, min), span.mul_add(t1, min))
53}
54
55/// The line style of a [`Decoration`].
56#[derive(Copy, Clone, Debug, PartialEq, Eq)]
57pub enum DecoKind {
58    Solid,
59    /// `SGR 4:4`.
60    Dotted,
61    /// `SGR 4:5`.
62    Dashed,
63    /// `SGR 4:3`.
64    Curly,
65}
66
67/// The quad spans the decoration band: the line rectangle for the
68/// straight kinds, a taller band the sine swings inside for
69/// [`DecoKind::Curly`].
70#[derive(Copy, Clone, Debug, PartialEq)]
71pub struct Decoration {
72    pub origin_px: [f32; 2],
73    pub size_px: [f32; 2],
74    pub color: [f32; 4],
75    pub kind: DecoKind,
76    pub thickness_px: f32,
77    /// Pattern period (dotted / dashed) or sine wavelength (curly).
78    /// Unused by [`DecoKind::Solid`].
79    pub period_px: f32,
80}
81
82#[derive(Debug, Default, PartialEq)]
83pub struct RowPaint {
84    pub rects: Vec<Rect>,
85    pub glyphs: Vec<GlyphDraw>,
86    /// Drawn after the glyphs so strikethrough sits over the ink.
87    pub decos: Vec<Decoration>,
88}
89
90impl RowPaint {
91    pub fn clear(&mut self) {
92        self.rects.clear();
93        self.glyphs.clear();
94        self.decos.clear();
95    }
96}