Add support for monospace font

This commit is contained in:
Emil Ernerfeldt 2019-12-02 20:08:49 +01:00
parent 100d4e328f
commit 157de8a966
3 changed files with 48 additions and 31 deletions

View file

@ -9,7 +9,7 @@ use crate::{
style::Style, style::Style,
types::{GuiInput, PaintCmd}, types::{GuiInput, PaintCmd},
widgets::*, widgets::*,
FontSizes, Fonts, Mesh, RawInput, Texture, FontDefinitions, Fonts, Mesh, RawInput, Texture,
}; };
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
@ -34,8 +34,9 @@ fn show_style(style: &mut Style, gui: &mut Region) {
gui.add(Slider::f32(&mut style.line_width, 0.0, 10.0).text("line_width")); gui.add(Slider::f32(&mut style.line_width, 0.0, 10.0).text("line_width"));
} }
fn show_font_sizes(font_sizes: &mut FontSizes, gui: &mut Region) { fn show_font_definitions(font_definitions: &mut FontDefinitions, gui: &mut Region) {
for (text_style, mut size) in font_sizes { for (text_style, (_family, mut size)) in font_definitions {
// TODO: radiobutton for family
gui.add(Slider::f32(&mut size, 4.0, 40.0).text(format!("{:?}", text_style))); gui.add(Slider::f32(&mut size, 4.0, 40.0).text(format!("{:?}", text_style)));
} }
} }
@ -171,13 +172,14 @@ impl Emigui {
}); });
region.foldable("Fonts", |gui| { region.foldable("Fonts", |gui| {
let old_font_sizes = self.data.fonts.sizes(); let old_font_definitions = self.data.fonts.definitions();
let mut new_font_sizes = old_font_sizes.clone(); let mut new_font_definitions = old_font_definitions.clone();
show_font_sizes(&mut new_font_sizes, gui); show_font_definitions(&mut new_font_definitions, gui);
show_font_texture(self.texture(), gui); show_font_texture(self.texture(), gui);
if *old_font_sizes != new_font_sizes { if *old_font_definitions != new_font_definitions {
let mut new_data = (*self.data).clone(); let mut new_data = (*self.data).clone();
let fonts = Fonts::from_sizes(new_font_sizes, self.data.input.pixels_per_point); let fonts =
Fonts::from_definitions(new_font_definitions, self.data.input.pixels_per_point);
new_data.fonts = Arc::new(fonts); new_data.fonts = Arc::new(fonts);
self.data = Arc::new(new_data); self.data = Arc::new(new_data);
} }

View file

@ -15,44 +15,51 @@ pub enum TextStyle {
Body, Body,
Button, Button,
Heading, Heading,
// Monospace, Monospace,
} }
pub type FontSizes = BTreeMap<TextStyle, f32>; #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum FontFamily {
Monospace,
VariableWidth,
}
pub type FontDefinitions = BTreeMap<TextStyle, (FontFamily, f32)>;
pub struct Fonts { pub struct Fonts {
pixels_per_point: f32, pixels_per_point: f32,
sizes: FontSizes, definitions: FontDefinitions,
fonts: BTreeMap<TextStyle, Font>, fonts: BTreeMap<TextStyle, Font>,
texture: Texture, texture: Texture,
} }
impl Fonts { impl Fonts {
pub fn new(pixels_per_point: f32) -> Fonts { pub fn new(pixels_per_point: f32) -> Fonts {
let mut sizes = FontSizes::new(); let mut definitions = FontDefinitions::new();
sizes.insert(TextStyle::Body, 16.0); definitions.insert(TextStyle::Body, (FontFamily::VariableWidth, 16.0));
sizes.insert(TextStyle::Button, 18.0); definitions.insert(TextStyle::Button, (FontFamily::VariableWidth, 18.0));
sizes.insert(TextStyle::Heading, 28.0); definitions.insert(TextStyle::Heading, (FontFamily::VariableWidth, 28.0));
Fonts::from_sizes(sizes, pixels_per_point) definitions.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0));
Fonts::from_definitions(definitions, pixels_per_point)
} }
pub fn from_sizes(sizes: FontSizes, pixels_per_point: f32) -> Fonts { pub fn from_definitions(definitions: FontDefinitions, pixels_per_point: f32) -> Fonts {
let mut fonts = Fonts { let mut fonts = Fonts {
pixels_per_point, pixels_per_point,
sizes: Default::default(), definitions: Default::default(),
fonts: Default::default(), fonts: Default::default(),
texture: Default::default(), texture: Default::default(),
}; };
fonts.set_sizes(sizes); fonts.set_sizes(definitions);
fonts fonts
} }
pub fn sizes(&self) -> &FontSizes { pub fn definitions(&self) -> &FontDefinitions {
&self.sizes &self.definitions
} }
pub fn set_sizes(&mut self, sizes: FontSizes) { pub fn set_sizes(&mut self, definitions: FontDefinitions) {
if self.sizes == sizes { if self.definitions == definitions {
return; return;
} }
@ -65,14 +72,22 @@ impl Fonts {
let atlas = Arc::new(Mutex::new(atlas)); let atlas = Arc::new(Mutex::new(atlas));
// TODO: figure out a way to make the wasm smaller despite including a font. Zip it? // TODO: figure out a way to make the wasm smaller despite including a font. Zip it?
let typeface_data = include_bytes!("../fonts/Comfortaa-Regular.ttf"); let monospae_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/ProggyClean.ttf"); // Use 13 for this. NOTHING ELSE. // let monospae_typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf");
// let typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf");
self.sizes = sizes.clone(); let variable_typeface_data = include_bytes!("../fonts/Comfortaa-Regular.ttf");
self.fonts = sizes // let variable_typeface_data = include_bytes!("../fonts/DejaVuSans.ttf");
self.definitions = definitions.clone();
self.fonts = definitions
.into_iter() .into_iter()
.map(|(text_style, size)| { .map(|(text_style, (family, size))| {
let typeface_data: &[u8] = match family {
FontFamily::Monospace => monospae_typeface_data,
FontFamily::VariableWidth => variable_typeface_data,
};
( (
text_style, text_style,
Font::new(atlas.clone(), typeface_data, size, self.pixels_per_point), Font::new(atlas.clone(), typeface_data, size, self.pixels_per_point),

View file

@ -21,7 +21,7 @@ pub mod widgets;
pub use crate::{ pub use crate::{
color::Color, color::Color,
emigui::Emigui, emigui::Emigui,
fonts::{FontSizes, Fonts, TextStyle}, fonts::{FontDefinitions, Fonts, TextStyle},
layout::{Align, Region}, layout::{Align, Region},
math::*, math::*,
mesher::{Mesh, Vertex}, mesher::{Mesh, Vertex},