// Performs the conversion from HtmlFontProperty to a string (only) 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)); }
// compares two Html Fonts for equality // equality opertors not defined (Design Time issue with override of Equals) public static bool IsEqual(HtmlFontProperty font1, HtmlFontProperty font2) { // assume not equal bool _equals = false; // perform the comparsion if (IsNotNull(font1) && 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); }
// Performs the conversion from string to a HtmlFontProperty (only) 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 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 string _fontName = _match.Result(FONT_PARSE_NAME); string _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)); } }
// based on a font name being null the font can be assumed to be null // default constructor will give a null object public static bool IsNotNull(HtmlFontProperty font) { return(!IsNull(font)); }
// based on a font name being null the font can be assumed to be null // default constructor will give a null object public static bool IsNull(HtmlFontProperty font) { return(font.Name == null || font.Name.Trim() == string.Empty); }
// compares two Html Fonts for equality // equality opertors not defined (Design Time issue with override of Equals) public static bool IsNotEqual(HtmlFontProperty font1, HtmlFontProperty font2) { return(!IsEqual(font1, font2)); }