コード例 #1
0
ファイル: FontWeight.cs プロジェクト: Venzz/Venz
 public static String ToXamlValue(this FontWeight source)
 {
     if (source.Equals(FontWeights.Black))
     {
         return("Black");
     }
     else if (source.Equals(FontWeights.Bold))
     {
         return("Bold");
     }
     else if (source.Equals(FontWeights.ExtraBlack))
     {
         return("ExtraBlack");
     }
     else if (source.Equals(FontWeights.ExtraBold))
     {
         return("ExtraBold");
     }
     else if (source.Equals(FontWeights.ExtraLight))
     {
         return("ExtraLight");
     }
     else if (source.Equals(FontWeights.Light))
     {
         return("Light");
     }
     else if (source.Equals(FontWeights.Medium))
     {
         return("Medium");
     }
     else if (source.Equals(FontWeights.Normal))
     {
         return("Normal");
     }
     else if (source.Equals(FontWeights.SemiBold))
     {
         return("SemiBold");
     }
     else if (source.Equals(FontWeights.SemiLight))
     {
         return("SemiLight");
     }
     else if (source.Equals(FontWeights.Thin))
     {
         return("Thin");
     }
     else
     {
         throw new ArgumentException("Can't convert FontWeight to XAML value because specified value is not supported by ToXamlValue method.");
     }
 }
コード例 #2
0
        protected WpfFontFamilyInfo GetTextFontFamilyInfo(SvgTextContentElement element)
        {
            _actualFontName = null;

            string fontFamily = element.GetPropertyValue("font-family");

            string[] fontNames = fontFamily.Split(new char[1] {
                ','
            });

            FontStyle   fontStyle   = GetTextFontStyle(element);
            FontWeight  fontWeight  = GetTextFontWeight(element);
            FontStretch fontStretch = GetTextFontStretch(element);

            var comparer   = StringComparison.OrdinalIgnoreCase;
            var docElement = element.OwnerDocument;

            ISet <string> svgFontFamilies = docElement.SvgFontFamilies;
            IDictionary <string, string> styledFontIds = docElement.StyledFontIds;

            IList <string> svgFontNames = null;

            if (svgFontFamilies != null && svgFontFamilies.Count != 0)
            {
                svgFontNames = new List <string>();
            }
            //var systemFontFamilies = Fonts.SystemFontFamilies;

            var wpfSettings         = _context.Settings;
            var fontFamilyNames     = wpfSettings.FontFamilyNames;
            var privateFontFamilies = wpfSettings.HasFontFamilies;

            FontFamily family = null;
            // using separate pointer to give less priority to generic font names
            FontFamily genericFamily = null;

            WpfFontFamilyType familyType = WpfFontFamilyType.None;

            foreach (string fn in fontNames)
            {
                try
                {
                    string fontName = fn.Trim(new char[] { ' ', '\'', '"' });
                    if (svgFontFamilies != null && svgFontFamilies.Count != 0)
                    {
                        if (svgFontFamilies.Contains(fontName))
                        {
                            svgFontNames.Add(fontName);
                            continue;
                        }
                        if (styledFontIds.ContainsKey(fontName))
                        {
                            string mappedFontName = styledFontIds[fontName];
                            if (svgFontFamilies.Contains(mappedFontName))
                            {
                                svgFontNames.Add(mappedFontName);
                                continue;
                            }
                        }
                    }

                    if (string.Equals(fontName, "serif", comparer))
                    {
                        genericFamily = WpfDrawingSettings.GenericSerif;
                    }
                    else if (string.Equals(fontName, "sans-serif", comparer) ||
                             string.Equals(fontName, "sans serif", comparer))
                    {
                        genericFamily = WpfDrawingSettings.GenericSansSerif;
                    }
                    else if (string.Equals(fontName, "monospace", comparer))
                    {
                        genericFamily = WpfDrawingSettings.GenericMonospace;
                    }
                    else if (styledFontIds.ContainsKey(fontName))
                    {
                        string mappedFontName = styledFontIds[fontName];
                        family = LookupFontFamily(mappedFontName, fontFamilyNames);
                        if (family != null)
                        {
                            _actualFontName = mappedFontName;
                            familyType      = WpfFontFamilyType.System;
                        }
                    }
                    else
                    {
                        // Try looking up fonts in the system font registry...
                        family = LookupFontFamily(fontName, fontFamilyNames);
                        if (family != null)
                        {
                            _actualFontName = fontName;
                            familyType      = WpfFontFamilyType.System;
                        }

                        // If not found, look through private fonts if available..
                        if (family == null && privateFontFamilies)
                        {
                            family = wpfSettings.LookupFontFamily(fontName);
                            if (family != null)
                            {
                                _actualFontName = fontName;
                                familyType      = WpfFontFamilyType.Private;
                            }
                        }
                    }

                    if (family != null)
                    {
                        return(new WpfFontFamilyInfo(familyType, _actualFontName, family,
                                                     fontWeight, fontStyle, fontStretch));
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.ToString());
                }
            }

            // If set, use the SVG-Font...
            if (svgFontNames != null && svgFontNames.Count != 0)
            {
                FontFamily altFamily = (genericFamily != null) ? genericFamily : WpfDrawingSettings.DefaultFontFamily;

                IList <SvgFontElement> svgFonts = docElement.GetFonts(svgFontNames);
                if (svgFonts != null && svgFonts.Count != 0)
                {
                    string fontVariant = element.GetPropertyValue("font-variant");

                    // For a single match...
                    if (svgFonts.Count == 1)
                    {
                        var fontFamilyInfo = new WpfFontFamilyInfo(svgFonts[0].FontFamily, svgFonts[0],
                                                                   fontWeight, fontStyle, fontStretch);

                        fontFamilyInfo.Variant = fontVariant;
                        // For rendering that do not support the SVG Fonts...
                        fontFamilyInfo.Family = altFamily;
                        return(fontFamilyInfo);
                    }

                    // For the defined font style...
                    if (fontStyle != FontStyles.Normal)
                    {
                        // Then it is either oblique or italic
                        SvgFontElement closeFont   = null;
                        SvgFontElement closestFont = null;
                        bool           isItalic    = fontStyle.Equals(FontStyles.Italic);
                        foreach (var svgFont in svgFonts)
                        {
                            var fontFace = svgFont.FontFace;
                            if (fontFace == null)
                            {
                                continue;
                            }
                            var typefaceStyle = GetTextFontStyle(fontFace.FontStyle);
                            if (fontStyle.Equals(typefaceStyle))
                            {
                                closeFont = svgFont;
                                if (closestFont == null)
                                {
                                    closestFont = svgFont;
                                }
                                var typefaceWeight = GetTextFontWeight(fontFace.FontWeight);
                                if (fontVariant.Equals(fontFace.FontVariant, comparer))
                                {
                                    closestFont = svgFont;
                                    if (fontWeight.Equals(typefaceWeight))
                                    {
                                        var fontFamilyInfo = new WpfFontFamilyInfo(svgFont.FontFamily, svgFont,
                                                                                   fontWeight, fontStyle, fontStretch);

                                        fontFamilyInfo.Variant = fontVariant;
                                        // For rendering that do not support the SVG Fonts...
                                        fontFamilyInfo.Family = altFamily;
                                        return(fontFamilyInfo);
                                    }
                                }
                            }
                            if (closeFont == null)
                            {
                                if (isItalic && typefaceStyle == FontStyles.Oblique)
                                {
                                    closeFont = svgFont;
                                }
                                if (!isItalic && typefaceStyle == FontStyles.Italic)
                                {
                                    closeFont = svgFont;
                                }
                            }
                        }
                        if (closestFont != null)
                        {
                            closeFont = closestFont;
                        }

                        if (closeFont != null)
                        {
                            var fontFamilyInfo = new WpfFontFamilyInfo(closeFont.FontFamily, closeFont,
                                                                       fontWeight, fontStyle, fontStretch);

                            fontFamilyInfo.Variant = fontVariant;
                            // For rendering that do not support the SVG Fonts...
                            fontFamilyInfo.Family = altFamily;
                            return(fontFamilyInfo);
                        }
                    }

                    SvgFontElement variantFont = null;
                    // For multiple matches, we will test the variants...
                    if (!string.IsNullOrWhiteSpace(fontVariant))
                    {
                        foreach (var svgFont in svgFonts)
                        {
                            var fontFace = svgFont.FontFace;
                            if (fontFace == null)
                            {
                                continue;
                            }
                            if (fontVariant.Equals(fontFace.FontVariant, comparer))
                            {
                                variantFont = svgFont;
                                // Check for more perfect match...
                                var typefaceWeight = GetTextFontWeight(fontFace.FontWeight);
                                var typefaceStyle  = GetTextFontStyle(fontFace.FontStyle);
                                if (fontStyle.Equals(typefaceStyle) && fontWeight.Equals(typefaceWeight))
                                {
                                    var fontFamilyInfo = new WpfFontFamilyInfo(svgFont.FontFamily, svgFont,
                                                                               fontWeight, fontStyle, fontStretch);

                                    fontFamilyInfo.Variant = fontVariant;
                                    // For rendering that do not support the SVG Fonts...
                                    fontFamilyInfo.Family = altFamily;
                                    return(fontFamilyInfo);
                                }
                            }
                        }

                        //if (variantFont != null)
                        //{
                        //    // If there was a matching variant but either style or weight not matched...
                        //    var fontFamilyInfo = new WpfFontFamilyInfo(variantFont.FontFamily, variantFont,
                        //        fontWeight, fontStyle, fontStretch);

                        //    fontFamilyInfo.Variant = fontVariant;
                        //    // For rendering that do not support the SVG Fonts...
                        //    fontFamilyInfo.Family = altFamily;
                        //    return fontFamilyInfo;
                        //}
                    }

                    // For the defined font weights...
                    if (fontWeight != FontWeights.Normal && fontWeight != FontWeights.Regular)
                    {
                        int            weightValue    = fontWeight.ToOpenTypeWeight();
                        int            selectedValue  = int.MaxValue;
                        SvgFontElement sameWeightFont = null;
                        SvgFontElement closestFont    = null;
                        foreach (var svgFont in svgFonts)
                        {
                            var fontFace = svgFont.FontFace;
                            if (fontFace == null)
                            {
                                continue;
                            }
                            var typefaceWeight = GetTextFontWeight(fontFace.FontWeight);
                            if (fontWeight.Equals(typefaceWeight))
                            {
                                sameWeightFont = svgFont;
                                var typefaceStyle = GetTextFontStyle(fontFace.FontStyle);
                                if (fontStyle.Equals(typefaceStyle))
                                {
                                    var fontFamilyInfo = new WpfFontFamilyInfo(svgFont.FontFamily, svgFont,
                                                                               fontWeight, fontStyle, fontStretch);

                                    fontFamilyInfo.Variant = fontVariant;
                                    // For rendering that do not support the SVG Fonts...
                                    fontFamilyInfo.Family = altFamily;
                                    return(fontFamilyInfo);
                                }
                            }

                            int weightDiff = Math.Abs(weightValue - typefaceWeight.ToOpenTypeWeight());
                            if (weightDiff < selectedValue)
                            {
                                closestFont   = svgFont;
                                selectedValue = weightDiff;
                            }
                        }

                        // If the weights matched, but not the style
                        if (sameWeightFont != null)
                        {
                            var fontFamilyInfo = new WpfFontFamilyInfo(sameWeightFont.FontFamily, sameWeightFont,
                                                                       fontWeight, fontStyle, fontStretch);

                            fontFamilyInfo.Variant = fontVariant;
                            // For rendering that do not support the SVG Fonts...
                            fontFamilyInfo.Family = altFamily;
                            return(fontFamilyInfo);
                        }
                        if (closestFont != null)
                        {
                            var fontFamilyInfo = new WpfFontFamilyInfo(closestFont.FontFamily, closestFont,
                                                                       fontWeight, fontStyle, fontStretch);

                            fontFamilyInfo.Variant = fontVariant;
                            // For rendering that do not support the SVG Fonts...
                            fontFamilyInfo.Family = altFamily;
                            return(fontFamilyInfo);
                        }
                    }

                    if (variantFont != null)
                    {
                        // If there was a matching variant but either style or weight not matched...
                        var fontFamilyInfo = new WpfFontFamilyInfo(variantFont.FontFamily, variantFont,
                                                                   fontWeight, fontStyle, fontStretch);

                        fontFamilyInfo.Variant = fontVariant;
                        // For rendering that do not support the SVG Fonts...
                        fontFamilyInfo.Family = altFamily;
                        return(fontFamilyInfo);
                    }
                    else // If the variant is not found, return the first match...
                    {
                        var fontFamilyInfo = new WpfFontFamilyInfo(svgFonts[0].FontFamily, svgFonts[0],
                                                                   fontWeight, fontStyle, fontStretch);

                        fontFamilyInfo.Variant = fontVariant;
                        // For rendering that do not support the SVG Fonts...
                        fontFamilyInfo.Family = altFamily;
                        return(fontFamilyInfo);
                    }

                    //// For multiple matches, we will test the variants...
                    //if (string.IsNullOrWhiteSpace(fontVariant))
                    //{
                    //    // Not found, return the first match...
                    //    var fontFamilyInfo = new WpfFontFamilyInfo(svgFonts[0].FontFamily, svgFonts[0],
                    //        fontWeight, fontStyle, fontStretch);

                    //    fontFamilyInfo.Variant = fontVariant;
                    //    // For rendering that do not support the SVG Fonts...
                    //    fontFamilyInfo.Family = altFamily;
                    //    return fontFamilyInfo;
                    //}
                }
            }

            if (genericFamily != null)
            {
                return(new WpfFontFamilyInfo(WpfFontFamilyType.Generic, _actualFontName, genericFamily,
                                             fontWeight, fontStyle, fontStretch));
            }

            // No known font-family was found => default to "Arial"
            return(new WpfFontFamilyInfo(familyType, _actualFontName,
                                         WpfDrawingSettings.DefaultFontFamily, fontWeight, fontStyle, fontStretch));
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
0
        public void Set(String propertyName, FontWeight value)
        {
            var weightValue = (Byte)4;

            if (value.Equals(FontWeights.Black))
            {
                weightValue = 0;
            }
            else if (value.Equals(FontWeights.Bold))
            {
                weightValue = 1;
            }
            else if (value.Equals(FontWeights.ExtraBlack))
            {
                weightValue = 2;
            }
            else if (value.Equals(FontWeights.ExtraBold))
            {
                weightValue = 3;
            }
            else if (value.Equals(FontWeights.ExtraLight))
            {
                weightValue = 4;
            }
            else if (value.Equals(FontWeights.Light))
            {
                weightValue = 5;
            }
            else if (value.Equals(FontWeights.Medium))
            {
                weightValue = 6;
            }
            else if (value.Equals(FontWeights.Normal))
            {
                weightValue = 7;
            }
            else if (value.Equals(FontWeights.SemiBold))
            {
                weightValue = 8;
            }
            else if (value.Equals(FontWeights.SemiLight))
            {
                weightValue = 9;
            }
            else if (value.Equals(FontWeights.Thin))
            {
                weightValue = 10;
            }

            if (!Properties.ContainsKey(propertyName))
            {
                Properties.Add(propertyName, weightValue);
            }
            else
            {
                Properties[propertyName] = weightValue;
            }
        }