Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/Profiles.cpp (C++)

Code before:
        com_ptr<IDWriteFontCollection> fontCollection;
        THROW_IF_FAILED(factory->GetSystemFontCollection(fontCollection.put(), TRUE));

        for (UINT32 i = 0; i < fontCollection->GetFontFamilyCount(); ++i)
        {
            try
            {
                // get the font family
                com_ptr<IDWriteFontFamily> fontFamily;
                THROW_IF_FAILED(fontCollection->GetFontFamily(i, fontFamily.put()));

                // get the font's localized names
                com_ptr<IDWriteLocalizedStrings> localizedFamilyNames;
                THROW_IF_FAILED(fontFamily->GetFamilyNames(localizedFamilyNames.put()));

                // use our current locale to find the correct name
                BOOL exists{ FALSE };
                UINT32 index;
                HRESULT hr;
                wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
                if (GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH))
                {
                    hr = localizedFamilyNames->FindLocaleName(localeName, &index, &exists);
                }
                if (SUCCEEDED(hr) && !exists)
                {
                    hr = localizedFamilyNames->FindLocaleName(L"en-us", &index, &exists);
                }
                if (!exists)
                {
                    // failed to find the correct locale, using the first one
                    index = 0;
                }

                // get the localized name
                UINT32 nameLength;
                THROW_IF_FAILED(localizedFamilyNames->GetStringLength(index, &nameLength));

                std::wstring localizedName;
                localizedName.resize(nameLength);
                THROW_IF_FAILED(localizedFamilyNames->GetString(index, localizedName.data(), nameLength + 1));

                // get the standard version of that font
                com_ptr<IDWriteFont> font;
                THROW_IF_FAILED(fontFamily->GetFirstMatchingFont(DWRITE_FONT_WEIGHT::DWRITE_FONT_WEIGHT_NORMAL,
                                                                 DWRITE_FONT_STRETCH::DWRITE_FONT_STRETCH_NORMAL,
                                                                 DWRITE_FONT_STYLE::DWRITE_FONT_STYLE_NORMAL,
                                                                 font.put()));

                // add the font name to our list
                if (font.as<IDWriteFont1>()->IsMonospacedFont())

Review comment (bug):
Please leave link to reference `https://docs.microsoft.com/en-us/windows/win32/api/dwrite/nf-dwrite-idwritelocalizedstrings-findlocalename` that told you to do "en-us" if things otherwise fail. 

Sometimes hardcoded locales show up as a no no in code analysis.

Code after:
        com_ptr<IDWriteFontCollection> fontCollection;
        THROW_IF_FAILED(factory->GetSystemFontCollection(fontCollection.put(), TRUE));

        for (UINT32 i = 0; i < fontCollection->GetFontFamilyCount(); ++i)
        {
            try
            {
                // get the font family
                com_ptr<IDWriteFontFamily> fontFamily;
                THROW_IF_FAILED(fontCollection->GetFontFamily(i, fontFamily.put()));

                // get the font's localized names
                com_ptr<IDWriteLocalizedStrings> localizedFamilyNames;
                THROW_IF_FAILED(fontFamily->GetFamilyNames(localizedFamilyNames.put()));

                // construct a font entry for tracking
                if (const auto fontEntry{ _GetFont(localizedFamilyNames) })
                {
                    // check if the font is monospaced
                    try
                    {
                        com_ptr<IDWriteFont> font;
                        THROW_IF_FAILED(fontFamily->GetFirstMatchingFont(DWRITE_FONT_WEIGHT::DWRITE_FONT_WEIGHT_NORMAL,
                                                                         DWRITE_FONT_STRETCH::DWRITE_FONT_STRETCH_NORMAL,
                                                                         DWRITE_FONT_STYLE::DWRITE_FONT_STYLE_NORMAL,
                                                                         font.put()));

                        // add the font name to our list of monospace fonts
                        const auto castedFont{ font.try_as<IDWriteFont1>() };
                        if (castedFont && castedFont->IsMonospacedFont())
                        {
                            monospaceFontList.emplace_back(fontEntry);
                        }
                    }
                    CATCH_LOG();

                    // add the font name to our list of all fonts
                    fontList.emplace_back(std::move(fontEntry));
                }
            }
            CATCH_LOG();
        }

        // sort and save the lists
        std::sort(begin(fontList), end(fontList));
        _FontList = single_threaded_observable_vector<Editor::Font>(std::move(fontList));

        std::sort(begin(monospaceFontList), end(monospaceFontList));
        _MonospaceFontList = single_threaded_observable_vector<Editor::Font>(std::move(monospaceFontList));
    }
    CATCH_LOG();