I want to draw a single character to a specific position.
You can get the glyphs for a spritefont by copying out the glyph dictionary then use regular draw.
spriteBatch.Draw(myfont.Texture, position , Dict[letter].Glyph.BoundsInTexture....ect... , ... ect...
A spritefont is really just a souped up spritesheet.
What I need is a way to get the real character size without any space or margin.
Im not sure if i fully understand what is meant by the real character size here without space or margin.
For the character size each character has a glyph which has a Bounds Rectangle this is a raw rectangle to the sprite in the fonts spritesheet texture for that character that includes its raw width and height rectangle.
This however doesn't tell you how to line up the characters or space them if that character is in the middle of a string all the other values do including all the previous characters cumulatively.
Spaces for instance just move the offset with the right side bering as they don't need to actually draw anything for a blank space. They just affect were the next letter is drawn. The left and right side bering can alter were a character is drawn as well as the croping values.
The position of a character is based on the position that is passed to drawstring as if that is a origin and the current offset add to it. The offset itself is only affected by the left and right sidebering and the characters width and padding.
It is calculated in steps in a loop.
Each characters left side bering is added to the offset at the start then this is copied to a temp value were the glyphs cropping are added and that determines the destination rectangle for the character.
However the offset varys it is dependent on the last characters glyph width and right side bering as well which is added to the offset after the destination has been calculated for the next character and the cropping is not actually added to that offset.
in the below you will find and notice the following lines which are very important.
The newline code block at
offset.X = 0; // each newline resets the x offset
offset.Y += spriteFont.LineSpacing; // and moves the y offset down
657 offset.X += spriteFont.Spacing + pCurrentGlyph->LeftSideBearing;
660 var p = offset; // this demarks a temporary variable copied from the offset to be used
// this adjust the offset for the next character such as when the next character will be after a space character.
683 offset.X += pCurrentGlyph->Width + pCurrentGlyph->RightSideBearing;