Пример #1
0
        //---------------------------------------------------------------------------------------------
        public override bool Equals(object obj)
        {
            TypefaceStyle style = obj as TypefaceStyle;
            bool          check = false;

            if (FFontStyle.Equals(style.FontStyle) && (FFontStretch.Equals(style.FontStretch)) && (FFontWeight.Equals(style.FontWeight)))
            {
                check = true;
            }

            return(check);
        }
Пример #2
0
        private FontFamily GetMatchingFontFamily(IList <FontFamily> fontFamilies,
                                                 FontWeight weight, FontStyle style, FontStretch stretch)
        {
            if (fontFamilies == null || fontFamilies.Count == 0)
            {
                return(null);
            }
            // For a single match...
            if (fontFamilies.Count == 1)
            {
                return(fontFamilies[0]);
            }

            // 1. Look for a possibility of all properties matching
            foreach (FontFamily fontFamily in fontFamilies)
            {
                // Return the family typeface collection for the font family.
                FamilyTypefaceCollection familyTypefaces = fontFamily.FamilyTypefaces;

                // Enumerate the family typefaces in the collection.
                foreach (FamilyTypeface typeface in familyTypefaces)
                {
                    FontStyle   fontStyle   = typeface.Style;
                    FontWeight  fontWeight  = typeface.Weight;
                    FontStretch fontStretch = typeface.Stretch;

                    if (fontStyle.Equals(style) && fontWeight.Equals(weight) && fontStretch.Equals(stretch))
                    {
                        return(fontFamily);
                    }
                }
            }

            // For the defined font style...
            if (style != FontStyles.Normal)
            {
                // Then it is either oblique or italic
                FontFamily closeFamily   = null;
                FontFamily closestFamily = null;
                bool       isItalic      = style.Equals(FontStyles.Italic);

                foreach (FontFamily fontFamily in fontFamilies)
                {
                    // Return the family typeface collection for the font family.
                    FamilyTypefaceCollection familyTypefaces = fontFamily.FamilyTypefaces;

                    // Enumerate the family typefaces in the collection.
                    foreach (FamilyTypeface typeface in familyTypefaces)
                    {
                        FontStyle   fontStyle   = typeface.Style;
                        FontWeight  fontWeight  = typeface.Weight;
                        FontStretch fontStretch = typeface.Stretch;

                        if (fontStyle.Equals(style))
                        {
                            closeFamily = fontFamily;
                            if (closestFamily == null)
                            {
                                closestFamily = fontFamily;
                            }
                            if (fontStretch.Equals(stretch))
                            {
                                closestFamily = fontFamily;
                                if (fontWeight.Equals(weight))
                                {
                                    return(fontFamily);
                                }
                            }
                        }
                        if (closeFamily == null)
                        {
                            if (isItalic && fontStyle == FontStyles.Oblique)
                            {
                                closeFamily = fontFamily;
                            }
                            if (!isItalic && fontStyle == FontStyles.Italic)
                            {
                                closeFamily = fontFamily;
                            }
                        }
                    }
                    if (closestFamily != null)
                    {
                        closeFamily = closestFamily;
                    }

                    if (closeFamily != null)
                    {
                        return(closeFamily);
                    }
                }
            }

            // For the defined font weights...
            if (weight != FontWeights.Normal && weight != FontWeights.Regular)
            {
                int        weightValue      = weight.ToOpenTypeWeight();
                int        selectedValue    = int.MaxValue;
                FontFamily sameWeightFamily = null;
                FontFamily closestFamily    = null;
                foreach (FontFamily fontFamily in fontFamilies)
                {
                    // Return the family typeface collection for the font family.
                    FamilyTypefaceCollection familyTypefaces = fontFamily.FamilyTypefaces;

                    // Enumerate the family typefaces in the collection.
                    foreach (FamilyTypeface typeface in familyTypefaces)
                    {
                        FontStyle   fontStyle   = typeface.Style;
                        FontWeight  fontWeight  = typeface.Weight;
                        FontStretch fontStretch = typeface.Stretch;

                        if (fontWeight.Equals(weight))
                        {
                            sameWeightFamily = fontFamily;
                            if (fontStyle.Equals(style))
                            {
                                return(fontFamily);
                            }
                        }

                        int weightDiff = Math.Abs(weightValue - fontWeight.ToOpenTypeWeight());
                        if (weightDiff < selectedValue)
                        {
                            closestFamily = fontFamily;
                            selectedValue = weightDiff;
                        }

                        // If the weights matched, but not the style
                        if (sameWeightFamily != null)
                        {
                            return(sameWeightFamily);
                        }
                        if (closestFamily != null)
                        {
                            return(closestFamily);
                        }
                    }
                }
            }

            return(null);
        }