2
0

Font.cs 7.7 KB

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