Re: gtk_icon_theme_load_icon and GDK_SCALE=2 results in blurry icon
- Date: Tue, 3 Oct 2017 19:32:22 +0200
- From: infirit <infirit@xxxxxxxxx>
- Subject: Re: gtk_icon_theme_load_icon and GDK_SCALE=2 results in blurry icon
Op 10/03/2017 om 03:28 PM schreef Alexander Shaduri:
> I have a HiDPI Screen with DPI set to 192, GDK_SCALE=2 and GDK_DPI_SCALE=-1.
> I need to get an icon as a pixbuf for GtkIconView.
Short answer, there is no way to do scaling properly with pixbufs. The
only way I found to do this was setting icon names and using cairo surfaces.
> This is the code in gtkmm:
> Glib::RefPtr<Gtk::IconTheme> default_icon_theme = Gtk::IconTheme::get_default();
> Glib::RefPtr<Gdk::Pixbuf> icon = default_icon_theme->load_icon("drive-harddisk", 64, Gtk::IconLookupFlags(0));
As others have mentioned use the _at_scale() version of the icon theme
functions, it handles all the necessary scaling for you. To get the
scale factor use gtk_widget_get_scale_factor on the window/widget.
Another thing to think about is the quality of the icons. I added proper
scaling for a project and found that the a couple of png icons it used
where in only a single size and of low quality. No matter what you do in
this case there is no way to prevent blurry icons.
Internally Gtk handles this transparently as long as use icon-names
instead of pixbufs. I highly recommend you use icon-names whenever
possible and use cairo surfaces if you need to draw over or transform
I ended up using gtk_icon_theme_lookup_icon_for_scale and then retrieve
the surface at the very last moment with gtk_icon_info_load_surface.
gtk_icon_info_load_surface also sets the correct scaling factor on the
surface so there is no need to change it with
cairo_surface_set_device_scale. Most widgets will accept the surface
these days and you should start using them.
> Is there a way to get a sharp icon with gtk_icon_theme_load_icon?
Yes I managed it as described above. I have a silly experiment in python
that uses cairo surfaces at , maybe this helps. The moment you write
to png and use it as source on a widget things go blurry.
gtk-list mailing list