Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG images are blurry when ctx.pixel_per_point is not equal to 1.0 #3453

Closed
mkrueger opened this issue Oct 11, 2023 · 3 comments · Fixed by #4823
Closed

SVG images are blurry when ctx.pixel_per_point is not equal to 1.0 #3453

mkrueger opened this issue Oct 11, 2023 · 3 comments · Fixed by #4823
Labels
bug Something is broken egui_extras good first issue Good for newcomers

Comments

@mkrueger
Copy link
Contributor

mkrueger commented Oct 11, 2023

New API:
grafik

Old API :
grafik

It's a 4k issue - somewhere internal, the pixels per point don't get valued.

My usage is:
ui.add(ImageButton::new(icon.clone().fit_to_exact_size(size_points).tint(tint)))

the icon is loaded with:
pub static ref DOCK_LEFT_SVG: Image<'static> = Image::new(egui::include_image!("../../data/icons/dock_left.svg"));

The file is:
<svg width="56" height="56" fill="#ffffff" xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960"><path d="M180-120q-24.75 0-42.375-17.625T120-180v-600q0-24.75 17.625-42.375T180-840h600q24.75 0 42.375 17.625T840-780v600q0 24.75-17.625 42.375T780-120H180Zm207-60h393v-600H387v600Z"/></svg>

I had the issue with the old API as well - the solution was to double the width/height in the SVG and it looked sharp.
fit_to_exact_size needs to value the pixels_per_point setting - or did I do something wrong on my side?. When I double the size_points the image becomes larger - not sharper btw.

@mkrueger mkrueger added the bug Something is broken label Oct 11, 2023
@AurevoirXavier
Copy link
Contributor

Still got this in the latest version.

@passbyval
Copy link

passbyval commented Jul 4, 2024

Switching to TextureOptions::NEAREST has improved the image sharpness for me:

let base = 24.0;
let multiplier = 12.0;
let true_size = base * multiplier;

let size = Vec2 {
    x: true_size / ctx.pixels_per_point(),
    y: true_size / ctx.pixels_per_point(),
};

let img_src = include_image!("images/folder_open.svg");

ui.add(ImageButton::new(
    Image::new(img_src)
        .max_size(size)
        .maintain_aspect_ratio(true)
        .texture_options(TextureOptions::NEAREST)
        .rounding(4.0),
));
@emilk emilk changed the title Icons on mac look fuzzy with the new image API Jul 4, 2024
@emilk
Copy link
Owner

emilk commented Jul 4, 2024

We need to take ctx.pixels_per_point() into account somewhere in

let result = crate::image::load_svg_bytes_with_size(&bytes, Some(size_hint))
.map(Arc::new);
log::trace!("finished loading {uri:?}");
and/or
/// Load an SVG and rasterize it into an egui image with a scaling parameter.
///
/// Requires the "svg" feature.
///
/// # Errors
/// On invalid image
#[cfg(feature = "svg")]
pub fn load_svg_bytes_with_size(
svg_bytes: &[u8],
size_hint: Option<SizeHint>,
) -> Result<egui::ColorImage, String> {
use resvg::tiny_skia::{IntSize, Pixmap};

Should be an easy fix for anyone who is interested!

@emilk emilk added the good first issue Good for newcomers label Jul 4, 2024
@emilk emilk changed the title SVG images look fuzzy with the new image API Jul 9, 2024
@emilk emilk closed this as completed in 1741f0a Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken egui_extras good first issue Good for newcomers
4 participants