From e4417f5e0688313ab7bad21c762e89e5a540c6b6 Mon Sep 17 00:00:00 2001 From: Aloxaf Date: Thu, 5 Nov 2020 11:12:34 +0800 Subject: [PATCH] fix: ignore the invalid highlight line number fix #124 --- src/formatter.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 22b0bde..79b5bef 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -239,7 +239,7 @@ impl ImageFormatter { } } - fn highlight_lines(&self, image: &mut DynamicImage, lines: &[u32]) { + fn highlight_lines>(&self, image: &mut DynamicImage, lines: I) { let width = image.width(); let height = self.font.get_font_height() + self.line_pad; let mut color = image.get_pixel(20, 20); @@ -251,7 +251,7 @@ impl ImageFormatter { let shadow = RgbaImage::from_pixel(width, height, color); for i in lines { - let y = self.get_line_y(*i - 1); + let y = self.get_line_y(i - 1); copy_alpha(&shadow, image.as_mut_rgba8().unwrap(), 0, y); } } @@ -278,7 +278,12 @@ impl ImageFormatter { let mut image = DynamicImage::ImageRgba8(RgbaImage::from_pixel(size.0, size.1, background)); if !self.highlight_lines.is_empty() { - self.highlight_lines(&mut image, &self.highlight_lines); + let highlight_lines = self + .highlight_lines + .iter() + .cloned() + .filter(|&n| n >= 1 && n <= drawables.max_lineno + 1); + self.highlight_lines(&mut image, highlight_lines); } if self.line_number { self.draw_line_number(&mut image, drawables.max_lineno, foreground);