[reportlab-users] Size of a text object

455 views
Skip to first unread message

Chris Hobbs

unread,
Mar 25, 2014, 10:22:38 PM3/25/14
to reportlab-users
I am fitting various pieces of text onto a page and need to know the size of a text object (so that I know whether it will fit onto a page or not).

It appears from the code that I can use

(x,y) = obj.getStartOfLine()

to find the y position of the last (current) line, but that's no use in finding the maximum value of x.

Adding a obj.getX() after each obj.textLine() doesn't help because the cursor is back on the left margin by then.

So, in summary, I would like to do

canvas.setFont("Helvetica", 8)
instructions = canvas.beginText(mm*20, mm*yPosn)
instructions.textLine("The boy stood on the burning deck")
instructions.textLine("whence all but he had fled")
instructions.textLine(..........

[find the size of this text object]

canvas.drawText(instructions)   # if small enough to fit on page

Is there are way I can do this in reportlab?

Cheers

Chris

Andy Robinson

unread,
Mar 26, 2014, 3:40:49 AM3/26/14
to reportlab-users
A couple of options:
(a) You could use textOut instead of textLine, which leaves getX()
where it is; then when you have queried it, output a new line (e.g.
with canvas.textLine('')).
(b) write a helper function which takes a canvas, a font name, size
and list of lines. Explicitly call stringWidth on each line of text
to work out the longest, then draw all the text as you do above, and
return the max width at the end.

Or for maximum points, you could add some tracking code to textobject
so it knew its width at all times and submit a patch - we'd like that!

- Andy
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users
>



--
Andy Robinson
Managing Director
ReportLab Europe Ltd.
Thornton House, Thornton Road, Wimbledon, London SW19 4NG, UK
Tel +44-20-8405-6420
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users
Reply all
Reply to author
Forward
0 new messages