From 11d98ad9fafc79cf6ef92ef1e7659d62e54d4269 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Dec 2019 03:35:47 +0100 Subject: center skills in output image --- src/render.rs | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/render.rs b/src/render.rs index 1b15c1c..126fbea 100644 --- a/src/render.rs +++ b/src/render.rs @@ -22,6 +22,13 @@ quick_error! { } } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum Alignment { + Left, + Center, + Right, +} + #[derive(Debug, Clone)] pub struct RenderOptions { skill_size: u32, @@ -40,6 +47,7 @@ pub struct RenderOptions { background_color: Rgba, render_specialization_names: bool, skill_offset: u32, + skill_alignment: Alignment, } impl Default for RenderOptions { @@ -62,6 +70,7 @@ impl Default for RenderOptions { background_color: Rgba([0, 0, 0, 0]), render_specialization_names: true, skill_offset: 30, + skill_alignment: Alignment::Center, } } } @@ -277,17 +286,17 @@ impl<'r> Renderer<'r> { self.merge_parts(&images) } - fn construct_parts(&mut self, build: &BuildTemplate) -> Result, RenderError> { - let mut images: Vec = Vec::new(); + fn construct_parts(&mut self, build: &BuildTemplate) -> Result, RenderError> { + let mut images: Vec<(Alignment, RgbaImage)> = Vec::new(); for traitline in build.traitlines().iter().filter(|x| x.is_some()) { let traitline = traitline.as_ref().unwrap(); if self.options.render_specialization_names { let header = self.render_specialization_name(&traitline.0)?; - images.push(header); + images.push((Alignment::Left, header)); } let inner = self.render_traitline(&traitline)?; - images.push(inner); + images.push((Alignment::Left, inner)); } let needs_space = build.skill_count() > 0 && build.traitline_count() > 0; @@ -297,25 +306,30 @@ impl<'r> Renderer<'r> { self.options.skill_offset, self.options.background_color, ); - images.push(separator); + images.push((Alignment::Left, separator)); } if build.skill_count() > 0 { let skills = self.render_skills(build.skills())?; - images.push(skills); + images.push((self.options.skill_alignment, skills)); } Ok(images) } - fn merge_parts(&mut self, images: &[RgbaImage]) -> Result { - let width = images.iter().map(RgbaImage::width).max().unwrap(); - let height = images.iter().map(RgbaImage::height).sum(); + fn merge_parts(&mut self, images: &[(Alignment, RgbaImage)]) -> Result { + let width = images.iter().map(snd).map(RgbaImage::width).max().unwrap(); + let height = images.iter().map(snd).map(RgbaImage::height).sum(); let mut buffer = ImageBuffer::from_pixel(width, height, self.options.background_color); let mut pos_y = 0; - for image in images { - imageops::overlay(&mut buffer, image, 0, pos_y); + for (alignment, image) in images { + let pos_x = match alignment { + Alignment::Left => 0, + Alignment::Center => (width - image.width()) / 2, + Alignment::Right => width - image.width(), + }; + imageops::overlay(&mut buffer, image, pos_x, pos_y); pos_y += image.height(); } @@ -323,6 +337,10 @@ impl<'r> Renderer<'r> { } } +fn snd(input: &(A, B)) -> &B { + &input.1 +} + fn with_mask(input: &I, mask: &J) -> RgbaImage where I: GenericImage>, -- cgit v1.2.3