From 5e9e7db9356cbdecc3c302c69eeb2f6723915508 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 4 Dec 2021 19:08:55 +0100 Subject: [PATCH 1/2] Fix preview image text wrap length Closes #1634 --- bookwyrm/preview_images.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/bookwyrm/preview_images.py b/bookwyrm/preview_images.py index 32465d6ed..164ba9e98 100644 --- a/bookwyrm/preview_images.py +++ b/bookwyrm/preview_images.py @@ -49,6 +49,25 @@ def get_font(font_name, size=28): return font +def get_wrapped_text(text, font, content_width): + """text wrap length depends on the max width of the content""" + + low = 0 + high = len(text) + + # ideal length is determined via binary search + while low < high: + mid = math.floor(low + high) + wrapped_text = textwrap.fill(text, width=mid) + width = font.getsize_multiline(wrapped_text)[0] + if width < content_width: + low = mid + else: + high = mid - 1 + + return wrapped_text + + def generate_texts_layer(texts, content_width): """Adds text for images""" font_text_zero = get_font("bold", size=20) @@ -63,7 +82,8 @@ def generate_texts_layer(texts, content_width): if "text_zero" in texts and texts["text_zero"]: # Text one (Book title) - text_zero = textwrap.fill(texts["text_zero"], width=72) + text_zero = get_wrapped_text(texts["text_zero"], font_text_zero, content_width) + text_layer_draw.multiline_text( (0, text_y), text_zero, font=font_text_zero, fill=TEXT_COLOR ) @@ -75,7 +95,8 @@ def generate_texts_layer(texts, content_width): if "text_one" in texts and texts["text_one"]: # Text one (Book title) - text_one = textwrap.fill(texts["text_one"], width=28) + text_one = get_wrapped_text(texts["text_one"], font_text_one, content_width) + text_layer_draw.multiline_text( (0, text_y), text_one, font=font_text_one, fill=TEXT_COLOR ) @@ -87,7 +108,8 @@ def generate_texts_layer(texts, content_width): if "text_two" in texts and texts["text_two"]: # Text one (Book subtitle) - text_two = textwrap.fill(texts["text_two"], width=36) + text_two = get_wrapped_text(texts["text_two"], font_text_two, content_width) + text_layer_draw.multiline_text( (0, text_y), text_two, font=font_text_two, fill=TEXT_COLOR ) @@ -99,7 +121,10 @@ def generate_texts_layer(texts, content_width): if "text_three" in texts and texts["text_three"]: # Text three (Book authors) - text_three = textwrap.fill(texts["text_three"], width=36) + text_three = get_wrapped_text( + texts["text_three"], font_text_three, content_width + ) + text_layer_draw.multiline_text( (0, text_y), text_three, font=font_text_three, fill=TEXT_COLOR ) From 5b690532fadea83fcb5ddfcb0db4ee75f439f963 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 4 Dec 2021 19:59:45 +0100 Subject: [PATCH 2/2] Add an AttributeError exception for CI tests --- bookwyrm/preview_images.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bookwyrm/preview_images.py b/bookwyrm/preview_images.py index 164ba9e98..a97ae2d5c 100644 --- a/bookwyrm/preview_images.py +++ b/bookwyrm/preview_images.py @@ -55,15 +55,18 @@ def get_wrapped_text(text, font, content_width): low = 0 high = len(text) - # ideal length is determined via binary search - while low < high: - mid = math.floor(low + high) - wrapped_text = textwrap.fill(text, width=mid) - width = font.getsize_multiline(wrapped_text)[0] - if width < content_width: - low = mid - else: - high = mid - 1 + try: + # ideal length is determined via binary search + while low < high: + mid = math.floor(low + high) + wrapped_text = textwrap.fill(text, width=mid) + width = font.getsize_multiline(wrapped_text)[0] + if width < content_width: + low = mid + else: + high = mid - 1 + except AttributeError: + wrapped_text = text return wrapped_text