方法: フォント メトリックを取得するHow to: Obtain Font Metrics

FontFamilyクラスは、特定のファミリとスタイルの組み合わせに対してさまざまなメトリックを取得する次のメソッドを提供します。The FontFamily class provides the following methods that retrieve various metrics for a particular family/style combination:

これらのメソッドによって返される値は、サイズと、特定の単位から独立しているためフォント デザイン単位ではFontオブジェクト。The values returned by these methods are in font design units, so they are independent of the size and units of a particular Font object.

次の図は、さまざまなメトリックを示しています。The following illustration shows the various metrics:

フォント メトリックの例: アセント、降下法、および行の間隔。

Example

次の例では、Arial フォント ファミリの標準のスタイルのメトリックが表示されます。The following example displays the metrics for the regular style of the Arial font family. コードを作成も、 Font 16 ピクセルのサイズと、その特定のピクセル単位のメトリックが表示されます (Arial ファミリに基づく) オブジェクトFontオブジェクト。The code also creates a Font object (based on the Arial family) with size 16 pixels and displays the metrics (in pixels) for that particular Font object.

次の図は、コード例の出力を示しています。The following illustration shows the output of the example code:

Arial フォント メトリックの例のコード出力します。

前の図の出力の最初の 2 つの行に注意してください。Note the first two lines of output in the preceding illustration. Fontオブジェクトは、16 のサイズを返します、FontFamilyオブジェクトは、2,048 の em の高さを返します。The Font object returns a size of 16, and the FontFamily object returns an em height of 2,048. これら 2 つの数値 (16 と 2,048) は、キーをフォント デザイン単位と単位 (このケースのピクセル単位) の間の変換、Fontオブジェクト。These two numbers (16 and 2,048) are the key to converting between font design units and the units (in this case pixels) of the Font object.

たとえば、次のようにアセントをデザイン単位からピクセルに変換できます。For example, you can convert the ascent from design units to pixels as follows:

デザイン単位からピクセルへの変換を示す式

次のコードを配置テキスト垂直方向に設定して、Yのデータ メンバーをPointFオブジェクト。The following code positions text vertically by setting the Y data member of a PointF object. Y 座標が増加してfont.Heightテキストの新しい行はごとです。The y-coordinate is increased by font.Height for each new line of text. HeightのプロパティをFontオブジェクトがその特定の行間 (ピクセル単位) でを返すFontオブジェクト。The Height property of a Font object returns the line spacing (in pixels) for that particular Font object. この例では、によって返される数Height19。In this example, the number returned by Height is 19. これは行間隔をピクセルに変換することで得られる数値 (整数に切り上げたもの) と同じであることに注意してください。Note that this is the same as the number (rounded up to an integer) obtained by converting the line-spacing metric to pixels.

Em 高 (サイズまたは em サイズとも呼ばれます) がアセントと降下の合計ではないことに注意してください。Note that the em height (also called size or em size) is not the sum of the ascent and the descent. アセントと降下の合計には、セルの高さが呼び出されます。The sum of the ascent and the descent is called the cell height. 内部のレディング マイナス セルの高さは、em 高さと同じです。The cell height minus the internal leading is equal to the em height. セルの高さと外部レディングは、線の間隔と同じです。The cell height plus the external leading is equal to the line spacing.

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)

コードのコンパイルCompiling the Code

前の例は、Windows フォームで使用するために設計されています。 また必要がPaintEventArgs e、はのパラメーターPaintEventHandlerします。The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

関連項目See also