如何:获取字体规格

FontFamily 类提供了下列方法来检索特定字体/字形组合的各种规格:

这些方法返回的数字使用的是字体设计单位,因此,它们与特定的 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 高度。 单元格高度加外部间隙等于行距。

        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)

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);

编译代码

前面的示例是为使用 Windows 窗体而设计的,它需要 PaintEventHandler 的参数 PaintEventArgs e。

请参见

其他资源

Windows 窗体中的图形和绘制

使用字体和文本