-
Notifications
You must be signed in to change notification settings - Fork 3
/
SnippetGneration_Lib.py
143 lines (117 loc) · 4.82 KB
/
SnippetGneration_Lib.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from pygments import highlight
from pygments.styles import get_style_by_name
from pygments.lexers import JavaLexer
from pygments.formatters import BBCodeFormatter
from PIL import Image, ImageDraw, ImageFont
import os
import re
import json
def parse_bbcode_list(bbcode):
result = []
pattern = '\[[^\[\]]*?\]'
idx = 0
base_color = (0,0,0,255)
color = base_color
bold = False
italic = False
aoi_default = "None"
aoi_name = aoi_default
while idx < len(bbcode):
data = re.search(pattern, bbcode[idx:])
meta = False
if data is not None and data.start(0) == 0:
value = data.group(0).strip("[").strip("]")
if '/aoi' in value:
meta = True
aoi_name = aoi_default
elif 'aoi' in value:
meta = True
aoi_name = value[4:]
if '/color' in value:
meta = True
color = base_color
elif 'color' in value:
meta = True
color = tuple(int(value[i:i+2], 16) for i in (7, 9, 11))
color = (color[0], color[1], color[2], 255)
#if '/b' in value:
# meta = True
# bold = False
#elif 'b' in value:
# meta = True
# bold = True
#if '/i' in value:
# meta = True
# italic = False
#elif 'i' in value:
# meta = True
# italic = True
if meta==True:
idx += data.end(0)
if meta == False:
result.append({'letter':bbcode[idx],
'color':color,
'bold':bold,
'italic':italic,
'AOI': aoi_name})
idx+=1
return result
def set_font(data, regular, bold, italic, bolditalic):
new_result = []
for entry in data:
if entry["bold"] == True and entry["italic"] == True:
entry["font"] = bolditalic
if entry["bold"] == True and entry["italic"] == False:
entry["font"] = bold
if entry["bold"] == False and entry["italic"] == True:
entry["font"] = italic
if entry["bold"] == False and entry["italic"] == False:
entry["font"] = regular
new_result.append(entry)
return new_result
def create_image(source_json, font_path = "\\fonts\\ttf\\", lexer = JavaLexer):
data = {}
with open(source_json) as f:
data = json.load(f)
code = ""
for line in data["source-code"]:
code += line + "\n"
background_color = (data["background-color"][0], data["background-color"][1], data["background-color"][2])
width_margin = data["width-margin"]
height_margin = data["height-margin"]
spacing = data["spacing"]
regular = ImageFont.truetype(os.getcwd() + font_path + data["font-normal"], data["font-size"], encoding="unic")
bold = ImageFont.truetype(os.getcwd() + font_path + data["font-bold"], data["font-size"], encoding="unic")
italic = ImageFont.truetype(os.getcwd() + font_path + data["font-italic"], data["font-size"], encoding="unic")
bolditalic = ImageFont.truetype(os.getcwd() + font_path + data["font-bold-italic"], data["font-size"], encoding="unic")
code = highlight(code, lexer(), BBCodeFormatter(line_numbers=False, style=get_style_by_name("vs")))
code = parse_bbcode_list(code)
code = set_font(code, regular, bold, italic, bolditalic)
source_code = []
tmp_source_code = ""
for tmp_data in code:
if tmp_data["letter"] == "\n":
source_code.append(tmp_source_code)
tmp_source_code = ""
else:
tmp_source_code += tmp_data["letter"]
img = Image.new("RGBA", (1, 1), color = 'white')
draw = ImageDraw.Draw(img)
max_length = max([draw.textsize(txt, regular)[0] for txt in source_code]) + 2*width_margin
max_height = sum([draw.textsize("placeholder", regular)[1]+spacing for txt in data["source-code"]]) + 2*height_margin
img = Image.new("RGBA", (max_length, max_height), color = background_color)
draw = ImageDraw.Draw(img)
width_current = width_margin
height_current = height_margin
result = []
for letter in code:
if letter["letter"] == "\n":
width_current = width_margin
height_current = height_current + draw.textsize("placeholder", regular)[1] + spacing
letter["BoundingBox"] = (0,0,0,0)
else:
letter["BoundingBox"] = draw.textbbox((width_current, height_current), letter["letter"], font=letter["font"])
draw.text((width_current, height_current), letter["letter"], letter["color"], font=letter["font"])
width_current += draw.textsize(letter["letter"], regular)[0]
result.append(letter)
return img, result