2016-11-01 1 views
0

Ich versuche, alle verfügbaren Schriftarten von Windows durch IDWriteFactory :: GetSystemFontCollection in C++ zu erhalten. Ich habe alle Microsoft MSDN-Schritte befolgt. Jetzt kann ich alle wichtigen Schriftarten erfolgreich erhalten, aber es werden keine "Varianten" -Schriften bereitgestellt.IDWriteFactory :: GetSystemFontCollection gibt keine Varianten von Schriftarten

ZB bekomme ich "Arial", aber kein "Arial Black" und "Arial Narrow", "Yu Gothic" aber kein "Yu Gothic Medium" und "Yu Gothic Light". Jeder weiß, wie man alle diese Schriften in C++ bekommt? Vielen Dank!

Unten ist mein Programm:

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

Antwort

1

GDI hatte eine Beschränkung von vier Varianten pro verschiedene Schriftfamilie. Daher mussten Arial Black und Arial Narrow als separate Schriftfamilien behandelt werden, obwohl es sich in Wirklichkeit nur um verschiedene Varianten von Arial handelt.

DirectWrite hat diese Einschränkung nicht, daher gibt es jetzt nur Arial und Arial Black und Arial Narrow kommen als Varianten von Arial vor. Versuchen Sie es: Rufen Sie GetFonts() auf der IDWriteFontFamily für Arial und drucken Sie die GetFaceNames() s aller resultierenden IDWriteFonts.

Natürlich kann dies gegen jemanden inkorrekt sein, der mit einem Programm arbeitet, das GDI-Schriftartenaufzählung verwendet, oder erwartet, dass der Familienname von Arial Black Arial Black anstelle von Arial ist (z. B. in einem Dateiformat). Ich bin mir nicht sicher, was die Lösung für dieses Problem ist ...

+0

Danke! Das hat funktioniert! – user3508838

Verwandte Themen