__init__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. def loadFontSize(text, draw, image, fontpath):
  4. from PIL import ImageFont
  5. fontsize = 1 # starting font size
  6. img_fraction = (1,1) # fraction of screen covered
  7. font = ImageFont.truetype(fontpath, fontsize)
  8. fsize = (0,0)
  9. imgsize = (0.9,0.9)
  10. longline = getLongLine(text.splitlines())
  11. while True:
  12. font_last = font
  13. fontsize += 1
  14. font = ImageFont.truetype(fontpath, fontsize)
  15. fsize = draw.textsize(text, font=font)
  16. imgsize = (img_fraction[0]*image.size[0], img_fraction[1]*image.size[1])
  17. if (fsize[0] > imgsize[0] or fsize[1] > imgsize[1]):
  18. break
  19. print("Fontsize:", fontsize, fsize, imgsize)
  20. return font_last
  21. def wrapText(text):
  22. import textwrap
  23. splittext = []
  24. longline = ""
  25. iter_width = 500
  26. line_width_factor = 5
  27. while len(splittext)*line_width_factor < iter_width:
  28. iter_width -= 1
  29. splittext = textwrap.wrap(text, width=iter_width)
  30. longline = getLongLine(splittext)
  31. print("Wrap:", iter_width, len(splittext))
  32. return "\n".join(splittext), longline
  33. def getLongLine(split):
  34. longline = ""
  35. for l in split:
  36. if len(l) > len(longline):
  37. longline = l
  38. return longline