Navigation Menu

Skip to content

Commit

Permalink
Add SVGImage_Render sample
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinD42 committed Sep 5, 2019
1 parent 5a3f0dd commit 83e2212
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
4 changes: 3 additions & 1 deletion demo/SVGImage.py → demo/SVGImage_Bitmap.py
@@ -1,4 +1,3 @@
#!/usr/bin/env python

import os
import glob
Expand Down Expand Up @@ -93,6 +92,9 @@ def runTest(frame, nb, log):
Scalable Vector Graphics (SVG) files. The advantage of SVG files is that
they can be used to create bitmaps of any size without loss of quality.
This sample demonstrates rasterizing an SVG image to wx.Bitmaps of various
sizes.
</body></html>
"""

Expand Down
117 changes: 117 additions & 0 deletions demo/SVGImage_Render.py
@@ -0,0 +1,117 @@

import os
import glob
import wx
from wx.svg import SVGimage

#----------------------------------------------------------------------

class SVGRenderPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.FULL_REPAINT_ON_RESIZE)

self._renderer = None
self._img = None

self.Bind(wx.EVT_PAINT, self.OnPaint)
self.SetBackgroundStyle(wx.BG_STYLE_PAINT)


def SetRenderer(self, renderer):
self._renderer = renderer
self.Refresh()


def SetSVGFile(self, svg_filename):
self._img = SVGimage.CreateFromFile(svg_filename)
self.Refresh()


def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.Clear()

iw, ih = (self._img.width, self._img.height) if self._img else (100,100)
dcdim = min(self.Size.width, self.Size.height)
imgdim = min(iw, ih)
scale = dcdim / imgdim
width = int(iw * scale)
height = int(ih * scale)

dc.SetBrush(wx.Brush('white'))
dc.DrawRectangle(0,0, width, height)

if self._renderer and self._img:
ctx = self._renderer.CreateContext(dc)
self._img.RenderToGC(ctx, scale)



#----------------------------------------------------------------------

class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)

self.listbox = wx.ListBox(self, style=wx.LB_SINGLE, size=(250, -1))
self.listbox.AppendItems(glob.glob(os.path.join('data', '*.svg')))

self.renderPanel = SVGRenderPanel(self)
rightSizer = wx.BoxSizer(wx.VERTICAL)
rightSizer.Add(self.renderPanel, wx.SizerFlags(1).Expand())

self.Sizer = wx.BoxSizer(wx.HORIZONTAL)
self.Sizer.Add(self.listbox, wx.SizerFlags(1).Border(wx.ALL, 10).Expand())
self.Sizer.Add(rightSizer, wx.SizerFlags(2).Border(wx.RIGHT|wx.BOTTOM|wx.TOP, 10).Expand())

self.Bind(wx.EVT_LISTBOX, self.OnSelectItem)
self.listbox.SetSelection(0)

# TODO: Add ability for the user to select the renderer
if 'wxMSW' in wx.PlatformInfo:
renderer = wx.GraphicsRenderer.GetDirect2DRenderer()
# renderer = wx.GraphicsRenderer.GetCairoRenderer()
else:
renderer = wx.GraphicsRenderer.GetDefaultRenderer()
self.renderPanel.SetRenderer(renderer)

# Load the first SVG in the list into the static bitmaps
self.renderPanel.SetSVGFile(self.listbox.GetString(0))


def OnSelectItem(self, evt):
filename = self.listbox.GetStringSelection()
self.renderPanel.SetSVGFile(filename)


#----------------------------------------------------------------------

def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win

#----------------------------------------------------------------------



overview = """<html><body>
<h2><center>SVGImage</center></h2>
The wx.svg.SVGimage class provides the ability to load, parse and render
Scalable Vector Graphics (SVG) files. The advantage of SVG files is that
they can be used to create bitmaps of any size without loss of quality.
This sample demonstrates rendering an SVG image directly on to a
wx.GraphicsContext using the GC's daring capabilities.
</body></html>
"""



if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

14 changes: 8 additions & 6 deletions demo/demodata.py
Expand Up @@ -24,10 +24,10 @@
#
# _treeList
#
# These are the Demo Catagory Headers
# These are the Demo Category Headers
# and Demo Module Names(Ex: Frame.py without ext)
#
# ('Demo Catagory Name String', [
# ('Demo Category Name String', [
# 'DemoModuleName1',
# 'DemoModuleName2',
# 'Etc',
Expand All @@ -44,7 +44,8 @@
'TreeListCtrl',
'NotificationMessage',
'AddPrivateFont',
'SVGImage',
'SVGImage_Bitmap',
'SVGImage_Render',
]),

# managed windows == things with a (optional) caption you can close
Expand Down Expand Up @@ -255,7 +256,8 @@
'Img2PyArtProvider',
'Mask',
'RawBitmapAccess',
'SVGImage',
'SVGImage_Bitmap',
'SVGImage_Render',
'Throbber',
]),

Expand All @@ -266,7 +268,6 @@
'Cairo',
'Cairo_Snippets',
'ColourDB',
##'DialogUnits', # needs more explanations
'DragScroller',
'DrawXXXList',
'FileHistory',
Expand All @@ -290,7 +291,8 @@
'ShapedWindow',
'Sound',
'StandardPaths',
'SVGImage',
'SVGImage_Bitmap',
'SVGImage_Render',
'SystemSettings',
'ToolTip',
'UIActionSimulator',
Expand Down

0 comments on commit 83e2212

Please sign in to comment.