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    },
109    /// A see-through wash with round corners over what's already drawn, text included, and
110    /// a border twice a panel's weight: where something dragged would land.
111    Wash {
112        bounds: Bounds,
113        /// As a percentage of a row's height.
114        radius: u8,
115        color: Rgb,
116        /// Out of 255.
117        alpha: u8,
118        border: Option<Rgb>,
119    },
120    /// A line one device pixel thick, along the top or left of `bounds`.
121    Hairline { bounds: Bounds, color: Rgb },
122    Text {
123        x: i32,
124        /// The row it's centred on, in paint units.
125        y: i32,
126        max_width: i32,
127        text: String,
128        color: Rgb,
129        font: Font,
130        /// `x` is the right edge rather than the left.
131        align_right: bool,
132        /// `x` is the middle of the text. Only the rasterizer knows a text's true width.
133        centred: bool,
134        /// Drawn under the text at its true width.
135        underline: bool,
136    },
137    Icon {
138        bounds: Bounds,
139        icon: Icon,
140        color: Rgb,
141    },
142    /// Fills the listed corners of `bounds` back in. Cells can't be rounded and paint
143    /// can't go under text, so a rounded surface is cells with its corners painted out.
144    Corners {
145        bounds: Bounds,
146        /// As a percentage of a row's height.
147        radius: u8,
148        color: Rgb,
149        which: u8,
150    },
151    /// A row's share of a minimap: many small bars, which as panels would be slow.
152    Strips { bounds: Bounds, strips: Vec<Strip> },
153    /// A bitmap scaled to `bounds`. Coverage is inked in `color`.
154    Image {
155        bounds: Bounds,
156        /// What it stays inside, since a page scrolls past its pictures.
157        clip: Bounds,
158        bitmap: Bitmap,
159        color: Rgb,
160    },
161}
162
163impl Paint {
164    /// Paint units of the columns this can touch, as (left, right). It may overstate them:
165    /// that costs a tile, where understating would leave stale pixels.
166    pub fn reach(&self) -> (i32, i32) {
167        match self {
168            Self::Panel { bounds, shadow, .. } => {
169                let shade = if *shadow { 2 * UNIT } else { 0 };
170                (bounds.x - shade, bounds.x + bounds.w + shade)
171            }
172            Self::Hairline { bounds, .. }
173            | Self::Wash { bounds, .. }
174            | Self::Icon { bounds, .. }
175            | Self::Strips { bounds, .. }
176            | Self::Corners { bounds, .. } => (bounds.x, bounds.x + bounds.w.max(1)),
177            Self::Image { bounds, clip, .. } => (
178                bounds.x.max(clip.x),
179                (bounds.x + bounds.w.max(1)).min(clip.x + clip.w),
180            ),
181            Self::Text {
182                x,
183                max_width,
184                text,
185                align_right,
186                centred,
187                ..
188            } => {
189                // Only the rasterizer knows true widths, but a character is never this wide.
190                let wide = (text.chars().count() as i32 + 1).saturating_mul(4 * UNIT);
191                if *align_right {
192                    (x.saturating_sub(wide), *x)
193                } else if *centred {
194                    (x.saturating_sub(wide), x.saturating_add(wide))
195                } else {
196                    (*x, x.saturating_add(*max_width))
197                }
198            }
199        }
200    }
201
202    /// This as seen from a row starting at `top`. What's drawn in a row depends on this and not
203    /// on where the row stands, so a row that scrolls keeps its tile.
204    pub fn on_row(&self, top: i32) -> Self {
205        let mut paint = self.clone();
206        match &mut paint {
207            Self::Panel { bounds, .. }
208            | Self::Wash { bounds, .. }
209            | Self::Hairline { bounds, .. }
210            | Self::Icon { bounds, .. }
211            | Self::Strips { bounds, .. }
212            | Self::Corners { bounds, .. } => bounds.y -= top,
213            Self::Image { bounds, clip, .. } => {
214                // A clip that doesn't reach into the row makes no difference to it.
215                let bottom = (clip.y + clip.h).min(top + UNIT);
216                clip.y = clip.y.max(top);
217                clip.h = bottom - clip.y;
218                clip.y -= top;
219                bounds.y -= top;
220            }
221            Self::Text { y, .. } => *y -= top,
222        }
223        paint
224    }
225
226    /// Paint units of the rows this touches, as (top, bottom).
227    pub(crate) fn span(&self) -> (i32, i32) {
228        match self {
229            Self::Panel { bounds, shadow, .. } => {
230                let reach = if *shadow { UNIT } else { 0 };
231                (bounds.y - reach, bounds.y + bounds.h + reach)
232            }
233            Self::Hairline { bounds, .. }
234            | Self::Wash { bounds, .. }
235            | Self::Icon { bounds, .. }
236            | Self::Strips { bounds, .. }
237            | Self::Corners { bounds, .. } => (bounds.y, bounds.y + bounds.h.max(1)),
238            Self::Image { bounds, clip, .. } => (
239                bounds.y.max(clip.y),
240                (bounds.y + bounds.h.max(1)).min(clip.y + clip.h),
241            ),
242            // Large text stands taller than its row.
243            Self::Text { y, font, .. } if font.size > 100 => (*y - UNIT, *y + 2 * UNIT),
244            Self::Text { y, .. } => (*y, *y + UNIT),
245        }
246    }
247}