| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #!/usr/bin/env python3
- # -*- coding:utf-8 -*-
- def loadFontSize(text, draw, image, fontpath):
- from PIL import ImageFont
- fontsize = 1 # starting font size
- img_fraction = (1,1) # fraction of screen covered
- font = ImageFont.truetype(fontpath, fontsize)
- fsize = (0,0)
- imgsize = (0.9,0.9)
- longline = getLongLine(text.splitlines())
- while True:
- font_last = font
- fontsize += 1
- font = ImageFont.truetype(fontpath, fontsize)
- fsize = draw.textsize(text, font=font)
- imgsize = (img_fraction[0]*image.size[0], img_fraction[1]*image.size[1])
- if (fsize[0] > imgsize[0] or fsize[1] > imgsize[1]):
- break
- print("Fontsize:", fontsize, fsize, imgsize)
- return font_last
- def wrapText(text):
- import textwrap
- splittext = []
- longline = ""
- iter_width = 500
- line_width_factor = 5
- while len(splittext)*line_width_factor < iter_width:
- iter_width -= 1
- splittext = textwrap.wrap(text, width=iter_width)
- longline = getLongLine(splittext)
- print("Wrap:", iter_width, len(splittext))
- return "\n".join(splittext), longline
- def getLongLine(split):
- longline = ""
- for l in split:
- if len(l) > len(longline):
- longline = l
- return longline
|