-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtypeset.py
73 lines (63 loc) · 2.14 KB
/
typeset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import math
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from translate import TranslatedBlurb
def flow_into_box(text, w, font=None, min_word_on_line=.3):
def text_width(l):
if l:
return d.textsize(l, font=font)[0]
else:
return 0
dImg = Image.new("RGB", (100, 100))
d = ImageDraw.Draw(dImg)
lines = []
idx = 0
line = ""
while idx < len(text):
if not line and text[idx] == " ":
idx += 1
running_width = text_width(line)
next_token = text.find(" ", idx + 1)
if next_token != -1:
c_text = text[idx:next_token]
else:
c_text = text[idx:]
c_width = text_width(c_text)
proportion_of_fit = float(w - running_width) / c_width
if proportion_of_fit > .95:
line += c_text
idx += len(c_text)
# elif proportion_of_fit > min_word_on_line:
# split = max(int(proportion_of_fit * len(c_text)), 1)
# c_text = c_text[:split] + "-"
# line += c_text
# idx += len(c_text) - 1
else:
if len(line) > 0:
lines.append(line)
line = ""
else:
split = max(int(proportion_of_fit * len(c_text)) / 2, 1)
c_text = c_text[:split] + "-"
lines.append(c_text)
idx += len(c_text) - 1
if len(line) > 0:
lines.append(line)
return "\n".join(lines)
def typeset_blurb(img, blurb):
if isinstance(blurb, TranslatedBlurb):
text = blurb.translation
else:
text = blurb.text
area = blurb.w * blurb.h
fontsize = int(math.sqrt(area) / 10)
usingFont = ImageFont.truetype(font="ccwildword.ttf", size=fontsize)
# pickle.dump(img, open("tts.pkl", mode="w"))
flowed = flow_into_box(text, blurb.w, usingFont)
d = ImageDraw.Draw(img)
size = d.textsize(flowed)
x = (blurb.x + blurb.w / 2) - (size[0] / 2)
y = (blurb.y + blurb.h / 2) - (size[1] / 2)
img.paste((255, 255, 255), (x, y, x + size[0], y + size[1]))
d.text((x, y), flowed.strip(), fill=(0, 0, 0))