Text Coordinates and Font Metrics

Warning

Drawing functionality described in this topic is out of date. If you need to work with text coordinates and font metrics using the new drawing engine in Graphics Mill, read the Fonts and Measuring Text article.

To draw text accurately to pixel level, you need to know the details that describe a font numerically. This information is called font metrics. You may also need to get coordinates of individual characters in the string. Graphics Mill provides you with this functionality.

Definitions

Each character in a font can be represented as a character image (also known as glyph) which is put into a character cell. All glyphs in the font are aligned by a line which is called the baseline. Note, some elements of the glyph can be placed below the baseline (e.g. for such characters as "q", "g", "y", etc.).

The distance between the top of the character cell and the baseline is called ascent. The distance between the baseline and the bottom of the character cell is called descent.

When two lines are displayed one under another, the distance between the baselines of these lines is called line spacing. The distance between character cells is called the line gap.

These metrics are displayed at the figure below.

Font metrics LineGap Descent Ascent

These font metrics are independent on the characters and depend only on the font size, name, style, etc. However there are metrics called ABC metrics and black box which depend on characters.

ABC metrics make sense only for TrueType fonts. They are defined in the following way:

  • A spacing - the distance to add to the current position before drawing the glyph.
  • B spacing - the width of the drawn portion of the glyph.
  • C spacing - the distance to add to the current position to provide white space to the right of the glyph.

The total width of a character is the sum of the A, B, and C spaces. Either the A or the C space can be negative to indicate underhangs or overhangs.

Black box of the glyph is a smallest bounding rectangle of the glyph inside of the character cell. In fact, the dimensions of the black box can be calculated in alternative ways through other metrics; however the black box top left coordinate is very useful information.

Obtaining Metrics Using Graphics Mill

Graphics Mill provides the Font class that enables you to obtain font metrics. To use this class you should fill in the properties that relate to the font metrics (Name, Size, Italic, etc.). After that you can read font metrics with such properties as:

Additionally, to get character-specific metrics, pass the desired character into methods:

Measuring Text Dimensions and Individual Position of Each Pixel

To determine how much space will be occupied with the entire text string, you can use the Graphics.MeasureText methods. They return the dimensions of the text bounding rectangle. If you need to know the text width without taking into consideration overhanging parts of the string, you can use it in conjunction with the GetA(Char) and GetC(Char) methods (passing the first and the last character into them accordingly).

You may also need to get the positions of each character in the string. For example, to draw text on the curve. To do it, use the GetCharacterPositions(String, Single) method.

Text Output Position

When text is drawn by the Graphics.DrawString method, the coordinates of the text output start point are passed. The interpretation of this point depends on the horizontal and vertical alignment. The figure below demonstrates possible values and interpretations for the text alignments.

Text alignment

The code example that uses different text alignments can be found in the article Simple Text String.