Esempio n. 1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
            string strValue = value as string;

            if (strValue != null) {

                string text = strValue.Trim();

                // Expected string format: "name[, size[, units[, style=style1[, style2[...]]]]]"
                // Example using 'vi-VN' culture: "Microsoft Sans Serif, 8,25pt, style=Italic, Bold"
                
                if (text.Length == 0) {
                    return null;
                }
                else {
                    if (culture == null) {
                        culture = CultureInfo.CurrentCulture;
                    } 
                                       
                    char sep = culture.TextInfo.ListSeparator[0];   // For vi-VN: ','
                    string name = text; // start with the assumption that only the font name was provided.
                    string styleStr = null;
                    string sizeStr  = null;
                    float size = 8.25f;
                    FontStyle style = FontStyle.Regular;
                    GraphicsUnit units =  GraphicsUnit.Point;

                    // Get the index of the first separator (would indicate the end of the name in the string).
                    int nameIndex = text.IndexOf(sep); 

                    if( nameIndex > 0 ){ // some parameters provided in addtion to name.
                        name = text.Substring(0, nameIndex);

                        if( nameIndex < text.Length - 1 ){ // other parameters provided.
                            // Get the style index (if any).  The size is a bit problematic because it can be formatted differently 
                            // depending on the culture, we'll parse it last.
                            int styleIndex = text.IndexOf(styleHdr);
                
                            if (styleIndex != -1) { // style found.
                                styleStr = text.Substring(styleIndex, text.Length - styleIndex);
                        
                                // Expected style format ~ "style=Italic, Bold"
                                if (!styleStr.StartsWith(styleHdr)){
                                    throw GetFormatException(text, sep);
                                }

                                // Get the mid-substring containing the size information.
                                sizeStr = text.Substring(nameIndex + 1, styleIndex - nameIndex - 1);
                            }
                            else { // no style.
                                sizeStr = text.Substring(nameIndex + 1, text.Length - nameIndex - 1);
                            }

                            // Parse size.
                            string[] unitTokens = ParseSizeTokens(sizeStr, sep);

                            if (unitTokens[0] != null) {
                                try{
                                    size = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(context, culture, unitTokens[0]); 
                                }
                                catch{  // Exception from converter is too generic.
                                    throw GetFormatException(text, sep);
                                }
                            }

                            if (unitTokens[1] != null) {
                                // ParseGraphicsUnits throws an ArgumentException if format is invalid.
                                units = ParseGraphicsUnits(unitTokens[1]);
                            }

                            if (styleStr != null) {
                                int eqIndex = styleStr.IndexOf("=");
                                styleStr = styleStr.Substring(eqIndex + 1, styleStr.Length - styleHdr.Length);
                                string[] styleTokens = styleStr.Split(sep);

                                for (int tokenCount = 0; tokenCount < styleTokens.Length; tokenCount++) {
                                    string styleText = styleTokens[tokenCount];
                                    styleText = styleText.Trim();
                                    // Note: Enum.Parse will throw InvalidEnumArgumentException if the styleText is not valid but could
                                    // throw other exceptions depending on the string passed.
                                    try{
                                        style |= (FontStyle)Enum.Parse(typeof(FontStyle), styleText, true);
                                    }
                                    catch(Exception ex ){
                                        if( ex is InvalidEnumArgumentException ){
                                            throw;
                                        }
                                        throw GetFormatException(text, sep);
                                    }

                                    // Enum.IsDefined doesn't do what we want on flags enums...
                                    FontStyle validBits = FontStyle.Regular | FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout;
                                    if ((style | validBits) != validBits){
                                        throw new InvalidEnumArgumentException("style", (int)style, typeof(FontStyle));
                                    }
                                }
                            }
                        }
                    }

                    if (fontNameConverter == null) {
                        fontNameConverter = new FontNameConverter();
                    }

                    // should get cached version from TypeDescriptor                                                                                                
                    name = (string)(fontNameConverter.ConvertFrom(context, culture, name));
                    
                    // Name is the only parameter not validated, if it is invalid a default Font will be created.
                    return new Font(name, size, style, units);
                }
            }

            return base.ConvertFrom(context, culture, value);
        }
Esempio n. 2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string strValue = value as string;

            if (strValue != null)
            {
                string text = strValue.Trim();

                // Expected string format: "name[, size[, units[, style=style1[, style2[...]]]]]"
                // Example using 'vi-VN' culture: "Microsoft Sans Serif, 8,25pt, style=Italic, Bold"

                if (text.Length == 0)
                {
                    return(null);
                }
                else
                {
                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }

                    char         sep      = culture.TextInfo.ListSeparator[0]; // For vi-VN: ','
                    string       name     = text;                              // start with the assumption that only the font name was provided.
                    string       styleStr = null;
                    string       sizeStr  = null;
                    float        size     = 8.25f;
                    FontStyle    style    = FontStyle.Regular;
                    GraphicsUnit units    = GraphicsUnit.Point;

                    // Get the index of the first separator (would indicate the end of the name in the string).
                    int nameIndex = text.IndexOf(sep);

                    if (nameIndex > 0)   // some parameters provided in addtion to name.
                    {
                        name = text.Substring(0, nameIndex);

                        if (nameIndex < text.Length - 1)   // other parameters provided.
                        // Get the style index (if any).  The size is a bit problematic because it can be formatted differently
                        // depending on the culture, we'll parse it last.
                        {
                            int styleIndex = text.IndexOf(styleHdr);

                            if (styleIndex != -1)   // style found.
                            {
                                styleStr = text.Substring(styleIndex, text.Length - styleIndex);

                                // Expected style format ~ "style=Italic, Bold"
                                if (!styleStr.StartsWith(styleHdr))
                                {
                                    throw GetFormatException(text, sep);
                                }

                                // Get the mid-substring containing the size information.
                                sizeStr = text.Substring(nameIndex + 1, styleIndex - nameIndex - 1);
                            }
                            else   // no style.
                            {
                                sizeStr = text.Substring(nameIndex + 1, text.Length - nameIndex - 1);
                            }

                            // Parse size.
                            string[] unitTokens = ParseSizeTokens(sizeStr, sep);

                            if (unitTokens[0] != null)
                            {
                                try{
                                    size = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(context, culture, unitTokens[0]);
                                }
                                catch {  // Exception from converter is too generic.
                                    throw GetFormatException(text, sep);
                                }
                            }

                            if (unitTokens[1] != null)
                            {
                                // ParseGraphicsUnits throws an ArgumentException if format is invalid.
                                units = ParseGraphicsUnits(unitTokens[1]);
                            }

                            if (styleStr != null)
                            {
                                int eqIndex = styleStr.IndexOf("=");
                                styleStr = styleStr.Substring(eqIndex + 1, styleStr.Length - styleHdr.Length);
                                string[] styleTokens = styleStr.Split(sep);

                                for (int tokenCount = 0; tokenCount < styleTokens.Length; tokenCount++)
                                {
                                    string styleText = styleTokens[tokenCount];
                                    styleText = styleText.Trim();
                                    // Note: Enum.Parse will throw InvalidEnumArgumentException if the styleText is not valid but could
                                    // throw other exceptions depending on the string passed.
                                    try{
                                        style |= (FontStyle)Enum.Parse(typeof(FontStyle), styleText, true);
                                    }
                                    catch (Exception ex) {
                                        if (ex is InvalidEnumArgumentException)
                                        {
                                            throw;
                                        }
                                        throw GetFormatException(text, sep);
                                    }

                                    // Enum.IsDefined doesn't do what we want on flags enums...
                                    FontStyle validBits = FontStyle.Regular | FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout;
                                    if ((style | validBits) != validBits)
                                    {
                                        throw new InvalidEnumArgumentException("style", (int)style, typeof(FontStyle));
                                    }
                                }
                            }
                        }
                    }

                    if (fontNameConverter == null)
                    {
                        fontNameConverter = new FontNameConverter();
                    }

                    // should get cached version from TypeDescriptor
                    name = (string)(fontNameConverter.ConvertFrom(context, culture, name));

                    // Name is the only parameter not validated, if it is invalid a default Font will be created.
                    return(new Font(name, size, style, units));
                }
            }

            return(base.ConvertFrom(context, culture, value));
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string str = value as string;

            if (str == null)
            {
                return(base.ConvertFrom(context, culture, value));
            }
            string text = str.Trim();

            if (text.Length == 0)
            {
                return(null);
            }
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }
            char         ch      = culture.TextInfo.ListSeparator[0];
            string       str3    = text;
            string       str4    = null;
            string       str5    = null;
            float        emSize  = 8.25f;
            FontStyle    regular = FontStyle.Regular;
            GraphicsUnit point   = GraphicsUnit.Point;
            int          index   = text.IndexOf(ch);

            if (index > 0)
            {
                str3 = text.Substring(0, index);
                if (index < (text.Length - 1))
                {
                    int startIndex = text.IndexOf("style=");
                    if (startIndex != -1)
                    {
                        str4 = text.Substring(startIndex, text.Length - startIndex);
                        if (!str4.StartsWith("style="))
                        {
                            throw this.GetFormatException(text, ch);
                        }
                        str5 = text.Substring(index + 1, (startIndex - index) - 1);
                    }
                    else
                    {
                        str5 = text.Substring(index + 1, (text.Length - index) - 1);
                    }
                    string[] strArray = this.ParseSizeTokens(str5, ch);
                    if (strArray[0] != null)
                    {
                        try
                        {
                            emSize = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(context, culture, strArray[0]);
                        }
                        catch
                        {
                            throw this.GetFormatException(text, ch);
                        }
                    }
                    if (strArray[1] != null)
                    {
                        point = this.ParseGraphicsUnits(strArray[1]);
                    }
                    if (str4 != null)
                    {
                        int num4 = str4.IndexOf("=");
                        foreach (string str6 in str4.Substring(num4 + 1, str4.Length - "style=".Length).Split(new char[] { ch }))
                        {
                            str6 = str6.Trim();
                            try
                            {
                                regular |= (FontStyle)Enum.Parse(typeof(FontStyle), str6, true);
                            }
                            catch (Exception exception)
                            {
                                if (exception is InvalidEnumArgumentException)
                                {
                                    throw;
                                }
                                throw this.GetFormatException(text, ch);
                            }
                            FontStyle style2 = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic | FontStyle.Bold;
                            if ((regular | style2) != style2)
                            {
                                throw new InvalidEnumArgumentException("style", (int)regular, typeof(FontStyle));
                            }
                        }
                    }
                }
            }
            if (this.fontNameConverter == null)
            {
                this.fontNameConverter = new FontNameConverter();
            }
            return(new Font((string)this.fontNameConverter.ConvertFrom(context, culture, str3), emSize, regular, point));
        }
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     string str = value as string;
     if (str == null)
     {
         return base.ConvertFrom(context, culture, value);
     }
     string text = str.Trim();
     if (text.Length == 0)
     {
         return null;
     }
     if (culture == null)
     {
         culture = CultureInfo.CurrentCulture;
     }
     char ch = culture.TextInfo.ListSeparator[0];
     string str3 = text;
     string str4 = null;
     string str5 = null;
     float emSize = 8.25f;
     FontStyle regular = FontStyle.Regular;
     GraphicsUnit point = GraphicsUnit.Point;
     int index = text.IndexOf(ch);
     if (index > 0)
     {
         str3 = text.Substring(0, index);
         if (index < (text.Length - 1))
         {
             int startIndex = text.IndexOf("style=");
             if (startIndex != -1)
             {
                 str4 = text.Substring(startIndex, text.Length - startIndex);
                 if (!str4.StartsWith("style="))
                 {
                     throw this.GetFormatException(text, ch);
                 }
                 str5 = text.Substring(index + 1, (startIndex - index) - 1);
             }
             else
             {
                 str5 = text.Substring(index + 1, (text.Length - index) - 1);
             }
             string[] strArray = this.ParseSizeTokens(str5, ch);
             if (strArray[0] != null)
             {
                 try
                 {
                     emSize = (float) TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(context, culture, strArray[0]);
                 }
                 catch
                 {
                     throw this.GetFormatException(text, ch);
                 }
             }
             if (strArray[1] != null)
             {
                 point = this.ParseGraphicsUnits(strArray[1]);
             }
             if (str4 != null)
             {
                 int num4 = str4.IndexOf("=");
                 foreach (string str6 in str4.Substring(num4 + 1, str4.Length - "style=".Length).Split(new char[] { ch }))
                 {
                     str6 = str6.Trim();
                     try
                     {
                         regular |= (FontStyle) Enum.Parse(typeof(FontStyle), str6, true);
                     }
                     catch (Exception exception)
                     {
                         if (exception is InvalidEnumArgumentException)
                         {
                             throw;
                         }
                         throw this.GetFormatException(text, ch);
                     }
                     FontStyle style2 = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic | FontStyle.Bold;
                     if ((regular | style2) != style2)
                     {
                         throw new InvalidEnumArgumentException("style", (int) regular, typeof(FontStyle));
                     }
                 }
             }
         }
     }
     if (this.fontNameConverter == null)
     {
         this.fontNameConverter = new FontNameConverter();
     }
     return new Font((string) this.fontNameConverter.ConvertFrom(context, culture, str3), emSize, regular, point);
 }