PageRenderTime 60ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/Rendering/Helpers/Glyph.cs

#
C# | 134 lines | 46 code | 10 blank | 78 comment | 3 complexity | 73c8db81d6ecc5b44ba909b642d5157b MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.Utilities.Datatypes;
  3. using Delta.Utilities.Helpers;
  4. namespace Delta.ContentSystem.Rendering.Helpers
  5. {
  6. /// <summary>
  7. /// Info for each glyph in the font, used for FontData to store all glyph
  8. /// data. Glyphs are characters and their information on where it is found
  9. /// in a texture, only needed in the Font and FontGenerator classes.
  10. /// </summary>
  11. /// <remarks>
  12. /// Based on Benjamin Nitschke's older BitmapFont class for XNA,
  13. /// which was based on the XNAExtras project from Gary Kacmarcik
  14. /// (garykac@microsoft.com), more information can be found at:
  15. /// http://blogs.msdn.com/garykac/articles/749188.aspx
  16. /// If you want to know more details about creating bitmap fonts in XNA,
  17. /// how to generate the bitmaps and more details about using it, please
  18. /// check out the following links:
  19. /// http://blogs.msdn.com/garykac/archive/2006/08/30/728521.aspx
  20. /// http://blogs.msdn.com/garykac/articles/732007.aspx
  21. /// http://www.angelcode.com/products/bmfont/
  22. /// </remarks>
  23. public class Glyph
  24. {
  25. #region FontMapId (Public)
  26. /// <summary>
  27. /// Font map number (starts with 0) in case we need multiple images for
  28. /// this font (the bigger the font is and the more characters we got in
  29. /// there, the more important this gets). Links to the materialList in
  30. /// the Font class (which loads the font maps via the Material, which has
  31. /// images that are loaded from our font maps)!
  32. /// </summary>
  33. public int FontMapId;
  34. #endregion
  35. #region UV (Public)
  36. /// <summary>
  37. /// UV Rectangle (in Pixels) used for drawing this character. Includes
  38. /// the position and size obviously. We should not use atlas textures
  39. /// because fonts are already atlases and we can directly use this UVs
  40. /// for drawing.
  41. /// </summary>
  42. public Rectangle UV;
  43. #endregion
  44. #region AdvanceWidth (Public)
  45. /// <summary>
  46. /// Advance width (in pixels) for this character. This is the offset
  47. /// we have to move to the right after drawing the character and it
  48. /// already includes the left and right side bearings. The precise width
  49. /// has to be calculated with this the glyph distance from the kerning,
  50. /// for this reason this is a float value, not a integer value as for all
  51. /// other properties here.
  52. /// </summary>
  53. public float AdvanceWidth;
  54. #endregion
  55. #region LeftSideBearing (Public)
  56. /// <summary>
  57. /// Left side bearing (in pixels) is used to offset this character to the
  58. /// left. This means that rendering starts earlier than the currently
  59. /// specified pixel position to make sure the text looks right on the
  60. /// screen. Often unused and just 0, but sometimes has negative values.
  61. /// Only used to correctly position the first character!
  62. /// </summary>
  63. public int LeftSideBearing;
  64. #endregion
  65. #region RightSideBearing (Public)
  66. /// <summary>
  67. /// Right side bearing (in pixels) is used to offset this character to the
  68. /// right. This means that rendering starts earlier than the currently
  69. /// specified pixel position to make sure the text looks right on the
  70. /// screen. Often unused and just 0, but sometimes has negative values.
  71. /// Only used to correctly position the last character (right aligned)!
  72. /// </summary>
  73. public int RightSideBearing;
  74. #endregion
  75. #region FontMapUV (Public)
  76. /// <summary>
  77. /// Precomputed font map UVs. Not stored in the Xml font file because we
  78. /// can easily generate them at load time (just divide the UV by the image
  79. /// size). Handled in Delta.ContentSystem.Rendering.FontData class.
  80. /// </summary>
  81. public Rectangle FontMapUV;
  82. #endregion
  83. #region Kernings (Public)
  84. /// <summary>
  85. /// That dictionary contains the amount of extra distances offsets between
  86. /// the current character to any other one. But usually there are only some
  87. /// few cases like combinations with 'W', 'I', and other big or small
  88. /// glyph's. Mostly the is no need for an extra distance offset to other
  89. /// characters.
  90. /// </summary>
  91. public Dictionary<char, int> Kernings;
  92. #endregion
  93. #region GetDrawWidth (Public)
  94. /// <summary>
  95. /// Gets the (to fully pixel rounded) drawing width of the glyph based on
  96. /// the previous one.
  97. /// </summary>
  98. /// <param name="nextChar">Next char</param>
  99. /// <param name="trackingPercentage">Tracking percentage</param>
  100. /// <returns>Draw width</returns>
  101. public int GetDrawWidth(char nextChar, float trackingPercentage)
  102. {
  103. // At first let the 'out' parameter know the width of the character
  104. float glyphWidth = AdvanceWidth;
  105. // and look if an extra kerning rule exists
  106. int charKerning;
  107. if (Kernings != null &&
  108. Kernings.TryGetValue(nextChar, out charKerning))
  109. {
  110. glyphWidth += charKerning;
  111. } // if
  112. // Now we still need to compute the distance to the next character
  113. // (based on the current character position). To get the final width we
  114. // still need to check if tracking is wished too.
  115. // Note: The width have to be whole numbers because the rendering have to
  116. // be pixel-aligned else we would get blurry text instead of a sharp one
  117. return MathHelper.Round(
  118. trackingPercentage != 0.0f
  119. ? glyphWidth + AdvanceWidth * trackingPercentage
  120. : glyphWidth);
  121. }
  122. #endregion
  123. }
  124. }