[reportlab-users] Gradient support (patch)

Peter Johnson johnson.peter at gmail.com
Fri Aug 3 22:30:27 EDT 2012


I've implemented axial (linear) and radial gradient support for
ReportLab (the attached patch is against the latest SVN). The current
function calls are simplified over what PDF supports internally, but
multiple color "stops" and extending the gradient are supported.

Primary user functions:
canvas.linearGradient(x0, y0, x1, y1, colors, positions=None, extend=False)
canvas.radialGradient(x, y, radius, colors, positions=None, extend=False)

These use a new lower-level canvas.shade(shading) function and the
following classes which have been added to pdfdoc.py to add basic
functions and shading support:
PDFExponentialFunction
PDFStitchingFunction
PDFFunctionShading (currently not used)
PDFAxialShading
PDFRadialShading

It should be relatively easy to extend these to add support for the
more advanced PDF shading types.

Example usage:
# Draw a linear gradient from (0, 2*inch) to (5*inch, 3*inch), from
orange to white.
# The gradient will extend past the endpoints (so you probably want a
clip path in place)
canvas.linearGradient(0, 2*inch, 5*inch, 3*inch, [orange, white])

# Draw a radial gradient with a radius of 3 inches.
# The color starts orange and stays orange until 20% of the radius,
then fades to white at 80%, and ends up green at 3 inches from the
center.
# Since extend is false, the gradient stops drawing at the edge of the circle.
canvas.radialGradient(4*inch, 4*inch, 3*inch, [orange, white, green],
[0.2, 0.8, 1.0], extend=False)

I hope other folks find this useful (and it gets integrated into
ReportLab, hopefully with better docs than the above...).

-Peter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gradients.patch
Type: application/octet-stream
Size: 13668 bytes
Desc: not available
Url : <http://two.pairlist.net/pipermail/reportlab-users/attachments/20120803/380268a4/attachment.obj>


More information about the reportlab-users mailing list