Repository: microsoft/PowerToys
File: src/modules/MeasureTool/MeasureToolCore/PerGlyphOpacityTextRender.cpp (C++)

Code before:
            glyphRun->isSideways,
            glyphRun->bidiLevel % 2,
            pSink.get());
    }

    if (pSink)
    {
        hr = pSink->Close();
    }

    // Initialize a matrix to translate the origin of the glyph run.
    D2D1::Matrix3x2F const matrix = D2D1::Matrix3x2F(
        1.0f, 0.0f, 0.0f, 1.0f, baselineOriginX, baselineOriginY);

    // Create the transformed geometry
    wil::com_ptr<ID2D1TransformedGeometry> pTransformedGeometry;
    if (SUCCEEDED(hr))
    {
        hr = _pD2DFactory->CreateTransformedGeometry(pathGeometry.get(), &matrix, &pTransformedGeometry);
    }

    float prevOpacity = _baseBrush->GetOpacity();
    auto opacityEffect = clientDrawingEffect.try_query<IDrawingEffect>();

    if (opacityEffect)
        _baseBrush->SetOpacity(dynamic_cast<OpacityEffect*>(opacityEffect.get())->alpha);

    if (SUCCEEDED(hr))
    {
        _rt->DrawGeometry(pTransformedGeometry.get(), _baseBrush);
        _rt->FillGeometry(pTransformedGeometry.get(), _baseBrush);
        _baseBrush->SetOpacity(prevOpacity);
    }

    return hr;
}

HRESULT __stdcall PerGlyphOpacityTextRender::DrawUnderline(void* /*clientDrawingContext*/,
                                                           FLOAT /*baselineOriginX*/,
                                                           FLOAT /*baselineOriginY*/,
                                                           _In_ const DWRITE_UNDERLINE* /*underline*/,
                                                           IUnknown* /*clientDrawingEffect*/) noexcept
{
    return E_NOTIMPL;
}

HRESULT __stdcall PerGlyphOpacityTextRender::DrawStrikethrough(void* /*clientDrawingContext*/,
                                                               FLOAT /*baselineOriginX*/,
                                                               FLOAT /*baselineOriginY*/,
                                                               _In_ const DWRITE_STRIKETHROUGH* /*strikethrough*/,
                                                               IUnknown* /*clientDrawingEffect*/) noexcept

Review comment (bug):
it's impossible to fail here, since the Render itself is made to operate on such types only (`OpacityEffect`). I guess we could also change it to perform `dynamic_cast` and then `assert` it's not `nullptr`, since generally dynamic_cast can fail.

Code after:
            glyphRun->isSideways,
            glyphRun->bidiLevel % 2,
            pSink.get());
    }

    if (pSink)
    {
        hr = pSink->Close();
    }

    // Initialize a matrix to translate the origin of the glyph run.
    D2D1::Matrix3x2F const matrix = D2D1::Matrix3x2F(
        1.0f, 0.0f, 0.0f, 1.0f, baselineOriginX, baselineOriginY);

    // Create the transformed geometry
    wil::com_ptr<ID2D1TransformedGeometry> pTransformedGeometry;
    if (SUCCEEDED(hr))
    {
        hr = _pD2DFactory->CreateTransformedGeometry(pathGeometry.get(), &matrix, &pTransformedGeometry);
    }

    float prevOpacity = _baseBrush->GetOpacity();
    auto opacityEffect = clientDrawingEffect.try_query<IDrawingEffect>();

    if (opacityEffect)
    {
        const auto temp_opacity = dynamic_cast<OpacityEffect*>(opacityEffect.get());
        assert(nullptr != temp_opacity);
        _baseBrush->SetOpacity(temp_opacity->alpha);
    }
    

    if (SUCCEEDED(hr))
    {
        _rt->DrawGeometry(pTransformedGeometry.get(), _baseBrush);
        _rt->FillGeometry(pTransformedGeometry.get(), _baseBrush);
        _baseBrush->SetOpacity(prevOpacity);
    }

    return hr;
}

HRESULT __stdcall PerGlyphOpacityTextRender::DrawUnderline(void* /*clientDrawingContext*/,
                                                           FLOAT /*baselineOriginX*/,
                                                           FLOAT /*baselineOriginY*/,
                                                           _In_ const DWRITE_UNDERLINE* /*underline*/,
                                                           IUnknown* /*clientDrawingEffect*/) noexcept
{
    return E_NOTIMPL;
}