如何:取得字型度量資訊
FontFamily 類別提供下列方法來擷取特定家族/樣式組合的各種計量:
GetEmHeight(FontStyle)
GetCellAscent(FontStyle)
GetCellDescent(FontStyle)
GetLineSpacing(FontStyle)
這些方法傳回的值以字型設計單位表示,因此它們與特定 Font 物件的大小和單位無關。
下圖顯示各種計量:
範例
下列範例顯示 Arial 字型家族一般樣式的計量。 此程式碼也會建立大小為 16 像素的 Font 物件 (以 Arial 家族為基礎),並顯示該特定 Font 物件的計量 (以像素為單位)。
下圖顯示範例程式碼的輸出:
請注意上圖中的前兩行輸出。 Font 物件會傳回大小 16,而 FontFamily 物件會傳回 em 高度 2,048。 這兩個數字 (16 和 2,048) 是在字型設計單位與 Font 物件的單位 (在此案例中為像素) 之間進行轉換的關鍵。
例如,您可以將上方位移從設計單位轉換成像素,如下所示:
下列程式碼會設定 PointF 物件的 Y 資料成員,以垂直放置文字。 每新增一行文字,Y 座標都會增加 font.Height
。 Font 物件的 Height 屬性會傳回該特定 Font 物件的前置 (以像素為單位)。 在此範例中,Height 傳回的數字是 19。 請注意,這與將行距計量轉換成像素所取得的數字 (四捨五入至整數) 相同。
請注意,em 高度 (也稱為大小或 em 大小) 不是上方位移和下方位移的總和。 上方位移和下方位移的總和稱為字元格高度。 字元格高度減去內部行距等於 em 高度。 字元格高度加上外部行距等於前置。
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)
編譯程式碼
上述範例的設計目的是要與 Windows Forms 搭配使用,而且需要 PaintEventArgse
,這是 PaintEventHandler 的參數。