aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2020-06-19 01:08:29 -0600
committerMelody Horn <melody@boringcactus.com>2020-06-19 01:08:29 -0600
commit6f4472ae6272a670ef29ad1ff844c601b1cb5d95 (patch)
tree0c8db7b263849870ee82facef520dbdcbeaeefee /bot.py
parent672f3edd7aabe124b9075b50da9bbb85684bd86c (diff)
downloadcrabravebot-6f4472ae6272a670ef29ad1ff844c601b1cb5d95.tar.gz
crabravebot-6f4472ae6272a670ef29ad1ff844c601b1cb5d95.zip
fix word wrap on short lines
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/bot.py b/bot.py
index d4fc88f..d7590bf 100644
--- a/bot.py
+++ b/bot.py
@@ -9,6 +9,7 @@ import tempfile
from queue import Queue
from threading import Thread
import json
+from typing import Optional
import telegram
from telegram.ext import Dispatcher, MessageHandler, CommandHandler, InlineQueryHandler, Filters, CallbackContext
@@ -81,9 +82,11 @@ def word_wrap(text: str, draw: ImageDraw.ImageDraw, width: int) -> str:
else:
wrapped_text.append(line)
return '\n'.join(wrapped_text)
+ else:
+ return text
-def render_text(text: str, base: Image):
+def render_text(text: Optional[str], base: Image):
if base.mode == 'RGB':
white = (255, 255, 255)
black = (0, 0, 0)
@@ -93,6 +96,8 @@ def render_text(text: str, base: Image):
else:
raise ValueError('Base image {}, not RGB/RGBA'.format(base.mode))
+ if text is None:
+ text = ''
draw = ImageDraw.Draw(base)
text = word_wrap(text, draw, base.width)
text_width, text_height = draw.multiline_textsize(text, font=font)