[reportlab-users] Implementation of drawImage anchoring

Yoann Roman yroman-reportlab at altalang.com
Thu Aug 28 13:40:56 EDT 2008


Robin Becker wrote...

> >> 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:

> >>

> >> [snip]

> >>

> >> 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.

> >

> > Anyone have any ideas on this? If the implementation is correct, what's

> > the proper way to get the behavior described in the doc (i.e., centering

> > an image within a box anchored at x, y)?

>

> So your original box corners are

>

> (x,y), (x+width,y+width)

>

> you wish to place the centre of the image at the centre of the box so use

> anchor 'c' and then use x+width*0.5, y+height*0.5; that should place the

> centre of the image on the centre of the original box.

>

> If the image or object doesn't support anchoring then you'll need to know

> the size of the image or box in order to display it properly using some

> other anchor point. It seems that drawImage does so you should be OK here.


So centering the image was a poor example. What I was looking for was a
generic way to anchor an image anywhere in a *virtual* box with a given
(x, y) and (width, height). As I understood it, that's what drawImage's
anchor was for.

As an alternate example, if I want to anchor my (100, 50) image in the
northeast corner of a (200, 200) box, I'd expect to do this:

canvas = Canvas('canvas.pdf', pagesize=LETTER)
canvas.rect(0, 0, 200, 200, fill=1) # for illustration only
size = canvas.drawImage('test.png', 0, 0, 200, 200, \
preserveAspectRatio=1, anchor='ne')
canvas.save()

In this case, the image ends up off the page with *its* northeast corner
anchored at (0, 0). To get it right, the (x, y) for drawImage would have
to be (200, 250) or (width, height + imgHeight).

I could wrap all my drawImage calls to translate the (x, y) coordinates
to the appropriate values for each anchor, but I'd need to know the
dimensions of the image, which I don't get back until *after* drawImage.
It feels like I'd need to duplicate a lot of drawImage's image loading code
to do this translation.

Am I just missing something?

Thanks,

--
Yoann Roman



More information about the reportlab-users mailing list