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

Code before:
    return FontFaceWithAttribute(_defaultFontInfo.GetWeight(), _defaultFontInfo.GetStyle(), _defaultFontInfo.GetStretch());
}

[[nodiscard]] Microsoft::WRL::ComPtr<IBoxDrawingEffect> DxFontRenderData::DefaultBoxDrawingEffect()
{
    if (!_boxDrawingEffect)
    {
        // Calculate and cache the box effect for the base font. Scale is 1.0f because the base font is exactly the scale we want already.
        THROW_IF_FAILED(s_CalculateBoxEffect(DefaultTextFormat().Get(), _glyphCell.width(), DefaultFontFace().Get(), 1.0f, &_boxDrawingEffect));
    }

    return _boxDrawingEffect;
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteTextFormat> DxFontRenderData::TextFormatWithAttribute(DWRITE_FONT_WEIGHT weight,
                                                                                                  DWRITE_FONT_STYLE style,
                                                                                                  DWRITE_FONT_STRETCH stretch)
{
    const auto defaultIt = _textFormatMap.find(_defaultFontInfo);
    if (weight == _defaultFontInfo.GetWeight() &&
        style == _defaultFontInfo.GetStyle() &&
        stretch == _defaultFontInfo.GetStretch() &&
        defaultIt != _textFormatMap.end())
    {
        return (*defaultIt).second;
    }

    DxFontInfo fontInfo = _defaultFontInfo;
    fontInfo.SetWeight(weight);
    fontInfo.SetStyle(style);
    fontInfo.SetStretch(stretch);

    const auto textFormatIt = _textFormatMap.find(fontInfo);
    if (textFormatIt == _textFormatMap.end())
    {
        // Create the font with the fractional pixel height size.
        // It should have an integer pixel width by our math.
        // Then below, apply the line spacing to the format to position the floating point pixel height characters
        // into a cell that has an integer pixel height leaving some padding above/below as necessary to round them out.
        std::wstring localeName = UserLocaleName();
        Microsoft::WRL::ComPtr<IDWriteTextFormat> textFormat;
        THROW_IF_FAILED(_BuildTextFormat(fontInfo, localeName).As(&textFormat));
        THROW_IF_FAILED(textFormat->SetLineSpacing(_lineSpacing.method, _lineSpacing.height, _lineSpacing.baseline));
        THROW_IF_FAILED(textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR));
        THROW_IF_FAILED(textFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP));

        _textFormatMap.insert({ fontInfo, textFormat });
        return textFormat;
    }
    else
    {

Review comment (suggestion):
Since we only use weight, style and stretch for hashing now, this lookup is redundant with the existing lookup below.

Also I'd suggest to not use `DxFontInfo` at all, but rather introduce a small struct that contains just the necessary 3 fields as a key for the `std::unordered_map`.

Code after:
    return FontFaceWithAttribute(_defaultFontInfo.GetWeight(), _defaultFontInfo.GetStyle(), _defaultFontInfo.GetStretch());
}

[[nodiscard]] Microsoft::WRL::ComPtr<IBoxDrawingEffect> DxFontRenderData::DefaultBoxDrawingEffect()
{
    if (!_boxDrawingEffect)
    {
        // Calculate and cache the box effect for the base font. Scale is 1.0f because the base font is exactly the scale we want already.
        THROW_IF_FAILED(s_CalculateBoxEffect(DefaultTextFormat().Get(), _glyphCell.width(), DefaultFontFace().Get(), 1.0f, &_boxDrawingEffect));
    }

    return _boxDrawingEffect;
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteTextFormat> DxFontRenderData::TextFormatWithAttribute(DWRITE_FONT_WEIGHT weight,
                                                                                                  DWRITE_FONT_STYLE style,
                                                                                                  DWRITE_FONT_STRETCH stretch)
{
    const auto textFormatIt = _textFormatMap.find(_ToMapKey(weight, style, stretch));
    if (textFormatIt == _textFormatMap.end())
    {
        DxFontInfo fontInfo = _defaultFontInfo;
        fontInfo.SetWeight(weight);
        fontInfo.SetStyle(style);
        fontInfo.SetStretch(stretch);

        // Create the font with the fractional pixel height size.
        // It should have an integer pixel width by our math.
        // Then below, apply the line spacing to the format to position the floating point pixel height characters
        // into a cell that has an integer pixel height leaving some padding above/below as necessary to round them out.
        std::wstring localeName = UserLocaleName();
        Microsoft::WRL::ComPtr<IDWriteTextFormat> textFormat;
        THROW_IF_FAILED(_BuildTextFormat(fontInfo, localeName).As(&textFormat));
        THROW_IF_FAILED(textFormat->SetLineSpacing(_lineSpacing.method, _lineSpacing.height, _lineSpacing.baseline));
        THROW_IF_FAILED(textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR));
        THROW_IF_FAILED(textFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP));

        _textFormatMap.emplace(_ToMapKey(weight, style, stretch), textFormat);
        return textFormat;
    }
    else
    {
        return textFormatIt->second;
    }
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteFontFace1> DxFontRenderData::FontFaceWithAttribute(DWRITE_FONT_WEIGHT weight,
                                                                                               DWRITE_FONT_STYLE style,
                                                                                               DWRITE_FONT_STRETCH stretch)
{
    const auto fontFaceIt = _fontFaceMap.find(_ToMapKey(weight, style, stretch));