From 9e77e25afb0899a002b9f3883863d29b33256478 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Mon, 23 Jan 2023 12:20:04 +0100 Subject: [PATCH] Refactoring Move text block generation code into its own class. --- test/fixtures/page_generation/page.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 5f7ae1e..ac1942a 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -963,17 +963,33 @@ class ParagraphLineFormatter(LineFormatter): return line +class TextBlockGenerator(abc.ABC): + pass + + +class ParagraphGenerator(TextBlockGenerator): + def __init__(self): + self.line_formatter = ParagraphLineFormatter(blank_line_percentage=random.uniform(0, 0.5)) + + def __call__(self, rectangle, n_sentences): + return self.generate_paragraph(rectangle, n_sentences) + + def generate_paragraph(self, rectangle, n_sentences): + lines = generate_random_text_lines(rectangle, self.line_formatter, n_sentences) + return lines + + class TextBlock(ContentRectangle): - def __init__(self, x1, y1, x2, y2, line_formatter=None): + def __init__(self, x1, y1, x2, y2, line_generator=None, font=None): super().__init__(x1, y1, x2, y2) - self.font = ImageFont.load_default() # pick_random_font_available_on_system() - self.line_formatter = line_formatter or ParagraphLineFormatter(blank_line_percentage=random.uniform(0, 0.5)) + self.font = ImageFont.load_default() if not font else pick_random_font_available_on_system() + self.line_generator = line_generator or ParagraphGenerator() def __call__(self, *args, **kwargs): pass def generate_random_text(self, rectangle: Rectangle, n_sentences=3000): - lines = generate_random_text_lines(rectangle, self.line_formatter, n_sentences) + lines = self.line_generator(rectangle, n_sentences) image = write_lines_to_image(lines, rectangle, self.font) return self.__put_content(image)