egui/emigui/src/fonts.rs

125 lines
3.8 KiB
Rust
Raw Normal View History

2019-01-12 23:55:56 +00:00
use std::{
2019-01-17 17:03:39 +00:00
collections::{hash_map::DefaultHasher, BTreeMap},
hash::{Hash, Hasher},
2020-04-17 21:30:01 +00:00
sync::Arc,
2019-01-12 23:55:56 +00:00
};
2020-04-17 21:30:01 +00:00
use parking_lot::Mutex;
2019-01-16 23:09:12 +00:00
use crate::{
font::Font,
texture_atlas::{Texture, TextureAtlas},
};
2019-01-12 23:55:56 +00:00
/// TODO: rename
2019-01-17 17:03:39 +00:00
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
2019-01-12 23:55:56 +00:00
pub enum TextStyle {
Body,
Button,
Heading,
2019-12-02 19:08:49 +00:00
Monospace,
2019-01-12 23:55:56 +00:00
}
2019-12-02 19:08:49 +00:00
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum FontFamily {
Monospace,
VariableWidth,
}
pub type FontDefinitions = BTreeMap<TextStyle, (FontFamily, f32)>;
2019-01-17 17:03:39 +00:00
2020-04-12 10:07:51 +00:00
pub fn default_font_definitions() -> FontDefinitions {
let mut definitions = FontDefinitions::new();
definitions.insert(TextStyle::Body, (FontFamily::VariableWidth, 16.0));
definitions.insert(TextStyle::Button, (FontFamily::VariableWidth, 18.0));
definitions.insert(TextStyle::Heading, (FontFamily::VariableWidth, 28.0));
definitions.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0));
definitions
}
2019-01-12 23:55:56 +00:00
pub struct Fonts {
2019-01-19 16:10:28 +00:00
pixels_per_point: f32,
2019-12-02 19:08:49 +00:00
definitions: FontDefinitions,
2019-01-12 23:55:56 +00:00
fonts: BTreeMap<TextStyle, Font>,
2019-01-16 23:09:12 +00:00
texture: Texture,
2019-01-12 23:55:56 +00:00
}
impl Fonts {
2019-01-19 16:10:28 +00:00
pub fn new(pixels_per_point: f32) -> Fonts {
2020-04-12 10:07:51 +00:00
Fonts::from_definitions(default_font_definitions(), pixels_per_point)
2019-01-17 17:03:39 +00:00
}
2019-12-02 19:08:49 +00:00
pub fn from_definitions(definitions: FontDefinitions, pixels_per_point: f32) -> Fonts {
2019-01-17 17:03:39 +00:00
let mut fonts = Fonts {
2019-01-19 16:10:28 +00:00
pixels_per_point,
2019-12-02 19:08:49 +00:00
definitions: Default::default(),
2019-01-17 17:03:39 +00:00
fonts: Default::default(),
texture: Default::default(),
};
2019-12-02 19:08:49 +00:00
fonts.set_sizes(definitions);
2019-01-17 17:03:39 +00:00
fonts
}
2019-12-02 19:08:49 +00:00
pub fn definitions(&self) -> &FontDefinitions {
&self.definitions
2019-01-17 17:03:39 +00:00
}
2019-12-02 19:08:49 +00:00
pub fn set_sizes(&mut self, definitions: FontDefinitions) {
if self.definitions == definitions {
2019-01-17 17:03:39 +00:00
return;
}
let mut atlas = TextureAtlas::new(512, 8); // TODO: better default?
2019-01-12 23:55:56 +00:00
// Make the top left four pixels fully white:
let pos = atlas.allocate((2, 2));
assert_eq!(pos, (0, 0));
atlas.texture_mut()[(0, 0)] = 255;
atlas.texture_mut()[(0, 1)] = 255;
atlas.texture_mut()[(1, 0)] = 255;
atlas.texture_mut()[(1, 1)] = 255;
2019-01-12 23:55:56 +00:00
let atlas = Arc::new(Mutex::new(atlas));
2019-01-17 17:03:39 +00:00
// TODO: figure out a way to make the wasm smaller despite including a font. Zip it?
2019-12-02 19:08:49 +00:00
let monospae_typeface_data = include_bytes!("../fonts/ProggyClean.ttf"); // Use 13 for this. NOTHING ELSE.
2020-04-27 14:53:14 +00:00
// let monospae_typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf");
2019-12-02 19:08:49 +00:00
2020-04-27 14:53:14 +00:00
// let variable_typeface_data = include_bytes!("../fonts/Comfortaa-Regular.ttf"); // Funny, hard to read
let variable_typeface_data = include_bytes!("../fonts/DejaVuSans.ttf"); // Basic, boring
2019-12-02 19:08:49 +00:00
self.definitions = definitions.clone();
self.fonts = definitions
2019-01-17 17:03:39 +00:00
.into_iter()
2019-12-02 19:08:49 +00:00
.map(|(text_style, (family, size))| {
let typeface_data: &[u8] = match family {
FontFamily::Monospace => monospae_typeface_data,
FontFamily::VariableWidth => variable_typeface_data,
};
2019-01-19 16:10:28 +00:00
(
text_style,
Font::new(atlas.clone(), typeface_data, size, self.pixels_per_point),
)
})
2019-01-17 17:03:39 +00:00
.collect();
2020-04-17 21:30:01 +00:00
self.texture = atlas.lock().texture().clone();
2019-01-12 23:55:56 +00:00
2019-01-17 17:03:39 +00:00
let mut hasher = DefaultHasher::new();
self.texture.pixels.hash(&mut hasher);
self.texture.id = hasher.finish();
2019-01-12 23:55:56 +00:00
}
2019-01-16 23:09:12 +00:00
pub fn texture(&self) -> &Texture {
&self.texture
2019-01-12 23:55:56 +00:00
}
}
impl std::ops::Index<TextStyle> for Fonts {
type Output = Font;
fn index(&self, text_style: TextStyle) -> &Font {
&self.fonts[&text_style]
}
}