TrueTypeフォントの場所を指定します

このトピックでは、オペレーティングシステム固有の違いを含むTrueTypeフォントを検索するときのAspose.Wordsの既定の動作について説明し、ユーザーフォントソースを指

FontSourceBaseクラスは、さまざまなフォントソースを指定するために使用されます。 FontSourceBaseクラスにはいくつかの実装があります:

いくつかのクラスの実装の詳細は以下に説明されています。

システム{#loading-fonts-from-system}からフォントを読み込む

デフォルトで常に使用される特別なSystemFontSourceクラスがあります。 システムにインストールされているすべてのTrueTypeフォントを表します。 したがって、SystemFontSourceとその他の必要なソースを使用してソースリストを作成することができます:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Rendering.doc");
System::SharedPtr<FontSettings> fontSettings = System::MakeObject<FontSettings>();
// Retrieve the array of environment-dependent font sources that are searched by default. For example this will contain a "Windows\Fonts\" source on a Windows machines.
// We add this array to a new ArrayList to make adding or removing font entries much easier.
System::SharedPtr<TFontSourceBasePtrList> fontSources = System::MakeObject<TFontSourceBasePtrList>(fontSettings->GetFontsSources());
// Add a new folder source which will instruct Aspose.Words to search the following folder for fonts.
System::SharedPtr<FolderFontSource> folderFontSource = System::MakeObject<FolderFontSource>(u"C:\\MyFonts\\", true);
// Add the custom folder which contains our fonts to the list of existing font sources.
fontSources->Add(folderFontSource);
// Convert the Arraylist of source back into a primitive array of FontSource objects.
System::ArrayPtr<TFontSourceBasePtr> updatedFontSources = fontSources->ToArray();
// Apply the new set of font sources to use.
fontSettings->SetFontsSources(updatedFontSources);
// Set font settings
doc->set_FontSettings(fontSettings);
System::String outputPath = outputDataDir + u"SetFontsFoldersSystemAndCustomFolder.pdf";
doc->Save(outputPath);

SystemFontSourceクラスの単一のインスタンスは、FontSettingsでデフォルトで定義されています。 オペレーティングシステムが異なれば、フォントは異なる場所に配置される場合があります。 ただし、各ドキュメントにFontSettingsインスタンスを使用することは最適な解決策ではありません。 ほとんどの場合、DefaultInstanceを使用するだけで十分です。

ドキュメントごとのインスタンスは、ドキュメントごとに異なるフォントソースを使用する必要がある場合にのみ必要ですが、これはまれなケースです。 複数のFontSettingsインスタンスを使用すると、キャッシュを共有しないため、パフォーマンスが低下します。

ここでAspose.WordsはWindowsのTrueTypeフォントを探します

ほとんどの場合、Windowsユーザーはフォントの欠落や誤ったレイアウトで重大な問題に直面することはありません。 通常、Aspose.Wordsはドキュメントを通過し、フォントのリンクに遭遇すると、システムフォルダからフォントデータを正常に取得します。

Windowsでは、Aspose.Wordsはまず_%windir%\Fontsフォルダから利用可能なすべてのフォントを取得します。 この設定は、ほとんどの場合、あなたのために動作します。 必要な場合にのみ、独自のフォントフォルダを指定します。 Aspose.Wordsは、HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fontsレジストリキーに登録されている追加のフォントも検索します。 また、Windows10では、現在のユーザーのフォントのインストールが有効になります。 フォントは%userprofile%\AppData\Local\Microsoft\Windows\Fontsフォルダに配置され、HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Fontsレジストリでも指定されます。Aspose.Wordsはこれらのフォントを探します。

文書に埋め込みフォントが含まれている場合、Aspose.Wordsは文書から関連するフォントデータを読み取り、それを使用して文書のレイアウトを作成できます。 ドキュメントには、システムフォルダにないフォントへのリンクが含まれている場合もあります。:

  • ユーザーはFontSettingsクラスを使用して新しいフォントソースを設定できます
  • Aspose.Wordsは、欠落したフォントを同様のフォントに置き換えることができます

Windows以外のシステム上のフォント

Aspose.Wordsはシステムフォントフォルダ内のフォントを探します。 これらのフォルダのリストはGetSystemFontFoldersメソッドで見ることができます。 サポートされているフォントが見つからない場合、Aspose.Wordsは組み込みのデフォルトフォントFanwoodを使用します。ttf.

WindowsとWindows以外のOSのフォントメトリックは異なるため、Aspose.Wordsは同様のフォントを見つけてオリジナルと同様のレイアウトを構築するために可能な限り ただし、これが常に可能であるとは限りません。 このような場合は、カスタムフォントまたは置換ルールを追加するためにFontSettingsクラスを使用する必要があります。

ここでAspose.WordsはLinuxのTrueTypeフォントを探します

Linuxディストリビューションが異なれば、フォントを異なるフォルダに格納することがあります。 Aspose.Words複数の場所でフォントを探します。 デフォルトでは、Aspose.Wordsは以下のすべての場所でフォントを探します。/usr/share/fonts /usr/local/share/fonts /usr/X11R6/lib/X11/fonts. Thisデフォルトの動作はほとんどのLinuxディストリビューションで これを行うには、TrueTypeフォントがLinuxディストリビューションのどこにインストールされているかを知る必要があります。

Mac OS XでAspose.WordsがTrueTypeフォントを探す場所

Aspose.Wordsは、Mac OS X上のTrueTypeフォントの標準的な場所である/Library/Fontsフォルダ内のフォントを探します。

TrueTypeAndroid上のフォント

Androidでは、フォントワークフローはTypefaceクラスにカプセル化されています。 書体には5つのタイプがあり、各書体は類似のフォントファミリのグループを表しています:

  • デフォルト
  • DEFAULT_BOLD
  • モノスペース
  • サンセリフ
  • セリフ

たとえば、Androidによると フォント。xml 設定ファイル、“times"は"serif"ファミリに属しているので、NotoSerif-Regular。ttfは、「times」が要求されたときに使用されます:

フォント。xml

<family name="serif">
        <font weight="400" style="normal">NotoSerif-Regular.ttf</font>
        <font weight="700" style="normal">NotoSerif-Bold.ttf</font>
        <font weight="400" style="italic">NotoSerif-Italic.ttf</font>
        <font weight="700" style="italic">NotoSerif-BoldItalic.ttf</font>
</family>
<alias name="times" to="serif" />
<alias name="times new roman" to="serif" />

類似のフォントを検索するには、先に説明した戦略が使用されます。

それらに加えて、Aspose.WordsにはAndroidプラットフォームの代替品の独自のリストがあります。

文書にPMingLiU-ExtBフォントが含まれているとしましょう。Aspose.Wordsはシステムソース内で必要なフォントを探しています。

Androidフォントのフォルダのデフォルトのリストは次のとおりです:

  • /システム/フォント
  • /システム/フォント
  • /データ/フォント

Aspose.Wordsは、メソッドで設定されたユーザー定義のソースを調べます:

Java

fontSettings->SetFontsFolder(u"C:\\MyFonts\\", true);

明示的な置換が指定されている場合、Aspose.Wordsは不足しているフォントをユーザーの提案に置き換えます:

Java

fontSettings->get_SubstitutionSettings()->get_TableSubstitution()->SetSubstitutes(u"PMingLiU-ExtB", System::MakeArray<System::String>({ u"Liberation Serif" }));

いずれのルールも機能しなかった場合は、Aspose.Words内部置換テーブルを確認します。 テーブルに適切な適合に関する情報が含まれている場合、フォントは置き換えられます。 私たちの場合、Aspose.WordsはTypeface.SERIFを選択します。 しかし、テーブルが要求されたフォントについて何も知らない場合、Aspose.Wordsは特別なMS WordルールまたはPanose空間内の最も近い距離に基づいてフォントを取得します。

.NET CoreとXamarinのTrueTypeフォント

.NET CoreとXamarinの場合は、同じルールが適用されます Aspose.WordsのためのJava バージョン。 デフォルトでは、アプリケーションが実行されているプラットフォームのすべてのシステムフォントが使用可能です。 検索が実行されるフォルダのリストは、メソッドを呼び出すことで見つけることができます:

Java

SystemFontSource()->GetAvailableFonts()

フォルダ{#loading-fonts-from-folder}からフォントを読み込む

処理中のドキュメントに、システム上にないフォントへのリンクが含まれている場合、またはシステムフォルダに追加したくない場合、または権限がない場合は、SetFontsSourcesメソッドを使用して独自のフォントを含むフォルダを追加するのが最善の解決策です。 これにより、システムソースをユーザーソースに置き換えることができます。 Aspose.WordsはレジストリまたはWindows\Fontフォルダ内のフォントを検索せず、代わりに指定されたフォルダ内のフォントのみをスキャンします。 GetFontSourcesメソッドは対応する値を返します。

1つまたは複数のフォントフォルダを指定します

SetFontsFolderメソッドとSetFontsFoldersメソッドは、一つまたは複数のFolderFontSourceインスタンスを持つSetFontSourcesメソッドへのショートカットです。 これらのメソッドは、Aspose.Wordsがフォントを探す場所を示すために使用されます。 フォルダが存在しないか、アクセスできない場合、Aspose.Wordsはこのフォルダを無視します。 フォント置換のソースを含むすべてのフォルダが無視された場合、Aspose.WordsはデフォルトとしてFanwoodフォントを使用します。

次の例は、フォントのレンダリングまたは埋め込み中にAspose.Wordsがその後TrueTypeフォントを検索するために使用するフォルダまたはソースを設定する方法を示:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_RenderingAndPrinting();
System::String outputDataDir = GetOutputDataDir_RenderingAndPrinting();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Rendering.doc");
System::SharedPtr<FontSettings> fontSettings = System::MakeObject<FontSettings>();
// Note that this setting will override any default font sources that are being searched by default. Now only these folders will be searched for
// Fonts when rendering or embedding fonts. To add an extra font source while keeping system font sources then use both FontSettings.GetFontSources and
// FontSettings.SetFontSources instead.
fontSettings->SetFontsFolder(u"C:\\MyFonts\\", false);
// Set font settings
doc->set_FontSettings(fontSettings);
System::String outputPath = outputDataDir + u"SetTrueTypeFontsFolder.pdf";
doc->Save(outputPath);

この例のテンプレートファイルは、次の場所からダウンロードできます Aspose.Words GitHub.

余分なBooleanパラメータは、フォントがすべてのフォルダを再帰的にスキャンされるかどうかを制御し、指定されたフォルダのすべての子フ 次の例では、フォントのレンダリングまたは埋め込み時にTrueTypeフォントを複数のフォルダーで検索するようにAspose.Wordsを設定する方法を示します:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_RenderingAndPrinting();
System::String outputDataDir = GetOutputDataDir_RenderingAndPrinting();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Rendering.doc");
System::SharedPtr<FontSettings> fontSettings = System::MakeObject<FontSettings>();
// Note that this setting will override any default font sources that are being searched by default. Now only these folders will be searched for
// Fonts when rendering or embedding fonts. To add an extra font source while keeping system font sources then use both FontSettings.GetFontSources and
// FontSettings.SetFontSources instead.
fontSettings->SetFontsFolders(System::MakeArray<System::String>({u"C:\\MyFonts\\", u"D:\\Misc\\Fonts\\"}), true);
// Set font settings
doc->set_FontSettings(fontSettings);
System::String outputPath = outputDataDir + u"SetFontsFoldersMultipleFolders.pdf";
doc->Save(outputPath);

優先順位に注意してください。 異なるフォントソースに同じファミリ名とスタイルのフォントがある場合、Aspose.Wordsはソースから優先度の高いフォントを選択します。 以下の"優先度"フィールドの説明を参照してください。

システムフォントをまったく使用したくない場合は、Aspose.Wordsを使用してそれらを無視し、独自のフォントのみを使用できます:

C++

System::SharedPtr<FontSettings> fontSettings = System::MakeObject<FontSettings>()->get_DefaultInstance();
fontSettings->SetFontsFolder(u"C:\\MyFonts\\", true);

優先度プロパティ

Priorityプロパティは、異なるフォントソースに同じファミリ名とスタイルを持つフォントがある場合に使用されます。 この場合、Aspose.Wordsは、優先度の高い値を持つソースからフォントを選択します。 たとえば、システムフォルダに古いバージョンのフォントがあり、顧客がカスタムフォルダに同じフォントの新しいバージョンを追加したとします。

C++

System::SharedPtr<FolderFontSource> folderFontSource = System::MakeObject<FolderFontSource>(u"C:\\MyFonts\\", true, 1);

ストリーム{#loading-fonts-from-stream}からフォントを読み込む

Aspose.WordsはStreamFontSourceクラスを提供し、ストリームからフォントを読み込むことができます。 ストリームフォントソースを使用するには、ユーザーはStreamFontSourceから派生クラスを作成し、OpenFontDataStreamメソッドの実装を提供する必要があります。 OpenFontDataStreamメソッドは複数回呼び出すことができます。 Aspose.Wordsが提供されたフォントソースをスキャンして利用可能なフォントのリストを取得するときに初めて呼び出されます。 後で、フォントが文書内で使用されてフォントデータを解析し、フォントデータをいくつかの出力形式に埋め込む場合に呼び出されることがあります。 StreamFontSourceは、フォントデータを必要なときにのみ読み込むことができ、FontSettingsの存続期間中はメモリに格納しないため、便利です。

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
namespace
{
class ResourceSteamFontSourceExample : public StreamFontSource
{
typedef ResourceSteamFontSourceExample ThisType;
typedef Aspose::Words::Fonts::StreamFontSource BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
RTTI_INFO(ThisType, ThisTypeBaseTypesInfo);
public:
System::SharedPtr<System::IO::Stream> OpenFontDataStream() override;
};
System::SharedPtr<System::IO::Stream> ResourceSteamFontSourceExample::OpenFontDataStream()
{
return System::Reflection::Assembly::GetExecutingAssembly()->GetManifestResourceStream(u"resourceName");
}
}
void ResourceSteamFontSource()
{
std::cout << "ResourceSteamFontSource example started." << std::endl;
// The path to the documents directory.
System::String dataDir = GetInputDataDir_RenderingAndPrinting();
System::SharedPtr<Document> doc = System::MakeObject<Document>(dataDir + u"Rendering.doc");
// FontSettings.SetFontSources instead.
FontSettings::get_DefaultInstance()->SetFontsSources(System::MakeArray<System::SharedPtr<FontSourceBase>>({System::MakeObject<SystemFontSource>(), System::MakeObject<ResourceSteamFontSourceExample>()}));
System::String outputPath = GetOutputDataDir_RenderingAndPrinting() + u"ResourceSteamFontSource.pdf";
doc->Save(outputPath);
std::cout << "ResourceSteamFontSource example finished." << std::endl << std::endl;
}

StreamFontSource

フォント検索キャッシュの保存と読み込み

最初にフォントを検索するとき、Aspose.Wordsはユーザーが指定したフォントソースを反復処理し、これらのソースからのデータに基づいてフォント検索キャッシュを形成 したがって、キャッシュは利用可能なフォントに関する情報を収集します:フォントファミリ、スタイル、完全なフォント名など。 その後の呼び出しでは、Aspose.Wordsはフォント検索キャッシュ内の名前で目的のフォントに関する情報を検索し、その後、指定されたファイルを解析してフ

キャッシュを初期化するために使用可能なすべてのフォントファイルを解析する手順は非常に時間がかかります。 Aspose.Wordsを使用すると、パフォーマンスの問題を解決するためにSaveSearchCacheメソッドを使用してキャッシュを保存およびロードできます。 つまり、ユーザーは以前に保存したキャッシュをファイルからロードし、使用可能なすべてのフォントファイルを解析する手順をスキップできます。

利用可能なフォントのリストを取得

使用可能なフォントのリストを取得する場合、たとえばPDFドキュメントのレンダリングに使用できる場合は、次のコード例に示すように、GetAvailableFontsメソッドを使 PhysicalFontInfoクラスは、Aspose.Wordsフォントエンジンで使用可能な物理フォントに関する情報を指定します:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Get available system fonts
for (auto fontInfo : System::IterateOver(System::MakeObject<SystemFontSource>()->GetAvailableFonts()))
{
std::cout << "FontFamilyName : " << fontInfo->get_FontFamilyName().ToUtf8String() << std::endl;
std::cout << "FullFontName : " << fontInfo->get_FullFontName().ToUtf8String() << std::endl;
std::cout << "Version : " << fontInfo->get_Version().ToUtf8String() << std::endl;
std::cout << "FilePath : " << fontInfo->get_FilePath().ToUtf8String() << std::endl;
}
// Get available fonts in folder
for (auto fontInfo : System::IterateOver(System::MakeObject<FolderFontSource>(inputDataDir, true)->GetAvailableFonts()))
{
std::cout << "FontFamilyName : " << fontInfo->get_FontFamilyName().ToUtf8String() << std::endl;
std::cout << "FullFontName : " << fontInfo->get_FullFontName().ToUtf8String() << std::endl;
std::cout << "Version : " << fontInfo->get_Version().ToUtf8String() << std::endl;
std::cout << "FilePath : " << fontInfo->get_FilePath().ToUtf8String() << std::endl;
}
// Get available fonts from FontSettings
for (System::SharedPtr<FontSourceBase> fontsSource : FontSettings::get_DefaultInstance()->GetFontsSources())
{
for (auto fontInfo : System::IterateOver(fontsSource->GetAvailableFonts()))
{
std::cout << "FontFamilyName : " << fontInfo->get_FontFamilyName().ToUtf8String() << std::endl;
std::cout << "FullFontName : " << fontInfo->get_FullFontName().ToUtf8String() << std::endl;
std::cout << "Version : " << fontInfo->get_Version().ToUtf8String() << std::endl;
std::cout << "FilePath : " << fontInfo->get_FilePath().ToUtf8String() << std::endl;
}
}