Porady: uzyskiwanie miar czcionek

Klasa FontFamily udostępnia następujące metody, które pobierają różne metryki dla określonej kombinacji rodziny/stylu:

Wartości zwracane przez te metody są w jednostkach projektowych czcionek, więc są niezależne od rozmiaru i jednostek określonego Font obiektu.

Na poniższej ilustracji przedstawiono różne metryki:

Illustration of font metrics: ascent, descent, and line spacing.

Przykład

Poniższy przykład przedstawia metryki dla zwykłego stylu rodziny czcionek Arial. Kod tworzy Font również obiekt (oparty na rodzinie Arial) o rozmiarze 16 pikseli i wyświetla metryki (w pikselach) dla tego określonego Font obiektu.

Poniższa ilustracja przedstawia dane wyjściowe przykładowego kodu:

Example code output of Arial font metrics.

Zwróć uwagę na dwa pierwsze wiersze danych wyjściowych na poprzedniej ilustracji. Obiekt Font zwraca rozmiar 16, a FontFamily obiekt zwraca wysokość em 2048. Te dwie liczby (16 i 2048) są kluczem do konwersji między jednostkami projektu czcionki a jednostkami (w tym przypadku pikseli) Font obiektu.

Na przykład możesz przekonwertować wznoszenie z jednostek projektowych na piksele w następujący sposób:

Formula showing the conversion from design units to pixels

Poniższy kod umieszcza tekst w pionie, ustawiając Y składową PointF danych obiektu. Współrzędna y jest zwiększana przez font.Height każdy nowy wiersz tekstu. Właściwość HeightFont obiektu zwraca odstęp między wierszami (w pikselach) dla tego określonego Font obiektu. W tym przykładzie liczba zwracana przez Height wartość to 19. Należy pamiętać, że jest to taka sama liczba (zaokrąglona w górę do liczby całkowitej) uzyskana przez przekonwertowanie metryki odstępów między wierszami na piksele.

Należy pamiętać, że wysokość em (nazywana również rozmiarem lub rozmiarem em) nie jest sumą wzrostu i spadku. Suma wzrostu i zejścia jest nazywana wysokością komórki. Wysokość komórki minus wewnętrznych wiodących jest równa wysokości em. Wysokość komórki i zewnętrzna linia wiodąca jest równa odstępom między wierszami.

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);
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)

Kompilowanie kodu

Powyższy przykład jest przeznaczony do użycia z formularzami Systemu Windows i wymaga PaintEventArgseparametru , który jest parametrem PaintEventHandler.

Zobacz też