C#でフォントメトリックを取得する方法

このステップバイステップガイドでは、C#でフォントメトリックを取得する方法を説明します。このチュートリアルでは、C#でType1フォントメトリックを取得するためのコードを提供しますが、C#フォントライブラリであるAspose.Font for .NETは、TruTypeおよびOpenTypeフォント形式を含む他のフォントタイプからC#のフォントメトリックを読み取るのに役立ちます。

C#でフォントメトリックを取得する手順

  1. NuGet.orgからAspose.Font for .NETパッケージをインストールします
  2. Aspose.FontAspose.Font.Sources、およびAspose.Font.Type1名前空間を含める
  3. 評価透かしを回避するために、Aspose.Fontfor.NETにライセンスを適用します
  4. 入力Type1フォントをFileSystemStreamSourceオブジェクトにロードします
  5. ストリームソースからFontFileDefinitionオブジェクトを作成します
  6. ファイル定義からFontDefinitionオブジェクトを作成します
  7. フォント定義をType1Fontオブジェクトとして開きます
  8. 次に、Type1フォントのすべてのメトリック属性を取得します

フォントには、フォント内のグリフを説明するのに役立つメトリック情報が含まれています。フォントのこのメトリック情報は、コンピューターが画面上に文字や文を描画する方法を学習するのに役立ちます。 .NETアプリケーションのコードを介してこのメトリック情報を処理する場合、それは簡単な作業ではありません。ただし、Aspose.Font for .NETは、上記のいくつかの手順を使用して、プロセス全体を非常にシンプルかつ簡単にするのに役立ちます。

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

上記のコードでは、PFBファイル形式を読み込んでいます。この形式には、C#コードで処理する必要のあるType1フォントが含まれています。 fontをロードしてType1Fontオブジェクトに変換すると、このファイルからC#のフォントメトリックを抽出できるようになります。

このコードは、Web、デスクトップ、Windows、Microsoft Storeアプリケーションなどの.NETアプリケーションでC#フォントリーダーを簡単に作成するのに役立ちます。

 日本語