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

Code before:
        .h = p.s->font->cellSize.y,
    };

    const auto lineRendition = static_cast<LineRendition>(fontFaceEntry.lineRendition);
    auto baseline = p.s->font->baseline;

    if (lineRendition != LineRendition::SingleWidth)
    {
        const auto heightShift = static_cast<u8>(lineRendition >= LineRendition::DoubleHeightTop);
        rect.w <<= 1;
        rect.h <<= heightShift;
        baseline <<= heightShift;
    }

    if (!stbrp_pack_rects(&_rectPacker, &rect, 1))
    {
        _drawGlyphPrepareRetry(p);
        return false;
    }

    if (!_softFontBitmap)
    {
        // Allocating such a tiny texture is very wasteful (min. texture size on GPUs
        // right now is 64kB), but this is a seldomly used feature so it's fine...
        const D2D1_SIZE_U size{
            static_cast<UINT32>(p.s->font->softFontCellSize.width),
            static_cast<UINT32>(p.s->font->softFontCellSize.height),
        };
        const D2D1_BITMAP_PROPERTIES1 bitmapProperties{
            .pixelFormat = { DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED },
            .dpiX = static_cast<f32>(p.s->font->dpi),
            .dpiY = static_cast<f32>(p.s->font->dpi),
        };
        THROW_IF_FAILED(_d2dRenderTarget->CreateBitmap(size, nullptr, 0, &bitmapProperties, _softFontBitmap.addressof()));
    }

    {
        const auto width = static_cast<size_t>(p.s->font->softFontCellSize.width);
        const auto height = static_cast<size_t>(p.s->font->softFontCellSize.height);

        auto bitmapData = Buffer<u32>{ width * height };
        const auto glyphIndex = glyphEntry.glyphIndex - 0xEF20u;
        auto src = p.s->font->softFontPattern.begin() + height * glyphIndex;
        auto dst = bitmapData.begin();

        for (size_t y = 0; y < height; y++)
        {
            auto srcBits = *src++;
            for (size_t x = 0; x < width; x++)
            {
                const auto srcBitIsSet = (srcBits & 0x8000) != 0;

Review comment (suggestion):
This code moved down from line 1302, because calculating the `PushAxisAlignedClip` rectangle is much simpler that way (the transform is still in absolute coordinates, instead of being relative to the baseline origin of the glyph).

Code after:

    _d2dBeginDrawing();

    if (isBoxGlyph)
    {
        const D2D1_RECT_F clipRect{
            static_cast<f32>(rect.x) / transform.m11,
            static_cast<f32>(rect.y) / transform.m22,
            static_cast<f32>(rect.x + rect.w) / transform.m11,
            static_cast<f32>(rect.y + rect.h) / transform.m22,
        };
        _d2dRenderTarget4->PushAxisAlignedClip(&clipRect, D2D1_ANTIALIAS_MODE_ALIASED);
    }
    const auto boxGlyphCleanup = wil::scope_exit([&]() {
        if (isBoxGlyph)
        {
            _d2dRenderTarget4->PopAxisAlignedClip();
        }
    });

    if (needsTransform)
    {
        transform.dx = (1.0f - transform.m11) * baselineOrigin.x;
        transform.dy = (1.0f - transform.m22) * baselineOrigin.y;
        _d2dRenderTarget->SetTransform(&transform);
    }

    if (!isColorGlyph)
    {
        _d2dRenderTarget->DrawGlyphRun(baselineOrigin, &glyphRun, _brush.get(), DWRITE_MEASURING_MODE_NATURAL);
    }
    else
    {
        const auto enumerator = TranslateColorGlyphRun(p.dwriteFactory4.get(), baselineOrigin, &glyphRun);
        while (ColorGlyphRunMoveNext(enumerator.get()))
        {
            const auto colorGlyphRun = ColorGlyphRunGetCurrentRun(enumerator.get());
            ColorGlyphRunDraw(_d2dRenderTarget4.get(), _emojiBrush.get(), _brush.get(), colorGlyphRun);
        }
    }

    // Ligatures are drawn with strict cell-wise foreground color, while other text allows colors to overhang
    // their cells. This makes sure that italics and such retain their color and don't look "cut off".
    //
    // The former condition makes sure to exclude diacritics and such from being considered a ligature,
    // while the latter condition-pair makes sure to exclude regular BMP wide glyphs that overlap a little.
    const auto horizontalScale = lineRendition != LineRendition::SingleWidth ? 2 : 1;
    const auto triggerLeft = _ligatureOverhangTriggerLeft * horizontalScale;
    const auto triggerRight = _ligatureOverhangTriggerRight * horizontalScale;
    const auto overlapSplit = rect.w >= p.s->font->cellSize.x && (bl <= triggerLeft || br >= triggerRight);