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

Code before:
        if (axesCount > 0)
        {
            uint32_t localeIndex;
            BOOL localeExists;
            wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
            const auto localeToTry = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) ? localeName : L"en-US";

            axesVector.resize(axesCount);
            fontFace5->GetFontAxisValues(axesVector.data(), axesCount);
            for (uint32_t i = 0; i < axesCount; ++i)
            {
                const auto tagString = _axisTagToString(axesVector[i].axisTag);

                winrt::com_ptr<IDWriteLocalizedStrings> names;
                THROW_IF_FAILED(fontResource->GetAxisNames(i, names.put()));

                if (!SUCCEEDED(names->FindLocaleName(localeToTry, &localeIndex, &localeExists)) || !localeExists)
                {
                    // default to the first locale in the list
                    localeIndex = 0;
                }

                uint32_t length;
                if (SUCCEEDED(names->GetStringLength(0, &length)))
                {
                    wchar_t* name = new (std::nothrow) wchar_t[length + 1];
                    if (SUCCEEDED(names->GetString(0, name, length + 1)))
                    {
                        fontAxesTagsAndNames.Insert(tagString, winrt::hstring{ name });
                        continue;
                    }
                }
                fontAxesTagsAndNames.Insert(tagString, tagString);
            }
        }
        return fontAxesTagsAndNames;
    }

    winrt::hstring Font::_axisTagToString(DWRITE_FONT_AXIS_TAG tag)
    {
        std::wstring result;
        for (int i = 0; i < 4; ++i)
        {
            result.push_back((tag >> (i * 8)) & 0xFF);
        }
        return winrt::hstring{ result };
    }

    AppearanceViewModel::AppearanceViewModel(const Model::AppearanceConfig& appearance) :
        _appearance{ appearance }
    {

Review comment (suggestion):
There has to be a better way to initialize `name`. This is the way the [docs](https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritelocalizedstrings) do it though

Code after:
                if (axesCount > 0)
                {
                    std::vector<DWRITE_FONT_AXIS_VALUE> axesVector(axesCount);
                    fontFace5->GetFontAxisValues(axesVector.data(), axesCount);

                    uint32_t localeIndex;
                    BOOL localeExists;
                    wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
                    const auto localeToTry = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) ? localeName : L"en-US";

                    std::unordered_map<winrt::hstring, winrt::hstring> fontAxesTagsAndNames;
                    for (uint32_t i = 0; i < axesCount; ++i)
                    {
                        wil::com_ptr<IDWriteLocalizedStrings> names;
                        THROW_IF_FAILED(fontResource->GetAxisNames(i, names.put()));

                        if (!SUCCEEDED(names->FindLocaleName(localeToTry, &localeIndex, &localeExists)) || !localeExists)
                        {
                            // default to the first locale in the list
                            localeIndex = 0;
                        }

                        UINT32 length = 0;
                        if (SUCCEEDED(names->GetStringLength(localeIndex, &length)))
                        {
                            winrt::impl::hstring_builder builder{ length };
                            if (SUCCEEDED(names->GetString(localeIndex, builder.data(), length + 1)))
                            {
                                fontAxesTagsAndNames.insert(std::pair<winrt::hstring, winrt::hstring>(_axisTagToString(axesVector[i].axisTag), builder.to_hstring()));
                                continue;
                            }
                        }
                        // if there was no name found, it means the font does not actually support this axis
                        // don't insert anything into the vector in this case
                    }
                    _fontAxesTagsAndNames = winrt::single_threaded_map(std::move(fontAxesTagsAndNames));
                }
            }
        }
        return _fontAxesTagsAndNames;
    }

    winrt::hstring Font::_axisTagToString(DWRITE_FONT_AXIS_TAG tag)
    {
        std::wstring result;
        for (int i = 0; i < 4; ++i)
        {
            result.push_back((tag >> (i * 8)) & 0xFF);
        }
        return winrt::hstring{ result };
    }