BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / badge.rs

badge.rs

badge.rs 4.6 KiB Download Raw
1pub const BADGE_SIZE: usize = 32;
2const GLYPH_WIDTH: usize = 5;
3const GLYPH_HEIGHT: usize = 7;
4const GLYPH_SCALE: usize = 2;
5const GLYPH_GAP: usize = 1;
6const RED: [u8; 3] = [0xef, 0x44, 0x44];
7const INK: [u8; 3] = [0xff, 0xff, 0xff];
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Badge {
11 pub text: String,
12}
13
14pub fn badge_for(unread_chats: usize) -> Option<Badge> {
15 if unread_chats == 0 {
16 return None;
17 }
18 let text = if unread_chats >= 10 {
19 "9+".to_owned()
20 } else {
21 unread_chats.to_string()
22 };
23 Some(Badge { text })
24}
25
26const GLYPHS: [[u8; GLYPH_HEIGHT]; 11] = [
27 [0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110],
28 [0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110],
29 [0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111],
30 [0b11110, 0b00001, 0b00001, 0b01110, 0b00001, 0b00001, 0b11110],
31 [0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010],
32 [0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110],
33 [0b00110, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110],
34 [0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000],
35 [0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110],
36 [0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b01100],
37 [0b00000, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0b00000],
38];
39
40fn glyph_for(character: char) -> Option<&'static [u8; GLYPH_HEIGHT]> {
41 match character {
42 '0'..='9' => GLYPHS.get(character as usize - '0' as usize),
43 '+' => GLYPHS.get(10),
44 _ => None,
45 }
46}
47
48pub fn text_width(text: &str) -> usize {
49 let glyphs = text.chars().count();
50 if glyphs == 0 {
51 return 0;
52 }
53 glyphs * GLYPH_WIDTH * GLYPH_SCALE + (glyphs - 1) * GLYPH_GAP * GLYPH_SCALE
54}
55
56/// BGRA, premultiplied, top-down, `BADGE_SIZE` square.
57pub fn render_badge(badge: &Badge) -> Vec<u8> {
58 let mut pixels = vec![0u8; BADGE_SIZE * BADGE_SIZE * 4];
59 let center = (BADGE_SIZE as f32 - 1.) / 2.;
60 let radius = BADGE_SIZE as f32 / 2.;
61 for row in 0..BADGE_SIZE {
62 for column in 0..BADGE_SIZE {
63 let distance = ((column as f32 - center).powi(2) + (row as f32 - center).powi(2)).sqrt();
64 if distance <= radius - 0.5 {
65 put(&mut pixels, column, row, RED);
66 }
67 }
68 }
69 let width = text_width(&badge.text);
70 let left = (BADGE_SIZE - width.min(BADGE_SIZE)) / 2;
71 let top = (BADGE_SIZE - GLYPH_HEIGHT * GLYPH_SCALE) / 2;
72 for (index, character) in badge.text.chars().enumerate() {
73 let Some(glyph) = glyph_for(character) else {
74 continue;
75 };
76 let origin = left + index * (GLYPH_WIDTH + GLYPH_GAP) * GLYPH_SCALE;
77 for (glyph_row, bits) in glyph.iter().enumerate() {
78 for glyph_column in 0..GLYPH_WIDTH {
79 if bits & (1 << (GLYPH_WIDTH - 1 - glyph_column)) == 0 {
80 continue;
81 }
82 for dy in 0..GLYPH_SCALE {
83 for dx in 0..GLYPH_SCALE {
84 put(
85 &mut pixels,
86 origin + glyph_column * GLYPH_SCALE + dx,
87 top + glyph_row * GLYPH_SCALE + dy,
88 INK,
89 );
90 }
91 }
92 }
93 }
94 }
95 pixels
96}
97
98fn put(pixels: &mut [u8], column: usize, row: usize, rgb: [u8; 3]) {
99 if column >= BADGE_SIZE || row >= BADGE_SIZE {
100 return;
101 }
102 let offset = (row * BADGE_SIZE + column) * 4;
103 pixels[offset..offset + 4].copy_from_slice(&[rgb[2], rgb[1], rgb[0], 0xff]);
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn no_unread_means_no_badge() {
112 assert_eq!(badge_for(0), None);
113 }
114
115 #[test]
116 fn counts_chats_and_caps_at_nine_plus() {
117 assert_eq!(badge_for(3).unwrap().text, "3");
118 assert_eq!(badge_for(9).unwrap().text, "9");
119 assert_eq!(badge_for(10).unwrap().text, "9+");
120 assert_eq!(badge_for(250).unwrap().text, "9+");
121 }
122
123 #[test]
124 fn rendered_badge_has_fill_ink_and_transparent_corner() {
125 let badge = badge_for(3).unwrap();
126 let pixels = render_badge(&badge);
127 assert_eq!(pixels.len(), BADGE_SIZE * BADGE_SIZE * 4);
128 assert_eq!(pixels[3], 0);
129 let has = |blue: u8, green: u8, red: u8| {
130 pixels
131 .chunks(4)
132 .any(|pixel| pixel == [blue, green, red, 0xff])
133 };
134 assert!(has(0x44, 0x44, 0xef));
135 assert!(has(0xff, 0xff, 0xff));
136 }
137
138 #[test]
139 fn two_glyphs_fit_the_badge() {
140 assert!(text_width("9+") < BADGE_SIZE);
141 }
142}