[reportlab-users] hard text clipping

rptl at s.rhythm.cx rptl at s.rhythm.cx
Wed Feb 16 09:22:34 EST 2005


On Tue, Feb 15, 2005 at 09:21:51PM -0800, Paul McNett wrote:
> rptl at s.rhythm.cx wrote:
> >Hi. I have another question about something I'm not quite sure how to
> >implement. I'm trying to draw some arbitrary text on the page which will be
> >clipped to a certain rectangle. Nothing fancy - no wrapping, just draw what
> >will fit and forget the rest.
> 
> First, you should wrap your canvas operations in 
> saveState()/restoreState() calls which make it easy to do an action and 
> then revert the state of the canvas back to the way it was before, 
> without having to know anything about how it was before.

I see - doing this seems to do exactly what I want.

> Second, why make a path of lines when you can use a rectangle instead?

I'm new to reportlab - I didn't even realize there was a rect path method.

> Here is my modification to your code which should work:
> 
> def block_text(canvas,text,posx,posy,sizex,sizey):
>     canvas.saveState()
>     p = canvas.beginPath()
>     p.rect(posx,posy,sizex,sizey)
>     canvas.clipPath(p,stroke=0)
>     canvas.drawString(posx,posy,text)
>     canvas.restoreState()

Yes, this does exactly what I want - thanks.

> I hope it helps. Your post helped me, actually: I had no idea there was 
> even a path object, nor how to use it to clip text, which was something 
> on my list of things to learn how to do. I was able to take your code, 
> modify it to my needs, and drop it into my project and verify that it 
> works, so thank you! :)

Glad I could "help" :). Thanks. 



More information about the reportlab-users mailing list