Skip to content

Commit

Permalink
Make sure SVGs are crisp (emilk#4823)
Browse files Browse the repository at this point in the history
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes emilk#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
emilk#3453 (comment).

I believe this is an easier way to achieve that, though I'm not certain
if it's the best method.

<img width="760" alt="image"
src="https://github.com/user-attachments/assets/4b3c561f-1c24-446b-9581-a2f4e9858480">

I get really nice svg with this patch.

@emilk Can you please take a look? I really need this!
  • Loading branch information
aurexav authored and lucasmerlin committed Jul 30, 2024
1 parent f7e1b60 commit b59c4c1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub type Result<T, E = LoadError> = std::result::Result<T, E>;
/// 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)]
Expand Down
10 changes: 4 additions & 6 deletions crates/egui/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()),
}
}

Expand Down

0 comments on commit b59c4c1

Please sign in to comment.