共用方式為


取得字型矩陣

FontFamily 類別提供了下列方法,可從特定家族/樣式組合中擷取不同的矩陣:

  • GetEmHeight(FontStyle)
  • GetCellAscent(FontStyle)
  • GetCellDescent(FontStyle)
  • GetLineSpacing(FontStyle)

由這些方法傳回來的數字是以字型設計單位為單位,因此與特定 Font 物件的大小和單位無關。

下圖顯示的是不同的矩陣。

下列範例會顯示 Arial 字型家族標準樣式的矩陣。程式碼也會建立大小為 16 個像素的 Font 物件 (根據 Arial 家族),並顯示該特定 Font 物件的矩陣 (以像素為單位)。

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 + "."
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)
[C#]
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);

下圖顯示的是上述程式碼的輸出。

請注意上圖中輸出的最前面兩行。Font 物件傳回的大小為 16 個像素,FontFamily 物件會傳回 2,048 em 高度。這兩個數字 (16 和 2,048) 是在字型設計單位和 Font 物件單位 (在這個範例中為像素) 之間轉換的關鍵。

例如,您可以依照下列方式,將上升部分從設計單位轉換為像素:

上述程式碼會藉由設定 PointF 物件的 Y 資料成員來垂直放置文字。Y 座標會依照每一個新文字行的 font.Height 增加。Font 物件的 Height 屬性會傳回該特定 Font 物件的行距 (以像素為單位)。在這個範例中,Height 傳回的數字是 19。請注意,這個數字與轉換行距度量資訊 (Metrix) 為像素時取得的數字 (四捨五入為整數) 相同。

請注意,em 高度 (也稱為大小或 em 大小) 不是上升部分和下降部分的總和。上升部分和下降部分的總和稱為儲存格高度。儲存格高度減去內部前置字元就等於 em 高度。儲存格高度加上外部前置字元等於行距。