다음을 통해 공유


방법: 글꼴 메트릭 얻기

FontFamily 클래스에는 특정 패밀리/스타일 조합을 위한 다양한 메트릭을 가져오는 다음과 같은 메서드가 있습니다.

이러한 메서드에서 반환되는 숫자는 글꼴 디자인 단위를 사용하므로 특정 Font 개체의 크기 및 단위와 관계가 없습니다.

아래 그림에 다양한 매트릭이 나와 있습니다.

글꼴 텍스트

예제

아래 예제에서는 Arial 글꼴 패밀리의 보통 스타일에 대한 매트릭을 표시합니다. 또한 이 코드에서는 Arial 패밀리를 기반으로 크기가 16픽셀인 Font 개체를 만들고 이 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 Forms에서 사용해야 하며 PaintEventHandler의 매개 변수인 PaintEventArgs e를 필요로 합니다.

참고 항목

기타 리소스

Windows Forms의 그래픽 및 그리기

글꼴 및 텍스트 사용