DropShadow assertion using DropShadow::drawForImage()

Hi,

In my button’s resize method, I’m using an Image Object, Graphics object and DropShadow in order to render some text that looks like its “glowing”

Draw base text → Blur that text → draw text above → Gives and “led” effect

This is so when it comes time to paint the button, I simply draw this image, instead of creating GlyphArrangement, turning into a Path, and applying the DropShadow on the path in paint().

void resized() override {
    //blurred text is a member variable
    blurredText = juce::Image(juce::Image::PixelFormat::ARGB, getWidth(), getHeight(), true);
    auto g = juce::Graphics(blurredText);

    //draw text to be blurred
    g.setColour(colours::accent);
    g.setFont(fonts::getPlainFont());
    g.drawFittedText(getButtonText(), getLocalBounds(), juce::Justification::centred, 1);
    
    auto ds = juce::DropShadow(colours::accent, 3.f, {0, 0});
    ds.drawForImage(g, blurredText);
    
    //draw text
    g.setColour(colours::led);
    g.setFont(fonts::getPlainFont());
    g.drawFittedText(getButtonText(), getLocalBounds(), juce::Justification::centred, 1);
}

However when ds.drawForImage() gets called, I end up hitting an assertion in juce_Direct2DImage_windows.cpp:

void Direct2DPixelData::initialiseBitmapData (Image::BitmapData& bitmap,
                                              int x,
                                              int y,
                                              Image::BitmapData::ReadWriteMode mode)
{
    // If this is hit, there's already another BitmapData or Graphics context active on this
    // image. Only one BitmapData or Graphics context may be active on an Image at a time.
    jassert (state != State::drawing);

I’m not really sure what I’m doing wrong. I’ve tried using the DropShadowEffect class instead (not sure whats the difference). I’m not sure what other BitmapData or Graphics context could be active. This only happens on windows. I’m using JUCE 8.0.10

I think the problem is likely that g is rendering into blurredText, but the line ds.drawForImage (g, blurredText) is trying to read from that image at the same time as writing into it. You should use an extra intermediate/temporary image to avoid reading and writing into the same image during the same operation.