From e3591f9f2ba4202ae570bda457e0a9e43e91b9e7 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 25 Dec 2019 01:49:22 +0100 Subject: allow the spec names to be aligned center/right --- src/render.rs | 31 +++++++++++++++++++------------ src/useropts.rs | 2 ++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/render.rs b/src/render.rs index d4f9c49..3560b5a 100644 --- a/src/render.rs +++ b/src/render.rs @@ -70,6 +70,7 @@ pub struct RenderOptions { pub render_specialization_names: bool, pub skill_offset: u32, pub skill_alignment: Alignment, + pub specialization_name_alignment: Alignment, } impl Default for RenderOptions { @@ -93,6 +94,7 @@ impl Default for RenderOptions { render_specialization_names: true, skill_offset: 30, skill_alignment: Alignment::Center, + specialization_name_alignment: Alignment::Left, } } } @@ -312,23 +314,22 @@ impl<'r> Renderer<'r> { &mut self, specialization: &Specialization, ) -> Result { - let scale = self.options.text_size; - let mut buffer = ImageBuffer::from_pixel( - self.options.traitline_width, - scale, - self.options.background_color, - ); + let text = &specialization.name; + let scale = Scale { + x: self.options.text_size as f32, + y: self.options.text_size as f32, + }; + let width = text_width(text, &self.options.font, scale); + let mut buffer = + ImageBuffer::from_pixel(width, self.options.text_size, self.options.background_color); drawing::draw_text_mut( &mut buffer, self.options.text_color, 0, 0, - Scale { - x: scale as f32, - y: scale as f32, - }, + scale, &self.options.font, - &specialization.name, + text, ); Ok(buffer) } @@ -354,7 +355,7 @@ impl<'r> Renderer<'r> { let traitline = traitline.as_ref().unwrap(); if self.options.render_specialization_names { let header = self.render_specialization_name(&traitline.0)?; - images.push((Alignment::Left, header)); + images.push((self.options.specialization_name_alignment, header)); } let inner = self.render_traitline(&traitline)?; images.push((Alignment::Left, inner)); @@ -497,6 +498,12 @@ fn draw_thick_line( ); } +fn text_width(text: &str, font: &Font, scale: Scale) -> u32 { + font.glyphs_for(text.chars()) + .map(|glyph| glyph.scaled(scale).h_metrics().advance_width) + .sum::() as u32 +} + /// A helper structure representing a grid of squares. /// /// This can be used to calculate the centers of the traits that should be placed on the diff --git a/src/useropts.rs b/src/useropts.rs index 2e98926..b342839 100644 --- a/src/useropts.rs +++ b/src/useropts.rs @@ -86,6 +86,7 @@ pub struct UserOptions { pub render_specialization_names: Option, pub skill_offset: Option, pub skill_alignment: Option, + pub specialization_name_alignment: Option, } impl UserOptions { @@ -125,6 +126,7 @@ impl UserOptions { render_specialization_names, skill_offset, skill_alignment, + specialization_name_alignment, } Ok(result) -- cgit v1.2.3