message: Improve load_font in DWrite shaper
diff: @@ -140,33 +140,47 @@ optional<font_key> directwrite_shaper::load_font(font_description const& _descri
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wStringConverter;
  std::wstring familyName = wStringConverter.from_bytes(_description.familyName);
  - for (UINT32 i = 0, e = fontCollection->GetFontFamilyCount(); i < e; ++i)
  - {
  - IDWriteFontFamily* family{};
  - fontCollection->GetFontFamily(i, &family);
  + UINT32 familyIndex;
  + BOOL familyExists = FALSE;
  + fontCollection->FindFamilyName(familyName.data(), &familyIndex, &familyExists);
  +
  + if (!familyExists) {
  + // Fallback to Consolas
  + const wchar_t* consolas = L"Consolas";
  + fontCollection->FindFamilyName(consolas, &familyIndex, &familyExists);
  + }
  +
  + Microsoft::WRL::ComPtr<IDWriteFontFamily> fontFamily;
  + fontCollection->GetFontFamily(familyIndex, &fontFamily);
  - IDWriteLocalizedStrings* names;
  - family->GetFamilyNames(&names);
  + Microsoft::WRL::ComPtr<IDWriteLocalizedStrings> familyNames;
  + fontFamily->GetFamilyNames(&familyNames);
  - BOOL exists = FALSE;
  + BOOL localeExists = FALSE;
  unsigned index{};
  - names->FindLocaleName(d->userLocale.c_str(), &index, &exists);
  + familyNames->FindLocaleName(d->userLocale.c_str(), &index, &localeExists);
  +
  + if (!localeExists) {
  + // Fallback to en-US
  + const wchar_t* localeName = L"en-US";
  + familyNames->FindLocaleName(localeName, &index, &localeExists);
  + }
  +
  + if (!localeExists) {
  + // Fallback to locale where index = 0;
  + return nullopt;
  + }
  +
  + UINT32 length = 0;
  + // TODO: very long font name?
  wchar_t name[64];
  - if (exists)
  - names->GetString(index, name, _countof(name));
  - else
  - // TODO: there was a different way of fallback that I have a tab open on
  - // one of my smartphones. Please find me.
  - names->GetString(i, name, sizeof(name));
  -
  - if (familyName != name)
  - continue;
  + familyNames->GetString(index, name, _countof(name));
  - for (UINT32 k = 0, ke = family->GetFontCount(); k < ke; ++k)
  + for (UINT32 k = 0, ke = fontFamily->GetFontCount(); k < ke; ++k)
  {
  ComPtr<IDWriteFont> font;
  - family->GetFont(k, font.GetAddressOf());
  + fontFamily->GetFont(k, font.GetAddressOf());
  font_weight weight = dwFontWeight(font->GetWeight());
  if (weight != _description.weight)
  @@ -203,7 +217,7 @@ optional<font_key> directwrite_shaper::load_font(font_description const& _descri
  return key;
  }
  - }
  +
  debuglog(FontFallbackTag).write("Font not found.");
  return nullopt;
  