1use crate::geom::{Bounds, Point};
2use crate::view::Align;
3use embedded_graphics::mono_font::MonoFont;
4use embedded_graphics::pixelcolor::Rgb565;
5
6pub struct TextStyle<'a> {
7 pub halign: Align,
8 pub valign: Align,
9 pub underline: bool,
10 pub font: &'a MonoFont<'static>,
11 pub color: &'a Rgb565,
12}
13
14impl<'a> TextStyle<'a> {
15 pub fn new(font: &'a MonoFont<'static>, color: &'a Rgb565) -> TextStyle<'a> {
16 TextStyle {
17 font,
18 color,
19 underline: false,
20 valign: Align::Center,
21 halign: Align::Start,
22 }
23 }
24 pub fn with_underline(&self, underline: bool) -> Self {
25 TextStyle {
26 color: self.color,
27 font: self.font,
28 underline,
29 halign: self.halign,
30 valign: self.valign,
31 }
32 }
33 pub fn with_halign(&self, halign: Align) -> Self {
34 TextStyle {
35 color: self.color,
36 font: self.font,
37 underline: self.underline,
38 halign,
39 valign: self.valign,
40 }
41 }
42}
43
44pub trait DrawingContext {
45 fn fill_rect(&mut self, bounds: &Bounds, color: &Rgb565);
46 fn stroke_rect(&mut self, bounds: &Bounds, color: &Rgb565);
47 fn line(&mut self, start: &Point, end: &Point, color: &Rgb565);
48 fn fill_text(&mut self, bounds: &Bounds, text: &str, style: &TextStyle);
49 fn text(&mut self, text: &str, position: &Point, style: &TextStyle);
50 fn translate(&mut self, offset: &Point);
51}
52
53pub fn draw_centered_text(
54 ctx: &mut dyn DrawingContext,
55 text: &str,
56 bounds: &Bounds,
57 font: &MonoFont<'static>,
58 color: &Rgb565,
59) {
60 ctx.text(
61 text,
62 &bounds.center(),
63 &TextStyle {
64 font,
65 color,
66 valign: Align::Center,
67 halign: Align::Center,
68 underline: false,
69 },
70 )
71}