1use crate::IndexedImage;
7use crate::atlas::{FONT_DATA, GlyphAtlas};
8use crate::palette::{EMBER_BASE, INK_BASE, IndexedPalette, LEVELS, SPRITE_BASE};
9use clairlune_scene::grid_spec;
10
11pub const SIGNATURE: &str = "clairlune \u{b7} by yam333";
13
14const STRENGTH: u32 = 4;
17
18#[derive(Clone, Debug)]
20pub struct Signature {
21 atlas: GlyphAtlas,
22 chars: u32,
23 x: u32,
25 y: u32,
26 width: u32,
27 height: u32,
28}
29
30impl Signature {
31 pub fn new(width: u32, height: u32, text: &str) -> Option<Signature> {
35 let spec = grid_spec(width, height);
36 let chars: Vec<char> = text.chars().collect();
37 let n = chars.len() as u32;
38 if n == 0 || n > 200 || spec.cols < 2 * (n + 3) || spec.rows < 6 {
39 return None;
40 }
41 let atlas =
42 GlyphAtlas::with_chars(FONT_DATA, &chars, spec.cell_w, spec.cell_h, spec.font_px)
43 .ok()?;
44 let col = spec.cols - n - 2;
46 let row = spec.rows - 2;
47 Some(Signature {
48 atlas,
49 chars: n,
50 x: spec.origin_x + col * spec.cell_w,
51 y: spec.origin_y + row * spec.cell_h,
52 width,
53 height,
54 })
55 }
56
57 pub fn apply(&self, img: &mut IndexedImage) {
60 if (img.width, img.height) != (self.width, self.height) {
61 return;
62 }
63 let (cw, ch) = (self.atlas.cell_w, self.atlas.cell_h);
64 let w = img.width as usize;
65 let x0 = self.x.saturating_sub(cw);
67 let x1 = (self.x + (self.chars + 1) * cw).min(img.width);
68 let y1 = (self.y + ch).min(img.height);
69 for py in self.y..y1 {
70 for px in x0..x1 {
71 let i = py as usize * w + px as usize;
72 img.pixels[i] = band_of(img.pixels[i]);
73 }
74 }
75 for c in 0..self.chars {
76 let cx = self.x + c * cw;
77 for (k, &a) in self.atlas.mask(c as u8).iter().enumerate() {
78 let level = ((u32::from(a) * STRENGTH + 127) / 255) as u8;
79 let (px, py) = (cx + k as u32 % cw, self.y + k as u32 / cw);
80 if level == 0 || px >= img.width || py >= img.height {
81 continue;
82 }
83 let i = py as usize * w + px as usize;
84 img.pixels[i] = IndexedPalette::ink(img.pixels[i], level);
85 }
86 }
87 }
88}
89
90fn band_of(index: u8) -> u8 {
93 if index < INK_BASE {
94 index
95 } else if index < EMBER_BASE {
96 (index - INK_BASE) / LEVELS
97 } else if index < SPRITE_BASE {
98 (index - EMBER_BASE) / LEVELS
99 } else {
100 0
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 fn filled(w: u32, h: u32, index: u8) -> IndexedImage {
109 IndexedImage {
110 width: w,
111 height: h,
112 pixels: vec![index; (w * h) as usize],
113 }
114 }
115
116 #[test]
117 fn signs_the_bottom_right_in_the_backgrounds_own_ink() {
118 let mut img = filled(1920, 1080, IndexedPalette::ink(5, 6));
119 let sig = Signature::new(1920, 1080, SIGNATURE).expect("room for it");
120 sig.apply(&mut img);
121 let (w, h) = (1920usize, 1080usize);
122 let changed: Vec<usize> = (0..w * h)
123 .filter(|&i| img.pixels[i] != IndexedPalette::ink(5, 6))
124 .collect();
125 assert!(!changed.is_empty());
126 for &i in &changed {
127 let (x, y) = (i % w, i / w);
128 assert!(x > w / 2 && y > h * 9 / 10, "({x}, {y})");
130 let p = img.pixels[i];
132 assert!(
133 p == 5
134 || (IndexedPalette::ink(5, 1)..=IndexedPalette::ink(5, STRENGTH as u8))
135 .contains(&p),
136 "{p}"
137 );
138 }
139 assert!(changed.iter().any(|&i| img.pixels[i] != 5));
141 }
142
143 #[test]
144 fn small_images_go_unsigned() {
145 assert!(Signature::new(160, 90, SIGNATURE).is_none());
146 let sig = Signature::new(1920, 1080, SIGNATURE).unwrap();
148 let mut other = filled(1280, 720, 2);
149 sig.apply(&mut other);
150 assert!(other.pixels.iter().all(|&p| p == 2));
151 }
152
153 #[test]
154 fn every_entry_has_a_band() {
155 assert_eq!(band_of(3), 3);
156 assert_eq!(band_of(IndexedPalette::ink(6, 2)), 6);
157 assert_eq!(band_of(IndexedPalette::ember(1, 6)), 1);
158 assert_eq!(band_of(SPRITE_BASE + 3), 0);
159 }
160}