using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Text.RegularExpressions; namespace Volian.Svg.Library { [TypeConverter(typeof(MeasurementTypeConverter))] public class SvgMeasurement { #region Operators public static SvgMeasurement operator +(SvgMeasurement x1, SvgMeasurement x2) { E_MeasurementUnits tmpUnits = x1.Units; SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) + x2.GetSizeInPoints(DPI),E_MeasurementUnits.PT); tmp.Units = x1.Units; return tmp; } public static SvgMeasurement operator -(SvgMeasurement x1, SvgMeasurement x2) { E_MeasurementUnits tmpUnits = x1.Units; SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) - x2.GetSizeInPoints(DPI), E_MeasurementUnits.PT); tmp.Units = x1.Units; return tmp; } public static SvgMeasurement operator *(SvgMeasurement x1, float scaler) { E_MeasurementUnits tmpUnits = x1.Units; SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) * scaler, E_MeasurementUnits.PT); tmp.Units = x1.Units; return tmp; } public static SvgMeasurement operator *(float scaler, SvgMeasurement x1) { E_MeasurementUnits tmpUnits = x1.Units; SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) * scaler, E_MeasurementUnits.PT); tmp.Units = x1.Units; return tmp; } #endregion #region DPI private static float _DPI; public static float DPI { get { if (_DPI == 0) _DPI = 96; return _DPI; } } #endregion #region ctor public SvgMeasurement() { ;} public SvgMeasurement(int value) { _Value = Convert.ToSingle(value); _Units = E_MeasurementUnits.PX; } public SvgMeasurement(float value) { _Value = value; _Units = E_MeasurementUnits.PX; } public SvgMeasurement(float value, E_MeasurementUnits units) { _Value = value; _Units = units; } public SvgMeasurement(string val) { val = val.Replace("{size}", "10");// B2020-145 BWD SAMG TSG-6 Fix for Base Format {size} parameter rather than font size MatchCollection mc = Regex.Matches(val.ToLower(), "([+-]?[.0-9]+)|(in|cm|mm|em|ex|pc|pt|px|percentage)"); _Value = Convert.ToSingle(mc[0].Value); if (mc.Count == 2) { //Console.WriteLine("{0} - '{1}'", val, mc[1].Value.ToUpper()); _Units = (E_MeasurementUnits)Enum.Parse(typeof(E_MeasurementUnits), mc[1].Value.ToUpper()); } else _Units = E_MeasurementUnits.PX; } #endregion #region Properties private E_MeasurementUnits _Units; public E_MeasurementUnits Units { get { return _Units; } set { float tmp = GetSizeInPoints(DPI); _Units = value; _Value *= (tmp / GetSizeInPoints(DPI)); } } private float _Value; public float Value { get { return _Value; } set { _Value = value; } } #endregion #region ToString public override string ToString() { switch (_Units) { case E_MeasurementUnits.PX: // Default return string.Format("{0}", _Value); case E_MeasurementUnits.PERCENTAGE: // Precentage is preceded by a space return string.Format("{0} {1}", _Value, _Units); } return string.Format("{0}{1}", _Value, _Units); } #endregion //public float SizeInPixels //{ // get // { // switch (_Units) // { // case E_MeasurementUnits.PT: //Points - 72 per inch // return _Value * DPI / 72F; // case E_MeasurementUnits.PC: //Picas - 6 per inch // return _Value * DPI / 6F; // case E_MeasurementUnits.IN: //inches // return _Value * DPI; // case E_MeasurementUnits.CM: // Centimeter // return _Value * DPI / 2.54F; // case E_MeasurementUnits.MM: // Millimeter // return _Value * DPI / 25.4F; // case E_MeasurementUnits.EM: // EMs // return _Value * DPI / 8; // case E_MeasurementUnits.EX: // EXs // return _Value * DPI / 16; // default: // return _Value; // } // } //} public float GetSizeInPoints(float myDPI) { switch (_Units) { case E_MeasurementUnits.PX: //Pixels myDPI per inch return _Value * 72F / myDPI; case E_MeasurementUnits.PC: //Picas - 6 per inch return _Value * 72F / 6F; case E_MeasurementUnits.IN: //inches return _Value * 72F; case E_MeasurementUnits.CM: // Centimeter return _Value * 72F / 2.54F; case E_MeasurementUnits.MM: // Millimeter return _Value * 72F / 25.4F; case E_MeasurementUnits.EM: // EMs return _Value * 72F / 8; case E_MeasurementUnits.EX: // EXs return _Value * 72F / 16; default: // Points return _Value; } } //public float SizeInPoints //{ get { return SizeInPixels * 72 / DPI; } } //public float SizeInInches //{ get { return SizeInPixels / DPI; } } //public float SizeInCentimeters //{ get { return SizeInPixels * 2.54F / DPI; } } //public float SizeInMillimeters //{ get { return SizeInPixels * 25.4F / DPI; } } //public float SizeInEms //{ get { return SizeInPixels * 5 / (6 * DPI); } } //public float SizeInExs //{ get { return SizeInPixels * 5 / (6 * DPI); } } //public float SizeInPicas //{ get { return SizeInPixels * 6 / DPI; } } //public Unit SizeInUnits //{ get { return new Unit((double)SizeInPoints, UnitTypeEnum.Point); } } public float GetSizeInPixels(float myDPI) { return GetSizeInPoints(myDPI) * myDPI / 72F;} public float GetSizeInInches(float myDPI) { return GetSizeInPoints(myDPI) / 72F; } public float GetSizeInCentimeters(float myDPI) { return GetSizeInPoints(myDPI) * 2.54F / 72F; } public float GetSizeInMillimeters(float myDPI) { return GetSizeInPoints(myDPI) * 25.4F / 72F; } public float GetSizeInEms(float myDPI) { return GetSizeInPoints(myDPI) * 5 / (6 * 72F); } public float GetSizeInExs(float myDPI) { return GetSizeInPoints(myDPI) * 5 / (6 * 72F); } public float GetSizeInPicas(float myDPI) { return GetSizeInPoints(myDPI) * 6 / 72F; } //public Unit GetSizeInUnits(float myDPI) //{ return new Unit((double)GetSizeInPoints(myDPI), UnitTypeEnum.Point); } public static float StringToPoints(string value) { SvgMeasurement tmp = new SvgMeasurement(value); float myPoints = tmp.GetSizeInPoints(72); return myPoints; } } public enum E_MeasurementUnits : int { [Description("Pixels")] PX, [Description("Points")] PT, [Description("Inches")] IN, [Description("Centimeters")] CM, [Description("Millimeters")] MM, [Description("EMs")] EM, [Description("EXs")] EX, [Description("Percentage")] PERCENTAGE, [Description("Picas")] PC } public class MeasurementTypeConverter : ExpandableObjectConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type t) { return t == typeof(String); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType) { if (destType == typeof(string) && value is SvgMeasurement) { return value.ToString(); } return base.ConvertTo(context, culture, value, destType); } public override bool CanConvertFrom(ITypeDescriptorContext context, Type t) { return t == typeof(String); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object val) { SvgMeasurement measurement = new SvgMeasurement((string)val); return measurement; } } }