방법: 개인 글꼴 컬렉션 만들기

PrivateFontCollection 클래스는 FontCollection 추상 기본 클래스에서 상속됩니다. PrivateFontCollection 개체를 사용하여 애플리케이션에 대한 글꼴 집합을 유지할 수 있습니다. 프라이빗 글꼴 컬렉션에는 컴퓨터에 설치되지 않은 글꼴뿐만 아니라 설치된 시스템 글꼴도 포함될 수 있습니다. 프라이빗 글꼴 컬렉션에 글꼴 파일을 추가하려면 PrivateFontCollection 개체의 AddFontFile 메서드를 호출합니다.

PrivateFontCollection 개체의 Families 속성은 FontFamily 개체의 배열을 포함합니다.

프라이빗 글꼴 컬렉션의 글꼴 패밀리 수가 반드시 컬렉션에 추가된 글꼴 파일 수와 동일하지는 않습니다. 예를 들어 ArialBd.tff, Times.tff, TimesBd.tff 파일을 컬렉션에 추가한다고 가정합니다. Times.tff와 TimesBd.tff가 같은 패밀리에 속하기 때문에 컬렉션에는 3개의 파일이 있지만 2개의 패밀리만 있습니다.

예제

다음 예제에서는 PrivateFontCollection 개체에 다음 세 개의 글꼴 파일을 추가합니다.

  • C:\systemroot\Fonts\Arial.tff(Arial, 기본)

  • C:\systemroot\Fonts\CourBI.tff(Courier New, 굵은 기울임꼴)

  • C:\systemroot\Fonts\TimesBd.tff(Times New Roman, 굵게)

이 코드는 PrivateFontCollection 개체의 Families 속성에서 FontFamily 개체 배열을 검색합니다.

컬렉션의 각 FontFamily 개체에 대해 코드는 IsStyleAvailable 메서드를 호출하여 다양한 스타일(일반, 굵게, 기울임꼴, 굵은 기울임꼴, 밑줄, 취소선)을 사용할 수 있는지 여부를 결정합니다. IsStyleAvailable 메서드에 전달되는 인수는 FontStyle 열거형의 멤버입니다.

지정된 패밀리/스타일 조합을 사용할 수 있는 경우 해당 패밀리 및 스타일을 사용하여 Font 개체가 생성됩니다. Font 생성자에 전달된 첫 번째 인수는 글꼴 패밀리 이름입니다(Font 생성자의 다른 변형에 대한 경우와 같이 FontFamily 개체가 아님). Font 개체가 생성되면 Graphics 클래스의 DrawString 메서드에 전달되어 스타일 이름과 함께 패밀리 이름을 표시합니다.

다음 코드의 출력은 다음 그림에 표시된 출력과 유사합니다.

다양한 글꼴의 텍스트를 보여주는 스크린샷.

Arial.tff(다음 코드 예제에서 프라이빗 글꼴 컬렉션에 추가됨)는 Arial 일반 스타일의 글꼴 파일입니다. 그러나 프로그램 출력에는 Arial 글꼴 패밀리에 대해 일반 스타일이 아닌 여러 가지 사용 가능한 스타일이 표시됩니다. 이는 GDI+가 일반 스타일에서 굵게, 기울임꼴, 굵은 기울임꼴 스타일을 시뮬레이션할 수 있기 때문입니다. GDI+는 일반 스타일에서 밑줄과 취소선을 생성할 수도 있습니다.

마찬가지로 GDI+는 굵은 스타일 또는 기울임꼴 스타일에서 굵은 기울임꼴 스타일을 시뮬레이션할 수 있습니다. 프로그램 출력은 TimesBd.tff(Times New Roman, 굵게)가 컬렉션의 유일한 Times 파일임에도 불구하고 Times 패밀리에서 굵은 기울임꼴 스타일을 사용할 수 있음을 보여 줍니다.

PointF pointF = new PointF(10, 0);
SolidBrush solidBrush = new SolidBrush(Color.Black);

int count = 0;
string familyName = "";
string familyNameAndStyle;
FontFamily[] fontFamilies;
PrivateFontCollection privateFontCollection = new PrivateFontCollection();

// Add three font files to the private collection.
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\Arial.ttf");
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\CourBI.ttf");
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\TimesBD.ttf");

// Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families;

// How many objects in the fontFamilies array?
count = fontFamilies.Length;

// Display the name of each font family in the private collection
// along with the available styles for that font family.
for (int j = 0; j < count; ++j)
{
    // Get the font family name.
    familyName = fontFamilies[j].Name;

    // Is the regular style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Regular";

        Font regFont = new Font(
           familyName,
           16,
           FontStyle.Regular,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           regFont,
           solidBrush,
           pointF);

        pointF.Y += regFont.Height;
    }

    // Is the bold style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Bold";

        Font boldFont = new Font(
           familyName,
           16,
           FontStyle.Bold,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF);

        pointF.Y += boldFont.Height;
    }
    // Is the italic style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Italic";

        Font italicFont = new Font(
           familyName,
           16,
           FontStyle.Italic,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           italicFont,
           solidBrush,
           pointF);

        pointF.Y += italicFont.Height;
    }

    // Is the bold italic style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) &&
    fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + "BoldItalic";

        Font italicFont = new Font(
           familyName,
           16,
           FontStyle.Italic | FontStyle.Bold,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           italicFont,
           solidBrush,
           pointF);

        pointF.Y += italicFont.Height;
    }
    // Is the underline style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Underline";

        Font underlineFont = new Font(
           familyName,
           16,
           FontStyle.Underline,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           underlineFont,
           solidBrush,
           pointF);

        pointF.Y += underlineFont.Height;
    }

    // Is the strikeout style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Strikeout";

        Font strikeFont = new Font(
           familyName,
           16,
           FontStyle.Strikeout,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           strikeFont,
           solidBrush,
           pointF);

        pointF.Y += strikeFont.Height;
    }

    // Separate the families with white space.
    pointF.Y += 10;
} // for
Dim pointF As New PointF(10, 0)
Dim solidBrush As New SolidBrush(Color.Black)

Dim count As Integer = 0
Dim familyName As String = ""
Dim familyNameAndStyle As String
Dim fontFamilies() As FontFamily
Dim privateFontCollection As New PrivateFontCollection()

' Add three font files to the private collection.
privateFontCollection.AddFontFile("D:\systemroot\Fonts\Arial.ttf")
privateFontCollection.AddFontFile("D:\systemroot\Fonts\CourBI.ttf")
privateFontCollection.AddFontFile("D:\systemroot\Fonts\TimesBD.ttf")

' Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families

' How many objects in the fontFamilies array?
count = fontFamilies.Length

' Display the name of each font family in the private collection
' along with the available styles for that font family.
Dim j As Integer

While j < count
    ' Get the font family name.
    familyName = fontFamilies(j).Name

    ' Is the regular style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Regular) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Regular"

        Dim regFont As New Font( _
           familyName, _
           16, _
           FontStyle.Regular, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           regFont, _
           solidBrush, _
           pointF)

        pointF.Y += regFont.Height
    End If

    ' Is the bold style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Bold) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Bold"

        Dim boldFont As New Font( _
           familyName, _
           16, _
           FontStyle.Bold, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           boldFont, _
           solidBrush, _
           pointF)

        pointF.Y += boldFont.Height
    End If

    ' Is the italic style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Italic) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Italic"

        Dim italicFont As New Font( _
           familyName, _
           16, _
           FontStyle.Italic, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           italicFont, _
           solidBrush, pointF)

        pointF.Y += italicFont.Height
    End If

    ' Is the bold italic style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Italic) And _
       fontFamilies(j).IsStyleAvailable(FontStyle.Bold) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & "BoldItalic"

        Dim italicFont As New Font( _
            familyName, _
            16, _
            FontStyle.Italic Or FontStyle.Bold, _
            GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           italicFont, _
           solidBrush, _
           pointF)

        pointF.Y += italicFont.Height
    End If
    ' Is the underline style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Underline) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Underline"

        Dim underlineFont As New Font( _
           familyName, _
           16, _
           FontStyle.Underline, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           underlineFont, _
           solidBrush, _
           pointF)

        pointF.Y += underlineFont.Height
    End If

    ' Is the strikeout style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Strikeout) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Strikeout"

        Dim strikeFont As New Font( _
           familyName, _
           16, _
           FontStyle.Strikeout, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           strikeFont, _
           solidBrush, _
           pointF)

        pointF.Y += strikeFont.Height
    End If

    ' Separate the families with white space.
    pointF.Y += 10
End While

코드 컴파일

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

참고 항목