initial text impl
This commit is contained in:
57
src/layout/text.rs
Normal file
57
src/layout/text.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
|
||||
use image::{Rgba, RgbaImage};
|
||||
|
||||
use crate::{
|
||||
HashMap,
|
||||
layout::{TextureHandle, Textures},
|
||||
};
|
||||
|
||||
pub(crate) struct TextData {
|
||||
font_system: FontSystem,
|
||||
cache: SwashCache,
|
||||
}
|
||||
|
||||
impl Default for TextData {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
font_system: FontSystem::new(),
|
||||
cache: SwashCache::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TextData {
|
||||
pub fn draw(&mut self, content: &str, textures: &mut Textures) -> TextureHandle {
|
||||
let metrics = Metrics::new(30.0, 30.0);
|
||||
let mut buffer = Buffer::new(&mut self.font_system, metrics);
|
||||
let mut buffer = buffer.borrow_with(&mut self.font_system);
|
||||
buffer.set_text(content, &Attrs::new(), Shaping::Advanced);
|
||||
|
||||
// dawg what is this api ???
|
||||
let mut pixels = HashMap::new();
|
||||
let mut min_x = 0;
|
||||
let mut min_y = 0;
|
||||
let mut max_x = 0;
|
||||
let mut max_y = 0;
|
||||
buffer.draw(
|
||||
&mut self.cache,
|
||||
cosmic_text::Color::rgb(0xff, 0xff, 0xff),
|
||||
|x, y, _, _, color| {
|
||||
min_x = min_x.min(x);
|
||||
min_y = min_y.min(y);
|
||||
max_x = max_x.max(x);
|
||||
max_y = max_y.max(y);
|
||||
pixels.insert((x, y), Rgba(color.as_rgba()));
|
||||
},
|
||||
);
|
||||
let width = (max_x - min_x + 1) as u32;
|
||||
let height = (max_y - min_y + 1) as u32;
|
||||
let mut image = RgbaImage::new(width, height);
|
||||
for ((x, y), color) in pixels {
|
||||
let x = (x - min_x) as u32;
|
||||
let y = (y - min_y) as u32;
|
||||
image.put_pixel(x, y, color);
|
||||
}
|
||||
textures.add(image)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user