[reportlab-users] Working out height of text

Ian Sparks reportlab-users@reportlab.com
Thu, 10 Apr 2003 15:37:21 -0400


Thanks Jerome. I'm doing something wrong though. Here is some test code =
:

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics


canv =3D canvas.Canvas('sizetest.pdf')

fontname =3D 'Helvetica'
fontsize =3D 8

face =3D pdfmetrics.getFont(fontname).face
ascent =3D (face.ascent * fontsize) / 1000.0
descent =3D (face.descent * fontsize) / 1000.0

height =3D ascent + descent

#draw a box 10 wide and "height" tall
canv.rect(50,600,10,height)

#Write the string next to the box, it should be the exact same height
canv.drawString(62,600 - height,'Testing')

canv.save()       =20

This produces a box which is supposed to be the same height as the =
string - it isn't. The string is about 2x the height of the box.

Any light anyone can shed would be helpful.

- Ian.

-----Original Message-----
From: Jerome Alet [mailto:alet@librelogiciel.com]
Sent: Thursday, April 10, 2003 9:59 AM
To: reportlab-users@reportlab.com
Subject: Re: [reportlab-users] Working out height of text


On Thu, Apr 10, 2003 at 09:52:13AM -0400, Ian Sparks wrote:
> I need to work out the height of a line of text to find out if it will =
fit the box I'm writing it to (using raw canvas commands not Platypus).
>=20
> >From somewhere I have the following function :
>=20
> #self is a "font" class which has a fontname and a size e.g. =
"Helvetica",8
> h =3D pdfmetrics.TypeFace(self.fontname)
> self.textHeight =3D ((h.ascent - h.descent) * self.size) / 1000

See http://www.aful.org/wws/arc/python/2003-04/msg00016.html

it's in french tough

bye,

Jerome Alet
_______________________________________________
reportlab-users mailing list
reportlab-users@reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users