[cairo-commit] [cairo-www] src/cookbook

Carl Worth cworth at freedesktop.org
Sat Feb 12 15:31:50 PST 2011


 src/cookbook/roundedrectangles.mdwn |   40 ++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

New commits:
commit f77cb4601fd3bb9565f51fe63c1840e33c98ecf5
Author: heltonbiker <heltonbiker at web>
Date:   Sat Feb 12 15:31:50 2011 -0800

diff --git a/src/cookbook/roundedrectangles.mdwn b/src/cookbook/roundedrectangles.mdwn
index c47bea1..3140ef2 100644
--- a/src/cookbook/roundedrectangles.mdwn
+++ b/src/cookbook/roundedrectangles.mdwn
@@ -107,3 +107,43 @@ I can't remember where I got this. If you are the author please add your name. W
         context.curve_to(x,y,x,y,x+r,y)             # Curve to A
         return 
 
+##Method D
+Created by Helton Moraes (heltonbiker at gmail dot com). Uses round arcs and takes advantage of the segment-creation property of cairo.arc() - just draw the arcs, no need to draw straight segments in between.
+
+    def draw_rounded(cr, area, radius):
+        """ draws rectangles with rounded (circular arc) corners """
+        from math import pi
+        a,b,c,d=area
+        cr.arc(a + radius, c + radius, radius,   2*(pi/2), 3*(pi/2))
+        cr.arc(b - radius, c + radius, radius,   3*(pi/2), 4*(pi/2))
+        cr.arc(b - radius, d - radius*2, radius, 0*(pi/2), 1*(pi/2))  # ;o)
+        cr.arc(a + radius, d - radius*2, radius, 1*(pi/2), 2*(pi/2))
+        cr.close_path()
+        cr.stroke()
+
+    ################################################################
+
+    ### EXAMPLE
+    import cairo, Image
+
+    # dimensions of some whole figure:
+    fig_size = (800,600)
+
+    # an area with coordinates of
+    # (top, bottom, left, right) edges in absolute coordinates:
+    inside_area = (100,700,100,600)
+
+    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, *fig_size)
+    cr = cairo.Context(surface)
+    cr.set_line_width(3)
+    cr.set_source_rgb(1,1,1)
+
+    draw_rounded(cr, inside_area, 100)
+
+    im = Image.frombuffer("RGBA",
+                           fig_size,
+                           surface.get_data(),
+                           "raw",
+                           "BGRA",
+                           0,1)
+    im.show()


More information about the cairo-commit mailing list