#if NET40 || NET461 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Media; namespace Apewer.Surface { /// internal class FontInstance { private FontFamily _fontfamily = null; private Dictionary _names = null; private FontInstance(FontFamily fontFamily) { _fontfamily = fontFamily; } /// public Dictionary FontFamiliy { get { if (_names == null) { _names = new Dictionary(); if (_fontfamily.FamilyNames.Keys.Count > 0) { foreach (var name in _fontfamily.FamilyNames) { var key = name.Key.IetfLanguageTag; var value = name.Value; if (!_names.ContainsKey(key)) _names.Add(key, value); } } } var result = new Dictionary(); foreach (var item in _names) result.Add(item.Key, item.Value); return result; } } /// public List EnemerateChars() { var list = new List(); if (_fontfamily == null) return list; var typefaces = _fontfamily.GetTypefaces(); foreach (var typeface in typefaces) { GlyphTypeface glyph; var tried = typeface.TryGetGlyphTypeface(out glyph); if (glyph != null) { list.Clear(); var map = glyph.CharacterToGlyphMap; for (int i = 0; i < map.Count; i++) { long index = map.Keys.ElementAt(i); try { var c = Convert.ToChar(index); list.Add(c.ToString()); } catch { } } } } return list; } /// public List EnemerateChars(string fontFamily) { var list = new List(); if (string.IsNullOrEmpty(fontFamily)) return list; var fontfamily = new FontFamily(fontFamily); var typefaces = fontfamily.GetTypefaces(); foreach (var typeface in typefaces) { GlyphTypeface glyph; var tried = typeface.TryGetGlyphTypeface(out glyph); if (glyph != null) { var map = glyph.CharacterToGlyphMap; for (int i = 0; i < map.Count; i++) { long index = map.Keys.ElementAt(i); try { var c = Convert.ToChar(index); list.Add(c.ToString()); } catch { } } } } return list; } /// public static List EnumerateSystemFontFamilies() { var list = new List(); var fontfamilies = Fonts.SystemFontFamilies; foreach (var fontfamily in fontfamilies) { list.Add(new FontInstance(fontfamily)); } return list; } /// public static List FromFile(string path) { var list = new List(); var fontfamilies = Fonts.GetFontFamilies(path); foreach (var fontfamily in fontfamilies) { list.Add(new FontInstance(fontfamily)); } return list; } } } #endif