Vertical table alignment change in ReportLab 3.5.44

Marius Gedminas <[email protected]> Thu, 12 Dec 2024 13:48:33 +0200
Newsgroups gmane.comp.python.reportlab.user
Message-ID <fnq6hkav7zkfgsomj7qwfs5oriqtxpr2tfsh7agb6el2ko2nlc@3ted4n2iynd2>
--bjovlrlgbrekdmve
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hello!

While updating dependencies of old application that was still using
ReportLab 3.5.23 I noticed a strange change in behavior with vertical
table alignment.  I've narrowed it down to a change between ReportLab
3.5.42 and 3.5.44 with the following code:

  https://gist.github.com/mgedmin/079923e0c115ce88db8a56496a9a3b92

The leftmost table cell that contains the 'A' is vertically aligned at
the top, like the table style requests in 3.5.42, but suddenly gets
aligned at the bottom with 3.5.44 and newer versions.  (You can see
screenshots of the two versions of the PDF in the comments below the
gist itself.)

Does anyone know what's happening there?  Have I found a bug?

I'm attaching the code for completeness, in case accessing the gist is
inconvenient.

(BTW I've been away for a long time.  Do you perhaps have a Git
repository available somewhere?)

Regards,
Marius Gedminas
-- 
<mcdonc> i'm going to start charging by the facepalm

--bjovlrlgbrekdmve
Content-Type: text/x-python; charset=us-ascii
Content-Disposition: attachment; filename="reportlab_table_bug.py"

#!/usr/bin/python
# I'm testing this on Python 3.8
# ReportLab 3.5.42 vertically aligns the first column at the top as requested
# Reportlab 3.5.43 does not exist on PyPI
# ReportLab 3.5.44 aligns it at the bottom
# /// script
# dependencies = ["ReportLab == 3.5.44"]
# ///

from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY, TA_LEFT
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import cm
from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.tables import Table, TableStyle


report = []

g_centre_col = 2
gap_colour = colors.HexColor('0xc2cbe1')

LIST_STYLE = TableStyle([
    # line, (col1, row1), (col2, row2), weight, colour[, extra params]
    ('FONT', (0, 1), (-1, -1), 'Times-Roman'),
    ('LINEABOVE', (0, 0), (-1, 0), 2, gap_colour),
    ('LINEABOVE', (0, 1), (-1, -1), 0.25, colors.grey),
    ('LINEBELOW', (0, -1), (-1, -1), 2, gap_colour),
    # property, (col1, row1), (col2, row2), value
    ('ALIGN', (g_centre_col, 0), (-1, -1), 'CENTRE'),
    ('VALIGN', (0, 0), (-1, -1), 'TOP'),     # <-------- aligment request
    ('LEFTPADDING', (0, 0), (-1, 0), 0),
    ('RIGHTPADDING', (0, 0), (-1, 0), 0),
    ('BACKGROUND', (g_centre_col, 0), (g_centre_col, -1), gap_colour),
])

normal = ParagraphStyle('normal')
normal.firstLineIndent = 0
normal.spaceBefore = 6
normal.fontSize = 11.0
normal.alignment = TA_JUSTIFY

black_text = ParagraphStyle('black_text', parent=normal)
black_text.textColor = colors.black
black_text.alignment = TA_CENTER

heading_text = ParagraphStyle('heading_text', parent=black_text)
heading_text.fontSize = 8.0

cell_text = ParagraphStyle('heading_text', parent=normal)
cell_text.fontSize = 10.0
cell_text.alignment = TA_LEFT

rows = [
    [
        '',
        Paragraph('heading', heading_text),
        Paragraph('heading', heading_text),
        Paragraph('heading', heading_text),
        Paragraph('heading', heading_text),
        Paragraph('heading', heading_text),
        Paragraph('heading', heading_text),
        '',
        Paragraph('heading', heading_text),
    ],
    [
        Paragraph('<font color="0x422110">A</font>', cell_text),
        Paragraph('Sphinx of black quartz, judge my vow', cell_text),
        Paragraph('4.0', black_text),
        Paragraph('+1.0', black_text),
        Paragraph('n/a', black_text),
        Paragraph('n/a', black_text),
        Paragraph('n/a', black_text),
        '',
        Paragraph('+1.0', black_text),
    ],
]
widths = [16, A4[0] - 6*cm - 1.5*cm * 6 - 16] + [1.5*cm] * 5 + [0.4*cm, 1.5*cm]
report.append(Table(rows, widths, style=LIST_STYLE, repeatRows=1))

doc = SimpleDocTemplate('test.pdf')
doc.build(report)

--bjovlrlgbrekdmve
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline