PyGame应用程序中的SVG渲染 [英] SVG rendering in a PyGame application

查看:164
本文介绍了PyGame应用程序中的SVG渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pyGame 应用程序中,我想呈现SVG中描述的无分辨率的GUI小部件.

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 ?

(我喜欢 OCEMP GUI 工具包,但它的呈现方式似乎取决于位图)

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

推荐答案

这是一个完整的示例,在此结合了其他人的提示. 它应该从当前目录渲染一个名为test.svg的文件.已在Ubuntu 10.10,python-cairo 1.8.8,python-pygame 1.9.1,python-rsvg 2.30.0上进行了测试.

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

这篇关于PyGame应用程序中的SVG渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆