Compartir a través de


Interfaz IDWriteFontCollection (dwrite.h)

Objeto que encapsula un conjunto de fuentes, como el conjunto de fuentes instaladas en el sistema o el conjunto de fuentes de un directorio determinado. La API de colección de fuentes se puede usar para detectar qué familias de fuentes y fuentes están disponibles y para obtener algunos metadatos sobre las fuentes.

Herencia

La interfaz IDWriteFontCollection hereda de la interfaz IUnknown . IDWriteFontCollection también tiene estos tipos de miembros:

Métodos

La interfaz IDWriteFontCollection tiene estos métodos.

 
IDWriteFontCollection::FindFamilyName

Busca la familia de fuentes con el nombre de familia especificado.
IDWriteFontCollection::GetFontFamily

Crea un objeto de familia de fuentes dado un índice de familia de fuentes de base cero.
IDWriteFontCollection::GetFontFamilyCount

Obtiene el número de familias de fuentes de la colección.
IDWriteFontCollection::GetFontFromFontFace

Obtiene el objeto de fuente que corresponde a la misma fuente física que el objeto de cara de fuente especificado. La fuente física especificada debe pertenecer a la colección de fuentes.

Comentarios

El método IDWriteFactory::GetSystemFontCollection le proporcionará un objeto IDWriteFontCollection , que encapsula el conjunto de fuentes instaladas en el sistema, como se muestra en el ejemplo de código siguiente.

IDWriteFontCollection* pFontCollection = NULL;

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

IDWriteTextFormat e IDWriteTextLayout tienen un método GetFontCollection que devuelve la colección de fuentes que usa el objeto . Estas interfaces usan la colección de fuentes del sistema de forma predeterminada, pero pueden usar una colección de fuentes personalizada en su lugar.

Para determinar qué fuentes están disponibles en el sistema, obtenga una referencia a la colección de fuentes del sistema. A continuación, puede usar el método IDWriteFontCollection::GetFontFamilyCount para determinar el número de fuentes y recorrer en bucle la lista. En el ejemplo siguiente se enumeran las fuentes de la colección de fuentes del sistema y se imprimen los nombres de familia de fuentes en la consola.


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


Requisitos

Requisito Value
Cliente mínimo compatible Windows 7, Windows Vista con SP2 y Platform Update para Windows Vista [aplicaciones de escritorio | Aplicaciones para UWP]
Servidor mínimo compatible Windows Server 2008 R2, Windows Server 2008 con SP2 y Actualización de plataforma para Windows Server 2008 [aplicaciones de escritorio | Aplicaciones para UWP]
Plataforma de destino Windows
Encabezado dwrite.h