1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#![feature(random, string_remove_matches)]
use ab_glyph::{FontRef, PxScale};
use anyhow::Result;
use image::{DynamicImage, ImageBuffer, ImageEncoder, Rgba, codecs::qoi::QoiEncoder};
use imageproc::drawing::{draw_text_mut, text_size};
use std::{
hash::{Hash, Hasher},
io::Write,
};
pub fn generate_fallback(name: &str, output: &mut dyn Write) -> Result<()> {
let width = 1024;
let height = (width * 1000) / 707;
let mut image = ImageBuffer::<Rgba<f32>, Vec<f32>>::new(width, height);
let text = name;
let bg = random_accent(text, 0.4);
image.pixels_mut().for_each(|p| *p = bg);
let font = FontRef::try_from_slice(include_bytes!(
"/usr/share/fonts/cantarell/Cantarell-VF.otf"
))
.unwrap();
let font_bold = FontRef::try_from_slice(include_bytes!(
"/usr/share/fonts/TTF/OpenSans-CondensedExtraBold.ttf"
))
.unwrap();
let mut bgtext = text.to_string();
bgtext.remove_matches(&[',', ' ']);
let bgtext = bgtext.repeat(3);
let scale = PxScale { x: 1000., y: 1000. };
let (w, h) = text_size(scale, &font, &bgtext);
for i in -1..4 {
draw_text_mut(
&mut image,
random_accent(&text, 0.6),
width as i32 / 2 - w as i32 / 2 + i * h as i32,
i * h as i32 * 2 / 3,
scale,
&font_bold,
&bgtext,
);
}
image.enumerate_pixels_mut().for_each(|(_x, y, p)| {
let f = 1. - (y as f32 / height as f32) * 0.5;
p.0[0] *= f;
p.0[1] *= f;
p.0[2] *= f;
});
let scale = PxScale { x: 200., y: 200. };
let (w, _h) = text_size(scale, &font, text);
draw_text_mut(
&mut image,
Rgba([1., 1., 1., 1.]),
width as i32 / 2 - w as i32 / 2,
height as i32 * 3 / 4,
scale,
&font,
text,
);
let image = DynamicImage::from(image).to_rgb8();
QoiEncoder::new(output).write_image(
image.as_raw(),
image.width(),
image.height(),
image::ExtendedColorType::Rgb8,
)?;
Ok(())
}
struct XorshiftHasher(u64);
impl Hasher for XorshiftHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
for b in bytes {
self.0 = self
.0
.wrapping_add((*b as u64).wrapping_shl(24) + (*b as u64).wrapping_shl(56));
self.0 ^= self.0.wrapping_shl(13);
self.0 ^= self.0.wrapping_shr(7);
self.0 ^= self.0.wrapping_shl(17);
}
}
}
fn random_accent(text: &str, y: f32) -> Rgba<f32> {
let mut hasher = XorshiftHasher(0);
text.hash(&mut hasher);
let h = hasher.finish();
let mut u = (h >> 32) as u32 as f32 / u32::MAX as f32;
let mut v = (h) as u32 as f32 / u32::MAX as f32;
u *= 0.2;
v *= 0.2;
Rgba([y - u * 0.5 - v * 0.5, y + v, y + u, 1.])
}
#[test]
fn generate_fallback_test() {
generate_fallback("Hello world!", &mut Vec::new()).unwrap();
}
|