} //CanConvertFrom /// <summary> /// Performs the conversion from string to a HtmlFontProperty (only) /// </summary> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { // define a new font property string fontString = (string)value; HtmlFontProperty font = new HtmlFontProperty(string.Empty);; try { // parse the contents of the given string using a regex string fontName = string.Empty; string fontSize = string.Empty; Regex expression = new Regex(FONT_PARSE_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture); Match match = expression.Match(fontString); // see if a match was found if (match.Success) { // extract the content type elements fontName = match.Result(FONT_PARSE_NAME); fontSize = match.Result(FONT_PARSE_SIZE); // set the fontname TextInfo text = Thread.CurrentThread.CurrentCulture.TextInfo; font.Name = text.ToTitleCase(fontName); // determine size from given string using Small if blank if (fontSize == string.Empty) { fontSize = "Small"; } font.Size = (HtmlFontSize)Enum.Parse(typeof(HtmlFontSize), fontSize, true); } } catch (Exception) { // do nothing but ensure font is a null font font.Name = string.Empty; } if (HtmlFontProperty.IsNull(font)) { // error performing the string conversion so throw exception given possible format string error = string.Format(@"Cannot convert '{0}' to Type HtmlFontProperty. Format: 'FontName, HtmlSize', Font Size values: {1}", fontString, string.Join(", ", Enum.GetNames(typeof(HtmlFontSize)))); throw new ArgumentException(error); } else { // return the font return(font); } } else { return(base.ConvertFrom(context, culture, value)); } } //ConvertFrom
} //CanConvertTo /// <summary> /// Performs the conversion from HtmlFontProperty to a string (only) /// </summary> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { // ensure working with the intented type HtmlFontProperty if (value is HtmlFontProperty) { HtmlFontProperty font = (HtmlFontProperty)value; if (destinationType == typeof(string)) { return(font.ToString()); } if (destinationType == typeof(InstanceDescriptor)) { // define array to hold the properties and values Object[] properties = new Object[8]; Type[] types = new Type[8]; // Name property properties[0] = font.Name; types[0] = typeof(string); // Size property properties[1] = font.Size; types[1] = typeof(HtmlFontSize); // Bold property properties[2] = font.Bold; types[2] = typeof(bool); // Italic property properties[3] = font.Italic; types[3] = typeof(bool); // Underline property properties[4] = font.Underline; types[4] = typeof(bool); // Strikeout property properties[5] = font.Strikeout; types[5] = typeof(bool); // Subscript property properties[6] = font.Subscript; types[6] = typeof(bool); // Superscript property properties[7] = font.Superscript; types[7] = typeof(bool); // create the instance constructor to return ConstructorInfo ci = typeof(HtmlFontProperty).GetConstructor(types); return(new InstanceDescriptor(ci, properties)); } } // have something other than InstanceDescriptor or sting return(base.ConvertTo(context, culture, value, destinationType)); } //ConvertTo
} //ToString /// <summary> /// Compares two Html Fonts for equality /// Equality opertors not defined (Design Time issue with override of Equals) /// </summary> public static bool IsEqual(HtmlFontProperty font1, HtmlFontProperty font2) { // assume not equal bool equals = false; // perform the comparsion if (HtmlFontProperty.IsNotNull(font1) && HtmlFontProperty.IsNotNull(font2)) { if (font1.Name == font2.Name && font1.Size == font2.Size && font1.Bold == font2.Bold && font1.Italic == font2.Italic && font1.Underline == font2.Underline && font1.Strikeout == font2.Strikeout && font1.Subscript == font2.Subscript && font1.Superscript == font2.Superscript) { equals = true; } } // return the calculated value return(equals); } //IsEquals
} //IsNull /// <summary> /// Based on a font name being null the font can be assumed to be null /// Default constructor will give a null object /// </summary> public static bool IsNotNull(HtmlFontProperty font) { return(!HtmlFontProperty.IsNull(font)); } //IsNull
} //IsNotEqual /// <summary> /// Based on a font name being null the font can be assumed to be null /// Default constructor will give a null object /// </summary> public static bool IsNull(HtmlFontProperty font) { return(font.Name == null || font.Name.Trim() == string.Empty); } //IsNull
} //IsEquals /// <summary> /// Compares two Html Fonts for equality /// Equality opertors not defined (Design Time issue with override of Equals) /// </summary> public static bool IsNotEqual(HtmlFontProperty font1, HtmlFontProperty font2) { return(!HtmlFontProperty.IsEqual(font1, font2)); } //IsNotEqual