[Mono-dev] Mono.Cairo Context.StrokeExtents() and Context.FillExtents()

Greg Lowe greg at vis.net.nz
Fri May 26 23:11:07 EDT 2006


I found a bug in Context.StrokeExtents() and Context.FillExtents().

Luckily it's got an easy fix:

		public Rectangle StrokeExtents ()
		{
			double x1, y1, x2, y2;
			CairoAPI.cairo_stroke_extents (state, out x1, out y1, out x2, out y2);		
			return new Rectangle (x1, y1, x2, y2);
			//fix: return new Rectangle (x1, y1, x2-x1, y2-y1);
		}

                public Rectangle FillExtents ()
		{
			double x1, y1, x2, y2;
			CairoAPI.cairo_fill_extents (state, out x1, out y1, out x2, out y2);
			return new Rectangle (x1, y1, x2, y2);
			//fix: return new Rectangle (x1, y1, x2-x1, y2-y1);			
		}




Here are some example results from my test code (See test code first):

 - Before the patch: http://greg.lowe.googlepages.com/mono-cairo-bug.png

 - After the patch: http://greg.lowe.googlepages.com/mono-cairo-after-patch.png


And the code which created these images:

using System;
using Cairo;

public class TestExtents
{				
	public static void Main(string[] arg)
	{
		Surface s = new ImageSurface( Format.ARGB32, 250, 250 );
		Context cr = new Context(s);
		
		DrawTestCase(cr);
		
		s.WriteToPng("test.png");
	}
	
	private static void DrawTestCase( Context cr ){
		cr.Arc( 100, 100, 25, 0, 2*Math.PI );
		cr.Color = new Color(1,0,0);

		// lookup extents
		Rectangle extents = cr.StrokeExtents();
		
		cr.Stroke();			
		
		// draw raw extents			
		cr.NewPath();
		cr.Rectangle( extents.X, extents.Y, extents.Width, extents.Height );
		cr.Color = new Color(0,0,1);
		cr.Stroke();
		
		// correct extents with workaround
		cr.NewPath();
		cr.Color = new Color(1,0,0);
		double realWidth, realHeight;
		realWidth = extents.Width - extents.X; // workaround
		realHeight = extents.Height - extents.Y; // workaround
		cr.Rectangle( extents.X, extents.Y, realWidth, realHeight );
		cr.Stroke();	
	}
	
}



More information about the Mono-devel-list mailing list