[reportlab-users] Size of a text object

Andy Robinson andy at reportlab.com
Wed Mar 26 03:40:49 EDT 2014


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


On 26 March 2014 02:22, Chris Hobbs <cwlh at farmhall.ca> wrote:

> 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

>

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at 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


More information about the reportlab-users mailing list