From 5de46ed5fdd46709401d2f54adfb2123e56259ff Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 3 Jan 2024 20:39:21 -0500 Subject: [PATCH] Refactor SVG rendering a bit --- py/hexagram/svg.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/py/hexagram/svg.py b/py/hexagram/svg.py index 91a5902..612f6a7 100644 --- a/py/hexagram/svg.py +++ b/py/hexagram/svg.py @@ -7,10 +7,7 @@ gi.require_version('Rsvg', '2.0') from gi.repository import Rsvg -def to_surface(path: str, width: float, height: float, style: Optional[str]=None) -> cairo.Surface: - surface = cairo.RecordingSurface(cairo.CONTENT_ALPHA, - cairo.Rectangle(0, 0, width, height)) - +def render_to_surface(path: str, surface: cairo.Surface, width: float, height: float, style: Optional[str]=None): cr = cairo.Context(surface) rect = Rsvg.Rectangle() @@ -26,4 +23,11 @@ def to_surface(path: str, width: float, height: float, style: Optional[str]=None svg.render_layer(cr, None, rect) + surface + +def render_to_image(path: str, width: float, height: float, style: Optional[str]=None) -> cairo.Surface: + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + + render_to_surface(path, surface, width, height, style) + return surface