Exemple #1
0
        internal static int ConvertKey(object key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            int value;

            string s = key as string;

            if (s != null)
            {
                int i = 0;
                if (!FontFamilyMap.ParseHexNumber(s, ref i, out value) || i < s.Length)
                {
                    throw new ArgumentException(SR.Get(SRID.CannotConvertStringToType, s, "int"), "key");
                }
            }
            else if (key is int)
            {
                value = (int)key;
            }
            else
            {
                throw new ArgumentException(SR.Get(SRID.CannotConvertType, key.GetType(), "int"), "key");
            }

            if (value < 0 || value > FontFamilyMap.LastUnicodeScalar)
            {
                throw new ArgumentException(SR.Get(SRID.CodePointOutOfRange, value), "key");
            }

            return(value);
        }
        /// <summary>
        /// Called by FontFamilyMapCollection when a FontFamilyMap is being added.
        /// </summary>
        internal void PrepareToAddFamilyMap(FontFamilyMap familyMap)
        {
            // Validate parameters.
            if (familyMap == null)
                throw new ArgumentNullException("familyMap");

            if (string.IsNullOrEmpty(familyMap.Target))
                throw new ArgumentException(SR.Get(SRID.FamilyMap_TargetNotSet));

            // If it's culture-specific make sure it's in the hash table.
            if (familyMap.Language != null)
            {
                if (_familyMapRangesByLanguage == null)
                {
                    _familyMapRangesByLanguage = new Dictionary<XmlLanguage, ushort[]>(InitialCultureCount);
                    _familyMapRangesByLanguage.Add(familyMap.Language, EmptyFamilyMapRanges);
                }
                else if (!_familyMapRangesByLanguage.ContainsKey(familyMap.Language))
                {
                    _familyMapRangesByLanguage.Add(familyMap.Language, EmptyFamilyMapRanges);
                }
            }
        }
        /// <summary>
        /// ConvertTo - Converts the specified object to an instance of the specified type.
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (null == value)
            {
                throw new ArgumentNullException("value");
            }

            FontFamily fontFamily = value as FontFamily;

            if (fontFamily == null)
            {
                throw new ArgumentException(SR.Get(SRID.General_Expected_Type, "FontFamily"), "value");
            }

            if (null == destinationType)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType == typeof(string))
            {
                if (fontFamily.Source != null)
                {
                    // Usual case: it's a named font family.
                    return(fontFamily.Source);
                }
                else
                {
                    // If client calls typeConverter.CanConvertTo(typeof(string)) then we'll return
                    // true always, even though we don't have access to the FontFamily instance; so
                    // we need to be able to return some kind of family name even if Source==null.
                    string name = null;

                    CultureInfo parentCulture = null;
                    if (culture != null)
                    {
                        if (culture.Equals(CultureInfo.InvariantCulture))
                        {
                            culture = null;
                        }
                        else
                        {
                            parentCulture = culture.Parent;
                            if (parentCulture != null &&
                                (parentCulture.Equals(CultureInfo.InvariantCulture) || parentCulture == culture))
                            {
                                parentCulture = null;
                            }
                        }
                    }

                    // Try looking up the name in the FamilyNames dictionary.
                    LanguageSpecificStringDictionary names = fontFamily.FamilyNames;

                    if (culture != null && names.TryGetValue(XmlLanguage.GetLanguage(culture.IetfLanguageTag), out name))
                    {
                        // LanguageSpecificStringDictionary does not allow null string to be added.
                        Debug.Assert(name != null);
                    }
                    else if (parentCulture != null && names.TryGetValue(XmlLanguage.GetLanguage(parentCulture.IetfLanguageTag), out name))
                    {
                        // LanguageSpecificStringDictionary does not allow null string to be added.
                        Debug.Assert(name != null);
                    }
                    else if (names.TryGetValue(XmlLanguage.Empty, out name))
                    {
                        // LanguageSpecificStringDictionary does not allow null string to be added.
                        Debug.Assert(name != null);
                    }
                    else
                    {
                        // Try the first target font compatible with the culture.
                        foreach (FontFamilyMap familyMap in fontFamily.FamilyMaps)
                        {
                            if (FontFamilyMap.MatchCulture(familyMap.Language, culture))
                            {
                                name = familyMap.Target;
                                break;
                            }
                        }

                        // Use global ui as a last resort.
                        if (name == null)
                        {
                            name = FontFamily.GlobalUI;
                        }
                    }

                    return(name);
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
        /// <summary>
        /// Parses the FontFamilyMap element, including its attributes and children, 
        /// and advances to the next sibling element.
        /// </summary> 
        private void ParseFamilyMapElement() 
        {
            FontFamilyMap fmap = new FontFamilyMap(); 

            // Parse the family map attributes.
            if (_reader.MoveToFirstAttribute())
            { 
                do
                { 
                    // Process attributes in the composite font namespace; ignore any others. 
                    if (IsCompositeFontAttribute())
                    { 
                        string name = _reader.LocalName;

                        if (name == UnicodeAttribute)
                        { 
                            fmap.Unicode = GetAttributeValue();
                        } 
                        else if (name == TargetAttribute) 
                        {
                            fmap.Target = GetAttributeValue(); 
                        }
                        else if (name == ScaleAttribute)
                        {
                            fmap.Scale = GetAttributeAsDouble(); 
                        }
                        else if (name == LanguageAttribute) 
                        { 
                            fmap.Language = GetAttributeAsXmlLanguage();
                        } 
                        else
                        {
                            FailUnknownAttribute();
                        } 
                    }
                    else if (!IsIgnorableAttribute()) 
                    { 
                        FailUnknownAttribute();
                    } 

                } while (_reader.MoveToNextAttribute());

                _reader.MoveToElement(); 
            }
 
            _compositeFontInfo.FamilyMaps.Add(fmap); 

            // There should be no child elements. 
            ParseEmptyElement();
        }