[reportlab-users] Table cell contents misaligned (RLTK > 2.0)

Robin Becker robin at reportlab.com
Thu Apr 2 13:13:41 EDT 2009


The layout algorithm in tables claims to know about strings, but it cannot know
about flowables/paragraphs etc etc in general. Therefore a string which has a
reference point called the baseline can be adjusted and I believe that's what's
intended here.

Unfortunately, the code for paragraph was updated to use the actual
Ascent/Descent figures.

If you run this script

############################
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.tables import Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.lib.colors import blue
styleSheet = getSampleStyleSheet()
bt=styleSheet['BodyText']
fontName=bt.fontName
fontSize=bt.fontSize
leading=bt.leading

t=Table(
[['Up and Downy', Paragraph('Up and Downy',bt)]],
2*[None],[None],
TableStyle([
('FONT', (0,0), (-1,-1), fontName,fontSize,leading),
('TOPPADDING', (0,0), (-1,-1), 0),
('LEFTPADDING', (0,0), (-1,-1), 0),
('RIGHTPADDING', (0,0), (-1,-1), 0),
('BOTTOMPADDING', (0,0), (-1,-1), 0),
('VALIGN', (0,0), (-1,-1), 'TOP'),
('GRID', (0,0), (-1,-1), 0.25, blue),
]),
)
SimpleDocTemplate('tth.pdf').build([t])
############################

then the observed difference in the positions is entirely explained by the
difference between using fontsize and ascent in the statement at line 1414 in
tables.py (method _drawCell).


> elif valign=='TOP':

> from reportlab.pdfbase.pdfmetrics import stringWidth, getFont, getAscentDescent

> ascent, descent = getAscentDescent(cellstyle.fontname,fontsize)

> y = rowpos + rowheight - cellstyle.topPadding - ascent #fontsize


if you try changing the code to the above then the two layouts match. Here
ascent,descent=6.83,-2.17 not 10 and -2 so our old approximation is wrong.

Arguably the ascent is more accurate as it brings the top of the letters to the
top of the cell in this case which is where TOP should make it. The real problem
is that we've assumed 1.2*fontSize=leading and arguable it should be smaller.
--
Robin Becker


More information about the reportlab-users mailing list