RaUI/Source/ryControls/HtmlRenderer/Adapters/GraphicsPathAdapter.cs

93 lines
2.9 KiB
C#
Raw Normal View History

// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.Drawing.Drawing2D;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
{
/// <summary>
/// Adapter for WinForms graphics path object for core.
/// </summary>
internal sealed class GraphicsPathAdapter : RGraphicsPath
{
/// <summary>
/// The actual WinForms graphics path instance.
/// </summary>
private readonly GraphicsPath _graphicsPath = new GraphicsPath();
/// <summary>
/// the last point added to the path to begin next segment from
/// </summary>
private RPoint _lastPoint;
/// <summary>
/// The actual WinForms graphics path instance.
/// </summary>
public GraphicsPath GraphicsPath
{
get { return _graphicsPath; }
}
public override void Start(double x, double y)
{
_lastPoint = new RPoint(x, y);
}
public override void LineTo(double x, double y)
{
_graphicsPath.AddLine((float)_lastPoint.X, (float)_lastPoint.Y, (float)x, (float)y);
_lastPoint = new RPoint(x, y);
}
public override void ArcTo(double x, double y, double size, Corner corner)
{
float left = (float)(Math.Min(x, _lastPoint.X) - (corner == Corner.TopRight || corner == Corner.BottomRight ? size : 0));
float top = (float)(Math.Min(y, _lastPoint.Y) - (corner == Corner.BottomLeft || corner == Corner.BottomRight ? size : 0));
_graphicsPath.AddArc(left, top, (float)size * 2, (float)size * 2, GetStartAngle(corner), 90);
_lastPoint = new RPoint(x, y);
}
public override void Dispose()
{
_graphicsPath.Dispose();
}
/// <summary>
/// Get arc start angle for the given corner.
/// </summary>
private static int GetStartAngle(Corner corner)
{
int startAngle;
switch (corner)
{
case Corner.TopLeft:
startAngle = 180;
break;
case Corner.TopRight:
startAngle = 270;
break;
case Corner.BottomLeft:
startAngle = 90;
break;
case Corner.BottomRight:
startAngle = 0;
break;
default:
throw new ArgumentOutOfRangeException("corner");
}
return startAngle;
}
}
}