PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 1ms

/ContentSystem/Rendering/Helpers/Glyph.cs

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