diff options
author | Daniel Schadt <kingdread@gmx.de> | 2019-12-25 01:49:22 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2019-12-25 01:49:22 +0100 |
commit | e3591f9f2ba4202ae570bda457e0a9e43e91b9e7 (patch) | |
tree | 97e3fcde57e57bf88cc79062ddbe9935263e1a72 | |
parent | 12d00d3cfed39c7bcabd0ded425abf39d402e91b (diff) | |
download | kondou-e3591f9f2ba4202ae570bda457e0a9e43e91b9e7.tar.gz kondou-e3591f9f2ba4202ae570bda457e0a9e43e91b9e7.tar.bz2 kondou-e3591f9f2ba4202ae570bda457e0a9e43e91b9e7.zip |
allow the spec names to be aligned center/right
-rw-r--r-- | src/render.rs | 31 | ||||
-rw-r--r-- | 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<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 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<bool>, pub skill_offset: Option<u32>, pub skill_alignment: Option<Alignment>, + pub specialization_name_alignment: Option<Alignment>, } impl UserOptions { @@ -125,6 +126,7 @@ impl UserOptions { render_specialization_names, skill_offset, skill_alignment, + specialization_name_alignment, } Ok(result) |