/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
- using System.Collections.Generic;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
-
- namespace Delta.ContentSystem.Rendering.Helpers
- {
- /// <summary>
- /// Info for each glyph in the font, used for FontData to store all glyph
- /// data. Glyphs are characters and their information on where it is found
- /// in a texture, only needed in the Font and FontGenerator classes.
- /// </summary>
- /// <remarks>
- /// Based on Benjamin Nitschke's older BitmapFont class for XNA,
- /// which was based on the XNAExtras project from Gary Kacmarcik
- /// (garykac@microsoft.com), more information can be found at:
- /// http://blogs.msdn.com/garykac/articles/749188.aspx
- /// If you want to know more details about creating bitmap fonts in XNA,
- /// how to generate the bitmaps and more details about using it, please
- /// check out the following links:
- /// http://blogs.msdn.com/garykac/archive/2006/08/30/728521.aspx
- /// http://blogs.msdn.com/garykac/articles/732007.aspx
- /// http://www.angelcode.com/products/bmfont/
- /// </remarks>
- public class Glyph
- {
- #region FontMapId (Public)
- /// <summary>
- /// Font map number (starts with 0) in case we need multiple images for
- /// this font (the bigger the font is and the more characters we got in
- /// there, the more important this gets). Links to the materialList in
- /// the Font class (which loads the font maps via the Material, which has
- /// images that are loaded from our font maps)!
- /// </summary>
- public int FontMapId;
- #endregion
-
- #region UV (Public)
- /// <summary>
- /// UV Rectangle (in Pixels) used for drawing this character. Includes
- /// the position and size obviously. We should not use atlas textures
- /// because fonts are already atlases and we can directly use this UVs
- /// for drawing.
- /// </summary>
- public Rectangle UV;
- #endregion
-
- #region AdvanceWidth (Public)
- /// <summary>
- /// Advance width (in pixels) for this character. This is the offset
- /// we have to move to the right after drawing the character and it
- /// already includes the left and right side bearings. The precise width
- /// has to be calculated with this the glyph distance from the kerning,
- /// for this reason this is a float value, not a integer value as for all
- /// other properties here.
- /// </summary>
- public float AdvanceWidth;
- #endregion
-
- #region LeftSideBearing (Public)
- /// <summary>
- /// Left side bearing (in pixels) is used to offset this character to the
- /// left. This means that rendering starts earlier than the currently
- /// specified pixel position to make sure the text looks right on the
- /// screen. Often unused and just 0, but sometimes has negative values.
- /// Only used to correctly position the first character!
- /// </summary>
- public int LeftSideBearing;
- #endregion
-
- #region RightSideBearing (Public)
- /// <summary>
- /// Right side bearing (in pixels) is used to offset this character to the
- /// right. This means that rendering starts earlier than the currently
- /// specified pixel position to make sure the text looks right on the
- /// screen. Often unused and just 0, but sometimes has negative values.
- /// Only used to correctly position the last character (right aligned)!
- /// </summary>
- public int RightSideBearing;
- #endregion
-
- #region FontMapUV (Public)
- /// <summary>
- /// Precomputed font map UVs. Not stored in the Xml font file because we
- /// can easily generate them at load time (just divide the UV by the image
- /// size). Handled in Delta.ContentSystem.Rendering.FontData class.
- /// </summary>
- public Rectangle FontMapUV;
- #endregion
-
- #region Kernings (Public)
- /// <summary>
- /// That dictionary contains the amount of extra distances offsets between
- /// the current character to any other one. But usually there are only some
- /// few cases like combinations with 'W', 'I', and other big or small
- /// glyph's. Mostly the is no need for an extra distance offset to other
- /// characters.
- /// </summary>
- public Dictionary<char, int> Kernings;
- #endregion
-
- #region GetDrawWidth (Public)
- /// <summary>
- /// Gets the (to fully pixel rounded) drawing width of the glyph based on
- /// the previous one.
- /// </summary>
- /// <param name="nextChar">Next char</param>
- /// <param name="trackingPercentage">Tracking percentage</param>
- /// <returns>Draw width</returns>
- public int GetDrawWidth(char nextChar, float trackingPercentage)
- {
- // At first let the 'out' parameter know the width of the character
- float glyphWidth = AdvanceWidth;
-
- // and look if an extra kerning rule exists
- int charKerning;
- if (Kernings != null &&
- Kernings.TryGetValue(nextChar, out charKerning))
- {
- glyphWidth += charKerning;
- } // if
-
- // Now we still need to compute the distance to the next character
- // (based on the current character position). To get the final width we
- // still need to check if tracking is wished too.
- // Note: The width have to be whole numbers because the rendering have to
- // be pixel-aligned else we would get blurry text instead of a sharp one
- return MathHelper.Round(
- trackingPercentage != 0.0f
- ? glyphWidth + AdvanceWidth * trackingPercentage
- : glyphWidth);
- }
- #endregion
- }
- }