Permalink
Browse files

Merge pull request #50 from don-mccomb/FixedStrokeExtents

Added FixedStrokeExtents() to correct Rectangle returned by StrokeExtents
  • Loading branch information...
2 parents e92a79f + f18ef8d commit 7d992a6babd3abd4d0d51f8c36ee14612a67c2d8 @cameronwhite cameronwhite committed Dec 27, 2012
@@ -63,7 +63,7 @@ public static Rectangle DrawRectangle (this Context g, Rectangle r, Color color,
g.LineWidth = lineWidth;
g.LineCap = LineCap.Square;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -99,7 +99,7 @@ public static Rectangle FillRectangle (this Context g, Rectangle r, Color color)
g.Color = color;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Fill ();
g.Restore ();
@@ -119,7 +119,7 @@ public static Rectangle FillRectangle (this Context g, Rectangle r, Pattern patt
g.Pattern = pattern;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Fill ();
g.Restore ();
@@ -140,7 +140,7 @@ public static Rectangle DrawPolygonal (this Context g, PointD[] points, Color co
g.Color = color;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -158,7 +158,7 @@ public static Rectangle FillPolygonal (this Context g, PointD[] points, Color co
g.Color = color;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Fill ();
g.Restore ();
@@ -192,7 +192,7 @@ public static Rectangle FillStrokedRectangle (this Context g, Rectangle r, Color
g.LineWidth = lineWidth;
g.LineCap = LineCap.Square;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -222,7 +222,7 @@ public static Rectangle DrawEllipse (this Context g, Rectangle r, Color color, i
g.Color = color;
g.LineWidth = lineWidth;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -251,7 +251,7 @@ public static Rectangle FillEllipse (this Context g, Rectangle r, Color color)
g.Color = color;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Fill ();
g.Restore ();
@@ -310,7 +310,7 @@ public static Rectangle FillStrokedEllipse (this Context g, Rectangle r, Color f
g.Color = stroke;
g.LineWidth = lineWidth;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -341,7 +341,7 @@ public static Rectangle FillStrokedRoundedRectangle (this Context g, Rectangle r
g.Color = stroke;
g.LineWidth = lineWidth;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -368,7 +368,7 @@ public static Rectangle FillRoundedRectangle (this Context g, Rectangle r, doubl
g.Color = fill;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Fill ();
g.Restore ();
@@ -391,7 +391,7 @@ public static void FillRegion (this Context g, Gdk.Region region, Color color)
g.Color = color;
- g.StrokeExtents ();
+ g.FixedStrokeExtents ();
g.Fill ();
}
@@ -409,7 +409,7 @@ public static Rectangle DrawRoundedRectangle (this Context g, Rectangle r, doubl
g.Color = stroke;
g.LineWidth = lineWidth;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
@@ -470,14 +470,35 @@ public static Rectangle DrawLine (this Context g, PointD p1, PointD p2, Color co
g.LineWidth = lineWidth;
g.LineCap = LineCap.Square;
- Rectangle dirty = g.StrokeExtents ();
+ Rectangle dirty = g.FixedStrokeExtents ();
g.Stroke ();
g.Restore ();
return dirty;
}
+ /// <summary>
+ /// Computes a bounding box in user coordinates covering the
+ /// area that would be affected by a call to Context.Stroke()
+ /// using the current stroke parameters.
+ ///
+ /// The rectangle returned by Cairo.Context.StrokeExtents()
+ /// incorrectly specifies the X & Y coordinates of the
+ /// bottom-right corner of the Rectangle in the width and
+ /// height members. This method corrects the rectangle to
+ /// contain the width and height in the width and height members.
+ /// </summary>
+ /// <returns>
+ /// The rectangle describing the area that would be
+ /// affected.
+ /// </returns>
+ public static Rectangle FixedStrokeExtents (this Context g)
+ {
+ Rectangle r = g.StrokeExtents();
+ return new Rectangle (r.X, r.Y, r.Width - r.X, r.Height - r.Y);
+ }
+
private static Pango.Style CairoToPangoSlant (FontSlant slant)
{
switch (slant) {
@@ -865,15 +886,15 @@ public static Gdk.Rectangle GetBounds (this Path path)
// of 1, but setting it to 0 returns an empty rectangle. Set
// it to a sufficiently small width and rounding takes care of it
g.LineWidth = .01;
- rect = g.StrokeExtents ();
+ rect = g.FixedStrokeExtents ();
}
int x = (int)Math.Round (rect.X);
int y = (int)Math.Round (rect.Y);
int w = (int)Math.Round (rect.Width);
int h = (int)Math.Round (rect.Height);
- return new Gdk.Rectangle (x, y, w - x, h - y);
+ return new Gdk.Rectangle (x, y, w, h);
}
public static Gdk.Color ToGdkColor (this Cairo.Color color)
@@ -58,8 +58,14 @@ protected override Gdk.Rectangle OnMouseMove (int x, int y, int lastX, int lastY
G.MoveTo (lastX + 0.5, lastY + 0.5);
G.LineTo (x + 0.5, y + 0.5);
G.StrokePreserve ();
-
- return G.StrokeExtents ().ToGdkRectangle ();
+
+ Gdk.Rectangle dirty = G.FixedStrokeExtents ().ToGdkRectangle ();
+
+ // For some reason (?!) we need to inflate the dirty
+ // rectangle for small brush widths in zoomed images
+ dirty.Inflate (1, 1);
+
+ return dirty;
}
}
}
@@ -75,7 +75,7 @@ protected override Gdk.Rectangle OnMouseMove (int x, int y, int lastX, int lastY
G.ClosePath ();
- Rectangle dirty = G.StrokeExtents ();
+ Rectangle dirty = G.FixedStrokeExtents ();
G.Fill ();
G.Restore ();
@@ -54,7 +54,7 @@ protected override Gdk.Rectangle OnMouseMove (int x, int y, int lastX, int lastY
G.StrokePreserve ();
- return G.StrokeExtents ().ToGdkRectangle ();
+ return G.FixedStrokeExtents ().ToGdkRectangle ();
}
}
}

0 comments on commit 7d992a6

Please sign in to comment.