Bagikan melalui


Cara: Mendapatkan Metrik Font

Kelas ini FontFamily menyediakan metode berikut yang mengambil berbagai metrik untuk kombinasi keluarga/gaya tertentu:

Nilai yang dikembalikan oleh metode ini berada dalam unit desain font, sehingga tidak bergantung pada ukuran dan unit objek tertentu Font .

Ilustrasi berikut menunjukkan berbagai metrik:

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

Contoh

Contoh berikut menampilkan metrik untuk gaya reguler keluarga font Arial. Kode ini juga membuat Font objek (berdasarkan keluarga Arial) dengan ukuran 16 piksel dan menampilkan metrik (dalam piksel) untuk objek tertentu Font .

Ilustrasi berikut menunjukkan output kode contoh:

Example code output of Arial font metrics.

Perhatikan dua baris output pertama dalam ilustrasi sebelumnya. Objek Font mengembalikan ukuran 16, dan FontFamily objek mengembalikan tinggi em 2.048. Kedua angka ini (16 dan 2.048) adalah kunci untuk mengonversi antara unit desain font dan unit (dalam hal ini piksel) Font objek.

Misalnya, Anda dapat mengonversi pendakian dari unit desain menjadi piksel sebagai berikut:

Formula showing the conversion from design units to pixels

Kode berikut memposisikan teks secara vertikal dengan mengatur Y anggota PointF data objek. Koordinat y ditingkatkan oleh font.Height untuk setiap baris teks baru. Properti HeightFont objek mengembalikan penspasian baris (dalam piksel) untuk objek tertentu Font . Dalam contoh ini, angka yang dikembalikan oleh Height adalah 19. Perhatikan bahwa ini sama dengan angka (dibulatkan ke bilangan bulat) yang diperoleh dengan mengonversi metrik penspasian baris menjadi piksel.

Perhatikan bahwa tinggi em (juga disebut ukuran atau ukuran em) bukan jumlah pendakian dan turunan. Jumlah pendataan dan turunan disebut tinggi sel. Tinggi sel dikurangi di depan internal sama dengan tinggi em. Tinggi sel ditambah di depan eksternal sama dengan penspasian baris.

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)

Mengompilasi Kode

Contoh sebelumnya dirancang untuk digunakan dengan Formulir Windows, dan memerlukan PaintEventArgse, yang merupakan parameter dari PaintEventHandler.

Baca juga