~glitter-team/glitter/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# !/usr/bin/python
# -*- coding: utf-8 -*-

# Glitter Toolkit
        
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "A CairoTexture from a SVG"

import os
import sys

from cluttercairo import CairoTexture
import clutter
import cairo
import rsvg

class SVGImage(CairoTexture):
    """ Creates a CairoTexture based on a given SVG """

    def __init__(self, uri, width, height):
        
        """
        uri (str) -- source location (*.svg), standard uri format
        width (int) -- width in pixels
        height (int) -- height in pixels
        """
        
        CairoTexture.__init__(self, width, height)
        
        self.uri = uri
        self.width = width
        self.height = height
        
        if not os.path.exists(self.uri):
            raise AttributeError, 'invalid source uri: %s' % self.uri
        
        context = self.cairo_create()
        svg = rsvg.Handle(self.uri)
        matrix = cairo.Matrix((self.width * 1.0 / svg.props.width), 
                              0, 
                              0,
                              (self.height * 1.0 / svg.props.height), 
                              0, 
                              0
                             )
        context.transform(matrix)
        svg.render_cairo(context)
               
        context.close_path()
        
        context.stroke()
        del(context)