Refactoring

Move text block generation code into its own class.
This commit is contained in:
Matthias Bisping 2023-01-23 12:20:04 +01:00
parent b3480491be
commit 9e77e25afb

View File

@ -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)