Game Development Reference
In-Depth Information
public Vector MeasureFont(string text, double maxWidth)
{
Vector dimensions = new Vector();
foreach (char c in text)
{
CharacterData data = _characterData[c];
dimensions.X += data.XAdvance;
dimensions.Y = Math.Max(dimensions.Y, data.Height + data.YOffset);
}
return dimensions;
}
There are two MeasureFont methods: the first method is an overload that
doesn't require a maximum width parameter, and the second is where all the
measurement happens.
A vector is returned containing the width and height as the X and Y values. The
Z component isn't used. It's returned as a vector, rather than some other data
structure such as PointF because vectors store doubles and that's how position
is stored. The width and height is going to be mostly used to alter the position of
other pieces of text or sprites; doubles mean no casting needs to be done. Vectors
are also easy to scale and transform.
The text is measured by iterating through the character data and adding up the
X advance to get the width for the entire string. The height of the string is the
height of the tallest character.
Rather than calculate the width and height every time they are needed, it's more
convenient to store these dimensions in the Text class.
public class Text
{
Font _font;
List<CharacterSprite> _bitmapText = new List<CharacterSprite>();
string _text;
Vector _dimensions;
public double Width
{
get { return _dimensions.X; }
}
public double Height
{
 
Search WWH ::




Custom Search