Centralized helper module to wrap texts
This commit is contained in:
parent
0400d29b10
commit
1b92692b51
1 changed files with 18 additions and 0 deletions
18
Calendar/TextWraper.py
Normal file
18
Calendar/TextWraper.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from Assets import path, defaultfont
|
||||
from PIL import ImageFont
|
||||
|
||||
def wrap_text_with_font (text, width, font):
|
||||
words = text.split(' ')
|
||||
result = ""
|
||||
for index, word in enumerate(words):
|
||||
until_current = (result + " " + word).strip()
|
||||
txt_width, _ = font.getsize_multiline(until_current)
|
||||
if txt_width > width:
|
||||
result += '\n'
|
||||
else:
|
||||
result += ' '
|
||||
result += word
|
||||
return result.strip()
|
||||
|
||||
def wrap_text (text, width, font_size, font_family = defaultfont):
|
||||
return wrap_text_with_font(text, width, ImageFont.truetype(path + font_family, font_size))
|
Loading…
Reference in a new issue