Font.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. namespace BansheeEngine
  5. {
  6. /// <summary>
  7. /// Font resource containing data about textual characters and how to render text. Contains one or multiple font
  8. /// bitmaps, each for a specific size.
  9. /// </summary>
  10. public sealed class Font : Resource
  11. {
  12. /// <summary>
  13. /// Creates a new font resource. For runtime use only.
  14. /// </summary>
  15. /// <param name="constructNative">Should the constructor also create the native interop object.</param>
  16. internal Font(bool constructNative)
  17. {
  18. if (constructNative)
  19. Internal_CreateInstance(this);
  20. }
  21. /// <summary>
  22. /// Returns font bitmap for a specific font size.
  23. /// </summary>
  24. /// <param name="size">Size of the bitmap in points.</param>
  25. /// <returns>Bitmap object if size was found, null otherwise.</returns>
  26. public FontBitmap GetBitmap(int size)
  27. {
  28. return Internal_GetBitmap(mCachedPtr, size);
  29. }
  30. /// <summary>
  31. /// Finds the available font bitmap size closest to the provided size.
  32. /// </summary>
  33. /// <param name="size">Size of the bitmap in points.</param>
  34. /// <returns>Nearest available bitmap size.</returns>
  35. public int GetClosestSize(int size)
  36. {
  37. return Internal_GetClosestSize(mCachedPtr, size);
  38. }
  39. [MethodImpl(MethodImplOptions.InternalCall)]
  40. private static extern void Internal_CreateInstance(Font instance);
  41. [MethodImpl(MethodImplOptions.InternalCall)]
  42. private static extern FontBitmap Internal_GetBitmap(IntPtr instance, int size);
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. private static extern int Internal_GetClosestSize(IntPtr instance, int size);
  45. }
  46. /// <summary>
  47. /// Contains textures and data about every character for a bitmap font of a specific size.
  48. /// </summary>
  49. public sealed class FontBitmap : ScriptObject
  50. {
  51. /// <summary>
  52. /// Constructor for internal runtime use only.
  53. /// </summary>
  54. internal FontBitmap()
  55. { }
  56. /// <summary>
  57. /// Size of the font in the bitmap, in points.
  58. /// </summary>
  59. public int Size { get { return Internal_GetSize(mCachedPtr); } }
  60. /// <summary>
  61. /// Y offset to the baseline on which the characters are placed, in pixels.
  62. /// </summary>
  63. public int BaselineOffset { get { return Internal_GetBaselineOffset(mCachedPtr); } }
  64. /// <summary>
  65. /// Height of a single line of the font, in pixels.
  66. /// </summary>
  67. public int LineHeight { get { return Internal_GetLineHeight(mCachedPtr); } }
  68. /// <summary>
  69. /// Width of a space in pixels.
  70. /// </summary>
  71. public int SpaceWidth { get { return Internal_GetSpaceWidth(mCachedPtr); } }
  72. /// <summary>
  73. /// Character to use when data for a character is missing.
  74. /// </summary>
  75. public CharDesc MissingChar { get { CharDesc value; Internal_GetMissingChar(mCachedPtr, out value); return value; } }
  76. /// <summary>
  77. /// Textures in which the character's pixels are stored.
  78. /// </summary>
  79. public Texture2D[] Pages { get { return Internal_GetPages(mCachedPtr); } }
  80. /// <summary>
  81. /// Returns a description of the character in the bitmap.
  82. /// </summary>
  83. /// <param name="id">Unicode key of the character to retrieve.</param>
  84. /// <returns>Character data if it was found in the bitmap, missing character data otherwise.</returns>
  85. public CharDesc GetChar(int id)
  86. {
  87. CharDesc value;
  88. Internal_GetChar(mCachedPtr, id, out value);
  89. return value;
  90. }
  91. /// <summary>
  92. /// Returns a set of pairs that determine if the provided character should be closer or father together than normal
  93. /// with a specific other character. e.g. the combination of "A" and "V" characters is normally a kerning pair "AV"
  94. /// as their bounds overlap and are closer together than characters would be normally.
  95. /// </summary>
  96. /// <param name="id">Unicode key of the character to retrieve kerning pairs for.</param>
  97. /// <returns>A set of kerning pairs for the character.</returns>
  98. public KerningPair[] GetKerning(int id)
  99. {
  100. return Internal_GetKerning(mCachedPtr, id);
  101. }
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern int Internal_GetSize(IntPtr instance);
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern int Internal_GetBaselineOffset(IntPtr instance);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern int Internal_GetLineHeight(IntPtr instance);
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern int Internal_GetSpaceWidth(IntPtr instance);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern void Internal_GetMissingChar(IntPtr instance, out CharDesc output);
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern Texture2D[] Internal_GetPages(IntPtr instance);
  114. [MethodImpl(MethodImplOptions.InternalCall)]
  115. private static extern void Internal_GetChar(IntPtr instance, int id, out CharDesc output);
  116. [MethodImpl(MethodImplOptions.InternalCall)]
  117. private static extern KerningPair[] Internal_GetKerning(IntPtr instance, int id);
  118. }
  119. /// <summary>
  120. /// Marks a range of characters in a font.
  121. /// </summary>
  122. [StructLayout(LayoutKind.Sequential), SerializeObject]
  123. public struct CharRange
  124. {
  125. public UInt32 start;
  126. public UInt32 end;
  127. }
  128. /// <summary>
  129. /// Kerning pair representing larger or smaller offset between a specific pair of characters.
  130. /// </summary>
  131. [StructLayout(LayoutKind.Sequential), SerializeObject]
  132. public struct KerningPair // Note: Must match C++ struct KerningPair
  133. {
  134. public int OtherChar;
  135. public int Amount;
  136. }
  137. /// <summary>
  138. /// Describes a single character in a font of a specific size.
  139. /// </summary>
  140. [StructLayout(LayoutKind.Sequential), SerializeObject]
  141. public struct CharDesc // Note: Must match C++ struct ScriptCharDesc
  142. {
  143. /// <summary>Character ID, corresponding to a Unicode key.</summary>
  144. public int Id;
  145. /// <summary>Index of the texture the character is located on.</summary>
  146. public int Page;
  147. /// <summary>Texture coordinates of the character in the page texture.</summary>
  148. public float UVX;
  149. /// <summary>Texture coordinates of the character in the page texture.</summary>
  150. public float UVY;
  151. /// <summary>Size of the character in texture coordinates.</summary>
  152. public float UVWidth;
  153. /// <summary>Size of the character in texture coordinates.</summary>
  154. public float UVHeight;
  155. /// <summary>Size of the character in pixels.</summary>
  156. public int Width;
  157. /// <summary>Size of the character in pixels.</summary>
  158. public int Height;
  159. /// <summary>Offset for the visible portion of the character in pixels.</summary>
  160. public int XOffset;
  161. /// <summary>Offset for the visible portion of the character in pixels.</summary>
  162. public int YOffset;
  163. /// <summary>Determines how much to advance the pen after writing this character, in pixels.</summary>
  164. public int XAdvance;
  165. /// <summary>Determines how much to advance the pen after writing this character, in pixels.</summary>
  166. public int YAdvance;
  167. }
  168. }