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

Code before:
    const auto axesCount = format->GetFontAxisValueCount();
    std::vector<DWRITE_FONT_AXIS_VALUE> axesVector;
    axesVector.resize(axesCount);
    format->GetFontAxisValues(axesVector.data(), axesCount);

    auto axisTagPresence = AxisTagPresence::None;
    for (const auto& fontAxisValue : axesVector)
    {
        switch (fontAxisValue.axisTag)
        {
        case DWRITE_FONT_AXIS_TAG_WEIGHT:
            WI_SetFlag(axisTagPresence, AxisTagPresence::Weight);
            break;
        case DWRITE_FONT_AXIS_TAG_WIDTH:
            WI_SetFlag(axisTagPresence, AxisTagPresence::Width);
            break;
        case DWRITE_FONT_AXIS_TAG_ITALIC:
            WI_SetFlag(axisTagPresence, AxisTagPresence::Italic);
            break;
        case DWRITE_FONT_AXIS_TAG_SLANT:
            WI_SetFlag(axisTagPresence, AxisTagPresence::Slant);
            break;
        }
    }

    if (WI_IsFlagClear(axisTagPresence, AxisTagPresence::Weight) || fontWeight != DWRITE_FONT_WEIGHT::DWRITE_FONT_WEIGHT_NORMAL)
    {
        axesVector.emplace_back(DWRITE_FONT_AXIS_VALUE{ DWRITE_FONT_AXIS_TAG_WEIGHT, gsl::narrow<float>(fontWeight) });
    }
    if (WI_IsFlagClear(axisTagPresence, AxisTagPresence::Width))
    {
        axesVector.emplace_back(DWRITE_FONT_AXIS_VALUE{ DWRITE_FONT_AXIS_TAG_WIDTH, _FontStretchToWidthAxisValue(fontStretch) });
    }
    if (WI_IsFlagClear(axisTagPresence, AxisTagPresence::Italic))
    {
        axesVector.emplace_back(DWRITE_FONT_AXIS_VALUE{ DWRITE_FONT_AXIS_TAG_ITALIC, (fontStyle == DWRITE_FONT_STYLE_ITALIC ? 1.0f : 0.0f) });
    }
    if (WI_IsFlagClear(axisTagPresence, AxisTagPresence::Slant))
    {
        axesVector.emplace_back(DWRITE_FONT_AXIS_VALUE{ DWRITE_FONT_AXIS_TAG_SLANT, _FontStyleToSlantFixedAxisValue(fontStyle) });
    }

    return axesVector;
}

// Routine Description:
// - Build the needed data for rendering according to the font used
// Arguments:
// - desired - Information specifying the font that is requested
// - actual - Filled with the nearest font actually chosen for drawing
// - dpi - The DPI of the screen

Review comment (bug):
This is almost certainly wrong, anyone have a better idea for how we can distinguish between 'the user set bold/something else' and 'this is the weight value to use when the font is normal'?

Code after:
    {
        fontStretch = DWRITE_FONT_STRETCH_NORMAL;
    }

    return til::at(fontStretchEnumToVal, fontStretch);
}

// Method Description:
// - Converts a DWRITE_FONT_STYLE enum into the corresponding float value to
//   create a DWRITE_FONT_AXIS_VALUE with
// Arguments:
// - fontStyle: the old DWRITE_FONT_STYLE enum to be converted into an axis value
// Return value:
// - The float value corresponding to the passed in fontStyle
float DxFontRenderData::_FontStyleToSlantFixedAxisValue(DWRITE_FONT_STYLE fontStyle) noexcept
{
    // DWRITE_FONT_STYLE_NORMAL (0), DWRITE_FONT_STYLE_OBLIQUE (1), DWRITE_FONT_STYLE_ITALIC (2)
    static constexpr auto fontStyleEnumToVal = std::array{ 0.0f, -20.0f, -12.0f };

    // Both DWRITE_FONT_STYLE_OBLIQUE and DWRITE_FONT_STYLE_ITALIC default to having slant.
    // Though an italic font technically need not have slant (there exist upright ones), the
    // vast majority of italic fonts are also slanted. Ideally the slant comes from the
    // 'slnt' value in the STAT or fvar table, or the post table italic angle.

    if (gsl::narrow_cast<size_t>(fontStyle) > fontStyleEnumToVal.size())
    {
        fontStyle = DWRITE_FONT_STYLE_NORMAL;
    }

    return til::at(fontStyleEnumToVal, fontStyle);
}

// Method Description:
// - Fill any missing axis values that might be known but were unspecified, such as omitting
//   the 'wght' axis tag but specifying the old DWRITE_FONT_WEIGHT enum
// - This function will only be called with a valid IDWriteTextFormat3
//   (on platforms where IDWriteTextFormat3 is supported)
// Arguments:
// - fontWeight: the old DWRITE_FONT_WEIGHT enum to be converted into an axis value
// - fontStretch: the old DWRITE_FONT_STRETCH enum to be converted into an axis value
// - fontStyle: the old DWRITE_FONT_STYLE enum to be converted into an axis value
// - fontSize: the number to convert into an axis value
// - format: the IDWriteTextFormat3 to get the defined axes from
// Return value:
// - The fully formed axes vector
#pragma warning(suppress : 26429) // the analyzer doesn't detect that our FAIL_FAST_IF_NULL macro \
    // checks format for nullness
std::vector<DWRITE_FONT_AXIS_VALUE> DxFontRenderData::GetAxisVector(const DWRITE_FONT_WEIGHT fontWeight,
                                                                    const DWRITE_FONT_STRETCH fontStretch,
                                                                    const DWRITE_FONT_STYLE fontStyle,
                                                                    IDWriteTextFormat3* format)