clock: Use font metrics for size

This commit is contained in:
Kenny Levinsen
2020-06-14 17:01:42 +02:00
parent 48985c470a
commit 2f61da8690
3 changed files with 39 additions and 10 deletions
+5 -2
View File
@@ -111,11 +111,14 @@ impl Widget {
.map(|x| x.unwrap()) .map(|x| x.unwrap())
.collect(), .collect(),
)), )),
Widget::Clock { font, font_size } => Some(widgets::clock::Clock::new( Widget::Clock { font, font_size } => match widgets::clock::Clock::new(
time, time,
get_font(&font.or_else(|| Some("sans".to_string())).unwrap(), &fonts), get_font(&font.or_else(|| Some("sans".to_string())).unwrap(), &fonts),
font_size, font_size,
)), ) {
Ok(w) => Some(w),
Err(_) => None,
},
Widget::Date { font, font_size } => Some(widgets::date::Date::new( Widget::Date { font, font_size } => Some(widgets::date::Date::new(
time, time,
get_font(&font.or_else(|| Some("sans".to_string())).unwrap(), &fonts), get_font(&font.or_else(|| Some("sans".to_string())).unwrap(), &fonts),
+21
View File
@@ -124,6 +124,27 @@ impl<'a> Font<'a> {
Ok((x_off as u32, self.size as u32)) Ok((x_off as u32, self.size as u32))
} }
pub fn auto_widest(&mut self, s: &str) -> Result<u32, ::std::io::Error> {
self.add_str_to_cache(s);
let mut max = 0;
for ch in s.chars() {
let glyph = match self.glyphs.get(&ch) {
Some(glyph) => glyph,
None => {
return Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
format!("glyph for {:} not in cache", ch),
))
}
};
let width = glyph.dimensions.0 as i32 + glyph.origin.0;
if width > max {
max = width
}
}
Ok(max as u32)
}
pub fn auto_draw_text( pub fn auto_draw_text(
&mut self, &mut self,
buf: &mut Buffer, buf: &mut Buffer,
+13 -8
View File
@@ -11,18 +11,25 @@ pub struct Clock<'a> {
cur_time: NaiveDateTime, cur_time: NaiveDateTime,
clock_cache: Font<'a>, clock_cache: Font<'a>,
size: f32, size: f32,
digit: u32,
colon: u32,
} }
impl<'a> Clock<'a> { impl<'a> Clock<'a> {
pub fn new(time: NaiveDateTime, font: FontRef, size: f32) -> Box<Clock> { pub fn new(time: NaiveDateTime, font: FontRef, size: f32) -> ::std::io::Result<Box<Clock>> {
let mut clock_cache = Font::new(font, size); let mut clock_cache = Font::new(font, size);
clock_cache.add_str_to_cache("0123456789:"); clock_cache.add_str_to_cache("0123456789:");
Box::new(Clock { let digit = clock_cache.auto_widest("123456789")?;
let colon = (clock_cache.auto_widest(":")? as f32 * 1.25) as u32;
Ok(Box::new(Clock {
cur_time: time, cur_time: time,
clock_cache, clock_cache,
size, size,
}) digit,
colon,
}))
} }
} }
@@ -38,9 +45,7 @@ impl<'a> Widget for Clock<'a> {
fn enter(&mut self) {} fn enter(&mut self) {}
fn leave(&mut self) {} fn leave(&mut self) {}
fn size(&self) -> (u32, u32) { fn size(&self) -> (u32, u32) {
let digit = (self.size * 0.45).ceil() as u32; (self.digit * 4 + self.colon, self.size.ceil() as u32)
let colon = (self.size * 0.20).ceil() as u32;
(digit * 4 + colon, self.size.ceil() as u32)
} }
fn draw( fn draw(
@@ -62,8 +67,8 @@ impl<'a> Widget for Clock<'a> {
let buf = &mut ctx.buf.subdimensions((pos.0, pos.1, width, height))?; let buf = &mut ctx.buf.subdimensions((pos.0, pos.1, width, height))?;
buf.memset(ctx.bg); buf.memset(ctx.bg);
let digit = (self.size * 0.45).ceil() as u32; let digit = self.digit;
let colon = (self.size * 0.20).ceil() as u32; let colon = self.colon;
self.clock_cache.draw_text_fixed_width( self.clock_cache.draw_text_fixed_width(
buf, buf,
ctx.bg, ctx.bg,