IDWriteFontCollection 介面 (dwrite.h)

物件,封裝一組字型,例如安裝在系統上的字型集,或特定目錄中的字型集合。 字型集合 API 可用來探索有哪些字型系列和字型可用,以及取得有關字型的一些元數據。

繼承

IDWriteFontCollection 介面繼承自 IUnknown 介面。 IDWriteFontCollection 也有下列類型的成員:

方法

IDWriteFontCollection 介面具有這些方法。

 
IDWriteFontCollection::FindFamilyName

尋找具有指定系列名稱的字型系列。
IDWriteFontCollection::GetFontFamily

指定以零起始的字型系列索引,建立字型系列物件。
IDWriteFontCollection::GetFontFamilyCount

取得集合中的字型系列數目。
IDWriteFontCollection::GetFontFromFontFace

取得對應至與指定字型臉部物件相同的實體字型的字型物件。 指定的實體字型必須屬於字型集合。

備註

IDWriteFactory::GetSystemFontCollection 方法會為您提供IDWriteFontCollection 物件,該物件會封裝安裝在系統上的字型集,如下列程式代碼範例所示。

IDWriteFontCollection* pFontCollection = NULL;

// Get the system font collection.
if (SUCCEEDED(hr))
{
    hr = pDWriteFactory->GetSystemFontCollection(&pFontCollection);
}

IDWriteTextFormatIDWriteTextLayout 都有 GetFontCollection 方法,可傳回 物件所使用的字型集合。 這些介面預設會使用系統字型集合,但可以改用自定義字型集合。

若要判斷系統上有哪些字型可用,請取得系統字型集合的參考。 然後,您可以使用 IDWriteFontCollection::GetFontFamilyCount 方法來判斷字型數目,並迴圈查看清單。 下列範例會列舉系統字型集合中的字型,並將字型系列名稱列印到主控台。


#include <dwrite.h>
#include <string.h>
#include <stdio.h>
#include <new>

// SafeRelease inline function.
template <class T> inline void SafeRelease(T **ppT)
{
    if (*ppT)
    {
        (*ppT)->Release();
        *ppT = NULL;
    }
}

void wmain()
{
    IDWriteFactory* pDWriteFactory = NULL;

    HRESULT hr = DWriteCreateFactory(
            DWRITE_FACTORY_TYPE_SHARED,
            __uuidof(IDWriteFactory),
            reinterpret_cast<IUnknown**>(&pDWriteFactory)
            );

    IDWriteFontCollection* pFontCollection = NULL;

    // Get the system font collection.
    if (SUCCEEDED(hr))
    {
        hr = pDWriteFactory->GetSystemFontCollection(&pFontCollection);
    }

    UINT32 familyCount = 0;

    // Get the number of font families in the collection.
    if (SUCCEEDED(hr))
    {
        familyCount = pFontCollection->GetFontFamilyCount();
    }

    for (UINT32 i = 0; i < familyCount; ++i)
    {
        IDWriteFontFamily* pFontFamily = NULL;

        // Get the font family.
        if (SUCCEEDED(hr))
        {
            hr = pFontCollection->GetFontFamily(i, &pFontFamily);
        }

        IDWriteLocalizedStrings* pFamilyNames = NULL;
        
        // Get a list of localized strings for the family name.
        if (SUCCEEDED(hr))
        {
            hr = pFontFamily->GetFamilyNames(&pFamilyNames);
        }

        UINT32 index = 0;
        BOOL exists = false;
        
        wchar_t localeName[LOCALE_NAME_MAX_LENGTH];

        if (SUCCEEDED(hr))
        {
            // Get the default locale for this user.
            int defaultLocaleSuccess = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH);

            // If the default locale is returned, find that locale name, otherwise use "en-us".
            if (defaultLocaleSuccess)
            {
                hr = pFamilyNames->FindLocaleName(localeName, &index, &exists);
            }
            if (SUCCEEDED(hr) && !exists) // if the above find did not find a match, retry with US English
            {
                hr = pFamilyNames->FindLocaleName(L"en-us", &index, &exists);
            }
        }
        
        // If the specified locale doesn't exist, select the first on the list.
        if (!exists)
            index = 0;

        UINT32 length = 0;

        // Get the string length.
        if (SUCCEEDED(hr))
        {
            hr = pFamilyNames->GetStringLength(index, &length);
        }

        // Allocate a string big enough to hold the name.
        wchar_t* name = new (std::nothrow) wchar_t[length+1];
        if (name == NULL)
        {
            hr = E_OUTOFMEMORY;
        }

        // Get the family name.
        if (SUCCEEDED(hr))
        {
            hr = pFamilyNames->GetString(index, name, length+1);
        }
        if (SUCCEEDED(hr))
        {
            // Print out the family name.
            wprintf(L"%s\n", name);
        }

        SafeRelease(&pFontFamily);
        SafeRelease(&pFamilyNames);

        delete [] name;
    }

    SafeRelease(&pFontCollection);
    SafeRelease(&pDWriteFactory);
}


規格需求

需求
最低支援的用戶端 適用於 Windows Vista 的 Windows 7、Windows Vista SP2 和平臺更新 [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2008 R2、Windows Server 2008 SP2 和 Platform Update for Windows Server 2008 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 dwrite.h