[reportlab-users] Bounded text (truncated)

Robin Becker robin at reportlab.com
Thu Jan 4 13:43:24 EST 2007


Tim Roberts wrote:

> Michael Hipp wrote:

>> Hello,

>>

>> I'm fairly new to ReportLab. Thanks for a great tool.

>>

>> I'm trying to find a way to print "bounded" text. Meaning I want the "field"

>> it is printed in to be a certain specified size and never exceed that size. If

>> a text value is printed that would go beyond that size, then the text should

>> be truncated.

>>

>> Is there some simple way to accomplish this?

>>

>

> This question started me to thinking -- always a dangerous thing.

>

> Postscript fully supports path clipping. Does the PDF subset support

> that as well? One way to implement what he describes would be to

> establish a clipping rectangle, then draw the text, then free the

> clipping rectangle.

>

That's another way to do it. And there's an example of just that in the output
of test_pdfgen_general.py where a clipping path derived from the word Python is
used to mask some underlying stuff. The code looks like this c is the canvas

#first the outline
c.saveState()
t = c.beginText(inch, 3.0 * inch)
t.setFont('Helvetica-BoldOblique',108)
t.setTextRenderMode(5) #stroke and add to path
t.textLine('Python!')
t.setTextRenderMode(0)
c.drawText(t) #this will make a clipping mask

#now some small stuff which wil be drawn into the current clip mask
t = c.beginText(inch, 4 * inch)
t.setFont('Times-Roman',6)
t.textLines((('spam ' * 40) + '\n') * 15)
c.drawText(t)

#now reset canvas to get rid of the clipping mask
c.restoreState()

Clearly we could have used a more normal clipping path setup eg

p = c.beginPath()
#make a chessboard effect, 1 cm squares
for i in range(14):
x0 = (3 + i) * cm
for j in range(7):
y0 = (16 + j) * cm
p.rect(x0, y0, 0.85*cm, 0.85*cm)
c.addLiteral('%Begin clip path')
c.clipPath(p)
c.addLiteral('%End clip path')



--
Robin Becker


More information about the reportlab-users mailing list