示例#1
0
        private static Text.TextInterface.Font GetFontFromFamily(Text.TextInterface.FontFamily fontFamily, string faceName)
        {
            faceName = faceName.ToUpper(CultureInfo.InvariantCulture);

            // The search that DWrite supports is a linear search.
            // Look at every font face.
            foreach (Text.TextInterface.Font font in fontFamily)
            {
                // and at every locale name this font face has.
                foreach (KeyValuePair <CultureInfo, string> name in font.FaceNames)
                {
                    string currentFontName = name.Value.ToUpper(CultureInfo.InvariantCulture);
                    if (currentFontName == faceName)
                    {
                        return(font);
                    }
                }
            }

            // This dictionary is used to store the faces (indexed by their names).
            // This dictionary will be used in case the exact string "faceName" was not found,
            // thus we will start again removing words (separated by ' ') from its end and looking
            // for the resulting faceName in that dictionary. So this dictionary is
            // used to speed the search.
            Dictionary <string, Text.TextInterface.Font> faces = new Dictionary <string, Text.TextInterface.Font>();

            //We could have merged this loop with the one above. However this will degrade the performance
            //of the scenario where the user entered  a correct face name (which is the common scenario).
            //Thus we adopt a pay for play approach, meaning that only whenever the face name does not
            //exactly correspond to an actual face name we will incure this overhead.
            foreach (Text.TextInterface.Font font in fontFamily)
            {
                foreach (KeyValuePair <CultureInfo, string> name in font.FaceNames)
                {
                    string currentFontName = name.Value.ToUpper(CultureInfo.InvariantCulture);
                    if (!faces.ContainsKey(currentFontName))
                    {
                        faces.Add(currentFontName, font);
                    }
                }
            }

            // An exact match was not found and so we will start looking for the best match.
            Text.TextInterface.Font matchingFont = null;
            int indexOfSpace = faceName.LastIndexOf(' ');

            while (indexOfSpace > 0)
            {
                faceName = faceName.Substring(0, indexOfSpace);
                if (faces.TryGetValue(faceName, out matchingFont))
                {
                    return(matchingFont);
                }

                indexOfSpace = faceName.LastIndexOf(' ');
            }

            // No match was found.
            return(null);
        }
示例#2
0
 internal GlyphTypeface GetGlyphTypeface(
     FontStyle style,
     FontWeight weight,
     FontStretch stretch
     )
 {
     Text.TextInterface.Font bestMatch = _family.GetFirstMatchingFont((Text.TextInterface.FontWeight)weight.ToOpenTypeWeight(),
                                                                      (Text.TextInterface.FontStretch)stretch.ToOpenTypeStretch(),
                                                                      (Text.TextInterface.FontStyle)style.GetStyleForInternalConstruction());
     Debug.Assert(bestMatch != null);
     return(new GlyphTypeface(bestMatch));
 }
示例#3
0
        internal FontFaceLayoutInfo(Text.TextInterface.Font font)
        {
            _fontTechnologyInitialized           = false;
            _typographyAvailabilitiesInitialized = false;
            _gsubInitialized            = false;
            _gposInitialized            = false;
            _gdefInitialized            = false;
            _embeddingRightsInitialized = false;
            _gsubCache = null;
            _gposCache = null;
            _gsub      = null;
            _gpos      = null;
            _gdef      = null;

            _font = font;
            _cmap = new IntMap(_font);
            _cmap.TryGetValue(' ', out _blankGlyphIndex);
        }
示例#4
0
        internal IFontFamily LookupFamily(
            string familyName,
            ref FontStyle fontStyle,
            ref FontWeight fontWeight,
            ref FontStretch fontStretch
            )
        {
            if (familyName == null || familyName.Length == 0)
            {
                return(null);
            }

            familyName = familyName.Trim();

            // If we are referencing fonts from the system fonts, then it is cheap to lookup the 4 composite fonts
            // that ship with WPF. Also, it happens often that familyName is "Global User Interface".
            // So in this case we preceed looking into SystemComposite Fonts.
            if (UseSystemFonts)
            {
                CompositeFontFamily compositeFamily = SystemCompositeFonts.FindFamily(familyName);
                if (compositeFamily != null)
                {
                    return(compositeFamily);
                }
            }

            Text.TextInterface.FontFamily fontFamilyDWrite = _fontCollection[familyName];

            // A font family was not found in DWrite's font collection.
            if (fontFamilyDWrite == null)
            {
                // Having user defined composite fonts is not very common. So we defer looking into them to looking DWrite
                // (which is opposite to what we do for system fonts).
                if (!UseSystemFonts)
                {
                    // The family name was not found in DWrite's font collection. It may possibly be the name of a composite font
                    // since DWrite does not recognize composite fonts.
                    CompositeFontFamily compositeFamily = LookUpUserCompositeFamily(familyName);
                    if (compositeFamily != null)
                    {
                        return(compositeFamily);
                    }
                }

                // The family name cannot be found. This may possibly be because the family name contains styling info.
                // For example, "Arial Bold"
                // We will strip off the styling info (one word at a time from the end) and try to find the family name.
                int indexOfSpace = -1;
                System.Text.StringBuilder potentialFaceName = new System.Text.StringBuilder();

                // Start removing off strings from the end hoping they are
                // style info so as to get down to the family name.
                do
                {
                    indexOfSpace = familyName.LastIndexOf(' ');
                    if (indexOfSpace < 0)
                    {
                        break;
                    }
                    else
                    {
                        // store the stripped off style names to look for the specific face later.
                        potentialFaceName.Insert(0, familyName.Substring(indexOfSpace));
                        familyName = familyName.Substring(0, indexOfSpace);
                    }

                    fontFamilyDWrite = _fontCollection[familyName];
                } while (fontFamilyDWrite == null);


                if (fontFamilyDWrite == null)
                {
                    return(null);
                }

                // If there was styling information.
                if (potentialFaceName.Length > 0)
                {
                    // The first character in the potentialFaceName will be a space so we need to strip it off.
                    Text.TextInterface.Font font = GetFontFromFamily(fontFamilyDWrite, potentialFaceName.ToString(1, potentialFaceName.Length - 1));

                    if (font != null)
                    {
                        fontStyle   = new FontStyle((int)font.Style);
                        fontWeight  = new FontWeight((int)font.Weight);
                        fontStretch = new FontStretch((int)font.Stretch);
                    }
                }
            }

            if (UseSystemFonts &&
                LegacyArabicFonts.UsePrivateFontCollectionForLegacyArabicFonts
                // familyName will hold the family name without any face info.
                && LegacyArabicFonts.IsLegacyArabicFont(familyName))
            {
                fontFamilyDWrite = LegacyArabicFonts.LegacyArabicFontCollection[familyName];
                if (fontFamilyDWrite == null)
                {
                    return(SystemCompositeFonts.GetFallbackFontForArabicLegacyFonts());
                }
            }

            return(new PhysicalFontFamily(fontFamilyDWrite));
        }
示例#5
0
 internal MatchingFace(Text.TextInterface.Font face)
 {
     _face  = face;
     _style = new MatchingStyle(new FontStyle((int)face.Style), new FontWeight((int)face.Weight), new FontStretch((int)face.Stretch));
 }
示例#6
0
 internal IntMap(Text.TextInterface.Font font)
 {
     _font = font;
     _cmap = null;
 }