Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG rendering in a PyGame application

In a pyGame application, I would like to render resolution-free GUI widgets described in SVG.

What tool and/or library can I use to reach this goal ?

(I like the OCEMP GUI toolkit but it seems to be bitmap dependent for its rendering)

like image 271
Pierre-Jean Coudert Avatar asked Sep 23 '08 12:09

Pierre-Jean Coudert


People also ask

Can you use SVG in Pygame?

The PyGame webpage has a HOWTO for rendering into a buffer with a Cairo, and using that buffer directly with PyGame. Since Version 2, SDL Image supports SVG (Scalable Vector Graphics) files (see SDL_image 2.0). Therefore, with pygame version 2.0. 1, SVG files can be loaded into a pygame.

Can pygame handle image formats?

Pygame is able to load images onto Surface objects from PNG, JPG, GIF, and BMP image files. The differences between these image file formats is described at http://invpy.com/formats.

How is SVG rendered?

SVG uses a "painters model" of rendering. Paint is applied in successive operations to the output device such that each operation paints onto some area of the output device, possibly obscuring paint that has previously been layed down.

What is SVG rendering?

Scalable Vector Graphics (SVG) is an XML-based vector image format for defining two-dimensional graphics, having support for interactivity and animation. The SVG specification is an open standard developed by the World Wide Web Consortium (W3C) since 1999. Scalable Vector Graphics.


1 Answers

This is a complete example which combines hints by other people here. It should render a file called test.svg from the current directory. It was tested on Ubuntu 10.10, python-cairo 1.8.8, python-pygame 1.9.1, python-rsvg 2.30.0.

#!/usr/bin/python  import array import math  import cairo import pygame import rsvg  WIDTH = 512 HEIGHT = 512  data = array.array('c', chr(0) * WIDTH * HEIGHT * 4) surface = cairo.ImageSurface.create_for_data(     data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT, WIDTH * 4)  pygame.init() window = pygame.display.set_mode((WIDTH, HEIGHT)) svg = rsvg.Handle(file="test.svg") ctx = cairo.Context(surface) svg.render_cairo(ctx)  screen = pygame.display.get_surface() image = pygame.image.frombuffer(data.tostring(), (WIDTH, HEIGHT),"ARGB") screen.blit(image, (0, 0))  pygame.display.flip()   clock = pygame.time.Clock() while True:     clock.tick(15)     for event in pygame.event.get():         if event.type == pygame.QUIT:             raise SystemExit 
like image 189
Johan Dahlin Avatar answered Sep 22 '22 04:09

Johan Dahlin