Comment : obtenir la métrique des polices

Mise à jour : novembre 2007

La classe FontFamily fournit les méthodes suivantes qui récupèrent diverses métriques pour une combinaison famille/style particulière :

Les nombres retournés par ces méthodes sont exprimés en unités de design de police, et sont donc indépendants de la taille et des unités d'un objet Font particulier.

L'illustration suivante présente les diverses métriques.

Polices du texte

Exemple

L'exemple suivant affiche la métrique du style Regular de la famille de polices Arial. Le code crée également un objet Font (basé sur la famille Arial) d'une taille de 16 pixels et affiche la métrique (en pixels) de cet objet Font particulier.

L'illustration suivante montre la sortie de l'exemple de code.

Polices du texte

Notez les deux premières lignes de la sortie présentée dans l'illustration précédente. L'objet Font retourne une taille de 16 et l'objet FontFamily une hauteur de 2 048. Ces deux nombres (16 et 2 048) constituent les éléments clés de la conversion entre les unités de design de police et les unités (dans ce cas des pixels) de l'objet Font.

Par exemple, vous pouvez convertir la hampe d'unités de design en pixels de la façon suivante :

Polices du texte

Le code suivant positionne le texte verticalement en définissant les données membres Y d'un objet PointF. La coordonnée y augmente de font.Height pour chaque ligne de texte. La propriété Height d'un objet Font retourne l'interligne (en pixels) pour cet objet Font particulier. Dans cet exemple, le nombre retourné par Height est 19. Notez que ce nombre (arrondi à un entier) est identique à celui qui est obtenu en convertissant la métrique d'interligne en pixels.

Notez que la hauteur exprimée en em (appelée également taille ou taille exprimée en em) n'est pas la somme du jambage ascendant et du jambage descendant. La somme du jambage ascendant et du jambage descendant s'appelle la hauteur de cellule. La hauteur de cellule moins l'espacement interne est égale à la hauteur exprimée en em. La hauteur de cellule plus l'espacement externe est égale à l'interligne.

Dim infoString As String = "" ' enough space for one line of output
Dim ascent As Integer ' font family ascent in design units
Dim ascentPixel As Single ' ascent converted to pixels
Dim descent As Integer ' font family descent in design units
Dim descentPixel As Single ' descent converted to pixels
Dim lineSpacing As Integer ' font family line spacing in design units
Dim lineSpacingPixel As Single ' line spacing converted to pixels
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   16, _
   FontStyle.Regular, _
   GraphicsUnit.Pixel)
Dim pointF As New PointF(10, 10)
Dim solidBrush As New SolidBrush(Color.Black)

' Display the font size in pixels.
infoString = "font.Size returns " & font.Size.ToString() & "."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)

' Move down one line.
pointF.Y += font.Height

' Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " & _
   fontFamily.GetEmHeight(FontStyle.Regular) & "."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)

' Move down two lines.
pointF.Y += 2 * font.Height

' Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular)

' 14.484375 = 16.0 * 1854 / 2048
ascentPixel = _
   font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The ascent is " & ascent & " design units, " & ascentPixel _
   & " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)

' Move down one line.
pointF.Y += font.Height

' Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular)

' 3.390625 = 16.0 * 434 / 2048
descentPixel = _
   font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The descent is " & descent & " design units, " & _
   descentPixel & " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)

' Move down one line.
pointF.Y += font.Height

' Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular)

' 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel = _
   font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The line spacing is " & lineSpacing & " design units, " & _
   lineSpacingPixel & " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)

string infoString = "";  // enough space for one line of output
int ascent;             // font family ascent in design units
float ascentPixel;      // ascent converted to pixels
int descent;            // font family descent in design units
float descentPixel;     // descent converted to pixels
int lineSpacing;        // font family line spacing in design units
float lineSpacingPixel; // line spacing converted to pixels

FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   16, FontStyle.Regular,
   GraphicsUnit.Pixel);
PointF pointF = new PointF(10, 10);
SolidBrush solidBrush = new SolidBrush(Color.Black);

// Display the font size in pixels.
infoString = "font.Size returns " + font.Size + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " +
   fontFamily.GetEmHeight(FontStyle.Regular) + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down two lines.
pointF.Y += 2 * font.Height;

// Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular);

// 14.484375 = 16.0 * 1854 / 2048
ascentPixel =
   font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The ascent is " + ascent + " design units, " + ascentPixel +
   " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular);

// 3.390625 = 16.0 * 434 / 2048
descentPixel =
   font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The descent is " + descent + " design units, " +
   descentPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

// 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel =
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The line spacing is " + lineSpacing + " design units, " +
   lineSpacingPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

Compilation du code

L'exemple précédent est destiné à une utilisation avec Windows Forms et nécessite PaintEventArgs e, qui est un paramètre de PaintEventHandler.

Voir aussi

Autres ressources

Graphiques et dessins dans les Windows Forms

Utilisation de polices et de texte