[reportlab-users] Working out height of text
François Heredero - Top Music SA
reportlab-users@reportlab.com
Fri, 11 Apr 2003 13:05:09 +0200
> 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