[cairo] Radii of radial gradients

Bill Spitzak spitzak at d2.com
Fri Mar 2 04:13:51 PST 2007

Carl Worth wrote:

> Another thing that PDF provides that doesn't have explicit support in
> cairo's radial gradients is gradient that extend from only one end or
> the other. For that, what I'd like to suggest is simply using
> EXTEND_REPEAT, (which affects both ends), and then using a transparent
> color stop the end where the extension is undesired.

Another possiblity is that EXTEND_NONE only removes one end of the 
gradient. The default setup should be such that drawing a normal 
(non-cone) gradient results in the "outside" being black, but the 
"inside" circle still being filled in. I feel that the filled circle is 
more useful than a donut, so it should be the more easily-achieved 
result. Also this clip is topologially the same as what EXTEND_NONE does 
with images and linear gradients.

I'm guessing you have added an argument for whether the front or back of 
the cone is drawn. For the non-cone this could reverse it so that 
EXTEND_NONE means a transparent hole is in the middle but the outside 
extends to infinity.

For your first cone drawing, EXTEND_NONE would clip it with the union of 
the larger circle and a triangle so you get a cone with a sharp point 
even if the smaller circle is non-zero.

For the second cone drawing, EXTEND_NONE would clip it with the same 
"hole" as before, so there is just a transparent circle, you still see 
the infinite linear gradient. This is necessary or there will still be 
the problems with discontinuities as inner circle moves outside the 
outer one. Also for my PDF solution (below) it avoids double multiplying 
of the edge pixels which would leak some back-side pixels where they 
should not be seen.

> For that to work, we'll only need one little addition to the current
> implementation. Right now, it only uses one real root of the quadratic
> equation, even if there is a second one available---it simply chooses
> the root that results in a larger value for the gradient's parametric
> control, (this gives the "last circle drawn wins" effect that PDF
> defines).
> To refine this, we'll simply need to check the second root if the
> first root results in a non-opaque color value and simply blend the
> two colors. This should give an interesting effect when transparency
> is used for interior stops, (an effect not expressible in PDF nor
> SVG), but should also allow for emulating the PDF single-sided
> extended gradients with a transparent stop on one end or the other.

I don't like this solution because it means you cannot use transparent 
parts in the more useful one-sided-cones you are producing now. Anybody 
trying to draw a real transparent cone is probably not going to use 
gradients to achieve this, as it is unlikely that they only want to draw 
3D cones but want to draw other geometry as well.

What could be done instead is that the PDF is produced by merging two 
gradients into a surface and using that as a source. This would only 
have to be done when a cone is being drawn and the inner circle is 
transparent, which is probably rare in real documents.

More information about the cairo mailing list