using System;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svg
{
///
/// It is often desirable to specify that a given set of graphics stretch to fit a particular container element. The viewBox attribute provides this capability.
///
[TypeConverter(typeof(SvgViewBoxConverter))]
public struct SvgViewBox
{
public static readonly SvgViewBox Empty = new SvgViewBox(-1, -1, -1, -1);
///
/// Gets or sets the position where the viewport starts horizontally.
///
public float MinX
{
get;
set;
}
///
/// Gets or sets the position where the viewport starts vertically.
///
public float MinY
{
get;
set;
}
///
/// Gets or sets the width of the viewport.
///
public float Width
{
get;
set;
}
///
/// Gets or sets the height of the viewport.
///
public float Height
{
get;
set;
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator RectangleF(SvgViewBox value)
{
return new RectangleF(value.MinX, value.MinY, value.Width, value.Height);
}
///
/// Initializes a new instance of the struct.
///
/// The min X.
/// The min Y.
/// The width.
/// The height.
public SvgViewBox(float minX, float minY, float width, float height) : this()
{
this.MinX = minX;
this.MinY = minY;
this.Width = width;
this.Height = height;
}
}
internal class SvgViewBoxConverter : TypeConverter
{
///
/// Converts the given object to the type of this converter, using the specified context and culture information.
///
/// An that provides a format context.
/// The to use as the current culture.
/// The to convert.
///
/// An that represents the converted value.
///
/// The conversion cannot be performed.
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
string[] coords = ((string)value).Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (coords.Length != 4)
{
throw new SvgException("The 'viewBox' attribute must be in the format 'minX, minY, width, height'.");
}
return new SvgViewBox(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2]), float.Parse(coords[3]));
}
return base.ConvertFrom(context, culture, value);
}
}
}