[reportlab-users] Problem with row&column spanning code not calculating table heights

Oliver Bleutgen reportlab-users@reportlab.com
Thu, 16 Oct 2003 13:04:18 +0200


Hmm, the first try didn't make it, resending, sorry if this appears twice.

Robin Becker wrote:
> In article <PGECLPOBGNBNKHNAGIJHIENEFJAA.andy@reportlab.com>, Andy
> Robinson <andy@reportlab.com> writes
>
>
>
> ....
> gurk. I believe it's more complicated
>
> [example snipped]
>
> Of course this is about the simplest possible row spanning problem. In a
> more general problem with multiple overlapping spans we can still solve
> the LP formulation to get the required minimum required overall heights.
> I'm not sure if there's a simple general formulation that is easy to
> solve.

Interesting riddle. I may be way off, but I think to describe that as LP
overstates the problem.
After all, the space of possible solutions is finite in this case, isn't it?

What about the following snipped (which is actually runnable):

nrows=3 # number of rows
ncols=3 # number of cols

# cell_row_heights is a list of dicts, indexed by column number.
# The dict at index N has a key M, if there is a cell in column N which ends
# at row M, and the corresponding value is the height of that cell.
# Example:
# cell_row_heights = [{0:cell_11.height,1:cell_12.height,2:cell_13.height},
#                    {0:cell_2122.height,2:cell_23.height},
#                    {1:cell_3132.height,2:cell33.height}]

cell_row_heights = [{0:1,1:3,2:1},{0:1,2:2},{0:2,2:1}]

# max_row_heights holds the absolute height of the rows,
# i.e. max_row_heights[-1] == height of table

max_row_heights = {}

col_heights_accumulator = [0,]*len(cell_row_heights) # accumulates the needed absolute height per col
for row in range(0,nrows):
   for col in range(0,ncols):
       col_heights_accumulator[col] = cell_row_heights[col].get(row,0)+col_heights_accumulator[col]
       max_row_heights[row] = max(max_row_heights.get(row,0),col_heights_accumulator[col])


cheers,
oliver