Ejemplo n.º 1
0
        public MediaFontInfo()
        {
            this.ProportionalList = new List <System.Windows.Media.FontFamily>();
            this.MonospaceList    = new List <System.Windows.Media.FontFamily>();

            XmlLanguage          lang      = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
            IEnumerable <string> fontNames = new InstalledFontCollection().Families.Select(x => x.Name);

            using (var bmp = new Bitmap(1, 1))
                using (var graphics = Graphics.FromImage(bmp))
                {
                    foreach (System.Windows.Media.FontFamily family in System.Windows.Media.Fonts.SystemFontFamilies)
                    {
                        string cltName = family.FamilyNames.FirstOrDefault(x => x.Key == lang).Value ?? family.Source;
                        string name    = fontNames.FirstOrDefault(x => x.StartsWith(cltName));
                        if (!string.IsNullOrEmpty(name))
                        {
                            using (Font font = new Font(name, emSize: 20))
                            {
                                float iiSize = graphics.MeasureString("iiii", font).Width;
                                float wwSize = graphics.MeasureString("wwww", font).Width;
                                if (iiSize == wwSize)
                                {
                                    float aaSize = graphics.MeasureString("ああ", font).Width;
                                    if (Math.Abs(iiSize - aaSize) < 1)
                                    {
                                        MonospaceList.Add(family);
                                        continue;
                                    }
                                }
                                ProportionalList.Add(family);
                            }
                        }
                    }
                }
        }