using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Xml.Serialization; using CPF.Drawing; using CPF.ReoGrid.Drawing.Text; namespace CPF.ReoGrid.Common { internal class TextFormatHelper { public static string EncodeRect(Rect rect) { return string.Format("({0},{1},{2},{3})", new object[] { rect.Left, rect.Top, rect.Width, rect.Height }); } public static string EncodePadding(PaddingValue pad) { return string.Format("({0},{1},{2},{3})", new object[] { pad.Left, pad.Top, pad.Right, pad.Bottom }); } public static string EncodeSize(Size size) { return string.Format("({0},{1})", size.Width, size.Height); } public static string EncodeColor(Color c) { return (c.A == byte.MaxValue) ? string.Format("#{0:x2}{1:x2}{2:x2}", c.R, c.G, c.B) : string.Format("#{0:x2}{1:x2}{2:x2}{3:x2}", new object[] { c.A, c.R, c.G, c.B }); } public static string EncodeColorNoAlpha(Color c) { return string.Format("FF{0:x2}{1:x2}{2:x2}", c.R, c.G, c.B).ToUpper(); } public static string EncodePoint(Point p) { return string.Format("({0},{1})", p.X, p.Y); } public static string EncodePoints(IEnumerable points) { StringBuilder stringBuilder = new StringBuilder(); foreach (Point point in points) { bool flag = stringBuilder.Length > 0; if (flag) { stringBuilder.Append(","); } stringBuilder.AppendFormat("{0} {1}", point.X, point.Y); } return stringBuilder.ToString(); } public static string EncodePointsHex(IEnumerable points) { StringBuilder stringBuilder = new StringBuilder(); foreach (Point point in points) { byte[] bytes = BitConverter.GetBytes(point.X); byte[] bytes2 = BitConverter.GetBytes(point.Y); stringBuilder.AppendFormat("{0:x1}{1:x1}{2:x1}{3:x1}{4:x1}{5:x1}{6:x1}{7:x1}", new object[] { bytes[0], bytes[1], bytes[2], bytes[3], bytes2[0], bytes2[1], bytes2[2], bytes2[3] }); } return stringBuilder.ToString(); } public static string EncodeFontStyle(CPF.ReoGrid.Drawing.Text.FontStyles fs) { StringBuilder stringBuilder = new StringBuilder(); bool flag = (fs & CPF.ReoGrid.Drawing.Text.FontStyles.Bold) > CPF.ReoGrid.Drawing.Text.FontStyles.Regular; if (flag) { stringBuilder.Append("blob"); } bool flag2 = (fs & CPF.ReoGrid.Drawing.Text.FontStyles.Italic) > CPF.ReoGrid.Drawing.Text.FontStyles.Regular; if (flag2) { bool flag3 = stringBuilder.Length > 0; if (flag3) { stringBuilder.Append(" "); } stringBuilder.Append("italic"); } bool flag4 = (fs & CPF.ReoGrid.Drawing.Text.FontStyles.Strikethrough) > CPF.ReoGrid.Drawing.Text.FontStyles.Regular; if (flag4) { bool flag5 = stringBuilder.Length > 0; if (flag5) { stringBuilder.Append(" "); } stringBuilder.Append("strikeout"); } bool flag6 = (fs & CPF.ReoGrid.Drawing.Text.FontStyles.Underline) > CPF.ReoGrid.Drawing.Text.FontStyles.Regular; if (flag6) { bool flag7 = stringBuilder.Length > 0; if (flag7) { stringBuilder.Append(" "); } stringBuilder.Append("underline"); } return (stringBuilder.Length == 0) ? "normal" : stringBuilder.ToString(); } public static string EncodeBool(bool p) { return p ? "true" : "false"; } public static string EncodeBool(bool p, bool def) { return (p == def) ? null : TextFormatHelper.EncodeBool(p); } public static string EncodeFloatArray(params float[] values) { StringBuilder stringBuilder = new StringBuilder(); foreach (float value in values) { bool flag = stringBuilder.Length > 0; if (flag) { stringBuilder.Append(","); } stringBuilder.Append(value); } return stringBuilder.ToString(); } public static string EncodeIntArray(IEnumerable values) { StringBuilder stringBuilder = new StringBuilder(); foreach (int num in values) { float value = (float)num; bool flag = stringBuilder.Length > 0; if (flag) { stringBuilder.Append(","); } stringBuilder.Append(value); } return stringBuilder.ToString(); } public static Guid DecodeGuid(string data) { Guid result; try { result = new Guid(data); } catch { result = Guid.Empty; } return result; } public static Rect DecodeRect(string data) { Match match = TextFormatHelper.RectRegex.Match(data); bool success = match.Success; Rect result; if (success) { float num = (float)TextFormatHelper.GetPixelValue(match.Groups[1].Value); float num2 = (float)TextFormatHelper.GetPixelValue(match.Groups[2].Value); float num3 = (float)TextFormatHelper.GetPixelValue(match.Groups[3].Value); float num4 = (float)TextFormatHelper.GetPixelValue(match.Groups[4].Value); result = new Rect(ref num, ref num2, ref num3, ref num4); } else { result = default(Rect); } return result; } public static PaddingValue DecodePadding(string data) { Rect rect = TextFormatHelper.DecodeRect(data); return new PaddingValue((float)((int)rect.Left), (float)((int)rect.Top), (float)((int)rect.Width), (float)((int)rect.Height)); } public static Size DecodeSize(string data) { Point point = TextFormatHelper.DecodePoint(data); float x = point.X; float y = point.Y; return new Size(ref x, ref y); } public static bool IsWebColorFormat(string data) { return TextFormatHelper.WebColorRegex.IsMatch(data); } public static bool DecodeColor(string data, out Color color) { bool flag = !string.IsNullOrEmpty(data) && !data.Equals("none", StringComparison.InvariantCultureIgnoreCase); if (flag) { Match match = TextFormatHelper.WebColorRegex.Match(data); bool success = match.Success; if (success) { color = Color.FromArgb((match.Groups[1].Value.Length > 0) ? Convert.ToByte(match.Groups[1].Value, 16) : byte.MaxValue, Convert.ToByte(match.Groups[2].Value, 16), Convert.ToByte(match.Groups[3].Value, 16), Convert.ToByte(match.Groups[4].Value, 16)); return true; } try { color = (Color)ColorConverter.ConvertFromString(data); return true; } catch { } } color = default(Color); return false; } public static bool IsRectFormat(string data) { return TextFormatHelper.RectRegex.IsMatch(data); } public static Point DecodePoint(string data) { Match match = TextFormatHelper.PointRegex.Match(data); bool success = match.Success; Point result; if (success) { float num = (float)TextFormatHelper.GetPixelValue(match.Groups[1].Value); float num2 = (float)TextFormatHelper.GetPixelValue(match.Groups[2].Value); result = new Point(ref num, ref num2); } else { float num = 0f; float num2 = 0f; result = new Point(ref num, ref num2); } return result; } public static List DecodePoints(string data) { List list = new List(5); foreach (object obj in TextFormatHelper.pointArrayRegex.Matches(data)) { Match match = (Match)obj; List list2 = list; float floatPixelValue = TextFormatHelper.GetFloatPixelValue(match.Groups[1].Value, 0f); float floatPixelValue2 = TextFormatHelper.GetFloatPixelValue(match.Groups[2].Value, 0f); list2.Add(new Point(ref floatPixelValue, ref floatPixelValue2)); } return list; } public static float[] DecodeFloatArray(string str) { string[] array = str.Split(new char[] { ',' }); float[] array2 = new float[array.Length]; for (int i = 0; i < array.Length; i++) { try { float num; float.TryParse(array[i], out num); array2[i] = num; } catch { } } return array2; } public static int[] DecodeIntArray(string str) { string[] array = str.Split(new char[] { ',' }); int[] array2 = new int[array.Length]; for (int i = 0; i < array.Length; i++) { try { int num; int.TryParse(array[i], out num); array2[i] = num; } catch { } } return array2; } public static float GetFloatValue(string str, float def) { float.TryParse(str, out def); return def; } public static float GetFloatValue(string str, float def, CultureInfo culture) { float.TryParse(str, NumberStyles.Any, culture, out def); return def; } public static float GetFloatPixelValue(string str, float def) { return TextFormatHelper.GetFloatPixelValue(str, def, CultureInfo.CurrentCulture); } public static float GetFloatPixelValue(string str, float def, CultureInfo culture) { bool flag = str == null; float result; if (flag) { result = def; } else { str = str.Trim(); bool flag2 = str.EndsWith("px"); if (flag2) { str = str.Substring(0, str.Length - 2).Trim(); } float num; float.TryParse(str, NumberStyles.Any, culture, out num); result = num; } return result; } public static int GetPixelValue(string str) { return TextFormatHelper.GetPixelValue(str, 0); } public static int GetPixelValue(string str, CultureInfo culture) { return TextFormatHelper.GetPixelValue(str, 0, culture); } public static int GetPixelValue(string str, int def) { return (int)TextFormatHelper.GetFloatPixelValue(str, (float)def, CultureInfo.CurrentCulture); } public static int GetPixelValue(string str, int def, CultureInfo culture) { return (int)TextFormatHelper.GetFloatPixelValue(str, (float)def, culture); } public static float GetPercentValue(string str, float def) { bool flag = string.IsNullOrEmpty(str); float result; if (flag) { result = def; } else { string str2 = str.Substring(0, str.Length - 1).Trim(); result = TextFormatHelper.GetFloatValue(str2, def) / 100f; } return result; } public static void ParseDataAttribute(string attr, Action a) { bool flag = attr != null; if (flag) { foreach (object obj in TextFormatHelper.AttrRegex.Matches(attr)) { Match match = (Match)obj; string value = match.Groups[1].Value; string arg = (match.Groups[5].Value != null && match.Groups[5].Length > 0) ? match.Groups[5].Value : match.Groups[4].Value; a(value, arg); } } } public static T CreateElementFromAttribute(T obj, string attr) where T : new() { T t = (obj == null) ? Activator.CreateInstance() : obj; TextFormatHelper.ParseDataAttribute(attr, delegate(string key, string value) { bool flag = key.Length > 0 && value != null; if (flag) { FieldInfo field = t.GetType().GetField(key); bool flag2 = field != null; if (flag2) { field.SetValue(t, value); } else { key = key.Substring(0, 1).ToUpper() + key.Substring(1); PropertyInfo propertyInfo = t.GetType().GetProperty(key); bool flag3 = propertyInfo == null; if (flag3) { propertyInfo = t.GetType().GetProperties().FirstOrDefault(delegate(PropertyInfo p) { XmlAttributeAttribute[] array = p.GetCustomAttributes(typeof(XmlAttributeAttribute), true) as XmlAttributeAttribute[]; return array != null && array.Length != 0 && array[0] != null && array[0].AttributeName.ToLower().Equals(key.ToLower()); }); } bool flag4 = propertyInfo != null; if (flag4) { propertyInfo.SetValue(t, value, null); } } } }); return t; } public static string GenerateDataAttributeString(Dictionary data) { StringBuilder stringBuilder = new StringBuilder(); foreach (string text in data.Keys) { bool flag = stringBuilder.Length > 0; if (flag) { stringBuilder.Append("; "); } stringBuilder.Append(string.Format("{0}: {1}", text, data[text])); } return stringBuilder.ToString(); } public static string GenerateDataAttributeString(params string[] data) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i += 2) { bool flag = stringBuilder.Length > 0; if (flag) { stringBuilder.Append(" "); } stringBuilder.Append(string.Format("{0}: {1};", data[i], data[i + 1])); } return stringBuilder.ToString(); } public static bool IsSwitchOn(string value) { return !string.IsNullOrEmpty(value) && (string.Equals(value, "1", StringComparison.CurrentCultureIgnoreCase) || string.Equals(value, "true", StringComparison.CurrentCultureIgnoreCase) || string.Equals(value, "yes", StringComparison.CurrentCultureIgnoreCase) || string.Equals(value, "on", StringComparison.CurrentCultureIgnoreCase)); } public static T LoadXml(string filepath) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); T result; using (FileStream fileStream = new FileStream(filepath, FileMode.Open)) { T t = (T)((object)xmlSerializer.Deserialize(fileStream)); result = t; } return result; } public static readonly Regex RectRegex = new Regex("\\(\\s*([-\\w.]+)\\s*,\\s*([-\\w.]+)\\s*,\\s*([-\\w.]+)\\s*,\\s*([-\\w.]+)\\)\\s*"); public static readonly Regex WebColorRegex = new Regex("\\#?([0-9a-fA-F]{2})?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})"); public static readonly Regex PointRegex = new Regex("\\(\\s*([-\\w.]+)\\s*,\\s*([-\\w.]+)\\)\\s*"); public static readonly Regex pointArrayRegex = new Regex("(\\d*\\.?\\d+)\\s(\\d*\\.?\\d+)"); public static readonly Regex PathDataRegex = new Regex("(\\w?)\\(([-?\\d+,?]+)\\),?"); public static readonly Regex AttrRegex = new Regex("\\s*([-_\\w.]+)\\s*:\\s*((\\'([^\\']*)\\')|([^;]*))\\s*;?"); } }