Make multi line text possible
This commit is contained in:
parent
601b0a0ada
commit
8c48b68f77
2 changed files with 17 additions and 9 deletions
|
@ -6,16 +6,22 @@ def gen_image(height, text1, text2):
|
||||||
font1 = ImageFont.truetype("ComicMono.ttf", size=text1["size"])
|
font1 = ImageFont.truetype("ComicMono.ttf", size=text1["size"])
|
||||||
font2 = ImageFont.truetype("ComicMono.ttf", size=text2["size"])
|
font2 = ImageFont.truetype("ComicMono.ttf", size=text2["size"])
|
||||||
|
|
||||||
text1_box = font1.getbbox(text1["string"])
|
text1["string"] = text1["string"].replace("\r\n", "\n")
|
||||||
text2_box = font2.getbbox(text2["string"])
|
text2["string"] = text2["string"].replace("\r\n", "\n")
|
||||||
|
|
||||||
text1_width = text1_box[2]
|
lines1 = text1["string"].split('\n')
|
||||||
text2_width = text2_box[2]
|
lines2 = text2["string"].split('\n')
|
||||||
|
|
||||||
|
text1_width = max([font1.getbbox(line)[2] for line in lines1])
|
||||||
|
text2_width = max([font2.getbbox(line)[2] for line in lines2])
|
||||||
|
|
||||||
|
text1_max_start = max([font1.getbbox(line)[0] for line in lines1])
|
||||||
|
text2_max_start = max([font2.getbbox(line)[0] for line in lines2])
|
||||||
|
|
||||||
# I am assuming the left corner of the bbox to always be 0, I have yet to
|
# I am assuming the left corner of the bbox to always be 0, I have yet to
|
||||||
# encounter a situation when this isn't the case
|
# encounter a situation when this isn't the case
|
||||||
if (text1_box[0] != 0 or text2_box[0] != 0):
|
if (text1_max_start != 0 or text2_max_start != 0):
|
||||||
print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1_box, "text2_box", text2_box)
|
print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1, "text2_box", text2)
|
||||||
|
|
||||||
# the 1 here assures we don't get a 0 width image
|
# the 1 here assures we don't get a 0 width image
|
||||||
img = Image.new('1', (max(text1_width, text2_width, 1), height), color=1)
|
img = Image.new('1', (max(text1_width, text2_width, 1), height), color=1)
|
||||||
|
@ -34,7 +40,7 @@ Text1 = {
|
||||||
}
|
}
|
||||||
|
|
||||||
Text2 = {
|
Text2 = {
|
||||||
"string": "Hello worhfeiheifhefld",
|
"string": "Hello worhfeiheifhefld\nafefe",
|
||||||
"size": 20,
|
"size": 20,
|
||||||
"pos": 40,
|
"pos": 40,
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,16 @@
|
||||||
|
|
||||||
<form method="POST" action="/text-form">
|
<form method="POST" action="/text-form">
|
||||||
<h2>String 1</h2>
|
<h2>String 1</h2>
|
||||||
Text:</span> <input type="text" value="{{text1["string"]}}" name="string1"><br>
|
Text:<br>
|
||||||
|
<textarea name="string1">{{text1["string"]}}</textarea><br>
|
||||||
Size: <input type="number" value="{{text1["size"]}}" name="size1" min="0" required
|
Size: <input type="number" value="{{text1["size"]}}" name="size1" min="0" required
|
||||||
style="width: 40px"><br>
|
style="width: 40px"><br>
|
||||||
Y position: <input type="number" value="{{text1["pos"]}}" name="pos1" required
|
Y position: <input type="number" value="{{text1["pos"]}}" name="pos1" required
|
||||||
style="width: 40px"><br>
|
style="width: 40px"><br>
|
||||||
|
|
||||||
<h2>String 2</h2>
|
<h2>String 2</h2>
|
||||||
Text: <input type="text" value="{{text2["string"]}}" name="string2"><br>
|
Text:<br>
|
||||||
|
<textarea name="string2">{{text2["string"]}}</textarea><br>
|
||||||
Size: <input type="number" value="{{text2["size"]}}" name="size2" min="0" required
|
Size: <input type="number" value="{{text2["size"]}}" name="size2" min="0" required
|
||||||
style="width: 40px"><br>
|
style="width: 40px"><br>
|
||||||
Y position: <input type="number" value="{{text2["pos"]}}" name="pos2" required
|
Y position: <input type="number" value="{{text2["pos"]}}" name="pos2" required
|
||||||
|
|
Loading…
Reference in a new issue