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

Code before:
            {
                winrt::com_ptr<IDWriteFont> font;
                THROW_IF_FAILED(_family->GetFont(0, font.put()));
                BOOL exists{};
                // We're actually checking for the "Extended" PowerLine glyph set.
                // They're more fun.
                THROW_IF_FAILED(font->HasCharacter(0xE0B6, &exists));
                _hasPowerlineCharacters = (exists == TRUE);
            }
            catch (...)
            {
                _hasPowerlineCharacters = false;
            }
        }
        return _hasPowerlineCharacters.value_or(false);
    }

    Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> Font::FontAxesTagsAndNames()
    {
        auto fontAxesTagsAndNames = winrt::single_threaded_map<winrt::hstring, winrt::hstring>();

        winrt::com_ptr<IDWriteFont> font;
        THROW_IF_FAILED(_family->GetFont(0, font.put()));
        winrt::com_ptr<IDWriteFontFace> fontFace;
        THROW_IF_FAILED(font->CreateFontFace(fontFace.put()));
        winrt::com_ptr<IDWriteFontFace5> fontFace5{ fontFace.as<IDWriteFontFace5>() };

        winrt::com_ptr<IDWriteFontResource> fontResource;
        THROW_IF_FAILED(fontFace5->GetFontResource(fontResource.put()));

        std::vector<DWRITE_FONT_AXIS_VALUE> axesVector;
        const auto axesCount = fontFace5->GetFontAxisValueCount();
        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

Review comment (refactor):
nit: I'd suggest replacing all these `winrt::com_ptr` with `wil::com_ptr` as the WIL helpers are more powerful and easier (and safer) to use. If you do choose to replace these, you'd also have to do it in other parts of this PR.

In either case though, the `.as` must be replaced with a `.try_as` (wil: `.try_query`), because `IDWriteFontFace5` is a very recent interface (build 20348).

Code after:
            {
                winrt::com_ptr<IDWriteFont> font;
                THROW_IF_FAILED(_family->GetFont(0, font.put()));
                BOOL exists{};
                // We're actually checking for the "Extended" PowerLine glyph set.
                // They're more fun.
                THROW_IF_FAILED(font->HasCharacter(0xE0B6, &exists));
                _hasPowerlineCharacters = (exists == TRUE);
            }
            catch (...)
            {
                _hasPowerlineCharacters = false;
            }
        }
        return _hasPowerlineCharacters.value_or(false);
    }

    Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> Font::FontAxesTagsAndNames()
    {
        if (!_fontAxesTagsAndNames)
        {
            wil::com_ptr<IDWriteFont> font;
            THROW_IF_FAILED(_family->GetFont(0, font.put()));
            wil::com_ptr<IDWriteFontFace> fontFace;
            THROW_IF_FAILED(font->CreateFontFace(fontFace.put()));
            wil::com_ptr<IDWriteFontFace5> fontFace5;
            if (fontFace5 = fontFace.try_query<IDWriteFontFace5>())
            {
                wil::com_ptr<IDWriteFontResource> fontResource;
                THROW_IF_FAILED(fontFace5->GetFontResource(fontResource.put()));

                const auto axesCount = fontFace5->GetFontAxisValueCount();
                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