Discussion:
[reportlab-users] Working out height of text
Ian Sparks
2003-04-10 13:52:13 UTC
Permalink
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).
#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

I'm not sure that the ascent and descent figures I'm getting back are =
correct however, they are 718 and -207 respectively.

Am I even going about this the correct way?

- Ian Sparks.
Jerome Alet
2003-04-10 13:59:19 UTC
Permalink
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).
#self is a "font" class which has a fontname and a size e.g. "Helvetica",8
h = pdfmetrics.TypeFace(self.fontname)
self.textHeight = ((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
Ian Sparks
2003-04-10 19:37:21 UTC
Permalink
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:***@librelogiciel.com]
Sent: Thursday, April 10, 2003 9:59 AM
To: reportlab-***@reportlab.com
Subject: Re: [reportlab-users] Working out height of text
Post by Ian Sparks
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).
Post by Ian Sparks
=20
=20
#self is a "font" class which has a fontname and a size e.g. =
"Helvetica",8
Post by Ian Sparks
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
François Heredero - Top Music SA
2003-04-11 11:05:09 UTC
Permalink
Post by Ian Sparks
Thanks Jerome. I'm doing something wrong though. Here is some test code
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.
You forget something; fix the size of your output text. The right code is
here (all lines with '<--' are modified)

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
canv = canvas.Canvas('test.pdf')
fontname = 'Helvetica'
fontsize = 8

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

height = ascent - descent # <-- descent it's negative

#draw a box 10 wide and "height" tall
canv.rect(50,600 - descent,10,height) # <--to keep the baseline at 600

#Write the string next to the box, it should be the exact same height
canv.setFont(fontname, fontsize) # <-- fix the size of your output text
canv.drawString(62,600,'Testing')

canv.save()

François
Ian Sparks
2003-04-11 13:26:32 UTC
Permalink
Good points Fran=E7ois,

This should probably go in the docs somewhere and/or this function added =
to the library.

- Ian.

-----Original Message-----
From: Fran=E7ois Heredero - Top Music SA [mailto:***@topmusic.ch]
Sent: Friday, April 11, 2003 9:24 AM
To: Ian Sparks
Subject: RE: [reportlab-users] Working out height of text
Ah..thanks Fran=E7ois. I managed to fix my real program but the
test program was puzzling me - thanks for debugging it for me!
Your welcome :-)

Another thing, face.descent returns a negative float with integrated =
fonts,
and positive float with ttf fonts... so, you can calculate the height =
height =3D ascent + abs(descent)
face =3D pdfmetrics.getFont(fontName).face
size =3D fontSize / 1000.0
ascent, descent =3D face.ascent * size, abs(face.descent * size)
return ascent+descent, (ascent, descent)
Fran=E7ois

Continue reading on narkive:
Loading...