Skip to main content

cellix_render/
paint.rs

1//! What the pixel tiers draw that a cell can't hold: proportional text, rounded panels,
2//! hairlines, shadows and small icons. The grid stays the source of truth for layout and
3//! hit-testing, and the cell tier ignores all of this.
4
5use std::hash::{Hash, Hasher};
6use std::sync::Arc;
7
8use crate::grid::{Rect, Rgb};
9use crate::icon::Icon;
10use crate::measure::Font;
11
12/// Paint positions are in 64ths of a cell, so they can sit between cells and still hash.
13pub const UNIT: i32 = 64;
14
15/// Converts cells to paint units.
16pub fn units(cells: f32) -> i32 {
17    (cells * UNIT as f32).round() as i32
18}
19
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
21pub struct Bounds {
22    pub x: i32,
23    pub y: i32,
24    pub w: i32,
25    pub h: i32,
26}
27
28impl Bounds {
29    pub fn cells(x: f32, y: f32, w: f32, h: f32) -> Self {
30        Self {
31            x: units(x),
32            y: units(y),
33            w: units(w),
34            h: units(h),
35        }
36    }
37
38    /// A rect shrunk by `x` columns and `y` rows on each side.
39    pub fn inset(rect: Rect, x: f32, y: f32) -> Self {
40        Self::cells(
41            rect.x as f32 + x,
42            rect.y as f32 + y,
43            rect.w as f32 - x * 2.0,
44            rect.h as f32 - y * 2.0,
45        )
46    }
47}
48
49/// One bar in a minimap, in 256ths of its box.
50#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
51pub struct Strip {
52    pub x: u8,
53    pub y: u8,
54    pub w: u8,
55    pub color: Rgb,
56}
57
58pub const TOP_LEFT: u8 = 1;
59pub const TOP_RIGHT: u8 = 2;
60pub const BOTTOM_RIGHT: u8 = 4;
61pub const BOTTOM_LEFT: u8 = 8;
62pub const ALL_CORNERS: u8 = 15;
63
64/// What a bitmap's bytes are.
65#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
66pub enum Pixels {
67    /// One byte a pixel of ink, drawn in a colour: a formula takes the text's.
68    Coverage,
69    /// Four bytes a pixel: a picture or a page of a PDF.
70    Rgba,
71}
72
73/// Pixels made elsewhere. `id` stands for the content, so a tile hashes it and not every byte.
74#[derive(Clone, Debug)]
75pub struct Bitmap {
76    pub id: u64,
77    pub width: u32,
78    pub height: u32,
79    pub kind: Pixels,
80    pub pixels: Arc<Vec<u8>>,
81}
82
83impl PartialEq for Bitmap {
84    fn eq(&self, other: &Self) -> bool {
85        (self.id, self.width, self.height) == (other.id, other.width, other.height)
86    }
87}
88
89impl Eq for Bitmap {}
90
91impl Hash for Bitmap {
92    fn hash<H: Hasher>(&self, state: &mut H) {
93        (self.id, self.width, self.height).hash(state);
94    }
95}
96
97#[derive(Clone, Debug, PartialEq, Eq, Hash)]
98pub enum Paint {
99    Panel {
100        bounds: Bounds,
101        /// As a percentage of a row's height.
102        radius: u8,
103        fill: Option<Rgb>,
104        border: Option<Rgb>,
105        shadow: bool,
106        /// The border in dashes.
107        dashed: bool,
108        /// A button on a row's wash. On a row taller than the terminal's line it keeps the
109        /// height it has on that line and isn't grown to a control's, which is the wash's
110        /// own: the wash shows over and under it. See `raster::inset`. A floating panel, with
111        /// a shadow, is drawn exactly over its bounds, however short of its row
112        /// (`Grid::exact_popup`).
113        inset: bool,
114    },
115    /// A see-through wash with round corners over what's already drawn, text included, and
116    /// a border twice a panel's weight: where something dragged would land.
117    Wash {
118        bounds: Bounds,
119        /// As a percentage of a row's height.
120        radius: u8,
121        color: Rgb,
122        /// Out of 255.
123        alpha: u8,
124        border: Option<Rgb>,
125    },
126    /// A line one device pixel thick, along the top or left of `bounds`.
127    Hairline { bounds: Bounds, color: Rgb },
128    Text {
129        x: i32,
130        /// The row it's centred on, in paint units.
131        y: i32,
132        max_width: i32,
133        text: String,
134        color: Rgb,
135        font: Font,
136        /// `x` is the right edge rather than the left.
137        align_right: bool,
138        /// `x` is the middle of the text. Only the rasterizer knows a text's true width.
139        centred: bool,
140        /// Drawn under the text at its true width.
141        underline: bool,
142        /// It's a button's words: a capital's height in its font, its top to the baseline,
143        /// stands in the middle of the row `y` starts, rather than the font's line, which stood
144        /// words of 13 pt a pixel or two high in a button. Every such run of one size stands
145        /// the same whatever its letters, so a row of buttons has one baseline, and tails hang
146        /// under it.
147        span_centred: bool,
148    },
149    Icon {
150        bounds: Bounds,
151        icon: Icon,
152        color: Rgb,
153    },
154    /// A program badge with its initial centred by the glyph's visible ink.
155    Monogram {
156        bounds: Bounds,
157        color: Rgb,
158        letter: char,
159        ground: Rgb,
160    },
161    /// Something under way: a faint ring with a quarter of it in full colour, at a step of its
162    /// turn, or with no step a still dot. Sized and placed as an icon is. See `spinner`.
163    Spinner {
164        bounds: Bounds,
165        color: Rgb,
166        step: Option<u8>,
167    },
168    /// Fills the listed corners of `bounds` back in. Cells can't be rounded and paint
169    /// can't go under text, so a rounded surface is cells with its corners painted out.
170    Corners {
171        bounds: Bounds,
172        /// As a percentage of a row's height.
173        radius: u8,
174        color: Rgb,
175        which: u8,
176    },
177    /// A row's share of a minimap: many small bars, which as panels would be slow.
178    Strips { bounds: Bounds, strips: Vec<Strip> },
179    /// A bitmap scaled to `bounds`. Coverage is inked in `color`.
180    Image {
181        bounds: Bounds,
182        /// What it stays inside, since a page scrolls past its pictures.
183        clip: Bounds,
184        bitmap: Bitmap,
185        color: Rgb,
186    },
187}
188
189/// How far past a row a shape's edge can still change it: the largest corner, 255% of a row,
190/// and a shadow's reach, with room to spare.
191const ROW_REACH: i32 = 4 * UNIT;
192
193/// `bounds` cut to within reach of the row starting at `top`, which leaves what the row shows
194/// of a plain shape as it was.
195fn beyond_the_row(bounds: Bounds, top: i32) -> Bounds {
196    let (low, high) = (top - ROW_REACH, top + UNIT + ROW_REACH);
197    let y = bounds.y.clamp(low, high);
198    let bottom = (bounds.y + bounds.h).clamp(low, high);
199    Bounds {
200        y,
201        h: bottom - y,
202        ..bounds
203    }
204}
205
206impl Paint {
207    /// Where its top is, in paint units from the grid's first row.
208    pub fn top(&self) -> i32 {
209        match self {
210            Self::Text { y, .. } => *y,
211            Self::Panel { bounds, .. }
212            | Self::Wash { bounds, .. }
213            | Self::Hairline { bounds, .. }
214            | Self::Icon { bounds, .. }
215            | Self::Monogram { bounds, .. }
216            | Self::Spinner { bounds, .. }
217            | Self::Corners { bounds, .. }
218            | Self::Strips { bounds, .. }
219            | Self::Image { bounds, .. } => bounds.y,
220        }
221    }
222
223    /// Moves it up by `by` paint units.
224    pub fn lift(&mut self, by: i32) {
225        match self {
226            Self::Text { y, .. } => *y -= by,
227            Self::Panel { bounds, .. }
228            | Self::Wash { bounds, .. }
229            | Self::Hairline { bounds, .. }
230            | Self::Icon { bounds, .. }
231            | Self::Monogram { bounds, .. }
232            | Self::Spinner { bounds, .. }
233            | Self::Corners { bounds, .. }
234            | Self::Strips { bounds, .. } => bounds.y -= by,
235            Self::Image { bounds, clip, .. } => {
236                bounds.y -= by;
237                clip.y -= by;
238            }
239        }
240    }
241
242    /// Paint units of the columns this can touch, as (left, right). It may overstate them:
243    /// that costs a tile, where understating would leave stale pixels.
244    pub fn reach(&self) -> (i32, i32) {
245        match self {
246            Self::Panel { bounds, shadow, .. } => {
247                let shade = if *shadow { 2 * UNIT } else { 0 };
248                (bounds.x - shade, bounds.x + bounds.w + shade)
249            }
250            Self::Hairline { bounds, .. }
251            | Self::Wash { bounds, .. }
252            | Self::Icon { bounds, .. }
253            | Self::Monogram { bounds, .. }
254            | Self::Spinner { bounds, .. }
255            | Self::Strips { bounds, .. }
256            | Self::Corners { bounds, .. } => (bounds.x, bounds.x + bounds.w.max(1)),
257            Self::Image { bounds, clip, .. } => (
258                bounds.x.max(clip.x),
259                (bounds.x + bounds.w.max(1)).min(clip.x + clip.w),
260            ),
261            Self::Text {
262                x,
263                max_width,
264                text,
265                align_right,
266                centred,
267                ..
268            } => {
269                // Only the rasterizer knows true widths, but a character is never this wide.
270                let wide = (text.chars().count() as i32 + 1).saturating_mul(4 * UNIT);
271                if *align_right {
272                    (x.saturating_sub(wide), *x)
273                } else if *centred {
274                    (x.saturating_sub(wide), x.saturating_add(wide))
275                } else {
276                    (*x, x.saturating_add(*max_width))
277                }
278            }
279        }
280    }
281
282    /// This as seen from a row starting at `top`. What's drawn in a row depends on this and not
283    /// on where the row stands, so a row that scrolls keeps its tile.
284    pub fn on_row(&self, top: i32) -> Self {
285        let mut paint = self.clone();
286        match &mut paint {
287            // A shape reaching well past the row on a side looks the same in it however far
288            // it goes, so that far is left out: a page's sheet runs the height of the view,
289            // and seen whole, every row under it looked new after each scroll.
290            Self::Panel {
291                bounds,
292                dashed: false,
293                ..
294            }
295            | Self::Wash { bounds, .. }
296            | Self::Hairline { bounds, .. }
297            | Self::Corners { bounds, .. } => {
298                *bounds = beyond_the_row(*bounds, top);
299                bounds.y -= top;
300            }
301            Self::Panel { bounds, .. }
302            | Self::Icon { bounds, .. }
303            | Self::Monogram { bounds, .. }
304            | Self::Spinner { bounds, .. }
305            | Self::Strips { bounds, .. } => bounds.y -= top,
306            Self::Image { bounds, clip, .. } => {
307                // A clip that doesn't reach into the row makes no difference to it.
308                let bottom = (clip.y + clip.h).min(top + UNIT);
309                clip.y = clip.y.max(top);
310                clip.h = bottom - clip.y;
311                clip.y -= top;
312                bounds.y -= top;
313            }
314            Self::Text { y, .. } => *y -= top,
315        }
316        paint
317    }
318
319    /// Paint units of the rows this touches, as (top, bottom).
320    pub(crate) fn span(&self) -> (i32, i32) {
321        match self {
322            Self::Panel { bounds, shadow, .. } => {
323                let reach = if *shadow { UNIT } else { 0 };
324                (bounds.y - reach, bounds.y + bounds.h + reach)
325            }
326            Self::Hairline { bounds, .. }
327            | Self::Wash { bounds, .. }
328            | Self::Icon { bounds, .. }
329            | Self::Monogram { bounds, .. }
330            | Self::Spinner { bounds, .. }
331            | Self::Strips { bounds, .. }
332            | Self::Corners { bounds, .. } => (bounds.y, bounds.y + bounds.h.max(1)),
333            Self::Image { bounds, clip, .. } => (
334                bounds.y.max(clip.y),
335                (bounds.y + bounds.h.max(1)).min(clip.y + clip.h),
336            ),
337            // Large text stands taller than its row.
338            Self::Text { y, font, .. } if font.size > 100 => (*y - UNIT, *y + 2 * UNIT),
339            Self::Text { y, .. } => (*y, *y + UNIT),
340        }
341    }
342}