방법: 글꼴 메트릭 가져오기

FontFamily 클래스는 특정 패밀리/스타일 조합에 대한 다양한 메트릭을 검색하는 다음 메서드를 제공합니다.

이러한 메서드에서 반환되는 값은 글꼴 디자인 단위이므로 특정 Font 개체의 크기와 단위와는 독립적입니다.

다음 그림은 다양한 메트릭을 보여 줍니다.

Illustration of font metrics: ascent, descent, and line spacing.

예시

다음 예제에서는 Arial 글꼴 패밀리의 일반 스타일에 대한 메트릭을 표시합니다. 또한 이 코드는 크기가 16픽셀인 Font 개체(Arial 패밀리 기반)를 만들고 해당 특정 Font 개체에 대한 메트릭을 픽셀 단위로 표시합니다.

다음 그림에서는 예제 코드의 출력을 보여줍니다.

Example code output of Arial font metrics.

앞의 그림에서 처음 두 줄의 출력을 확인합니다. Font 개체는 16의 크기를 반환하고 FontFamily 개체는 2,048의 em 높이를 반환합니다. 이러한 두 숫자(16 및 2,048)는 글꼴 디자인 단위와 Font 개체의 단위(이 경우 픽셀) 간에 변환하는 데 중요한 요소입니다.

예를 들어 다음과 같이 디자인 단위에서 픽셀로 상승을 변환할 수 있습니다.

Formula showing the conversion from design units to pixels

다음 코드는 PointF 개체의 Y 데이터 멤버를 설정하여 텍스트를 세로로 배치합니다. y 좌표는 텍스트의 새 줄마다 font.Height씩 증가합니다. Font 개체의 Height 속성은 해당 특정 Font 개체의 줄 간격(픽셀)을 반환합니다. 이 예에서 Height가 반환하는 숫자는 19입니다. 이는 줄 간격 메트릭을 픽셀로 변환하여 얻은 숫자(정수로 반올림됨)와 같습니다.

em 높이(크기 또는 em 크기라고도 함)는 상승 및 하강의 합계가 아닙니다. 상승 및 하강의 합계를 셀 높이라고 합니다. 셀 높이에서 내부 줄 간격을 뺀 값은 em 높이와 같습니다. 셀 높이에 외부 줄 간격을 더한 값은 줄 간격과 같습니다.

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)

코드 컴파일

앞의 예제는 Windows Forms에서 사용하도록 설계되었으며 PaintEventHandler의 매개 변수인 PaintEventArgse가 필요합니다.

참고 항목