Repository: microsoft/terminal
File: src/renderer/dx/DxFontRenderData.cpp (C++)

Code before:
    ::Microsoft::WRL::ComPtr<IDWriteTextFormat3> format3;
    if (!_axesVector.empty() && !FAILED(format->QueryInterface(IID_PPV_ARGS(&format3))))
    {
        std::optional<float> oldWeight;
        if (!_useUserWeight)
        {
            // Remove the user set weight if we were told not to use it
            // This is quite awkward, we don't want to permanently delete the weight axis from our _axesVector,
            // we only want to delete it from the list we are about to pass into the text format
            for (auto iter = _axesVector.begin(); iter != _axesVector.end(); ++iter)
            {
                if (iter->axisTag == DWRITE_FONT_AXIS_TAG_WEIGHT)
                {
                    oldWeight = iter->value;
                    _axesVector.erase(iter);
                    break;
                }
            }
        }
        DWRITE_FONT_AXIS_VALUE const* axesList = _axesVector.data();
        format3->SetFontAxisValues(axesList, gsl::narrow<uint32_t>(_axesVector.size()));
        // If we removed the user weight, make sure to put it back
        if (oldWeight)
        {
            _axesVector.emplace_back(DWRITE_FONT_AXIS_VALUE{ DWRITE_FONT_AXIS_TAG_WEIGHT, oldWeight.value() });
        }
    }

    return format;
}


Review comment (suggestion):
I'm generally quite against any code that takes the format, "Delete something from global state, then re-add it after the patrols have passed by". There is a better way to do this, and I suspect that we should do that instead.

Code after:
    // coordinates designate the center of the line.
    lineMetrics.underlineOffset += lineMetrics.underlineWidth / 2.0f;
    lineMetrics.underlineOffset2 += lineMetrics.underlineWidth / 2.0f;
    lineMetrics.strikethroughOffset += lineMetrics.strikethroughWidth / 2.0f;

    _lineMetrics = lineMetrics;

    _glyphCell = actual.GetSize();
}

Microsoft::WRL::ComPtr<IDWriteTextFormat> DxFontRenderData::_BuildTextFormat(const DxFontInfo fontInfo, const std::wstring_view localeName)
{
    Microsoft::WRL::ComPtr<IDWriteTextFormat> format;
    THROW_IF_FAILED(_dwriteFactory->CreateTextFormat(fontInfo.GetFamilyName().data(),
                                                     nullptr,
                                                     fontInfo.GetWeight(),
                                                     fontInfo.GetStyle(),
                                                     fontInfo.GetStretch(),
                                                     _fontSize,
                                                     localeName.data(),
                                                     &format));

    // If the OS supports IDWriteTextFormat3, set the font axes
    ::Microsoft::WRL::ComPtr<IDWriteTextFormat3> format3;
    if (!FAILED(format->QueryInterface(IID_PPV_ARGS(&format3))))
    {
        if (_inhibitUserWeight && !_axesVectorWithoutWeight.empty())
        {
            format3->SetFontAxisValues(_axesVectorWithoutWeight.data(), gsl::narrow<uint32_t>(_axesVectorWithoutWeight.size()));
        }
        else if (!_inhibitUserWeight && !_axesVector.empty())
        {
            format3->SetFontAxisValues(_axesVector.data(), gsl::narrow<uint32_t>(_axesVector.size()));
        }
    }

    return format;
}