static int FlattenPath(GraphicsPath path, Matrix matrix, float flatness)
{
var status = 0;
if (path == null)
return -1;
// apply matrix before flattening (as there's less points at this stage)
if (matrix != null) {
path.Transform(matrix);
}
// if no bezier are present then the path doesn't need to be flattened
if (!PathHasCurve (path))
return status;
var points = new List<PointF> ();
var types = new List<byte> ();
// Iterate the current path and replace each bezier with multiple lines
for (int i = 0; i < path.points.Count; i++) {
var point = path.points [i];
var type = path.types [i];
// PathPointTypeBezier3 has the same value as PathPointTypeBezier
if ((type & (byte)PathPointType.Bezier) == (byte)PathPointType.Bezier)
{
if (!ConvertBezierToLines (path, i, Math.Abs (flatness), points, types))
{
// uho, too much recursion - do not pass go, do not collect 200$
PointF pt = PointF.Empty;
// mimic MS behaviour when recursion becomes a problem */
// note: it's not really an empty rectangle as the last point isn't closing
points.Clear ();
types.Clear ();
type = (byte)PathPointType.Start;
points.Add (pt);
types.Add (type);
type = (byte)PathPointType.Line;
points.Add (pt);
types.Add (type);
points.Add (pt);
types.Add (type);
break;
}
// beziers have 4 points: the previous one, the current and the next two
i += 2;
} else {
// no change required, just copy the point
points.Add (point);
types.Add (type);
}
}
// transfer new path informations
path.points = points;
path.types = types;
// note: no error code is given for excessive recursion
return 0;
}