Interfaccia IDWriteFontCollection (dwrite.h)

Oggetto che incapsula un set di tipi di carattere, ad esempio il set di tipi di carattere installati nel sistema o il set di tipi di carattere in una determinata directory. L'API della raccolta di tipi di carattere può essere usata per individuare le famiglie di tipi di carattere e i tipi di carattere disponibili e per ottenere alcuni metadati sui tipi di carattere.

Ereditarietà

L'interfaccia IDWriteFontCollection eredita dall'interfaccia IUnknown . IDWriteFontCollection include anche questi tipi di membri:

Metodi

L'interfaccia IDWriteFontCollection include questi metodi.

 
IDWriteFontCollection::FindFamilyName

Trova la famiglia di caratteri con il nome della famiglia specificata.
IDWriteFontCollection::GetFontFamily

Crea un oggetto famiglia di caratteri dato un indice della famiglia di caratteri in base zero.
IDWriteFontCollection::GetFontFamilyCount

Ottiene il numero di famiglie di caratteri nella raccolta.
IDWriteFontCollection::GetFontFromFontFace

Ottiene l'oggetto carattere che corrisponde allo stesso tipo di carattere fisico dell'oggetto viso del carattere specificato. Il carattere fisico specificato deve appartenere alla raccolta di caratteri.

Commenti

Il metodo IDWriteFactory::GetSystemFontCollection ti darà un oggetto IDWriteFontCollection , che incapsula il set di tipi di carattere installati nel sistema, come illustrato nell'esempio di codice seguente.

IDWriteFontCollection* pFontCollection = NULL;

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

IDWriteTextFormat e IDWriteTextLayout hanno entrambi un metodo GetFontCollection che restituisce l'insieme di caratteri usato dall'oggetto . Queste interfacce usano la raccolta di tipi di carattere di sistema per impostazione predefinita, ma possono usare invece una raccolta di caratteri personalizzata.

Per determinare quali tipi di carattere sono disponibili nel sistema, ottenere un riferimento alla raccolta di caratteri di sistema. È quindi possibile usare il metodo IDWriteFontCollection::GetFontFamilyCount per determinare il numero di tipi di carattere e ciclo nell'elenco. Nell'esempio seguente vengono enumerati i tipi di carattere nella raccolta di tipi di carattere di sistema e vengono stampati i nomi della famiglia di caratteri nella console.


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


Requisiti

Requisito Valore
Client minimo supportato Windows 7, Windows Vista con SP2 e Aggiornamento della piattaforma per Windows Vista [app desktop | App UWP]
Server minimo supportato Windows Server 2008 R2, Windows Server 2008 con SP2 e Platform Update per Windows Server 2008 [app desktop | App UWP]
Piattaforma di destinazione Windows
Intestazione dwrite.h