Allow floating point font sizes
This commit is contained in:
parent
616245c323
commit
fe3542a28d
2 changed files with 15 additions and 12 deletions
|
@ -57,19 +57,19 @@ const LAST_ASCII: usize = 126;
|
||||||
pub struct Font {
|
pub struct Font {
|
||||||
font: rusttype::Font<'static>,
|
font: rusttype::Font<'static>,
|
||||||
/// Maximum character height
|
/// Maximum character height
|
||||||
scale: usize,
|
scale: f32,
|
||||||
/// NUM_CHARS big
|
/// NUM_CHARS big
|
||||||
glyph_infos: Vec<GlyphInfo>,
|
glyph_infos: Vec<GlyphInfo>,
|
||||||
atlas: Arc<Mutex<TextureAtlas>>,
|
atlas: Arc<Mutex<TextureAtlas>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Font {
|
impl Font {
|
||||||
pub fn new(atlas: Arc<Mutex<TextureAtlas>>, font_data: &'static [u8], scale: usize) -> Font {
|
pub fn new(atlas: Arc<Mutex<TextureAtlas>>, font_data: &'static [u8], scale: f32) -> Font {
|
||||||
let font = rusttype::Font::from_bytes(font_data).expect("Error constructing Font");
|
let font = rusttype::Font::from_bytes(font_data).expect("Error constructing Font");
|
||||||
|
|
||||||
// println!(
|
// println!(
|
||||||
// "font.v_metrics: {:?}",
|
// "font.v_metrics: {:?}",
|
||||||
// font.v_metrics(Scale::uniform(scale as f32))
|
// font.v_metrics(Scale::uniform(scale))
|
||||||
// );
|
// );
|
||||||
|
|
||||||
let glyphs: Vec<_> = Self::supported_characters()
|
let glyphs: Vec<_> = Self::supported_characters()
|
||||||
|
@ -81,7 +81,7 @@ impl Font {
|
||||||
"Failed to find a glyph for the character '{}'",
|
"Failed to find a glyph for the character '{}'",
|
||||||
c
|
c
|
||||||
);
|
);
|
||||||
let glyph = glyph.scaled(Scale::uniform(scale as f32));
|
let glyph = glyph.scaled(Scale::uniform(scale));
|
||||||
glyph.positioned(point(0.0, 0.0))
|
glyph.positioned(point(0.0, 0.0))
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -141,7 +141,7 @@ impl Font {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_spacing(&self) -> f32 {
|
pub fn line_spacing(&self) -> f32 {
|
||||||
self.scale as f32
|
self.scale
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn supported_characters() -> impl Iterator<Item = char> {
|
pub fn supported_characters() -> impl Iterator<Item = char> {
|
||||||
|
@ -168,7 +168,7 @@ impl Font {
|
||||||
|
|
||||||
/// Returns the a single line of characters separated into words
|
/// Returns the a single line of characters separated into words
|
||||||
pub fn layout_single_line(&self, text: &str) -> Vec<TextFragment> {
|
pub fn layout_single_line(&self, text: &str) -> Vec<TextFragment> {
|
||||||
let scale = Scale::uniform(self.scale as f32);
|
let scale = Scale::uniform(self.scale);
|
||||||
|
|
||||||
let mut current_fragment = TextFragment {
|
let mut current_fragment = TextFragment {
|
||||||
x_offsets: vec![0.0],
|
x_offsets: vec![0.0],
|
||||||
|
@ -276,8 +276,8 @@ impl Font {
|
||||||
let texture_mut = atlas_lock.texture_mut();
|
let texture_mut = atlas_lock.texture_mut();
|
||||||
|
|
||||||
let max_width = 160;
|
let max_width = 160;
|
||||||
let scale = Scale::uniform(self.scale as f32);
|
let scale = Scale::uniform(self.scale);
|
||||||
let mut pixel_rows = vec![vec![0; max_width]; self.scale];
|
let mut pixel_rows = vec![vec![0; max_width]; self.scale.ceil() as usize];
|
||||||
let mut cursor_x = 0.0;
|
let mut cursor_x = 0.0;
|
||||||
let cursor_y = 0;
|
let cursor_y = 0;
|
||||||
let mut last_glyph_id = None;
|
let mut last_glyph_id = None;
|
||||||
|
@ -291,7 +291,7 @@ impl Font {
|
||||||
for row in pixel_rows {
|
for row in pixel_rows {
|
||||||
println!("{}", as_ascii(&row));
|
println!("{}", as_ascii(&row));
|
||||||
}
|
}
|
||||||
pixel_rows = vec![vec![0; max_width]; self.scale];
|
pixel_rows = vec![vec![0; max_width]; self.scale.ceil() as usize];
|
||||||
cursor_x = 0.0;
|
cursor_x = 0.0;
|
||||||
}
|
}
|
||||||
if let Some(uv) = glyph.uv {
|
if let Some(uv) = glyph.uv {
|
||||||
|
@ -343,7 +343,7 @@ mod tests {
|
||||||
let atlas = TextureAtlas::new(128, 8);
|
let atlas = TextureAtlas::new(128, 8);
|
||||||
let atlas = Arc::new(Mutex::new(atlas));
|
let atlas = Arc::new(Mutex::new(atlas));
|
||||||
let font_data = include_bytes!("../fonts/Roboto-Regular.ttf");
|
let font_data = include_bytes!("../fonts/Roboto-Regular.ttf");
|
||||||
let font = Font::new(atlas, font_data, 13);
|
let font = Font::new(atlas, font_data, 13.0);
|
||||||
font.debug_print_all_chars();
|
font.debug_print_all_chars();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,11 +38,14 @@ impl Fonts {
|
||||||
let typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf");
|
let typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf");
|
||||||
|
|
||||||
let mut fonts = BTreeMap::new();
|
let mut fonts = BTreeMap::new();
|
||||||
fonts.insert(TextStyle::Body, Font::new(atlas.clone(), typeface_data, 20));
|
fonts.insert(
|
||||||
|
TextStyle::Body,
|
||||||
|
Font::new(atlas.clone(), typeface_data, 20.0),
|
||||||
|
);
|
||||||
fonts.insert(TextStyle::Button, fonts[&TextStyle::Body].clone());
|
fonts.insert(TextStyle::Button, fonts[&TextStyle::Body].clone());
|
||||||
fonts.insert(
|
fonts.insert(
|
||||||
TextStyle::Heading,
|
TextStyle::Heading,
|
||||||
Font::new(atlas.clone(), typeface_data, 30),
|
Font::new(atlas.clone(), typeface_data, 30.0),
|
||||||
);
|
);
|
||||||
|
|
||||||
let texture = atlas.lock().unwrap().clone().texture().clone();
|
let texture = atlas.lock().unwrap().clone().texture().clone();
|
||||||
|
|
Loading…
Reference in a new issue