From 1741f0a51c436fa15829d682dcfc55f8c32ec11b Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 16 Jul 2024 02:49:29 +0800 Subject: [PATCH] Make sure SVGs are crisp (#4823) * Closes https://github.com/emilk/egui/issues/3453 * [x] I have followed the instructions in the PR template I'm fairly new to egui. I read the code, but I didn't follow the approach mentioned in https://github.com/emilk/egui/issues/3453#issuecomment-2208255433. I believe this is an easier way to achieve that, though I'm not certain if it's the best method. image I get really nice svg with this patch. @emilk Can you please take a look? I really need this! --- crates/egui/src/load.rs | 1 + crates/egui/src/widgets/image.rs | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/egui/src/load.rs b/crates/egui/src/load.rs index 71d9d086f..ee12d8de8 100644 --- a/crates/egui/src/load.rs +++ b/crates/egui/src/load.rs @@ -123,6 +123,7 @@ pub type Result = std::result::Result; /// Given as a hint for image loading requests. /// /// Used mostly for rendering SVG:s to a good size. +/// The size is measured in texels, with the pixels per point already factored in. /// /// All variants will preserve the original aspect ratio. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/crates/egui/src/widgets/image.rs b/crates/egui/src/widgets/image.rs index ab2a10bd6..bf44d3988 100644 --- a/crates/egui/src/widgets/image.rs +++ b/crates/egui/src/widgets/image.rs @@ -326,7 +326,7 @@ impl<'a> Image<'a> { /// # Errors /// May fail if they underlying [`Context::try_load_texture`] call fails. pub fn load_for_size(&self, ctx: &Context, available_size: Vec2) -> TextureLoadResult { - let size_hint = self.size.hint(available_size); + let size_hint = self.size.hint(available_size, ctx.pixels_per_point()); self.source(ctx) .clone() .load(ctx, self.texture_options, size_hint) @@ -431,23 +431,21 @@ impl ImageFit { impl ImageSize { /// Size hint for e.g. rasterizing an svg. - pub fn hint(&self, available_size: Vec2) -> SizeHint { + pub fn hint(&self, available_size: Vec2, pixels_per_point: f32) -> SizeHint { let size = match self.fit { ImageFit::Original { scale } => return SizeHint::Scale(scale.ord()), ImageFit::Fraction(fract) => available_size * fract, ImageFit::Exact(size) => size, }; - let size = size.min(self.max_size); - - // TODO(emilk): take pixels_per_point into account here! + let size = size * pixels_per_point; // `inf` on an axis means "any value" match (size.x.is_finite(), size.y.is_finite()) { (true, true) => SizeHint::Size(size.x.round() as u32, size.y.round() as u32), (true, false) => SizeHint::Width(size.x.round() as u32), (false, true) => SizeHint::Height(size.y.round() as u32), - (false, false) => SizeHint::Scale(1.0.ord()), + (false, false) => SizeHint::Scale(pixels_per_point.ord()), } }