[WIP] head and bottom border (booktabs-like) for tables

This commit is contained in:
Matthias Bisping 2023-01-25 20:18:09 +01:00
parent d5e501a05d
commit daea7d2bf7

View File

@ -36,7 +36,7 @@ random_seed = random.randint(0, 2**32 - 1)
# random_seed = 1986343479
# random_seed = 273244862 # empty large table
random_seed = 3717442900
# random_seed = 3717442900
rnd = random.Random(random_seed)
logger.info(f"Random seed: {random_seed}")
@ -472,7 +472,7 @@ def generate_random_plot_with_caption(rectangle: Rectangle):
# TODO: deduplicate with generate_random_table_with_caption
def generate_recursive_random_table_with_caption(rectangle: Rectangle):
table_box, caption_box = split_into_figure_and_caption(rectangle)
table_box = generate_recursive_random_table(table_box)
table_box = generate_recursive_random_table(table_box, double_border=probably())
caption_box = generate_random_table_caption(caption_box)
return table_box, caption_box
@ -578,8 +578,12 @@ def get_random_background_color():
class RecursiveRandomTable(RandomContentRectangle):
def __init__(self, x1, y1, x2, y2, seed=None, border_width=1, layout=None):
def __init__(self, x1, y1, x2, y2, seed=None, border_width=1, layout=None, double_border=False):
super().__init__(x1, y1, x2, y2, seed=seed)
self.double_border = double_border
self.double_border_width = (5 * border_width) if self.double_border else 0
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))
self.cell_size = (self.width / self.n_columns, self.height / self.n_rows)
@ -637,7 +641,10 @@ class RecursiveRandomTable(RandomContentRectangle):
elif choice == "recurse":
return generate_recursive_random_table(
cell, border_width=1, layout=random.choice(["open", "horizontal", "vertical"])
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_border=False,
)
else:
@ -655,7 +662,10 @@ class RecursiveRandomTable(RandomContentRectangle):
else:
logger.debug(f"recurse {size:.0f} {get_size_class(cell).name}")
return generate_recursive_random_table(
cell, border_width=1, layout=random.choice(["open", "horizontal", "vertical"])
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_border=False,
)
else:
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
@ -691,6 +701,14 @@ class RecursiveRandomTable(RandomContentRectangle):
c.draw()
yield self
if self.double_border:
# TODO: Refactor
c = Cell(*self.coords)
c.draw_top_border(width=5)
c.draw_bottom_border(width=5)
self.content = superimpose_texture_with_transparency(c.content, self.content)
yield self
def generate_table(self) -> Iterable[ContentRectangle]:
yield from mapcat(self.generate_column, range(self.n_columns))
@ -722,13 +740,13 @@ class Cell(ContentRectangle):
self.background_color = color or (255, 255, 255, 0)
# to debug use random border color: tuple([random.randint(100, 200) for _ in range(3)] + [255])
self.cell_border_color = (10, 10, 10, 255)
self.cell_border_color = (0, 0, 0, 255)
self.border_width = 1
self.inset = 1
self.content = Image.new("RGBA", (self.width, self.height))
self.fill()
# self.fill()
def draw_top_border(self, width=None):
self.draw_line((0, 0, self.width - self.inset, 0), width=width)
@ -798,7 +816,7 @@ def generate_random_words(n_min, n_max):
def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle:
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, factor)
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, (1 - factor))
logger.trace(f"Shrinking {rectangle} by {factor} to ({x1}, {y1}, {x2}, {y2}).")
@ -818,7 +836,7 @@ def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle:
def compute_scaled_coordinates(rectangle: Rectangle, factor: float) -> Tuple[int, int, int, int]:
# TODO: Refactor: Using image to compute coordinates is not clean
image = Image.new("RGBA", (rectangle.width, rectangle.height))
scaled = image.resize((int(rectangle.width * (1 - factor)), int(rectangle.height * (1 - factor))))
scaled = image.resize((int(rectangle.width * factor), int(rectangle.height * factor)))
x1, y1 = compute_pasting_coordinates(scaled, image)
x1 = rectangle.x1 + x1
@ -971,7 +989,7 @@ def get_fonts(path: Path = None) -> List[str]:
@lru_cache(maxsize=None)
def get_font_picker(**kwargs):
return RandomFontPicker(**kwargs, return_default_font=False)
return RandomFontPicker(**kwargs, return_default_font=True)
@lru_cache(maxsize=None)
@ -1154,6 +1172,10 @@ def maybe():
return rnd.random() > 0.9
def probably():
return rnd.random() > 0.4
def generate_random_text_block(rectangle: Rectangle, n_sentences=3000) -> ContentRectangle:
block = TextBlock(
*rectangle.coords,