draw: lazy_static initialize fonts

This commit is contained in:
Kenny Levinsen
2019-08-06 00:09:41 +02:00
parent 11fd9a79c3
commit e63a28abb7
9 changed files with 36 additions and 42 deletions
Generated
+1
View File
@@ -413,6 +413,7 @@ dependencies = [
"chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
+1
View File
@@ -18,3 +18,4 @@ libpulse-binding = { version = "2.6.0"}
dbus = "0.6" dbus = "0.6"
atty = "0.2" atty = "0.2"
fuzzy-matcher = "0.2" fuzzy-matcher = "0.2"
lazy_static = "1.3.0"
+1 -1
View File
@@ -104,7 +104,7 @@ impl ModuleImpl for Backlight {
buf.memset(bg); buf.memset(bg);
let c = Color::new(1.0, 1.0, 1.0, 1.0); let c = Color::new(1.0, 1.0, 1.0, 1.0);
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 128, 24))?, &mut buf.subdimensions((0, 0, 128, 24))?,
bg, bg,
&c, &c,
+1 -1
View File
@@ -196,7 +196,7 @@ impl ModuleImpl for UpowerBattery {
let inner = self.inner.lock().unwrap(); let inner = self.inner.lock().unwrap();
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 128, 24))?, &mut buf.subdimensions((0, 0, 128, 24))?,
bg, bg,
&text_color, &text_color,
+13 -15
View File
@@ -17,8 +17,6 @@ fn draw_month(
orig: &Date<Local>, orig: &Date<Local>,
time: &Date<Local>, time: &Date<Local>,
) -> Result<(i32, i32, i32, i32), ::std::io::Error> { ) -> Result<(i32, i32, i32, i32), ::std::io::Error> {
buf.memset(background_color);
let mut time = time.clone(); let mut time = time.clone();
let mut y_off = 1; let mut y_off = 1;
let mut done = false; let mut done = false;
@@ -43,7 +41,7 @@ fn draw_month(
// Draw the of the month // Draw the of the month
// //
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 304, 72))?, &mut buf.subdimensions((0, 0, 304, 72))?,
background_color, background_color,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),
@@ -53,7 +51,7 @@ fn draw_month(
if time.year() != orig.year() { if time.year() != orig.year() {
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((320, 0, 64, 32))?, &mut buf.subdimensions((320, 0, 64, 32))?,
background_color, background_color,
&Color::new(0.8, 0.8, 0.8, 1.0), &Color::new(0.8, 0.8, 0.8, 1.0),
@@ -79,7 +77,7 @@ fn draw_month(
}; };
draw_text( draw_text(
DEJAVUSANS_MONO, &DEJAVUSANS_MONO,
&mut buf.subdimensions((idx * 48 + 4, (y_off * 32) + 64, 32, 16))?, &mut buf.subdimensions((idx * 48 + 4, (y_off * 32) + 64, 32, 16))?,
background_color, background_color,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),
@@ -107,7 +105,7 @@ fn draw_month(
// //
let wk = time.iso_week(); let wk = time.iso_week();
draw_text( draw_text(
DEJAVUSANS_MONO, &DEJAVUSANS_MONO,
&mut buf.subdimensions((0 * 48, (y_off * 32) + 64, 38, 32))?, &mut buf.subdimensions((0 * 48, (y_off * 32) + 64, 38, 32))?,
background_color, background_color,
&Color::new(0.75, 0.75, 0.75, 1.0), &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) Color::new(0.5, 0.5, 0.5, 1.0)
}; };
draw_text( draw_text(
DEJAVUSANS_MONO, &DEJAVUSANS_MONO,
&mut buf.subdimensions((x_pos * 48, (y_off * 32) + 64, 38, 32))?, &mut buf.subdimensions((x_pos * 48, (y_off * 32) + 64, 38, 32))?,
background_color, background_color,
&c, &c,
@@ -165,6 +163,7 @@ impl ModuleImpl for Calendar {
background_color: &Color, background_color: &Color,
time: &DateTime<Local>, time: &DateTime<Local>,
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> { ) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
buf.memset(background_color);
let time = time.date(); let time = time.date();
let mut t = time.with_day(1).unwrap(); let mut t = time.with_day(1).unwrap();
let o = (self.offset / 100.0) as i32; let o = (self.offset / 100.0) as i32;
@@ -185,31 +184,30 @@ impl ModuleImpl for Calendar {
.with_month((month + 1) as u32) .with_month((month + 1) as u32)
.unwrap(); .unwrap();
} }
let mut damage: Vec<(i32, i32, i32, i32)> = Vec::new(); draw_month(
damage.push(draw_month(
&mut buf.subdimensions((0, 0, 384, 344))?, &mut buf.subdimensions((0, 0, 384, 344))?,
background_color, background_color,
&time, &time,
&t.pred().with_day(1).unwrap(), &t.pred().with_day(1).unwrap(),
)?); )?;
damage.push(draw_month( draw_month(
&mut buf.subdimensions((448, 0, 384, 344))?, &mut buf.subdimensions((448, 0, 384, 344))?,
background_color, background_color,
&time, &time,
&t, &t,
)?); )?;
let n = if t.month() == 12 { let n = if t.month() == 12 {
t.with_year(t.year() + 1).unwrap().with_month(1).unwrap() t.with_year(t.year() + 1).unwrap().with_month(1).unwrap()
} else { } else {
t.with_month(t.month() + 1).unwrap() t.with_month(t.month() + 1).unwrap()
}; };
damage.push(draw_month( draw_month(
&mut buf.subdimensions((896, 0, 384, 344))?, &mut buf.subdimensions((896, 0, 384, 344))?,
background_color, background_color,
&time, &time,
&n, &n,
)?); )?;
Ok(damage) Ok(vec![buf.get_signed_bounds()])
} }
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> { fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
+2 -2
View File
@@ -45,7 +45,7 @@ impl ModuleImpl for Clock {
buf.memset(bg); buf.memset(bg);
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 448, 64))?, &mut buf.subdimensions((0, 0, 448, 64))?,
bg, bg,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),
@@ -60,7 +60,7 @@ impl ModuleImpl for Clock {
)?; )?;
draw_text_fixed_width( draw_text_fixed_width(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 64, 288 * 2 + 64, 256))?, &mut buf.subdimensions((0, 64, 288 * 2 + 64, 256))?,
bg, bg,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),
+11 -17
View File
@@ -1,28 +1,30 @@
use crate::buffer::Buffer; use crate::buffer::Buffer;
use crate::color::Color; use crate::color::Color;
use rusttype::{point, Font, Scale}; use rusttype::{point, Font, Scale};
use lazy_static::lazy_static;
pub static DEJAVUSANS_MONO: &'static [u8] = include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf"); pub static DEJAVUSANS_MONO_FONT_DATA: &'static [u8] = include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf");
pub static ROBOTO_REGULAR: &'static [u8] = include_bytes!("../fonts/Roboto-Regular.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( pub fn draw_text(
font_data: &'static [u8], font: &Font,
buf: &mut Buffer, buf: &mut Buffer,
background_color: &Color, background_color: &Color,
color: &Color, color: &Color,
size: f32, size: f32,
s: &str, s: &str,
) -> Result<(u32, u32), ::std::io::Error> { ) -> 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 // The font size to use
let scale = Scale::uniform(size); let scale = Scale::uniform(size);
let v_metrics = font.v_metrics(scale); let v_metrics = font.v_metrics(scale);
// layout the glyphs in a line with 20 pixels padding
let glyphs: Vec<_> = font let glyphs: Vec<_> = font
.layout(s, scale, point(0.0, v_metrics.ascent)) .layout(s, scale, point(0.0, v_metrics.ascent))
.collect(); .collect();
@@ -59,23 +61,19 @@ pub fn draw_text(
} }
pub fn draw_text_individual_colors( pub fn draw_text_individual_colors(
font_data: &'static [u8], font: &Font,
buf: &mut Buffer, buf: &mut Buffer,
background_color: &Color, background_color: &Color,
color: &[Color], color: &[Color],
size: f32, size: f32,
s: &str, s: &str,
) -> Result<(u32, u32), ::std::io::Error> { ) -> 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 // The font size to use
let scale = Scale::uniform(size); let scale = Scale::uniform(size);
let v_metrics = font.v_metrics(scale); let v_metrics = font.v_metrics(scale);
// layout the glyphs in a line with 20 pixels padding
let glyphs: Vec<_> = font let glyphs: Vec<_> = font
.layout(s, scale, point(0.0, v_metrics.ascent)) .layout(s, scale, point(0.0, v_metrics.ascent))
.collect(); .collect();
@@ -114,7 +112,7 @@ pub fn draw_text_individual_colors(
} }
pub fn draw_text_fixed_width( pub fn draw_text_fixed_width(
font_data: &'static [u8], font: &Font,
buf: &mut Buffer, buf: &mut Buffer,
background_color: &Color, background_color: &Color,
color: &Color, color: &Color,
@@ -122,16 +120,12 @@ pub fn draw_text_fixed_width(
distances: &[u32], distances: &[u32],
s: &str, s: &str,
) -> Result<(), ::std::io::Error> { ) -> 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 // The font size to use
let scale = Scale::uniform(size); let scale = Scale::uniform(size);
let v_metrics = font.v_metrics(scale); let v_metrics = font.v_metrics(scale);
// layout the glyphs in a line with 20 pixels padding
let glyphs: Vec<_> = font let glyphs: Vec<_> = font
.layout(s, scale, point(0.0, v_metrics.ascent)) .layout(s, scale, point(0.0, v_metrics.ascent))
.collect(); .collect();
+4 -4
View File
@@ -56,7 +56,7 @@ impl ModuleImpl for Launcher {
buf.memset(bg); buf.memset(bg);
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 128, 32))?, &mut buf.subdimensions((0, 0, 128, 32))?,
bg, bg,
&Color::new(0.75, 0.75, 0.75, 1.0), &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; let mut width_remaining: i32 = 1280 - 64;
if self.matches.len() == 0 && self.cur.len() > 0 { if self.matches.len() == 0 && self.cur.len() > 0 {
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf &mut buf
.subdimensions((64, 0, width_remaining as u32, 32)) .subdimensions((64, 0, width_remaining as u32, 32))
.unwrap(), .unwrap(),
@@ -95,10 +95,10 @@ impl ModuleImpl for Launcher {
colors.push(Color::new(0.75, 0.75, 0.75, 1.0)); 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 { } else {
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut b, &mut b,
bg, bg,
&Color::new(0.5, 0.5, 0.5, 1.0), &Color::new(0.5, 0.5, 0.5, 1.0),
+1 -1
View File
@@ -437,7 +437,7 @@ impl ModuleImpl for PulseAudio {
Color::new(1.0, 1.0, 1.0, 1.0) Color::new(1.0, 1.0, 1.0, 1.0)
}; };
draw_text( draw_text(
ROBOTO_REGULAR, &ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 128, 24))?, &mut buf.subdimensions((0, 0, 128, 24))?,
bg, bg,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),