Daniel Brötzmann pushed to branch mainwindow at gajim / gajim
Commits: cf2cc4a3 by wurstsalat at 2021-11-04T00:55:58+01:00 Avatar: Fix non-square images
- - - - -
1 changed file:
- gajim/gtk/avatar.py
Changes:
===================================== gajim/gtk/avatar.py ===================================== @@ -203,6 +203,29 @@ def get_show_circle(show, size, scale): return context.get_target()
+def fit(surface: cairo.Surface, size: int) -> cairo.Surface: + width = surface.get_width() + height = surface.get_height() + if width == height: + return surface + + # Fit any non-square image by: + # 1. cutting a square from the original surface + # 2. scaling the square to the desired size + min_size = min(width, height) + factor = size / min_size + + square_surface = square(surface, min_size) + + new_surface = cairo.ImageSurface(cairo.Format.ARGB32, size, size) + new_surface.set_device_scale(*surface.get_device_scale()) + context = cairo.Context(new_surface) + context.scale(factor, factor) + context.set_source_surface(square_surface, 0, 0) + context.paint() + return context.get_target() + + def square(surface, size): width = surface.get_width() height = surface.get_height() @@ -469,7 +492,7 @@ def surface_from_filename(self, filename, size, scale): return None
surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale) - return square(surface, size) + return fit(surface, size)
def _load_surface_from_storage(self, filename, size, scale): size = size * scale @@ -481,7 +504,7 @@ def _load_surface_from_storage(self, filename, size, scale): if pixbuf is None: return None surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale) - return square(surface, size) + return fit(surface, size)
def _get_avatar_from_storage(self, contact, size, scale, style): avatar_sha = contact.avatar_sha
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/cf2cc4a3d5903fc6f114ef7e4efc1a6b6...