[cairo-commit] pycairo/examples/svg svgview.py,1.2,1.3

Steve Chaplin commit at pdx.freedesktop.org
Sat Mar 19 20:46:11 PST 2005


Committed by: stevech1097

Update of /cvs/cairo/pycairo/examples/svg
In directory gabe:/tmp/cvs-serv32127/examples/svg

Modified Files:
	svgview.py 
Log Message:
SC 2005/03/20

Index: svgview.py
===================================================================
RCS file: /cvs/cairo/pycairo/examples/svg/svgview.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- svgview.py	19 Mar 2005 17:23:46 -0000	1.2
+++ svgview.py	20 Mar 2005 04:46:09 -0000	1.3
@@ -1,11 +1,11 @@
 #!/usr/bin/env python
 
 # Possible improvements:
-# - image shrinks in size OK, but does not enlarge > orig size?
 # - Zoom-in/out toolbutton
 
 from __future__ import division
 import os
+import sys
 
 import gtk
 import cairo
@@ -41,6 +41,10 @@
     return retval
 
 
+def gdkcolor_to_rgb (gdkcolor):
+    return gdkcolor.red/65535, gdkcolor.green/65535, gdkcolor.blue/65535
+
+
 class Window (gtk.Window):
     def __init__ (self, title=None, type=gtk.WINDOW_TOPLEVEL):
         gtk.Window.__init__ (self, type)
@@ -48,7 +52,6 @@
             self.set_title (title)
         self.set_default_size(300, 200)
 
-        self.pixmap = None
         self.af     = None
         self.da     = None
 
@@ -79,33 +82,48 @@
         self.fileselect = MyFileChooserDialog(parent=self)
 
 
+    def create_da (self):
+        """add AspectFrame and DrawingArea widgets
+        """
+        af = gtk.AspectFrame()
+        self.vbox.pack_start(af)
+        
+        da = gtk.DrawingArea()
+        af.add(da)
+        da.connect ('expose-event', self.cb_da_expose_event)
+        da.set_double_buffered(False)
+    
+        af.show_all()
+
+        da.realize()
+        self.rgb_bg = gdkcolor_to_rgb (da.style.bg[gtk.STATE_NORMAL])
+
+        self.af, self.da = af, da
+
+
     def cb_da_expose_event (self, da, event, data=None):
-        ctx = self.ctx
         width, height = da.allocation.width, da.allocation.height
         pixmap = gtk.gdk.Pixmap (da.window, width, height)
     
         surface = cairo.gtk.surface_create_for_pixmap_with_visual(
             pixmap, self.da.window.get_visual())
+        ctx = cairo.Context()
         ctx.set_target_surface (surface)
 
-        # draw bg
+        # draw to pixmap
         ctx.rectangle(0,0,width,height)
-        ctx.set_rgb_color(0.7,0.7,0.7)
+        ctx.set_rgb_color(*self.rgb_bg)
         ctx.fill()
     
-        # draw surface/pattern to surface
-        pattern = cairo.Pattern (surface=self.svg_surface)
-        ctx.set_pattern (pattern)
-
-        matrix = cairo.Matrix(a=self._svg_width/width,
-                              d=self._svg_height/height)
-        pattern.set_matrix (matrix)
-
-        ctx.rectangle(0, 0, self._svg_width, self._svg_height)
-        ctx.fill()
+        svg_width, svg_height = self.svg.size        
+        matrix = cairo.Matrix (a=width/svg_width,
+                               d=height/svg_height)
+        ctx.set_matrix (matrix)
+        self.svg.render (ctx)
 
-        gc = gtk.gdk.GC(da.window)
-        da.window.draw_drawable(gc, pixmap, 0,0, 0,0, -1,-1)
+        # draw pixmap to gdk.window
+        da.window.draw_drawable(gtk.gdk.GC(da.window),
+                                pixmap, 0,0, 0,0, -1,-1)
 
 
     def cb_open (self, action, data=None):
@@ -113,41 +131,26 @@
         pixmap
         """
         filename = self.fileselect.get_filename_from_user()
-        if filename == None:
-            return
+        if filename:
+            self.load_file (filename)
 
-        svg = cairo.svg.Context()
+
+    def load_file (self, filename):
+        """parse the svg file
+        """
+        self.svg = cairo.svg.Context()
         try:
-            svg.parse (filename)
+            self.svg.parse (filename)
         except Exception, exc:
             print exc
-            return
-            
-        width, height = svg.size
-        # save w,h for use in expose_event
-        self._svg_width, self._svg_height = width, height
-
-        # add AspectFrame and DrawingArea widgets
-        if self.af is None:
-            self.af = gtk.AspectFrame()
-            self.vbox.pack_start(self.af)
-        
-            self.da = gtk.DrawingArea()
-            self.af.add(self.da)
-            self.da.connect ('expose-event', self.cb_da_expose_event)
-            self.da.set_double_buffered(False)
-    
-            self.af.show_all()
 
-        self.af.set(xalign=0.5, yalign=0.5, ratio=width/height, obey_child=False)
-
-        # draw svg to a surface - use as image src to draw into gdk_window
-        self.ctx = cairo.Context()
-        self.svg_surface = cairo.Surface(cairo.FORMAT_ARGB32, width, height)
-        self.ctx.set_target_surface (self.svg_surface)
-        svg.render (self.ctx)
-        
-        self.da.queue_draw()
+        else:
+            if self.af is None:
+                self.create_da()
+            
+            width, height = self.svg.size
+            self.af.set(xalign=0.5, yalign=0.5, ratio=width/height, obey_child=False)
+            self.da.queue_draw()
 
 
     def cb_quit (self, action, data=None):  
@@ -216,7 +219,14 @@
 
 
 if __name__ == '__main__':
+    filename = None
+    if len(sys.argv) == 2:
+        filename = sys.argv[1]
+    
     app = Window (title='SVGView')
     app.connect('destroy', gtk.main_quit)
+    if filename:
+        app.load_file (filename)
+    
     app.show_all()
     gtk.main()




More information about the cairo-commit mailing list