コード例 #1
0
 public NativeFontStyle(NativeFontFamily family, string id, string name, string fullName, int weight, FontStyleType styleType, string path, bool resource = false)
 {
     _family   = family;
     Id        = id;
     Name      = name;
     FullName  = fullName;
     Weight    = weight;
     StyleType = styleType;
     Path      = path;
     Resource  = resource;
 }
コード例 #2
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (ReferenceEquals(this, obj))
            {
                return(true);
            }
            if (obj.GetType() != typeof(NativeFontFamily))
            {
                return(false);
            }
            NativeFontFamily other = (NativeFontFamily)obj;

            return(_name == other._name);
        }
コード例 #3
0
        public IFontFamily[] InitializeFonts()
        {
            var families   = new Dictionary <string, NativeFontFamily>();
            var familyList = new List <IFontFamily>();
            var analyzer   = new FontAnalyzer();

            var assembly  = typeof(NativeFontService).Assembly;
            var resources = assembly.GetManifestResourceNames();

            foreach (var resource in resources)
            {
                if (resource.EndsWith("tf", StringComparison.OrdinalIgnoreCase))
                {
                    var path       = resource.Split('.');
                    var id         = path[path.Length - 2];
                    var parts      = id.Split('-');
                    var familyName = parts[0];
                    var type       = parts[1];

                    if (familyName.StartsWith("TeXGyre", StringComparison.InvariantCulture))
                    {
                        familyName = "TeX Gyre " + familyName.Substring(7);
                    }

                    if (!families.TryGetValue(familyName, out var family))
                    {
                        family = new NativeFontFamily(familyName);
                        families[familyName] = family;
                        familyList.Add(family);
                    }

                    var weight = FontUtils.Regular;
                    if (type.Contains("Bold"))
                    {
                        weight = FontUtils.Bold;
                    }

                    var styleType = FontStyleType.Normal;
                    if (type.Contains("Italic"))
                    {
                        styleType = FontStyleType.Italic;
                    }

                    var fullName = $"{familyName} {type}";
                    if ("Regular".Equals(type))
                    {
                        fullName = familyName;
                    }

                    var style = new NativeFontStyle(family, id, type, fullName, weight, styleType, resource, true);
                    family.AddStyle(style);

                    if (FontAliasingEnabled)
                    {
                        var suffix = string.Empty;
                        var italic = "Italic";
                        if ("Arimo".Equals(familyName))
                        {
                            familyName = "Arial";
                            id         = "Arial";
                            suffix     = "MT";
                        }
                        else if ("Tinos".Equals(familyName))
                        {
                            familyName = "Times New Roman";
                            id         = "TimesNewRomanPS";
                            suffix     = "MT";
                        }
                        else if ("Cousine".Equals(familyName))
                        {
                            familyName = "Courier New";
                            id         = "CourierNewPS";
                            suffix     = "MT";
                        }
                        else if ("TeX Gyre Termes".Equals(familyName))
                        {
                            familyName = "Times";
                            id         = "Times";
                            suffix     = "";
                        }
                        else if ("TeX Gyre Heros".Equals(familyName))
                        {
                            familyName = "Helvetica";
                            id         = "Helvetica";
                            italic     = "Oblique";
                            suffix     = "";
                        }
                        else if ("TeX Gyre Cursor".Equals(familyName))
                        {
                            familyName = "Courier";
                            id         = "Courier";
                            italic     = "Oblique";
                            suffix     = "";
                        }

                        if (!families.TryGetValue(familyName, out family))
                        {
                            family = new NativeFontFamily(familyName);
                            families[familyName] = family;
                            familyList.Add(family);
                        }

                        fullName = $"{familyName} {type}";
                        if ("Regular".Equals(type))
                        {
                            fullName = familyName;
                        }

                        if (styleType == FontStyleType.Italic)
                        {
                            if (weight == FontUtils.Bold)
                            {
                                id = id + "-" + "Bold" + italic + suffix;
                            }
                            else
                            {
                                id = id + "-" + italic + suffix;
                            }

                            if ("Oblique".Equals(italic))
                            {
                                styleType = FontStyleType.Oblique;
                            }
                        }
                        else if (weight == FontUtils.Bold)
                        {
                            id = id + "-" + "Bold" + suffix;
                        }

                        style = new NativeFontStyle(family, id, type, fullName, weight, styleType, resource, true);
                        family.AddStyle(style);
                    }
                }
            }

            foreach (string searchPath in FontSearchPaths)
            {
                if (searchPath != null)
                {
                    var searchDirectory = new DirectoryInfo(searchPath);

                    if (searchDirectory.Exists)
                    {
                        var files = searchDirectory.GetFiles();
                        foreach (var file in files)
                        {
                            try
                            {
                                var fontInfo = analyzer.GetFontInfo(file.FullName);

                                if (fontInfo != null)
                                {
                                    if (IsValidFont(fontInfo))
                                    {
                                        if (!families.TryGetValue(fontInfo.Family, out var family))
                                        {
                                            family = new NativeFontFamily(fontInfo.Family);
                                            families[fontInfo.Family] = family;
                                            familyList.Add(family);
                                        }

                                        if (!family.HasStyle(fontInfo.Style))
                                        {
                                            var id        = fontInfo.FullName;
                                            var name      = fontInfo.Style;
                                            var weight    = FontUtils.GetFontWeight(name);
                                            var styleType = FontUtils.GetStyleType(name);

                                            string fullName = fontInfo.Family;
                                            if (!"Regular".Equals(fontInfo.Style))
                                            {
                                                fullName = $"{fontInfo.Family} {name}";
                                            }

                                            var style = new NativeFontStyle(family, id, name, fullName, weight, styleType, fontInfo.Path);
                                            family.AddStyle(style);
                                        }
                                        else
                                        {
                                            Logger.Info("Duplicate style found for font: {0} {1}", fontInfo.Family, fontInfo.Style);
                                        }
                                    }
                                }
                                else
                                {
                                    Logger.Info("Unable to load the font info for the font file: " + file.FullName);
                                }
                            }
                            catch (Exception exc)
                            {
                                Logger.Info("Unable to handle the font file: " + file.FullName, exc);
                            }
                        }
                    }
                }
            }

            familyList.Sort();
            return(familyList.ToArray());
        }