Font.cs 7.5 KB

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