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

Code before:
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteFontFace1> DxFontRenderData::DefaultFontFace()
{
    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)
{
    if (weight == _defaultFontInfo.GetWeight() &&
        style == _defaultFontInfo.GetStyle() &&
        stretch == _defaultFontInfo.GetStretch() &&
        _textFormatMap.find(_defaultFontInfo) != _textFormatMap.end())
    {
        return _textFormatMap.at(_defaultFontInfo);
    }

    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;

Review comment (performance):
The lookup here is expensive because of how `std::hash<DxFontInfo>` is implemented.

Code after:
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteFontFace1> DxFontRenderData::DefaultFontFace()
{
    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,