diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 79 |
1 files changed, 67 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index fa97c35..49ee13d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,7 +56,7 @@ struct Dock { window: Window, is_visible: bool, bounds: Arc<Rectangle<u16>>, - font: Arc<Font>, + fonts: Arc<Mutex<FontList>>, baseline_position: i16, facilities: Arc<Mutex<FacilityMap>>, modules: Vec<(Arc<Mutex<Box<dyn Module>>>, ModuleFraming)>, @@ -176,12 +176,16 @@ impl Dock { ], })?; - let font = Font::new(Arc::clone(&display), + let fonts = Arc::new(Mutex::new(FontList::new(Arc::clone(&display))?)); + fonts.lock()?.append( "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?; + fonts.lock()?.append("Symbola:size=18")?; + fonts.lock()?.append("Sazanami Mincho:size=18")?; - let font_height = font.ascent() + font.descent(); + let primary_font = fonts.lock()?.primary_font()?; + let font_height = primary_font.ascent() + primary_font.descent(); let vertical_padding = (dock_bounds.height - font_height) / 2; - let baseline_position = font.ascent() as i16 + let baseline_position = primary_font.ascent() as i16 + vertical_padding as i16 + configuration.baseline_offset as i16; @@ -221,7 +225,7 @@ impl Dock { window, configuration, is_visible: false, bounds: Arc::new(dock_bounds), - font, + fonts, baseline_position, facilities, modules, @@ -482,28 +486,79 @@ impl Dock { { let context = FontGraphicContext::new(self.window())?; let color = &self.configuration.foreground; - let text = self.font.text_span(text)?; + + let mut line = TextLine::new(Arc::clone(&self.fonts))?; + line.append_text(text)?; let mut location = Point::<i32>::zero(); - location.x += text.draw_offset().x as i32; location.y += self.baseline_position as i32; match alignment { Alignment::Left => { location.x += open_parcel.left as i32; + } + Alignment::Right => { + location.x += open_parcel.right as i32; + } + } - open_parcel.left += text.draw_size().width as u32; - open_parcel.left += self.configuration.intermodule_padding as u32; + /* A reversed iterator is a different type, so we need two of this loop. */ + match alignment { + Alignment::Left => { + for span in line.spans() { + self.layout_and_draw_span(open_parcel, &mut location, span, + &context, color, alignment)?; + } } Alignment::Right => { - location.x += open_parcel.right as i32 - text.draw_size().width as i32; + for span in line.spans().rev() { + self.layout_and_draw_span(open_parcel, &mut location, span, + &context, color, alignment)?; + } + } + } - open_parcel.right -= text.draw_size().width as u32; + match alignment { + Alignment::Left => { + open_parcel.left += self.configuration.intermodule_padding as u32; + } + Alignment::Right => { open_parcel.right -= self.configuration.intermodule_padding as u32; } } - text.draw(&context, color, location)?; + Ok(()) + } + + fn layout_and_draw_span(&self, open_parcel: &mut Parcel, + location: &mut Point<i32>, span: &TextSpan, + context: &FontGraphicContext, color: &Color, + alignment: Alignment) + -> Result<()> + { + match alignment { + Alignment::Left => { + location.x += span.draw_offset().x as i32; + } + Alignment::Right => { + location.x -= span.draw_size().width as i32; + } + } + + span.draw(&context, color, &location)?; + + match alignment { + Alignment::Left => { + location.x += span.draw_size().width as i32; + + open_parcel.left += span.draw_size().width as u32; + } + Alignment::Right => { + location.x -= span.draw_offset().x as i32; + + open_parcel.right -= span.draw_size().width as u32; + } + } Ok(()) } |