| ... |
... |
@@ -203,6 +203,29 @@ def get_show_circle(show, size, scale): |
|
203
|
203
|
return context.get_target()
|
|
204
|
204
|
|
|
205
|
205
|
|
|
|
206
|
+def fit(surface: cairo.Surface, size: int) -> cairo.Surface:
|
|
|
207
|
+ width = surface.get_width()
|
|
|
208
|
+ height = surface.get_height()
|
|
|
209
|
+ if width == height:
|
|
|
210
|
+ return surface
|
|
|
211
|
+
|
|
|
212
|
+ # Fit any non-square image by:
|
|
|
213
|
+ # 1. cutting a square from the original surface
|
|
|
214
|
+ # 2. scaling the square to the desired size
|
|
|
215
|
+ min_size = min(width, height)
|
|
|
216
|
+ factor = size / min_size
|
|
|
217
|
+
|
|
|
218
|
+ square_surface = square(surface, min_size)
|
|
|
219
|
+
|
|
|
220
|
+ new_surface = cairo.ImageSurface(cairo.Format.ARGB32, size, size)
|
|
|
221
|
+ new_surface.set_device_scale(*surface.get_device_scale())
|
|
|
222
|
+ context = cairo.Context(new_surface)
|
|
|
223
|
+ context.scale(factor, factor)
|
|
|
224
|
+ context.set_source_surface(square_surface, 0, 0)
|
|
|
225
|
+ context.paint()
|
|
|
226
|
+ return context.get_target()
|
|
|
227
|
+
|
|
|
228
|
+
|
|
206
|
229
|
def square(surface, size):
|
|
207
|
230
|
width = surface.get_width()
|
|
208
|
231
|
height = surface.get_height()
|
| ... |
... |
@@ -469,7 +492,7 @@ def surface_from_filename(self, filename, size, scale): |
|
469
|
492
|
return None
|
|
470
|
493
|
|
|
471
|
494
|
surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale)
|
|
472
|
|
- return square(surface, size)
|
|
|
495
|
+ return fit(surface, size)
|
|
473
|
496
|
|
|
474
|
497
|
def _load_surface_from_storage(self, filename, size, scale):
|
|
475
|
498
|
size = size * scale
|
| ... |
... |
@@ -481,7 +504,7 @@ def _load_surface_from_storage(self, filename, size, scale): |
|
481
|
504
|
if pixbuf is None:
|
|
482
|
505
|
return None
|
|
483
|
506
|
surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, scale)
|
|
484
|
|
- return square(surface, size)
|
|
|
507
|
+ return fit(surface, size)
|
|
485
|
508
|
|
|
486
|
509
|
def _get_avatar_from_storage(self, contact, size, scale, style):
|
|
487
|
510
|
avatar_sha = contact.avatar_sha
|