diff options
Diffstat (limited to 'src/render.rs')
-rw-r--r-- | src/render.rs | 31 |
1 files changed, 19 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<RgbaImage, RenderError> { - 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<I>( ); } +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::<f32>() 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 |