Beispiel #1
0
 public GdiFontFamilyInfo(GdiFontFamilyType familyType, FontFamily family, GdiFontWeights weight,
                          GdiFontStyles style, GdiFontStretches stretch)
 {
     _familyType = familyType;
     _family     = family;
     _weight     = weight;
     _style      = style;
     _stretch    = stretch;
 }
Beispiel #2
0
 public GdiFontFamilyInfo(string fontName, SvgFontElement fontElement,
                          GdiFontWeights weight, GdiFontStyles style, GdiFontStretches stretch)
 {
     _fontName    = fontName;
     _fontElement = fontElement;
     _familyType  = GdiFontFamilyType.Svg;
     _family      = null;
     _weight      = weight;
     _style       = style;
     _stretch     = stretch;
 }
        private GdiFontFamilyInfo GetTextFontFamilyInfo(SvgTextContentElement element)
        {
            _actualFontName = null;

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

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

            GdiFontStyles    fontStyle   = GetTextFontStyle(element);
            GdiFontWeights   fontWeight  = GetTextFontWeight(element);
            GdiFontStretches 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 = FontFamily.Families;

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

            GdiFontFamilyType familyType = GdiFontFamilyType.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 = GdiRenderingSettings.GenericSerif;
                    }
                    else if (string.Equals(fontName, "sans-serif", comparer) ||
                             string.Equals(fontName, "sans serif", comparer))
                    {
                        genericFamily = GdiRenderingSettings.GenericSansSerif;
                    }
                    else if (string.Equals(fontName, "monospace", comparer))
                    {
                        genericFamily = GdiRenderingSettings.GenericMonospace;
                    }
                    else if (styledFontIds.ContainsKey(fontName))
                    {
                        string mappedFontName = styledFontIds[fontName];
                        var    funcFamily     = new Func <FontFamily, bool>(ff => string.Equals(ff.Name, mappedFontName, comparer));
                        family = systemFontFamilies.FirstOrDefault(funcFamily);
                        if (family != null)
                        {
                            _actualFontName = mappedFontName;
                            familyType      = GdiFontFamilyType.System;
                        }
                    }
                    else
                    {
                        string normalizedFontName;
                        var    funcFamily = new Func <FontFamily, bool>(ff => string.Equals(ff.Name, fontName, comparer));
                        family = systemFontFamilies.FirstOrDefault(funcFamily);
                        if (family != null)
                        {
                            _actualFontName = fontName;
                            familyType      = GdiFontFamilyType.System;
                        }
                        else if (fontName.IndexOf('-') > 0)
                        {
                            normalizedFontName = fontName.Replace("-", " ");
                            funcFamily         = new Func <FontFamily, bool>(ff => string.Equals(ff.Name,
                                                                                                 normalizedFontName, comparer));
                            family = systemFontFamilies.FirstOrDefault(funcFamily);
                            if (family != null)
                            {
                                _actualFontName = normalizedFontName;
                                familyType      = GdiFontFamilyType.System;
                            }
                        }
                        else if (SplitByCaps(fontName, out normalizedFontName))
                        {
                            funcFamily = new Func <FontFamily, bool>(ff => string.Equals(ff.Name,
                                                                                         normalizedFontName, comparer));
                            family = systemFontFamilies.FirstOrDefault(funcFamily);
                            if (family != null)
                            {
                                _actualFontName = normalizedFontName;
                                familyType      = GdiFontFamilyType.System;
                            }
                        }
                    }

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

            //// If set, use the SVG-Font...Not Ready Yet!!!
            //if (svgFontNames != null && svgFontNames.Count != 0)
            //{
            //    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 GdiFontFamilyInfo(svgFonts[0].FontFamily, svgFonts[0],
            //                fontWeight, fontStyle, fontStretch);

            //            fontFamilyInfo.Variant = fontVariant;
            //            return fontFamilyInfo;
            //        }

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

            //            fontFamilyInfo.Variant = fontVariant;
            //            return fontFamilyInfo;
            //        }

            //        foreach (var svgFont in svgFonts)
            //        {
            //            var fontFace = svgFont.FontFace;
            //            if (fontFace == null)
            //            {
            //                continue;
            //            }
            //            if (fontVariant.Equals(fontFace.FontVariant, comparer))
            //            {
            //                var fontFamilyInfo = new GdiFontFamilyInfo(svgFont.FontFamily, svgFont,
            //                    fontWeight, fontStyle, fontStretch);

            //                fontFamilyInfo.Variant = fontVariant;
            //                return fontFamilyInfo;
            //            }
            //        }

            //        // If the variant is not found, return the first match...
            //        {
            //            var fontFamilyInfo = new GdiFontFamilyInfo(svgFonts[0].FontFamily, svgFonts[0],
            //                fontWeight, fontStyle, fontStretch);

            //            fontFamilyInfo.Variant = fontVariant;
            //            return fontFamilyInfo;
            //        }
            //    }
            //}

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

            // No known font-family was found => default to "Arial"
            return(new GdiFontFamilyInfo(familyType, _actualFontName,
                                         GdiRenderingSettings.DefaultFontFamily, fontWeight, fontStyle, fontStretch));
        }