2019-01-12 23:55:56 +00:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
sync::{Arc, 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
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
|
|
|
pub enum TextStyle {
|
|
|
|
Body,
|
|
|
|
Button,
|
|
|
|
Heading,
|
|
|
|
// Monospace,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Fonts {
|
|
|
|
fonts: BTreeMap<TextStyle, Font>,
|
2019-01-16 23:09:12 +00:00
|
|
|
texture: Texture,
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Fonts {
|
|
|
|
pub fn new() -> Fonts {
|
|
|
|
let mut atlas = TextureAtlas::new(128, 8); // TODO: better default?
|
|
|
|
|
|
|
|
// Make one white pixel for use for various stuff:
|
|
|
|
let pos = atlas.allocate((1, 1));
|
2019-01-16 23:09:12 +00:00
|
|
|
atlas.texture_mut()[pos] = 255;
|
2019-01-12 23:55:56 +00:00
|
|
|
|
|
|
|
let atlas = Arc::new(Mutex::new(atlas));
|
|
|
|
|
|
|
|
// TODO: figure out a way to make the wasm smaller despite including a font.
|
2019-01-16 23:09:12 +00:00
|
|
|
// let typeface_data = include_bytes!("../fonts/ProggyClean.ttf"); // Use 13 for this. NOTHING ELSE.
|
|
|
|
// let typeface_data = include_bytes!("../fonts/DejaVuSans.ttf");
|
|
|
|
let typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf");
|
2019-01-12 23:55:56 +00:00
|
|
|
|
|
|
|
let mut fonts = BTreeMap::new();
|
2019-01-16 23:09:12 +00:00
|
|
|
fonts.insert(TextStyle::Body, Font::new(atlas.clone(), typeface_data, 20));
|
2019-01-12 23:55:56 +00:00
|
|
|
fonts.insert(TextStyle::Button, fonts[&TextStyle::Body].clone());
|
2019-01-16 23:09:12 +00:00
|
|
|
fonts.insert(
|
|
|
|
TextStyle::Heading,
|
|
|
|
Font::new(atlas.clone(), typeface_data, 30),
|
|
|
|
);
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
let texture = atlas.lock().unwrap().clone().texture().clone();
|
2019-01-12 23:55:56 +00:00
|
|
|
|
|
|
|
Fonts { fonts, texture }
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|