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

Steve Chaplin commit at pdx.freedesktop.org
Sat Mar 19 09:23:49 PST 2005


Committed by: stevech1097

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

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

Index: svgview.py
===================================================================
RCS file: /cvs/cairo/pycairo/examples/svg/svgview.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- svgview.py	2 Mar 2005 13:12:56 -0000	1.1
+++ svgview.py	19 Mar 2005 17:23:46 -0000	1.2
@@ -1,10 +1,10 @@
 #!/usr/bin/env python
 
-# TODO
-# draw frame round pic to show its borderline
-# Zoom-in/out toolbutton
-
+# Possible improvements:
+# - image shrinks in size OK, but does not enlarge > orig size?
+# - Zoom-in/out toolbutton
 
+from __future__ import division
 import os
 
 import gtk
@@ -49,9 +49,11 @@
         self.set_default_size(300, 200)
 
         self.pixmap = None
+        self.af     = None
+        self.da     = None
 
-        vbox = gtk.VBox()
-        self.add (vbox)
+        self.vbox = gtk.VBox()
+        self.add (self.vbox)
 
         # create UIManager menus
         ag = gtk.ActionGroup ('WindowActions')
@@ -70,24 +72,41 @@
             path = '/MenuBar'
             menubar = self.ui.get_widget (path)
             if menubar:
-                vbox.pack_start (menubar, expand=False)
+                self.vbox.pack_start (menubar, expand=False)
             else:
                 print "Error: uimanager.get_widget('%s') failed" % path
 
         self.fileselect = MyFileChooserDialog(parent=self)
 
-        self.da = gtk.DrawingArea()
-        vbox.pack_start (self.da, expand=True)
-        def cb_expose_event (da, event, data=None):
-            if self.pixmap:
-                # center on screen
-                xdest = max (0, (da.allocation.width -da.svg_width)//2)
-                ydest = max (0, (da.allocation.height-da.svg_height)//2)
-                self.da.window.draw_drawable(self.style.bg_gc[gtk.STATE_NORMAL],
-                                             self.pixmap,
-                                             0, 0, xdest, ydest, -1, -1)
-        self.da.connect ('expose-event', cb_expose_event)
-            
+
+    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.set_target_surface (surface)
+
+        # draw bg
+        ctx.rectangle(0,0,width,height)
+        ctx.set_rgb_color(0.7,0.7,0.7)
+        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()
+
+        gc = gtk.gdk.GC(da.window)
+        da.window.draw_drawable(gc, pixmap, 0,0, 0,0, -1,-1)
+
 
     def cb_open (self, action, data=None):
         """Open svg file (if one is selected) and render to an off-screen
@@ -105,14 +124,29 @@
             return
             
         width, height = svg.size
-        self.da.svg_width, self.da.svg_height = width, height
+        # save w,h for use in expose_event
+        self._svg_width, self._svg_height = width, height
 
-        ctx = cairo.Context()
-        self.pixmap = gtk.gdk.Pixmap (self.da.window, width, height)
-        self.pixmap.draw_rectangle (self.style.bg_gc[gtk.STATE_NORMAL], True, 0, 0, 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()
 
-        cairo.gtk.set_target_drawable(ctx, self.pixmap)
-        svg.render (ctx)
+        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()
 
 




More information about the cairo-commit mailing list