FontManger.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using FontStashSharp.Interfaces;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Numerics;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Typography.OpenFont;
  12. using Vortice.DXGI;
  13. using Vulkan.Xcb;
  14. namespace FontStashSharp
  15. {
  16. public class FontManger
  17. {
  18. private ConcurrentDictionary<String,Typeface> _FontCache = new ConcurrentDictionary<String,Typeface>();
  19. private ConcurrentDictionary<String, String> _FontFileInfos = new ConcurrentDictionary<String, String>();
  20. private FontManger()
  21. {
  22. //InitSystemFontInfo();
  23. }
  24. private void LoadFont(string path)
  25. {
  26. using (var stream = System.IO.File.Open(path, FileMode.Open))
  27. {
  28. Typography.OpenFont.OpenFontReader reader = new OpenFontReader();
  29. Typeface typeface = reader.Read(stream);
  30. typeface.Filename = path;
  31. if (!_FontCache.ContainsKey($"{typeface.Name},{typeface.FontSubFamily}"))
  32. {
  33. _FontCache.TryAdd($"{typeface.FamilyName},{typeface.FontSubFamily}", typeface);
  34. }
  35. }
  36. }
  37. private Typeface LoadFontTypeface(String path)
  38. {
  39. using (var stream = System.IO.File.Open(path, FileMode.Open))
  40. {
  41. Typography.OpenFont.OpenFontReader reader = new OpenFontReader();
  42. Typeface typeface = reader.Read(stream);
  43. typeface.Filename = path;
  44. return typeface;
  45. }
  46. }
  47. private void InitSystemFontInfo()
  48. {
  49. AddFontDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));
  50. }
  51. public void AddFontDirectory(String directory)
  52. {
  53. System.IO.Directory.GetFiles(directory, "*.*")
  54. .Where(x => x.EndsWith(".ttf") || x.EndsWith(".ttc"))
  55. .Where(x=>!_FontFileInfos.Values.Contains(x))
  56. .ToList().
  57. ForEach(x =>
  58. {
  59. var typeface = LoadFontTypeface(x);
  60. String key = $"{typeface.FamilyName},{typeface.FontSubFamily}";
  61. if(!_FontFileInfos.TryGetValue(key,out _))
  62. {
  63. _FontFileInfos.TryAdd(key, typeface.Filename);
  64. }
  65. typeface = null;
  66. });
  67. }
  68. public FontSystem LoadFromSystemDirectory(String fontName, String fontStyle)
  69. {
  70. String key = $"{fontName},{fontStyle}";
  71. if (_FontCache.TryGetValue(key,out var val))
  72. {
  73. FontSystem fontSystem = new FontSystem();
  74. fontSystem.AddFont(val);
  75. return fontSystem;
  76. }
  77. else if(_FontFileInfos.TryGetValue(key,out var filename))
  78. {
  79. var typeface = LoadFontTypeface(filename);
  80. FontSystem fontSystem = new FontSystem();
  81. fontSystem.AddFont(typeface);
  82. _FontCache.TryAdd(key, typeface);
  83. return fontSystem;
  84. }
  85. else
  86. {
  87. throw new Exception("Cannot Find Font:" + fontName);
  88. }
  89. }
  90. static FontManger()
  91. {
  92. }
  93. public static FontManger Instance { get; } = new FontManger();
  94. }
  95. }