From 0eb57056babcde11b0e1adf5f70c1a4b49c9badd Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Mon, 23 Jan 2023 15:16:09 +0100 Subject: [PATCH] Fix / improve table cell border drawing --- test/fixtures/page_generation/page.py | 37 ++++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 6612972..4fb6667 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -104,9 +104,6 @@ from funcy import ( keep, repeatedly, mapcat, - lmapcat, - without, - merge, omit, project, complement, @@ -146,7 +143,6 @@ from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR # ] # ) from cv_analysis.utils.display import show_image -from cv_analysis.utils.drawing import draw_rectangles from cv_analysis.utils.rectangle import Rectangle transform = A.Compose( @@ -474,7 +470,9 @@ def split_into_figure_and_caption(rectangle: Rectangle): def generate_random_plot(rectangle: Rectangle) -> ContentRectangle: block = RandomPlot(*rectangle.coords) + # block.content = attrgetter("content")(block) block.content = rectangle.content if isinstance(rectangle, ContentRectangle) else None # TODO: Refactor + print(block.content) block.generate_random_plot(rectangle) return block @@ -490,7 +488,8 @@ def generate_random_table(rectangle: Rectangle) -> ContentRectangle: def generate_recursive_random_table(rectangle: Rectangle, **kwargs) -> ContentRectangle: block = RecursiveRandomTable(*rectangle.coords, **kwargs) - # block.content = rectangle.content if isinstance(rectangle, ContentRectangle) else None # TODO: Refactor + if isinstance(rectangle, RecursiveRandomTable): + block.content = rectangle.content if rectangle.content else None # TODO: Refactor block.generate_random_table() return block @@ -533,16 +532,16 @@ def get_size(rectangle: Rectangle): class RecursiveRandomTable(RandomContentRectangle): - def __init__(self, x1, y1, x2, y2, seed=None, border_width=2): + def __init__(self, x1, y1, x2, y2, seed=None, border_width=1): super().__init__(x1, y1, x2, y2, seed=seed) self.n_columns = random.randint(1, max(self.width // 100, 1)) self.n_rows = random.randint(1, max(self.height // random.randint(17, 100), 1)) self.cell_size = (self.width // self.n_columns, self.height // self.n_rows) - self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 255)) + self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 0)) self.background_color = tuple([random.randint(0, 100) for _ in range(4)]) self.cell_border_color = (*map(lambda x: int(x * 0.8), self.background_color[:3]), 255) - self.draw_single_cell_borders(self, border_width, fill=(0, 0, 0, 0)) + # self.draw_single_cell_borders(self, border_width, fill=(0, 0, 0, 0)) def generate_random_table(self): cells = list(self.generate_cells_with_content()) @@ -551,7 +550,7 @@ class RecursiveRandomTable(RandomContentRectangle): def generate_cells_with_content(self): for cell in self.generate_table(): - self.draw_single_cell_borders(cell, fill=(0, 0, 0, 0), width=2) + self.draw_single_cell_borders(cell, fill=(0, 0, 0, 0), width=1) def inner(cell): @@ -586,7 +585,7 @@ class RecursiveRandomTable(RandomContentRectangle): return generate_random_plot(cell) else: logger.debug(f"recurse {size:.0f} {get_size_class(cell).name}") - return generate_recursive_random_table(inner_region, border_width=4) + return generate_recursive_random_table(inner_region, border_width=0) else: return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}") @@ -603,10 +602,10 @@ class RecursiveRandomTable(RandomContentRectangle): def draw_single_cell_borders(self, cell: ContentRectangle, width=1, fill=None): fill = (0, 0, 0, 0) if fill is None else fill - image = cell.content or Image.new("RGBA", (cell.width, cell.height), (255, 255, 255)) + image = cell.content or Image.new("RGBA", (cell.width, cell.height), (255, 255, 255, 0)) assert image.mode == "RGBA" draw = ImageDraw.Draw(image) - draw.rectangle((0, 0, cell.width, cell.height), fill=fill, outline=self.cell_border_color, width=width) + draw.rectangle((0, 0, cell.width - 1, cell.height - 1), fill=fill, outline=self.cell_border_color, width=width) cell.content = image assert cell.content.mode == "RGBA" return cell @@ -673,6 +672,7 @@ def compute_scaled_coordinates(rectangle: Rectangle, factor: float) -> Tuple[int class RandomTable(RandomContentRectangle): def __init__(self, x1, y1, x2, y2, seed=None): super().__init__(x1, y1, x2, y2, seed=seed) + self.font = pick_random_mono_space_font_available_on_system(includes=("bold",), excludes=("italic", "oblique")) def generate_random_table(self, rectangle: Rectangle): """Generates the image of a random table. @@ -821,12 +821,13 @@ def pick_random_mono_space_font_available_on_system(**kwargs): @lru_cache(maxsize=None) def pick_random_font_available_on_system(**kwargs): - # kwargs["excludes"] = ( - # *kwargs.get( - # "excludes", - # ), - # "padauk", - # ) + kwargs["excludes"] = ( + *kwargs.get( + "excludes", + ), + "Kinnari", + "KacstOne", + ) font_picker = get_font_picker(**omit(kwargs, ["includes", "excludes"])) return font_picker.pick_random_font_available_on_system(**project(kwargs, ["includes", "excludes"]))