private GraphicsPath CreateGraphicsPath( Primitives.Path path ) { GraphicsPath gp; if( _mapPathToGraphicsPath.TryGetValue( path, out gp ) ) { return gp; } PathCommandVisitor visitor = new PathCommandVisitor(); foreach( Primitives.Path.Command pathCommand in path.Commands ) { pathCommand.Visit( visitor ); } gp = visitor.GetGraphicsPath(); _mapPathToGraphicsPath.Add( path, gp ); return gp; }
public override Primitives.Path FlattenPath( Primitives.Path source ) { PathCommandVisitor visitor = new PathCommandVisitor(); foreach( Primitives.Path.Command pathCommand in source.Commands ) { pathCommand.Visit( visitor ); } using( GraphicsPath gp = visitor.GetGraphicsPath() ) { gp.Flatten(); PointF lastPoint = PointF.Empty; Primitives.Path path = new Primitives.Path(); path.Pen = source.Pen; path.Brush = source.Brush; for( int i = 0; i < gp.PointCount; ++i ) { PointF point = gp.PathPoints[i]; byte type = gp.PathTypes[i]; PointF nextPoint = point; if( i < gp.PointCount - 1 && gp.PathTypes[i + 1] == 1 ) { nextPoint = gp.PathPoints[i + 1]; } switch( type ) { case 0: { path.Add( new Primitives.Path.Move( new Types.Point( point.X, point.Y ) ) ); break; } case 1: { bool first = (i == 0) || gp.PathTypes[i - 1] != 1; bool last = (i == gp.PointCount - 1) || gp.PathTypes[i + 1] != 1; if( first || last || Math.Sqrt( Math.Pow( point.X - lastPoint.X, 2 ) + Math.Pow( point.Y - lastPoint.Y, 2 ) ) > _accuracy || Math.Sqrt( Math.Pow( point.X - nextPoint.X, 2 ) + Math.Pow( point.Y - nextPoint.Y, 2 ) ) > _accuracy ) { path.Add( new Primitives.Path.Line( new Types.Point( point.X, point.Y ) ) ); lastPoint = point; } break; } case 129: { path.Add( new Primitives.Path.Line( new Types.Point( point.X, point.Y ) ) ); path.Add( new Primitives.Path.Close() ); break; } default: throw new InvalidOperationException(); } } return path; } }