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`.
111        inset: bool,
112    },
113    /// A see-through wash with round corners over what's already drawn, text included, and
114    /// a border twice a panel's weight: where something dragged would land.
115    Wash {
116        bounds: Bounds,
117        /// As a percentage of a row's height.
118        radius: u8,
119        color: Rgb,
120        /// Out of 255.
121        alpha: u8,
122        border: Option<Rgb>,
123    },
124    /// A line one device pixel thick, along the top or left of `bounds`.
125    Hairline { bounds: Bounds, color: Rgb },
126    Text {
127        x: i32,
128        /// The row it's centred on, in paint units.
129        y: i32,
130        max_width: i32,
131        text: String,
132        color: Rgb,
133        font: Font,
134        /// `x` is the right edge rather than the left.
135        align_right: bool,
136        /// `x` is the middle of the text. Only the rasterizer knows a text's true width.
137        centred: bool,
138        /// Drawn under the text at its true width.
139        underline: bool,
140    },
141    Icon {
142        bounds: Bounds,
143        icon: Icon,
144        color: Rgb,
145    },
146    /// Fills the listed corners of `bounds` back in. Cells can't be rounded and paint
147    /// can't go under text, so a rounded surface is cells with its corners painted out.
148    Corners {
149        bounds: Bounds,
150        /// As a percentage of a row's height.
151        radius: u8,
152        color: Rgb,
153        which: u8,
154    },
155    /// A row's share of a minimap: many small bars, which as panels would be slow.
156    Strips { bounds: Bounds, strips: Vec<Strip> },
157    /// A bitmap scaled to `bounds`. Coverage is inked in `color`.
158    Image {
159        bounds: Bounds,
160        /// What it stays inside, since a page scrolls past its pictures.
161        clip: Bounds,
162        bitmap: Bitmap,
163        color: Rgb,
164    },
165}
166
167/// How far past a row a shape's edge can still change it: the largest corner, 255% of a row,
168/// and a shadow's reach, with room to spare.
169const ROW_REACH: i32 = 4 * UNIT;
170
171/// `bounds` cut to within reach of the row starting at `top`, which leaves what the row shows
172/// of a plain shape as it was.
173fn beyond_the_row(bounds: Bounds, top: i32) -> Bounds {
174    let (low, high) = (top - ROW_REACH, top + UNIT + ROW_REACH);
175    let y = bounds.y.clamp(low, high);
176    let bottom = (bounds.y + bounds.h).clamp(low, high);
177    Bounds {
178        y,
179        h: bottom - y,
180        ..bounds
181    }
182}
183
184impl Paint {
185    /// Where its top is, in paint units from the grid's first row.
186    pub fn top(&self) -> i32 {
187        match self {
188            Self::Text { y, .. } => *y,
189            Self::Panel { bounds, .. }
190            | Self::Wash { bounds, .. }
191            | Self::Hairline { bounds, .. }
192            | Self::Icon { bounds, .. }
193            | Self::Corners { bounds, .. }
194            | Self::Strips { bounds, .. }
195            | Self::Image { bounds, .. } => bounds.y,
196        }
197    }
198
199    /// Moves it up by `by` paint units.
200    pub fn lift(&mut self, by: i32) {
201        match self {
202            Self::Text { y, .. } => *y -= by,
203            Self::Panel { bounds, .. }
204            | Self::Wash { bounds, .. }
205            | Self::Hairline { bounds, .. }
206            | Self::Icon { bounds, .. }
207            | Self::Corners { bounds, .. }
208            | Self::Strips { bounds, .. } => bounds.y -= by,
209            Self::Image { bounds, clip, .. } => {
210                bounds.y -= by;
211                clip.y -= by;
212            }
213        }
214    }
215
216    /// Paint units of the columns this can touch, as (left, right). It may overstate them:
217    /// that costs a tile, where understating would leave stale pixels.
218    pub fn reach(&self) -> (i32, i32) {
219        match self {
220            Self::Panel { bounds, shadow, .. } => {
221                let shade = if *shadow { 2 * UNIT } else { 0 };
222                (bounds.x - shade, bounds.x + bounds.w + shade)
223            }
224            Self::Hairline { bounds, .. }
225            | Self::Wash { bounds, .. }
226            | Self::Icon { bounds, .. }
227            | Self::Strips { bounds, .. }
228            | Self::Corners { bounds, .. } => (bounds.x, bounds.x + bounds.w.max(1)),
229            Self::Image { bounds, clip, .. } => (
230                bounds.x.max(clip.x),
231                (bounds.x + bounds.w.max(1)).min(clip.x + clip.w),
232            ),
233            Self::Text {
234                x,
235                max_width,
236                text,
237                align_right,
238                centred,
239                ..
240            } => {
241                // Only the rasterizer knows true widths, but a character is never this wide.
242                let wide = (text.chars().count() as i32 + 1).saturating_mul(4 * UNIT);
243                if *align_right {
244                    (x.saturating_sub(wide), *x)
245                } else if *centred {
246                    (x.saturating_sub(wide), x.saturating_add(wide))
247                } else {
248                    (*x, x.saturating_add(*max_width))
249                }
250            }
251        }
252    }
253
254    /// This as seen from a row starting at `top`. What's drawn in a row depends on this and not
255    /// on where the row stands, so a row that scrolls keeps its tile.
256    pub fn on_row(&self, top: i32) -> Self {
257        let mut paint = self.clone();
258        match &mut paint {
259            // A shape reaching well past the row on a side looks the same in it however far
260            // it goes, so that far is left out: a page's sheet runs the height of the view,
261            // and seen whole, every row under it looked new after each scroll.
262            Self::Panel {
263                bounds,
264                dashed: false,
265                ..
266            }
267            | Self::Wash { bounds, .. }
268            | Self::Hairline { bounds, .. }
269            | Self::Corners { bounds, .. } => {
270                *bounds = beyond_the_row(*bounds, top);
271                bounds.y -= top;
272            }
273            Self::Panel { bounds, .. }
274            | Self::Icon { bounds, .. }
275            | Self::Strips { bounds, .. } => bounds.y -= top,
276            Self::Image { bounds, clip, .. } => {
277                // A clip that doesn't reach into the row makes no difference to it.
278                let bottom = (clip.y + clip.h).min(top + UNIT);
279                clip.y = clip.y.max(top);
280                clip.h = bottom - clip.y;
281                clip.y -= top;
282                bounds.y -= top;
283            }
284            Self::Text { y, .. } => *y -= top,
285        }
286        paint
287    }
288
289    /// Paint units of the rows this touches, as (top, bottom).
290    pub(crate) fn span(&self) -> (i32, i32) {
291        match self {
292            Self::Panel { bounds, shadow, .. } => {
293                let reach = if *shadow { UNIT } else { 0 };
294                (bounds.y - reach, bounds.y + bounds.h + reach)
295            }
296            Self::Hairline { bounds, .. }
297            | Self::Wash { bounds, .. }
298            | Self::Icon { bounds, .. }
299            | Self::Strips { bounds, .. }
300            | Self::Corners { bounds, .. } => (bounds.y, bounds.y + bounds.h.max(1)),
301            Self::Image { bounds, clip, .. } => (
302                bounds.y.max(clip.y),
303                (bounds.y + bounds.h.max(1)).min(clip.y + clip.h),
304            ),
305            // Large text stands taller than its row.
306            Self::Text { y, font, .. } if font.size > 100 => (*y - UNIT, *y + 2 * UNIT),
307            Self::Text { y, .. } => (*y, *y + UNIT),
308        }
309    }
310}