Fix bug in table generation

- Remove the check `elif size < Size.LARGE.value` and made it into `else`,
since it was intended to cover all cells that are larger than medium
size.

- Also disbale page number generation for now
This commit is contained in:
Matthias Bisping 2023-02-01 09:58:30 +01:00
parent cbb3a8cc61
commit f98256d7e9

View File

@ -457,9 +457,6 @@ def page_with_opaque_content(
content_generator = ContentGenerator()
boxes = content_generator(boxes)
boxes.append(generate_random_page_number(texture)) # TODO: Refactor
print(boxes[-1])
page = paste_contents(texture, boxes)
return page, boxes
@ -482,8 +479,6 @@ def page_with_translucent_content(
boxes = content_generator(boxes)
page_content = paste_contents(blank_page, boxes)
boxes.append(generate_random_page_number(texture))
texture = random_flip(texture)
texture = texture_fn(texture)
@ -890,7 +885,6 @@ class RecursiveRandomTable(RandomContentRectangle):
def inner(cell):
choice = rnd.choice(["text", "plot", "recurse", "plain_table", "blank"])
size = get_size(cell)
if size <= Size.SMALL.value:
@ -915,7 +909,7 @@ class RecursiveRandomTable(RandomContentRectangle):
else:
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
elif size <= Size.LARGE.value:
else:
choice = rnd.choice(["plot", "recurse"])
@ -932,8 +926,6 @@ class RecursiveRandomTable(RandomContentRectangle):
layout=random.choice(["open", "horizontal", "vertical"]),
double_rule=False,
)
else:
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
cell = inner(cell)
@ -1370,7 +1362,6 @@ class RandomPlot(RandomContentRectangle):
y = np.random.normal(size=n)
scatter_fn = partial(
plt.scatter,
s=np.random.randint(low=1, high=100, size=n),
cmap=self.cmap,
marker=rnd.choice(["o", "*", "+", "x"]),
)
@ -1642,6 +1633,17 @@ class TextBlock(ContentRectangle):
return self
class RandomPageNumber(TextBlock):
def __init__(self, x1, y1, x2, y2):
super().__init__(x1, y1, x2, y2)
self.page_number = random.randint(1, 1000)
self.margin_distance_percentage = 0.05
self.margin_distance_x = int(self.width * self.margin_distance_percentage)
self.margin_distance_y = int(self.height * self.margin_distance_percentage)
self.location_coordinates = self.location_to_coordinates(self.pick_location())
def generate_random_text_lines(rectangle: Rectangle, line_formatter=identity, n_sentences=3000) -> List[str]:
text = Faker().paragraph(nb_sentences=n_sentences, variable_nb_sentences=False, ext_word_list=None)
unformatted_lines = textwrap.wrap(text, width=rectangle.width, break_long_words=False)
@ -1669,6 +1671,7 @@ def paste_contents(page, contents: Iterable[ContentRectangle]):
return page
# TODO: produce boxes for page numbers, headers and footers
class PagePartitioner(abc.ABC):
def __init__(self):
self.left_margin_percentage = 0.05
@ -1793,51 +1796,51 @@ def draw_boxes(page: Image, boxes: Iterable[Rectangle]):
show_image(page, backend="pil")
class RandomPageNumber(ContentRectangle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.page_number = random.randint(1, 1000)
self.margin_distance_percentage = 0.05
self.margin_distance_x = int(self.width * self.margin_distance_percentage)
self.margin_distance_y = int(self.height * self.margin_distance_percentage)
self.location_coordinates = self.location_to_coordinates(self.pick_location())
def __repr__(self):
return f"PageNumber({self.page_number}, {super().__repr__()})"
def generate_random_page_number(self, rectangle: Rectangle):
logger.info(f"Generating random page number for {rectangle}")
self.add_page_number()
def add_page_number(self):
page_number_image = self.get_page_number_image()
self.content.paste(page_number_image, self.location_coordinates, page_number_image)
def pick_location(self):
return rnd.choice(["top_left", "top_right", "bottom_left", "bottom_right", "center_top", "center_bottom"])
def location_to_coordinates(self, locations: str):
if locations == "top_left":
return self.x1 + self.margin_distance_x, self.y1 + self.margin_distance_y
elif locations == "top_right":
return self.x2 - self.margin_distance_x, self.y1 + self.margin_distance_y
elif locations == "bottom_left":
return self.x1 + self.margin_distance_x, self.y2 - self.margin_distance_y
elif locations == "bottom_right":
return self.x2 - self.margin_distance_x, self.y2 - self.margin_distance_y
elif locations == "center_top":
return self.x1 + self.width // 2, self.y1 + self.margin_distance_y
elif locations == "center_bottom":
return self.x1 + self.width // 2, self.y2 - self.margin_distance_y
else:
raise ValueError(f"Unknown location: {locations}")
def get_page_number_image(self):
font = pick_random_mono_space_font_available_on_system(excludes=("italic", "oblique"))
page_number_image = Image.new("RGBA", (100, 100), (255, 255, 255, 0))
draw = ImageDraw.Draw(page_number_image)
draw.text((0, 0), str(self.page_number), font=font, fill=(0, 0, 0, 255))
return page_number_image
# class RandomPageNumber(ContentRectangle):
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self.page_number = random.randint(1, 1000)
# self.margin_distance_percentage = 0.05
# self.margin_distance_x = int(self.width * self.margin_distance_percentage)
# self.margin_distance_y = int(self.height * self.margin_distance_percentage)
#
# self.location_coordinates = self.location_to_coordinates(self.pick_location())
#
# def __repr__(self):
# return f"PageNumber({self.page_number}, {super().__repr__()})"
#
# def generate_random_page_number(self, rectangle: Rectangle):
# logger.info(f"Generating random page number for {rectangle}")
# self.add_page_number()
#
# def add_page_number(self):
# page_number_image = self.get_page_number_image()
# self.content.paste(page_number_image, self.location_coordinates, page_number_image)
#
# def pick_location(self):
# return rnd.choice(["top_left", "top_right", "bottom_left", "bottom_right", "center_top", "center_bottom"])
#
# def location_to_coordinates(self, locations: str):
# if locations == "top_left":
# return self.x1 + self.margin_distance_x, self.y1 + self.margin_distance_y
# elif locations == "top_right":
# return self.x2 - self.margin_distance_x, self.y1 + self.margin_distance_y
# elif locations == "bottom_left":
# return self.x1 + self.margin_distance_x, self.y2 - self.margin_distance_y
# elif locations == "bottom_right":
# return self.x2 - self.margin_distance_x, self.y2 - self.margin_distance_y
# elif locations == "center_top":
# return self.x1 + self.width // 2, self.y1 + self.margin_distance_y
# elif locations == "center_bottom":
# return self.x1 + self.width // 2, self.y2 - self.margin_distance_y
# else:
# raise ValueError(f"Unknown location: {locations}")
#
# def get_page_number_image(self):
# font = pick_random_mono_space_font_available_on_system(excludes=("italic", "oblique"))
# page_number_image = Image.new("RGBA", (100, 100), (255, 255, 255, 0))
#
# draw = ImageDraw.Draw(page_number_image)
# draw.text((0, 0), str(self.page_number), font=font, fill=(0, 0, 0, 255))
#
# return page_number_image