This documentation is for the old version. Go to the latest Graphics Mill docs

Text Coordinates and Font Metrics

To draw a text accurate to pixel, you need to know details that describe font numerically. This information is called font metrics. Also, you may need to get coordinates of individual character in the string. Graphics Mill for .NET provides such functionality.

Definitions

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

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

When two lines are displayed one under another, the distance between baselines of these lines is called line spacing. The distance between character cells is called 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 font size, font name, italic and bold flag, etc. However there are also some metrics which depend on characters. These characters are ABC metrics and black box.

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 summation 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 another metrics, however black box top left coordinate is very useful information.

Obtaining Metrics Using Graphics Mill for .NET

Graphics Mill for .NET provides a class Font that enables you obtaining font metrics. To use this class you should fill the properties that influence at the font metrics (Name, Size, Italic, etc). After that you can read font metrics with such properties as:

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

Measuring Text Dimensions and Individual Position of Each Pixel

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

Also you may need to get positions of each character in the string. It is necessary, for example, to draw text on the curve. To do it, use GetCharacterPositions(String, Single) method of the Font class.

Code samples for using of these methods you can find the article Measuring Characters Positions.

Text Output Position

When text is drawn by DrawString method of the GdiGraphics class, the coordinates of the text output start point are passed into this method. The interpretation of this point depends on the horizontal and vertical aligment. 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 Drawing Text String.