From 4f788af35be6c5deca53c714e8932601964c305e Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 25 Jan 2023 10:31:15 +0100 Subject: [PATCH] [WIP] More table / cell edge fiddling and issue fixing Cells now draw only inner borders and the table draws the outer border if the layout is "closed". This avoids multiple lines around cells of nested tables, since nested tables are now created with the layout parameter set to "open", in which case the table does not draw its borders. --- test/fixtures/page_generation/page.py | 46 +++++++++++++++------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 102a77d..05f8eed 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -537,7 +537,7 @@ def get_size(rectangle: Rectangle): class RecursiveRandomTable(RandomContentRectangle): - def __init__(self, x1, y1, x2, y2, seed=None, border_width=1): + def __init__(self, x1, y1, x2, y2, seed=None, border_width=1, layout=None): super().__init__(x1, y1, x2, y2, seed=seed) self.n_columns = rnd.randint(1, max(self.width // 100, 1)) self.n_rows = rnd.randint(1, max(self.height // rnd.randint(17, 100), 1)) @@ -548,16 +548,21 @@ class RecursiveRandomTable(RandomContentRectangle): # self.background_color = tuple([random.randint(0, 100) for _ in range(4)]) self.cell_border_color = (0, 0, 0, 255) # (*map(lambda x: int(x * 0.8), self.background_color[:3]), 255) - self.layout = rnd.choice(["closed", "horizontal", "vertical", "open"]) - self.layout = "closed" - # Overwrite the layout choice in some cases - if self.n_columns == 1 and self.n_rows == 1: - self.layout = "closed" - elif self.n_columns == 1: - self.layout = rnd.choice(["vertical", "closed"]) - elif self.n_rows == 1: - self.layout = rnd.choice(["horizontal", "closed"]) + # TODO: Refactor layout selection + # self.layout = rnd.choice(["closed", "horizontal", "vertical", "open"]) + + # # Overwrite the layout choice in some cases + # if self.n_columns == 1 and self.n_rows == 1: + # self.layout = "closed" + # elif self.n_columns == 1: + # self.layout = rnd.choice(["vertical", "closed"]) + # elif self.n_rows == 1: + # self.layout = rnd.choice(["horizontal", "closed"]) + + self.layout = "closed" # TODO: Remove this line + + self.layout = layout or self.layout logger.debug(f"Layout: {self.layout}") # self.draw_single_cell_borders(self, border_width, fill=(0, 0, 0, 0)) @@ -578,7 +583,7 @@ class RecursiveRandomTable(RandomContentRectangle): def inner(cell): - inner_region = shrink_rectangle(cell, 0.01) + inner_region = shrink_rectangle(cell, 0.11) choice = rnd.choice(["text", "plot", "recurse", "plain_table", "blank"]) size = get_size(inner_region) @@ -595,7 +600,7 @@ class RecursiveRandomTable(RandomContentRectangle): return generate_random_plot(cell) elif choice == "recurse": - return generate_recursive_random_table(cell) + return generate_recursive_random_table(cell, border_width=1, layout="open") else: return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}") @@ -611,7 +616,7 @@ class RecursiveRandomTable(RandomContentRectangle): else: logger.debug(f"recurse {size:.0f} {get_size_class(cell).name}") - return generate_recursive_random_table(cell, border_width=0) + return generate_recursive_random_table(cell, border_width=1, layout="open") else: return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}") @@ -628,15 +633,16 @@ class RecursiveRandomTable(RandomContentRectangle): def draw_edges_based_on_position(cell: Cell, col_idx, row_index): # Draw the borders of the cell based on its position in the table - if col_idx == 0: - cell.draw_left_border() + if col_idx < self.n_columns - 1: + cell.draw_right_border() - cell.draw_right_border() + if row_index < self.n_rows - 1: + cell.draw_bottom_border() - if row_index == 0: - cell.draw_top_border() - - cell.draw_bottom_border() + if self.layout == "closed": + c = Cell(*self.coords, self.background_color) + c.content = self.content + c.draw() columns = chunks(self.n_rows, cells) for col_idx, columns in enumerate(columns):