Font.cs 7.8 KB

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