From e63a28abb72cb126cbc8a9a7dbad131a689967d1 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 6 Aug 2019 00:09:41 +0200 Subject: [PATCH] draw: lazy_static initialize fonts --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/backlight.rs | 2 +- src/battery.rs | 2 +- src/calendar.rs | 28 +++++++++++++--------------- src/clock.rs | 4 ++-- src/draw.rs | 28 +++++++++++----------------- src/launcher.rs | 8 ++++---- src/sound.rs | 2 +- 9 files changed, 36 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 955e8ce..60c479f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -413,6 +413,7 @@ dependencies = [ "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "dbus 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "fuzzy-matcher 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libpulse-binding 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "memmap 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index ee8c0d2..8c616b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ wayland-protocols = { version = "0.23", features = ["client", "unstable_protocol libpulse-binding = { version = "2.6.0"} dbus = "0.6" atty = "0.2" -fuzzy-matcher = "0.2" \ No newline at end of file +fuzzy-matcher = "0.2" +lazy_static = "1.3.0" \ No newline at end of file diff --git a/src/backlight.rs b/src/backlight.rs index f9a8193..7e5ac42 100644 --- a/src/backlight.rs +++ b/src/backlight.rs @@ -104,7 +104,7 @@ impl ModuleImpl for Backlight { buf.memset(bg); let c = Color::new(1.0, 1.0, 1.0, 1.0); draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 0, 128, 24))?, bg, &c, diff --git a/src/battery.rs b/src/battery.rs index 7f22ceb..ef5cd54 100644 --- a/src/battery.rs +++ b/src/battery.rs @@ -196,7 +196,7 @@ impl ModuleImpl for UpowerBattery { let inner = self.inner.lock().unwrap(); draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 0, 128, 24))?, bg, &text_color, diff --git a/src/calendar.rs b/src/calendar.rs index a74b628..00561af 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -17,8 +17,6 @@ fn draw_month( orig: &Date, time: &Date, ) -> Result<(i32, i32, i32, i32), ::std::io::Error> { - buf.memset(background_color); - let mut time = time.clone(); let mut y_off = 1; let mut done = false; @@ -43,7 +41,7 @@ fn draw_month( // Draw the of the month // draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 0, 304, 72))?, background_color, &Color::new(1.0, 1.0, 1.0, 1.0), @@ -53,7 +51,7 @@ fn draw_month( if time.year() != orig.year() { draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((320, 0, 64, 32))?, background_color, &Color::new(0.8, 0.8, 0.8, 1.0), @@ -79,7 +77,7 @@ fn draw_month( }; draw_text( - DEJAVUSANS_MONO, + &DEJAVUSANS_MONO, &mut buf.subdimensions((idx * 48 + 4, (y_off * 32) + 64, 32, 16))?, background_color, &Color::new(1.0, 1.0, 1.0, 1.0), @@ -107,7 +105,7 @@ fn draw_month( // let wk = time.iso_week(); draw_text( - DEJAVUSANS_MONO, + &DEJAVUSANS_MONO, &mut buf.subdimensions((0 * 48, (y_off * 32) + 64, 38, 32))?, background_color, &Color::new(0.75, 0.75, 0.75, 1.0), @@ -126,7 +124,7 @@ fn draw_month( Color::new(0.5, 0.5, 0.5, 1.0) }; draw_text( - DEJAVUSANS_MONO, + &DEJAVUSANS_MONO, &mut buf.subdimensions((x_pos * 48, (y_off * 32) + 64, 38, 32))?, background_color, &c, @@ -165,6 +163,7 @@ impl ModuleImpl for Calendar { background_color: &Color, time: &DateTime, ) -> Result, ::std::io::Error> { + buf.memset(background_color); let time = time.date(); let mut t = time.with_day(1).unwrap(); let o = (self.offset / 100.0) as i32; @@ -185,31 +184,30 @@ impl ModuleImpl for Calendar { .with_month((month + 1) as u32) .unwrap(); } - let mut damage: Vec<(i32, i32, i32, i32)> = Vec::new(); - damage.push(draw_month( + draw_month( &mut buf.subdimensions((0, 0, 384, 344))?, background_color, &time, &t.pred().with_day(1).unwrap(), - )?); - damage.push(draw_month( + )?; + draw_month( &mut buf.subdimensions((448, 0, 384, 344))?, background_color, &time, &t, - )?); + )?; let n = if t.month() == 12 { t.with_year(t.year() + 1).unwrap().with_month(1).unwrap() } else { t.with_month(t.month() + 1).unwrap() }; - damage.push(draw_month( + draw_month( &mut buf.subdimensions((896, 0, 384, 344))?, background_color, &time, &n, - )?); - Ok(damage) + )?; + Ok(vec![buf.get_signed_bounds()]) } fn update(&mut self, time: &DateTime, force: bool) -> Result { diff --git a/src/clock.rs b/src/clock.rs index 7d1daae..00d0a33 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -45,7 +45,7 @@ impl ModuleImpl for Clock { buf.memset(bg); draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 0, 448, 64))?, bg, &Color::new(1.0, 1.0, 1.0, 1.0), @@ -60,7 +60,7 @@ impl ModuleImpl for Clock { )?; draw_text_fixed_width( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 64, 288 * 2 + 64, 256))?, bg, &Color::new(1.0, 1.0, 1.0, 1.0), diff --git a/src/draw.rs b/src/draw.rs index 005d36b..b64f243 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -1,28 +1,30 @@ use crate::buffer::Buffer; use crate::color::Color; use rusttype::{point, Font, Scale}; +use lazy_static::lazy_static; -pub static DEJAVUSANS_MONO: &'static [u8] = include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf"); -pub static ROBOTO_REGULAR: &'static [u8] = include_bytes!("../fonts/Roboto-Regular.ttf"); +pub static DEJAVUSANS_MONO_FONT_DATA: &'static [u8] = include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf"); +pub static ROBOTO_REGULAR_FONT_DATA: &'static [u8] = include_bytes!("../fonts/Roboto-Regular.ttf"); + +lazy_static! { + pub static ref DEJAVUSANS_MONO: Font<'static> = Font::from_bytes(DEJAVUSANS_MONO_FONT_DATA as &[u8]).expect("error constructing DejaVuSansMono"); + pub static ref ROBOTO_REGULAR: Font<'static> = Font::from_bytes(ROBOTO_REGULAR_FONT_DATA as &[u8]).expect("error constructing Roboto-Regular"); +} pub fn draw_text( - font_data: &'static [u8], + font: &Font, buf: &mut Buffer, background_color: &Color, color: &Color, size: f32, s: &str, ) -> Result<(u32, u32), ::std::io::Error> { - // Load the font - // This only succeeds if collection consists of one font - let font = Font::from_bytes(font_data as &[u8]).expect("Error constructing Font"); // The font size to use let scale = Scale::uniform(size); let v_metrics = font.v_metrics(scale); - // layout the glyphs in a line with 20 pixels padding let glyphs: Vec<_> = font .layout(s, scale, point(0.0, v_metrics.ascent)) .collect(); @@ -59,23 +61,19 @@ pub fn draw_text( } pub fn draw_text_individual_colors( - font_data: &'static [u8], + font: &Font, buf: &mut Buffer, background_color: &Color, color: &[Color], size: f32, s: &str, ) -> Result<(u32, u32), ::std::io::Error> { - // Load the font - // This only succeeds if collection consists of one font - let font = Font::from_bytes(font_data as &[u8]).expect("Error constructing Font"); // The font size to use let scale = Scale::uniform(size); let v_metrics = font.v_metrics(scale); - // layout the glyphs in a line with 20 pixels padding let glyphs: Vec<_> = font .layout(s, scale, point(0.0, v_metrics.ascent)) .collect(); @@ -114,7 +112,7 @@ pub fn draw_text_individual_colors( } pub fn draw_text_fixed_width( - font_data: &'static [u8], + font: &Font, buf: &mut Buffer, background_color: &Color, color: &Color, @@ -122,16 +120,12 @@ pub fn draw_text_fixed_width( distances: &[u32], s: &str, ) -> Result<(), ::std::io::Error> { - // Load the font - // This only succeeds if collection consists of one font - let font = Font::from_bytes(font_data as &[u8]).expect("Error constructing Font"); // The font size to use let scale = Scale::uniform(size); let v_metrics = font.v_metrics(scale); - // layout the glyphs in a line with 20 pixels padding let glyphs: Vec<_> = font .layout(s, scale, point(0.0, v_metrics.ascent)) .collect(); diff --git a/src/launcher.rs b/src/launcher.rs index 8bd12a2..d5a1ebb 100644 --- a/src/launcher.rs +++ b/src/launcher.rs @@ -56,7 +56,7 @@ impl ModuleImpl for Launcher { buf.memset(bg); draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 0, 128, 32))?, bg, &Color::new(0.75, 0.75, 0.75, 1.0), @@ -68,7 +68,7 @@ impl ModuleImpl for Launcher { let mut width_remaining: i32 = 1280 - 64; if self.matches.len() == 0 && self.cur.len() > 0 { draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf .subdimensions((64, 0, width_remaining as u32, 32)) .unwrap(), @@ -95,10 +95,10 @@ impl ModuleImpl for Launcher { colors.push(Color::new(0.75, 0.75, 0.75, 1.0)); } } - draw_text_individual_colors(ROBOTO_REGULAR, &mut b, bg, &colors, 32.0, &m)? + draw_text_individual_colors(&ROBOTO_REGULAR, &mut b, bg, &colors, 32.0, &m)? } else { draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut b, bg, &Color::new(0.5, 0.5, 0.5, 1.0), diff --git a/src/sound.rs b/src/sound.rs index 1d6da96..e1cef37 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -437,7 +437,7 @@ impl ModuleImpl for PulseAudio { Color::new(1.0, 1.0, 1.0, 1.0) }; draw_text( - ROBOTO_REGULAR, + &ROBOTO_REGULAR, &mut buf.subdimensions((0, 0, 128, 24))?, bg, &Color::new(1.0, 1.0, 1.0, 1.0),