You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.1 KiB

  1. #if NET40 || NET461
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Media;
  7. namespace Apewer.Surface
  8. {
  9. /// <summary></summary>
  10. internal class FontInstance
  11. {
  12. private FontFamily _fontfamily = null;
  13. private Dictionary<string, string> _names = null;
  14. private FontInstance(FontFamily fontFamily) { _fontfamily = fontFamily; }
  15. /// <summary></summary>
  16. public Dictionary<string, string> FontFamiliy
  17. {
  18. get
  19. {
  20. if (_names == null)
  21. {
  22. _names = new Dictionary<string, string>();
  23. if (_fontfamily.FamilyNames.Keys.Count > 0)
  24. {
  25. foreach (var name in _fontfamily.FamilyNames)
  26. {
  27. var key = name.Key.IetfLanguageTag;
  28. var value = name.Value;
  29. if (!_names.ContainsKey(key)) _names.Add(key, value);
  30. }
  31. }
  32. }
  33. var result = new Dictionary<string, string>();
  34. foreach (var item in _names) result.Add(item.Key, item.Value);
  35. return result;
  36. }
  37. }
  38. /// <summary></summary>
  39. public List<string> EnemerateChars()
  40. {
  41. var list = new List<string>();
  42. if (_fontfamily == null) return list;
  43. var typefaces = _fontfamily.GetTypefaces();
  44. foreach (var typeface in typefaces)
  45. {
  46. GlyphTypeface glyph;
  47. var tried = typeface.TryGetGlyphTypeface(out glyph);
  48. if (glyph != null)
  49. {
  50. list.Clear();
  51. var map = glyph.CharacterToGlyphMap;
  52. for (int i = 0; i < map.Count; i++)
  53. {
  54. long index = map.Keys.ElementAt(i);
  55. try
  56. {
  57. var c = Convert.ToChar(index);
  58. list.Add(c.ToString());
  59. }
  60. catch { }
  61. }
  62. }
  63. }
  64. return list;
  65. }
  66. /// <summary></summary>
  67. public List<string> EnemerateChars(string fontFamily)
  68. {
  69. var list = new List<string>();
  70. if (string.IsNullOrEmpty(fontFamily)) return list;
  71. var fontfamily = new FontFamily(fontFamily);
  72. var typefaces = fontfamily.GetTypefaces();
  73. foreach (var typeface in typefaces)
  74. {
  75. GlyphTypeface glyph;
  76. var tried = typeface.TryGetGlyphTypeface(out glyph);
  77. if (glyph != null)
  78. {
  79. var map = glyph.CharacterToGlyphMap;
  80. for (int i = 0; i < map.Count; i++)
  81. {
  82. long index = map.Keys.ElementAt(i);
  83. try
  84. {
  85. var c = Convert.ToChar(index);
  86. list.Add(c.ToString());
  87. }
  88. catch { }
  89. }
  90. }
  91. }
  92. return list;
  93. }
  94. /// <summary></summary>
  95. public static List<FontInstance> EnumerateSystemFontFamilies()
  96. {
  97. var list = new List<FontInstance>();
  98. var fontfamilies = Fonts.SystemFontFamilies;
  99. foreach (var fontfamily in fontfamilies)
  100. {
  101. list.Add(new FontInstance(fontfamily));
  102. }
  103. return list;
  104. }
  105. /// <summary></summary>
  106. public static List<FontInstance> FromFile(string path)
  107. {
  108. var list = new List<FontInstance>();
  109. var fontfamilies = Fonts.GetFontFamilies(path);
  110. foreach (var fontfamily in fontfamilies)
  111. {
  112. list.Add(new FontInstance(fontfamily));
  113. }
  114. return list;
  115. }
  116. }
  117. }
  118. #endif