Beispiel #1
0
        } //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
Beispiel #2
0
        } //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