[reportlab-users] Implementation of drawImage anchoring

Yoann Roman yroman-reportlab at altalang.com
Fri Aug 22 14:18:07 EDT 2008


I've been reading (and re-reading) the doc for drawImage in Canvas, and I'm
pretty sure the implementation doesn't match what's described. To quote:

New post version 2.0: drawImage can center the image in a box you
provide.
This saves developers from doing lots of math in their code. You can
define a box with x,y,width and height. If 'preserveAspectRatio'
is set, the image will be scaled up or down as needed in proportion to
fit entirely within the box. Unless the aspect ratio perfectly matches
the box, this will leave some space. So, the boxAnchor property defines
how the image should be anchored in the box, using imaginary points of
the compass. 'sw' for SouthWest is the default, but 'c' for center
will center it in the given box.

What happens if I use 'c', though, is the image is centered on (x, y) not
within the box defined by x, y, width, and height (visually displayed here
with rect for testing):

canvas = Canvas('canvas.pdf', pagesize=LETTER)
canvas.rect(x, y, width, height, fill=1)
size = canvas.drawImage('test.png', x, y, width, height, \
preserveAspectRatio=1, anchor='c')
canvas.save()

I've got a patch for lib.boxstuff to get it to do what's stated in the doc,
but I wanted to verify whether the doc or the implementation was incorrect.
In my case, I needed what was stated in the doc.

Thanks,

--
Yoann Roman



More information about the reportlab-users mailing list