直写:IDWriteFontFamily::GetFontCount

DirectWrite: IDWriteFontFamily::GetFontCount

当我尝试使用 DirectWrite 获取字体系列中的字体数时,我得到了错误的结果。比如我查看系统字体文件夹,Arial字体家族有9个字体,但是,GetFontCount returns 14.那个多余的数字5是多少?那是怎么发生的?这是一个错误还是有什么我不知道的或者文档没有提到的?这是一个最小的重现示例。

 #include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
    
 #include <dwrite.h>
    
 #pragma comment(lib, "dwrite")
    
 IDWriteFactory* pDWriteFactory = NULL;
 IDWriteFontCollection* pFontCollection = NULL;
 IDWriteFontFamily* pFontFamily = NULL;
 IDWriteFont* pFont = NULL;
 IDWriteFontFace* pFontFace = NULL;
    
 int main()
 {
  HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, &IID_IDWriteFactory, &pDWriteFactory);
  if (FAILED(hr))
  return -1;
    
  hr = pDWriteFactory->GetSystemFontCollection(&pFontCollection, FALSE);
    
  if (FAILED(hr))
  return -2;
    
  UINT index = 0;
  BOOL exists;
  hr = pFontCollection->FindFamilyName(L"Arial", &index, &exists);
  if (FAILED(hr))
  return -3;
  hr = pFontCollection->GetFontFamily(index, &pFontFamily);
  if (FAILED(hr))
  return -4;
  UINT count;
    
  count = pFontFamily->GetFontCount();
  if (FAILED(hr))
  return -5;
  DWRITE_FONT_METRICS metrics;
  for (int i = 0; i < count; i++)
  {
  hr = pFontFamily->GetFont(i, &pFont);
  pFont->GetMetrics(&metrics);
    
  printf("%d %d %d %d\n", metrics.designUnitsPerEm, metrics.ascent, metrics.descent, metrics.lineGap);
  }
  return 0;
 }

DirectWrite simulates "oblique" fonts(不在物理文件中)。

For Oblique, the slant is achieved by performing a shear transformation on the characters from a normal font. When a true italic font is not available on a computer or printer, an oblique style can be generated from the normal font and used to simulate an italic font.

所以你会得到'Oblique'、'Narrow Oblique'、'Bold Oblique'、'Narrow Bold Oblique'、'Black Oblique'种模拟字体,共14种。

如果可以使用斜体,则不应使用斜体。

您可以使用 IDWriteFontFace::GetSimulations method, on each font, which will get you back DWRITE_FONT_SIMULATIONS_OBLIQUE 检查那些字体。