Repository: microsoft/terminal
File: src/renderer/atlas/BackendD2D.cpp (C++)

Code before:
}

void BackendD2D::_drawGridlineRow(const RenderingPayload& p, const ShapedRow* row, u16 y)
{
    const auto cellWidth = static_cast<f32>(p.s->font->cellSize.x);
    const auto cellHeight = static_cast<f32>(p.s->font->cellSize.y);
    const auto rowTop = cellHeight * y;
    const auto rowBottom = rowTop + cellHeight;
    const auto cellCenter = row->lineRendition == LineRendition::DoubleHeightTop ? rowBottom : rowTop;
    const auto scaleHorizontal = row->lineRendition != LineRendition::SingleWidth ? 0.5f : 1.0f;
    const auto scaledCellWidth = cellWidth * scaleHorizontal;
    const auto horizontalShift = static_cast<u8>(row->lineRendition != LineRendition::SingleWidth);

    const auto appendVerticalLines = [&](const GridLineRange& r, FontDecorationPosition pos) {
        const auto from = r.from * scaledCellWidth;
        const auto to = r.to * scaledCellWidth;
        auto x = from + pos.position;

        const auto colors = &p.foregroundBitmap[p.colorBitmapRowStride * y];

        D2D1_POINT_2F point0{ 0, cellCenter };
        D2D1_POINT_2F point1{ 0, cellCenter + cellHeight };
        const f32 w = pos.height;
        const f32 hw = w * 0.5f;

        auto c = r.from;
        for (; x < to; x += cellWidth, c += 1 << horizontalShift)
        {
            const auto brush = _brushWithColor(colors[c]);
            const auto centerX = x + hw;
            point0.x = centerX;
            point1.x = centerX;
            _renderTarget->DrawLine(point0, point1, brush, w, nullptr);
        }
    };
    const auto appendHorizontalLine = [&](const GridLineRange& r, FontDecorationPosition pos, ID2D1StrokeStyle* strokeStyle, const std::span<const u32>& colorBitmap) {
        const auto colors = &colorBitmap[p.colorBitmapRowStride * y];

        const f32 w = pos.height;
        const f32 centerY = cellCenter + pos.position + w * 0.5f;

        for (auto from = r.from; from < r.to;)
        {
            const auto start = colors[from];
            u16 run = 1u;
            for (; colors[from + run] == start && run < (r.to - from); ++run)
                ;

            const auto brush = _brushWithColor(start);
            const D2D1_POINT_2F point0{ from * scaledCellWidth, centerY };
            const D2D1_POINT_2F point1{ (from + run) * scaledCellWidth, centerY };

Review comment (suggestion):
Stylistically, this should probably go up to the `auto x =` assignment. (`const auto colors` should probably go the first line.)

Code after:
}

void BackendD2D::_drawGridlineRow(const RenderingPayload& p, const ShapedRow* row, u16 y)
{
    const auto cellWidth = static_cast<f32>(p.s->font->cellSize.x);
    const auto cellHeight = static_cast<f32>(p.s->font->cellSize.y);
    const auto rowTop = cellHeight * y;
    const auto rowBottom = rowTop + cellHeight;
    const auto cellCenter = row->lineRendition == LineRendition::DoubleHeightTop ? rowBottom : rowTop;
    const auto scaleHorizontal = row->lineRendition != LineRendition::SingleWidth ? 0.5f : 1.0f;
    const auto scaledCellWidth = cellWidth * scaleHorizontal;
    const auto horizontalShift = static_cast<u8>(row->lineRendition != LineRendition::SingleWidth);

    const auto appendVerticalLines = [&](const GridLineRange& r, FontDecorationPosition pos) {
        // Vertical lines are always gridlines, and gridlines are always rendered in the foreground color
        const auto colors = &p.foregroundBitmap[p.colorBitmapRowStride * y];
        const auto from = r.from * scaledCellWidth;
        const auto to = r.to * scaledCellWidth;
        auto x = from + pos.position;
        auto c = r.from;

        D2D1_POINT_2F point0{ 0, cellCenter };
        D2D1_POINT_2F point1{ 0, cellCenter + cellHeight };
        const f32 w = pos.height;
        const f32 hw = w * 0.5f;

        for (; x < to; x += cellWidth, c += 1 << horizontalShift)
        {
            const auto brush = _brushWithColor(colors[c]);
            const auto centerX = x + hw;
            point0.x = centerX;
            point1.x = centerX;
            _renderTarget->DrawLine(point0, point1, brush, w, nullptr);
        }
    };
    const auto appendHorizontalLine = [&](const GridLineRange& r, FontDecorationPosition pos, ID2D1StrokeStyle* strokeStyle, const std::span<const u32>& colorBitmap) {
        const auto colors = &colorBitmap[p.colorBitmapRowStride * y];

        const f32 w = pos.height;
        const f32 centerY = cellCenter + pos.position + w * 0.5f;

        for (auto from = r.from; from < r.to;)
        {
            const auto start = colors[from];
            u16 run = 1u;
            for (; colors[from + run] == start && run < (r.to - from); ++run)
                ;

            const auto brush = _brushWithColor(start);
            const D2D1_POINT_2F point0{ from * scaledCellWidth, centerY };
            const D2D1_POINT_2F point1{ (from + run) * scaledCellWidth, centerY };