Skip to content

Instantly share code, notes, and snippets.

@superlou
Created July 9, 2013 13:06
Show Gist options
  • Save superlou/5957173 to your computer and use it in GitHub Desktop.
Save superlou/5957173 to your computer and use it in GitHub Desktop.
Cairo Inkscape Rendering
import wx
import rsvg
import numpy
try:
import wx.lib.wxcairo
import cairo
haveCairo = True
except ImportError:
haveCairo = False
from lxml import etree as ET
nsmap = {
'sodipodi': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
'cc': 'http://web.resource.org/cc/',
'svg': 'http://www.w3.org/2000/svg',
'dc': 'http://purl.org/dc/elements/1.1/',
'xlink': 'http://www.w3.org/1999/xlink',
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'inkscape': 'http://www.inkscape.org/namespaces/inkscape'
}
class ComponentFrame(wx.Frame):
def __init__(self, parent, id, title, pos, size):
wx.Frame.__init__(self, parent, id, title, pos, size)
self.panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.HORIZONTAL)
component = SvgComponent(self.panel)
vbox.Add(component, 1, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(vbox)
self.Show(True)
class SvgComponent(wx.PyControl):
def __init__(self, parent, label="",
id=wx.ID_ANY,
pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator,
name="CustomMeter"):
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
if not haveCairo or not wx.GraphicsRenderer.GetCairoRenderer():
print "Cairo unavailable!"
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self)
dc.Clear()
self.Draw(dc)
def Draw(self, dc):
width, height = self.GetClientSize()
if not width or not height:
return
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = wx.lib.wxcairo.ContextFromDC(dc)
ctx.scale(width/300.0, height/300.0)
with open('demo.svg', 'r') as content_file:
xml_data = content_file.read()
xml_data = self.RotateNeedle(xml_data)
svg = rsvg.Handle(data=xml_data)
svg.render_cairo(ctx)
def OnSize(self, event):
self.Refresh(True)
def OnEraseBackground(self, event):
pass
def DoGetBestSize(self):
"""
Overridden base class virtual. Determines the best size of the control
based on the label size, the bitmap size and the current font.
"""
best = wx.Size(400, 400)
self.CacheBestSize(best)
return best
def RotateNeedle(self, xml):
tree = ET.fromstring(xml)
needle = tree.xpath("//svg:path[@id='needle']", namespaces=nsmap)[0]
needle_axis = tree.xpath("//svg:path[@id='needle_axis']", namespaces=nsmap)
#needle.set('transform', 'rotate(90, 161, 885)') manually determined
print ET.tostring(needle)
return ET.tostring(tree)
if __name__ == '__main__':
app = wx.App()
frame = ComponentFrame(None, wx.ID_ANY, 'test rsvg', (200, 200), (400, 400))
app.MainLoop()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment