SVG gradient drawing bug


#1

I somehow missed this until now, maybe because I mostly develop on Windows. But I found a bug drawing SVG files with gradients that only appears to affect Mac (I haven’t tried mobile or Linux, and I’m not developing for those).

The bug occurs in my program, but also in the Juce demo app. I just grabbed the latest from GitHub, and the problem is still there. Take a look at the screenshot below. I think the problems are obvious - gradients get cut off and go transparent for some reason.

I could provide my SVG file, but it happens in the Juce demo so everybody already has a set of files that don’t draw correctly on Mac.

Any ideas why gradients would draw correctly on Windows and not Mac? I don’t know how the drawing code works when it hits platform-specific land.


#2

gradients should be rendered identically on all platforms!

If you can give me some self-contained code that reproduces the problem, I’ll debug it and see what’s going on…


#3

I don’t think you need some self-contained code: as he pointed out, the problem is already there in the JUCE Demo application that is delivered with the latest develop tip, I can confirm this as well.

Just build it and see it yourself (I am currently on Mac OS X 10.10.5, it if makes any difference)


#4

Ah… you’re right. That’s strange, it always used to work, I’ll investigate!


#5

Oh yeah, I didn’t specify: OS X 10.11.1 for me, if it matters.

Also, I don’t have a solution, but I may have a clue: I have an OpenGL context where I am able to draw these SVG graphics with correct gradients. The way I do it is to call createOpenGLGraphicsContext, and then create a Graphics object from this. When I do that, the gradients display correctly.

I know I’m not really supposed to use the LowLevelGraphicsContext object, but in one instance I need to draw into an OpenGL view, and it works. But in all other cases (in non-OpenGL components), with normal use of a regular Graphics object I’m getting this gradient display bug.

I don’t know if that gives you any clues, but I thought I should mention it…


#6

Thanks - I’ve fixed this on the develop branch. Was pretty obscure, and not 100% sure if it’s something Apple changed or just something that changed in the SVG parser


#7

Thanks for the quick action, but there’s still a problem. If you run the JuceDemo in debug mode, and you go to either the toolbar demo or the 2DRendering/SVG demo, an assertion fails in juce_mac_CoreGraphicsContext.mm, line 713:

jassert (i == 0 || locations[i] != 0);

If I comment out that assertion, the demo runs correctly and the SVGs look correct (to my eye). Maybe instead of asserting, the code needs to prepare the gradient differently for CoreGraphics?

Two color stops at position 0 seems undefined to me. Maybe Juce can just skip one of them instead of asserting?


#8

Thanks, will investigate and sort that out!

Two colour stops at 0 is actually a bit of an edge-case because their order is ambiguous, and I’d rather avoid the situation occurring than hope that all the different renderers handle them in the same way.