이 단계별 가이드에서는 C#에서 글꼴 메트릭을 가져오는 방법을 설명합니다. 이 자습서는 C#에서 Type1 글꼴 메트릭을 가져오는 코드를 제공하지만 C# 글꼴 라이브러리인 Aspose.Font for .NET를 사용하면 TruType 및 OpenType 글꼴 형식을 비롯한 다른 글꼴 유형에서 C#의 글꼴 메트릭을 읽을 수 있습니다.
C#에서 글꼴 메트릭을 가져오는 단계
- NuGet.org에서 Aspose.Font for .NET 패키지 설치
- Aspose.Font, Aspose.Font.Sources 및 Aspose.Font.Type1 네임스페이스 포함
- 평가 워터마크를 피하기 위해 Aspose.Font for .NET에 라이선스 적용
- 입력 Type1 글꼴을 FileSystemStreamSource 개체에 로드
- 스트림 소스에서 FontFileDefinition 객체 생성
- 파일 정의에서 FontDefinition 객체 생성
- Type1Font 개체로 글꼴 정의 열기
- 이제 Type1 글꼴의 모든 메트릭 속성을 가져옵니다.
글꼴에는 글꼴 내부의 글리프를 설명하는 데 도움이 되는 메트릭 정보가 포함되어 있습니다. 이 글꼴의 메트릭 정보는 컴퓨터가 화면에 문자와 문장을 그리는 방법을 학습하는 데 도움이 됩니다. .NET 애플리케이션의 코드를 통해 이 메트릭 정보를 처리하려는 경우 쉬운 작업이 아닙니다. 그러나 .NET용 Aspose.Font는 위에서 언급한 몇 가지 단계를 통해 전체 프로세스를 매우 간단하고 쉽게 만드는 데 도움이 됩니다.
C#에서 글꼴 메트릭을 가져오는 코드
using System; | |
//Add reference to Aspose.Font for .NET API | |
//Use following namespaces to get font metrics | |
using Aspose.Font; | |
using Aspose.Font.Sources; | |
using Aspose.Font.Type1; | |
namespace GetFontMetrics | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before getting font metrics | |
//using Aspose.Font for .NET | |
Aspose.Font.License AsposeFontLicense = new Aspose.Font.License(); | |
AsposeFontLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Load Type1 font into a file stream object & read font definition | |
FileSystemStreamSource Type1FontFileSource = new FileSystemStreamSource("SampleInputType1Font.pfb"); | |
FontFileDefinition Type1FontFileDefintion = new FontFileDefinition(Type1FontFileSource); | |
FontDefinition Type1FontDefinition = new FontDefinition(FontType.Type1, Type1FontFileDefintion); | |
//Open Type1 font | |
Type1Font InputType1Font = Font.Open(Type1FontDefinition) as Type1Font; | |
//Read font metrics information and use it for further processing | |
string FontName = InputType1Font.FontName; | |
int FontGlyphcount = InputType1Font.NumGlyphs; | |
double FontMetricsAscender = InputType1Font.Metrics.Ascender; | |
double FontMetricsDescender = InputType1Font.Metrics.Descender; | |
double FontMetricsTypoAscender = InputType1Font.Metrics.TypoAscender; | |
double FontMetricsTypoDescender = InputType1Font.Metrics.TypoDescender; | |
double FontMetricsUnitsPerEM = InputType1Font.Metrics.UnitsPerEM; | |
//display information to console in this case | |
Console.WriteLine( | |
"Font Name: {0}, " + | |
"Glyph Count: {1}, " + | |
"Asender: {2}, " + | |
"Descender: {3}, " + | |
"Typo Ascender: {4}, " + | |
"Typo Descender: {5}, " + | |
"Units Per EM: {6}", | |
FontName, | |
FontGlyphcount, | |
FontMetricsAscender, | |
FontMetricsDescender, | |
FontMetricsTypoAscender, | |
FontMetricsTypoDescender, | |
FontMetricsUnitsPerEM | |
); | |
} | |
} | |
} |
위의 코드에서는 C# 코드에서 처리하려는 Type1 글꼴이 포함된 PFB 파일 형식을 로드합니다. font를 로드하고 Type1Font 개체로 변환하면 이 파일에서 C#의 글꼴 메트릭을 추출할 수 있습니다.
이 코드를 사용하면 웹, 데스크톱, Windows 및 Microsoft Store 응용 프로그램을 포함한 .NET 응용 프로그램에서 C# 글꼴 판독기를 쉽게 만들 수 있습니다.