summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2019-01-25 01:13:00 +0100
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2019-01-25 01:13:00 +0100
commitebc886d670c5df73d0ed55753562fe26cc2a2d11 (patch)
treede6b69a84d307f581be3a38957077ed4af46c9ed
parent71ef2a362e95500c9c3b53ef967e54da65145b30 (diff)
downloadpoezio-ebc886d670c5df73d0ed55753562fe26cc2a2d11.tar.gz
poezio-ebc886d670c5df73d0ed55753562fe26cc2a2d11.tar.bz2
poezio-ebc886d670c5df73d0ed55753562fe26cc2a2d11.tar.xz
poezio-ebc886d670c5df73d0ed55753562fe26cc2a2d11.zip
Render SVG avatars directly at the final resolution.
-rw-r--r--poezio/windows/image.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/poezio/windows/image.py b/poezio/windows/image.py
index 71913e43..96636ec7 100644
--- a/poezio/windows/image.py
+++ b/poezio/windows/image.py
@@ -31,17 +31,27 @@ from poezio.config import config
from typing import Tuple, Optional, Callable
+MAX_SIZE = 16
+
+
def render_svg(svg: bytes) -> Optional[Image.Image]:
if not HAS_RSVG:
return None
try:
handle = Rsvg.Handle.new_from_data(svg)
dimensions = handle.get_dimensions()
- surface = cairo.ImageSurface(cairo.Format.ARGB32, dimensions.width, dimensions.height)
+ biggest_dimension = max(dimensions.width, dimensions.height)
+ scale = MAX_SIZE / biggest_dimension
+ translate_x = (biggest_dimension - dimensions.width) / 2
+ translate_y = (biggest_dimension - dimensions.height) / 2
+
+ surface = cairo.ImageSurface(cairo.Format.ARGB32, MAX_SIZE, MAX_SIZE)
context = cairo.Context(surface)
+ context.scale(scale, scale)
+ context.translate(translate_x, translate_y)
handle.render_cairo(context)
data = surface.get_data()
- image = Image.frombytes('RGBA', (dimensions.width, dimensions.height), data.tobytes())
+ image = Image.frombytes('RGBA', (MAX_SIZE, MAX_SIZE), data.tobytes())
# This is required because Cairo uses a BGRA (in host endianess)
# format, and PIL an ABGR (in byte order) format. Yes, this is
# confusing.