FontManager.Skia.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #if SKIA
  2. using FastReport.Fonts;
  3. using SkiaSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Drawing.Text;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. namespace FastReport
  12. {
  13. public static partial class FontManager
  14. {
  15. // do not remove
  16. private static readonly CharacterMatcher characterMatcher = new();
  17. private static readonly FontFamilyMatcher1 fontFamilyMatcher1 = new();
  18. // Note to future implementors:
  19. // this might be added to gdi/uniscribe as well, but it produces not-so-good results,
  20. // because we cannot apply this to text measure phase, only to pdf render phase.
  21. private static List<FontFallback> FallbackFonts { get; } = new();
  22. /// <summary>
  23. /// Adds a new fallback font.
  24. /// </summary>
  25. /// <param name="fontName">The font name, e.g. "Noto Sans CJK JP".</param>
  26. /// <remarks>
  27. /// Fallback font is used to display characters that not present in the original font. It is often used for CJK.
  28. /// For example, you may add "Noto Sans CJK JP" fallback. User defined fallback fonts will then be checked first.
  29. /// </remarks>
  30. public static void AddFallbackFont(string fontName)
  31. {
  32. FallbackFonts.Add(new(fontName));
  33. }
  34. /// <summary>
  35. /// Removes specified fallback font.
  36. /// </summary>
  37. /// <param name="fontName">The font name.</param>
  38. public static void RemoveFallbackFont(string fontName)
  39. {
  40. for (int i = 0; i < FallbackFonts.Count; i++)
  41. {
  42. if (FallbackFonts[i].Name == fontName)
  43. {
  44. FallbackFonts.RemoveAt(i);
  45. i--;
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// Clears all fallback fonts.
  51. /// </summary>
  52. public static void ClearFallbackFonts()
  53. {
  54. FallbackFonts.Clear();
  55. }
  56. private static void AddFontInternal(byte[] buffer, PrivateFontCollection collection)
  57. {
  58. var ms = new MemoryStream(buffer);
  59. using (FontStream fs = new FontStream(ms))
  60. {
  61. var list = TrueTypeCollection.CreateFonts(fs);
  62. for (int ttcIndex = 0; ttcIndex < list.Count; ttcIndex++)
  63. {
  64. var ttf = list[ttcIndex];
  65. collection.AddFont(buffer, ttf.Family, ttcIndex);
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Adds a font from a memory buffer.
  71. /// </summary>
  72. /// <param name="buffer">The buffer containing font data.</param>
  73. public static void AddFont(byte[] buffer)
  74. {
  75. AddFontInternal(buffer, PrivateFontCollection);
  76. }
  77. /// <summary>
  78. /// Adds a font contained in system memory.
  79. /// </summary>
  80. /// <param name="memory">The memory address of the font to add.</param>
  81. /// <param name="length">The memory length of the font to add.</param>
  82. public static void AddFont(IntPtr memory, int length)
  83. {
  84. byte[] buffer = new byte[length];
  85. Marshal.Copy(memory, buffer, 0, length);
  86. AddFont(buffer);
  87. }
  88. /// <summary>
  89. /// Adds a font from a stream.
  90. /// </summary>
  91. /// <param name="stream">The stream containing font data.</param>
  92. public static void AddFont(Stream stream)
  93. {
  94. var ms = new MemoryStream();
  95. stream.CopyTo(ms);
  96. AddFont(ms.ToArray());
  97. }
  98. /// <summary>
  99. /// Adds a font from a file.
  100. /// </summary>
  101. /// <param name="fileName">The name of the font file.</param>
  102. /// <returns>true if file exists, otherwise false.</returns>
  103. public static bool AddFont(string fileName)
  104. {
  105. if (File.Exists(fileName))
  106. {
  107. AddFont(File.ReadAllBytes(fileName));
  108. return true;
  109. }
  110. else
  111. {
  112. Debug.WriteLine($"Font file not found {fileName}");
  113. }
  114. return false;
  115. }
  116. /// <summary>
  117. /// Adds a font from a file to a temporary font list.
  118. /// </summary>
  119. /// <param name="fileName">The name of the font file.</param>
  120. /// <returns>true if file exists, otherwise false.</returns>
  121. public static bool AddTempFont(string fileName)
  122. {
  123. if (File.Exists(fileName))
  124. {
  125. TemporaryFontCollection ??= new PrivateFontCollection();
  126. AddFontInternal(File.ReadAllBytes(fileName), TemporaryFontCollection);
  127. return true;
  128. }
  129. else
  130. {
  131. Debug.WriteLine($"Font file not found {fileName}");
  132. }
  133. return false;
  134. }
  135. /// <summary>
  136. /// Adds a font from a stream to a temporary font list.
  137. /// </summary>
  138. /// <param name="stream">The stream containing font data.</param>
  139. public static void AddTempFont(Stream stream)
  140. {
  141. TemporaryFontCollection ??= new PrivateFontCollection();
  142. var ms = new MemoryStream();
  143. stream.CopyTo(ms);
  144. AddFontInternal(ms.ToArray(), TemporaryFontCollection);
  145. }
  146. /// <summary>
  147. /// Clears temporary fonts.
  148. /// </summary>
  149. public static void ClearTempFonts()
  150. {
  151. var collection = TemporaryFontCollection;
  152. TemporaryFontCollection = null;
  153. collection?.Dispose();
  154. }
  155. // Defines a font fallback item.
  156. private class FontFallback
  157. {
  158. private FontFamily _family;
  159. public string Name { get; }
  160. // The font family, if found. May be null.
  161. public FontFamily FontFamily => _family ?? FindFontFamily(Name, SearchScope.NonInstalled);
  162. public FontFallback(string name)
  163. {
  164. Name = name;
  165. // do initial search in installed fonts. Other collections should be checked later.
  166. _family = FindFontFamily(name, SearchScope.Installed);
  167. }
  168. }
  169. // used by the font fallback code from RichTextKit to find a typeface that contains a given character
  170. private class CharacterMatcher : Topten.RichTextKit.ICharacterMatcher
  171. {
  172. private readonly Topten.RichTextKit.ICharacterMatcher oldCharacterMatcher;
  173. public CharacterMatcher()
  174. {
  175. oldCharacterMatcher = Topten.RichTextKit.FontFallback.CharacterMatcher;
  176. Topten.RichTextKit.FontFallback.CharacterMatcher = this;
  177. }
  178. public SKTypeface MatchCharacter(string familyName, int weight, int width, SKFontStyleSlant slant, string[] bcp47, int character)
  179. {
  180. // check user defined fallbacks first
  181. foreach (var ff in FallbackFonts)
  182. {
  183. var tf = ff.FontFamily?.GetTypeface(weight, width, slant);
  184. if (tf != null && tf.ContainsGlyph(character))
  185. {
  186. return tf;
  187. }
  188. }
  189. // check private fonts
  190. if (TemporaryFontCollection != null)
  191. {
  192. foreach (var f in TemporaryFontCollection.Families)
  193. {
  194. var tf = f.GetTypeface(weight, width, slant);
  195. if (tf.ContainsGlyph(character))
  196. {
  197. return tf;
  198. }
  199. }
  200. }
  201. foreach (var f in PrivateFontCollection.Families)
  202. {
  203. var tf = f.GetTypeface(weight, width, slant);
  204. if (tf.ContainsGlyph(character))
  205. {
  206. return tf;
  207. }
  208. }
  209. // do default check
  210. return oldCharacterMatcher?.MatchCharacter(familyName, weight, width, slant, bcp47, character);
  211. }
  212. }
  213. // used in the FontFamily to look up family name in all font collections
  214. private class FontFamilyMatcher1 : FontFamily.IFontFamilyMatcher
  215. {
  216. public FontFamilyMatcher1()
  217. {
  218. FontFamily.FontFamilyMatcher = this;
  219. }
  220. public FontFamily GetFontFamilyOrDefault(string name) =>
  221. FontManager.GetFontFamilyOrDefault(name);
  222. public FontFamily GetFontFamilyForTypeface(SKTypeface typeface, FontStyle style)
  223. {
  224. var family = TemporaryFontCollection?.Find(typeface, style);
  225. family ??= PrivateFontCollection.Find(typeface, style);
  226. family ??= InstalledFontCollection.Find(typeface, style);
  227. return family;
  228. }
  229. }
  230. }
  231. }
  232. #endif