次の方法で共有


フォント メトリックの取得

FontFamily クラスは、特定のファミリとスタイルの組み合わせに対して各種のメトリックを取得する次のメソッドを提供しています。

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

これらのメソッドによって返される数値は、フォントのデザイン単位で測定され、特定の Font オブジェクトのサイズや単位には依存しません。

各種のメトリックを次の図に示します。

Arial フォント ファミリの標準スタイルのメトリックの例を次に示します。このコードでは、Arial ファミリに基づいてサイズが 16 ピクセルの Font オブジェクも作成し、この特定の 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);

上のコードによる出力を次の図に示します。

上の図に示されている出力の最初の 2 行に注目してください。Font オブジェクトはサイズとして 16 を返し、FontFamily オブジェクトは高さとして 2,048 em を返します。これら 2 つの数値 (16 および 2,048) は、フォントのデザイン単位と Font オブジェクトの単位 (この場合はピクセル) の変換のキーとなります。

たとえば、次のように、傾斜度をデザイン単位からピクセル単位に変換できます。

上のコードは、PointF オブジェクトの Y データ メンバを設定することによって、テキストを垂直方向に配置しています。y 座標は、テキストを改行するたびに font.Height ずつ増加します。Font オブジェクトの Height プロパティは、その特定の Font オブジェクトの行間隔 (ピクセル単位) を返します。この例では、Height によって返される数字は 19 です。これは、行間隔をピクセル単位に変換して得られる数値 (小数値は四捨五入) と同じです。

em 高 (サイズまたは em サイズともいいます) は、アセントとディセントの合計ではないことに注意してください。アセントとディセントの合計は、セル高と呼ばれます。セル高から内部レディングを引いたものが、em 高です。セルの高さに外部レディングを加えた値が、行間隔になります。