-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSg.py
More file actions
352 lines (305 loc) · 12.3 KB
/
Sg.py
File metadata and controls
352 lines (305 loc) · 12.3 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
__hh__ = '''
/**
* SSAS - Simple Smart Automotive Software
* Copyright (C) 2021 Parai Wang <parai@foxmail.com>
*/
'''
import sys
import os
from PIL import Image
import xml.etree.ElementTree as ET
import re
import glob
import pickle
import hashlib
reSgBMP = re.compile(r'SgBMP\{(\d+)\}')
reSgTXT = re.compile(r'SgTXT\{(\d+)\}')
__SGL_MAX = 0
def CName(s):
return s.replace('.', '_').replace('/', '_').replace('\\', '_')
class Sg():
def __init__(self, file, option=None):
self.file = file
self.option = option
def getPixel(self, IM, x, y):
rgb = IM.getpixel((x, y))
if(type(rgb) == int):
pass
elif(len(rgb) == 4):
rgb = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2] + (rgb[3] << 24)
elif(len(rgb) == 3):
rgb = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]
else:
print('rgb is', rgb)
assert(0)
return rgb
def toU8Dot(self, fp, X=0, Y=0):
name = os.path.basename(self.file)
name = name[:name.find('.')]
code = hex(ord(name))[2:].upper()
aname = os.path.abspath(self.file)
fp.write('static const uint8_t sgf_dot_%s[] = \n{' % (code))
try:
IM = Image.open(self.file)
except NameError:
fp.write(' 0\n};\n')
return
fp.write('\n %s,%s,/* size(w,h) */' % (IM.size[0], IM.size[1]))
for y in range(0, IM.size[1]):
fp.write('\n ')
DOT = B = 0
for x in range(0, IM.size[0]):
rgb = self.getPixel(IM, x, y)
if(rgb != 0):
DOT = DOT | (1 << B)
B += 1
if(B == 8):
fp.write('0x%-2X,' % (DOT))
DOT = 0
B = 0
if(B > 0):
fp.write('0x%-2X,' % (DOT))
fp.write('\n};\n')
def filterForTelltale(self, rgb):
# as I don't want to use GIMP to create each valid TT PNG, so just
# use this API to filter out the black color
alpha = (rgb >> 24) & 0xFF
# if(alpha == 0): # this is empty color
# return 0
r = (rgb >> 16) & 0xFF
g = (rgb >> 8) & 0xFF
b = (rgb >> 0) & 0xFF
if(r < 80 and g < 80 and b < 80): # black
return 0
if(r > 0xE0 and g > 0xE0 and b > 0xE0): # white
return 0
return rgb
def toU8Pixel(self, fp, X=0, Y=0):
name = CName(os.path.basename(self.file))
aname = os.path.abspath(self.file)
fp.write('#include "Sg.h"\n')
fp.write('#include "SgRes.h"\n\n')
fp.write('static const uint32_t %s_bmp[] = \n{' % (name))
IM = Image.open(self.file)
for y in range(0, IM.size[1]): # height
fp.write('\n ')
for x in range(0, IM.size[0]): # width
rgb = self.getPixel(IM, x, y)
if(name[:3] == 'tt_'):
rgb = self.filterForTelltale(rgb)
fp.write('0x%-8X,' % (rgb))
fp.write('\n};\n')
fp.write('static const SgBMP %s_BMP=\n' % (name))
fp.write('{ /* %s */\n' % (aname))
fp.write(' /*x=*/%s,\n' % (X))
fp.write(' /*y=*/%s,\n' % (Y))
fp.write(' /*w=*/%s,\n' % (IM.size[0])) # width
fp.write(' /*h=*/%s,\n' % (IM.size[1])) # height
fp.write(' /*p=*/%s_bmp\n};\n' % (name))
def GetSgImage(IML, fp):
d = os.path.abspath(os.path.dirname(fp.name) + '/..')
for image in IML:
Sg('%s/%s' % (d, image[0])).toU8Pixel(fp, image[1], image[2])
def GetSgFont(IML, fp):
d = os.path.abspath(os.path.dirname(fp.name) + '/..')
for image in IML:
Sg('%s/%s' % (d, image)).toU8Dot(fp)
def GenearteSgBMP(widget, fph, fpc):
global __SGL_MAX
d = os.path.dirname(fph.name)
fp = open('%s/%s.c' % (d, widget.attrib['name']), 'w')
size = int(reSgBMP.search(widget.attrib['type']).groups()[0], 10)
IML = []
for p in widget:
if(p.tag == 'SgBMP'):
IML.append((p.attrib['locate'], p.attrib['x'], p.attrib['y']))
if(len(IML) == size):
GetSgImage(IML, fp)
else:
raise Exception(
'size SG widget picture is not right <size=%s,len(SgPciture)=%s>!' % (size, len(IML)))
fp.write(
'static const SgBMP* %s_BMPS[%s] = \n{\n' % (widget.attrib['name'], size))
for i, (file, x, y) in enumerate(IML):
name = CName(os.path.basename(file))
fp.write(' &%s_BMP,\n' % (name))
fph.write("#define SGR_%-32s %s\n" % (name.upper(), i))
fp.write('};\n\n')
fp.write('const SgSRC %s_SRC = \n{\n' % (widget.attrib['name']))
fp.write(' /*t =*/%s,\n' % ('SGT_BMP'))
fp.write(' /*rs=*/%s,\n' % (size))
fp.write(' /*r =*/(const SgRes**)%s_BMPS,\n' % (widget.attrib['name']))
fp.write(' /*rf=*/(void*(*)(void*))%s,\n' % (widget.attrib['refresh']))
fp.write(' /*cf=*/(void(*)(void*))%s,\n' % (widget.attrib['cache']))
fp.write(' /*weight=*/%s,\n' % (widget.attrib['weight']))
fp.write(' /*name=*/"%s"\n' % (widget.attrib['name']))
fp.write('};\n\n')
if(widget.attrib['refresh'] != 'NULL'):
fph.write('extern void* %s(SgWidget* w);\n' %
(widget.attrib['refresh']))
if(widget.attrib['cache'] != 'NULL'):
fph.write('extern void %s(SgWidget* w);\n' % (widget.attrib['cache']))
fph.write('extern const SgSRC %s_SRC;\n' % (widget.attrib['name']))
fpc.write(' { /* SGW_%s */\n' % (widget.attrib['name'].upper()))
fpc.write(' /*x =*/%s,\n' % (widget.attrib['x']))
fpc.write(' /*y =*/%s,\n' % (widget.attrib['y']))
fpc.write(' /*w =*/%s,\n' % (widget.attrib['w']))
fpc.write(' /*h =*/%s,\n' % (widget.attrib['h']))
fpc.write(' /*c =*/%s,\n' % (0))
fpc.write(' /*d =*/%s,\n' % ('0xFFFF/*no rotation*/'))
fpc.write(' /*l =*/%s,\n' % (widget.attrib['layer']))
if(int(widget.attrib['layer'], 10) > __SGL_MAX):
__SGL_MAX = int(widget.attrib['layer'], 10)
name = CName(os.path.basename(IML[0][0]))
fpc.write(' /*ri=*/SGR_%s,\n' % (name.upper()))
fpc.write(' /*src =*/&%s_SRC\n' % (widget.attrib['name']))
fpc.write(' },\n')
fp.close()
def GenearteSgTXT(widget, fph, fpc):
global __SGL_MAX
d = os.path.dirname(fph.name)
fp = open('%s/%s.c' % (d, widget.attrib['name']), 'w')
fp.write('#include "Sg.h"\n')
fp.write('#include "SgRes.h"\n\n')
size = int(reSgTXT.search(widget.attrib['type']).groups()[0], 10)
FNT = []
for p in widget:
if(p.tag == 'SgTXT'):
FNT.append(p.attrib['refer'])
if(len(FNT) == size):
pass
else:
raise Exception(
'size SG widget text is not right <size=%s,len(SgTXT)=%s>!' % (size, len(FNT)))
fp.write(
'static const SgTXT* %s_TXTS[%s] = \n{\n' % (widget.attrib['name'], size))
for i, font_name in enumerate(FNT):
fp.write(' &sgf%s,\n' % (font_name))
fph.write("#define SGR_%-32s %s\n" % (font_name.upper(), i))
fp.write('};\n\n')
if(widget.attrib['refresh'] != 'NULL'):
fph.write('extern void* %s(SgWidget* w);\n' %
(widget.attrib['refresh']))
fph.write('extern const SgSRC %s_SRC;\n' % (widget.attrib['name']))
fp.write('const SgSRC %s_SRC = \n{\n' % (widget.attrib['name']))
fp.write(' /*t =*/%s,\n' % ('SGT_TXT'))
fp.write(' /*rs=*/%s,\n' % (size))
fp.write(' /*r =*/(const SgRes**)%s_TXTS,\n' % (widget.attrib['name']))
fp.write(' /*rf=*/(void*(*)(void*))%s\n' % (widget.attrib['refresh']))
fp.write('};\n\n')
fpc.write(' { /* SGW_%s */\n' % (widget.attrib['name'].upper()))
fpc.write(' /*x =*/%s,\n' % (widget.attrib['x']))
fpc.write(' /*y =*/%s,\n' % (widget.attrib['y']))
fpc.write(' /*w =*/%s,\n' % (widget.attrib['w']))
fpc.write(' /*h =*/%s,\n' % (widget.attrib['h']))
fpc.write(' /*c =*/%s,\n' % (0))
fpc.write(' /*d =*/%s,\n' % ('0xFFFF/*no rotation*/'))
fpc.write(' /*l =*/%s,\n' % (widget.attrib['layer']))
if(int(widget.attrib['layer'], 10) > __SGL_MAX):
__SGL_MAX = int(widget.attrib['layer'], 10)
fpc.write(' /*ri=*/SGR_%s,\n' % (font_name.upper()))
fpc.write(' /*src =*/&%s_SRC\n' % (widget.attrib['name']))
fpc.write(' },\n')
fp.close()
def GenearteSgFont(widget, fph, fpc):
locate = widget.attrib['locate']
font_name = widget.attrib['name']
d = os.path.dirname(fph.name)
fp = open('%s/%s.c' % (d, font_name), 'w')
fp.write('#include "Sg.h"\n')
fp.write('#include "SgRes.h"\n\n')
IML = []
for png in glob.glob('%s/*.png' % (locate)):
IML.append(png)
GetSgFont(IML, fp)
fp.write('static const uint16_t sgf_%s_look_up_table[%s] = \n{' % (
font_name, len(IML)))
chars = []
for png in IML:
name = os.path.basename(png)
name = name[:name.find('.')]
code = ord(name)
chars.append(code)
chars.sort()
for i, chr in enumerate(chars):
if(i % 4 == 0):
fp.write('\n ')
fp.write('0x%-4s,' % (hex(chr).upper()[2:]))
fp.write('\n};\n\n')
fp.write(
'static const uint8_t* sgf_%s_res_pointer_table[%s] = \n{' % (font_name, len(IML)))
for i, chr in enumerate(chars):
if(i % 4 == 0):
fp.write('\n ')
fp.write('sgf_dot_%-4s,' % (hex(chr).upper()[2:]))
fp.write('\n};\n\n')
fph.write('extern const SgTXT sgf%s;\n' % (font_name))
fp.write('const SgTXT sgf%s = \n{\n' % (font_name))
fp.write(' /*l=*/sgf_%s_look_up_table,\n' % (font_name))
fp.write(' /*p=*/sgf_%s_res_pointer_table,\n' % (font_name))
fp.write(' /*w=*/%s,\n' % (widget.attrib['w']))
fp.write(' /*h=*/%s,\n' % (widget.attrib['h']))
fp.write(' /*s=*/%s\n' % (len(IML)))
fp.write('};\n\n')
fp.close()
def GenerateWidget(widget, fph, fpc):
print('## Process Widget %s' % (widget.attrib['name']))
if(reSgBMP.search(widget.attrib['type']) != None):
GenearteSgBMP(widget, fph, fpc)
elif(reSgTXT.search(widget.attrib['type']) != None):
GenearteSgTXT(widget, fph, fpc)
elif(widget.attrib['type'] == 'SgFont'):
GenearteSgFont(widget, fph, fpc)
else:
raise Exception('unknown SG widget type!')
def GenerateSg(file):
global __SGL_MAX
d = os.path.dirname(file)
pklpath = '%s/SgRes/.gen.pkl' % (d)
md5 = hashlib.md5()
md5.update(open(file, 'rb').read())
new_hash = md5.hexdigest()
if(os.path.exists('%s/SgRes' % (d)) == False):
os.mkdir('%s/SgRes' % (d))
else:
if os.path.exists(pklpath):
old_hash = pickle.load(open(pklpath, 'rb'))
if new_hash == old_hash:
return
root = ET.parse(file).getroot()
widgets = []
for w in root:
if(w.tag == 'SgWidget'):
widgets.append(w)
fonts = []
for f in root:
if(f.tag == 'SgFont'):
fonts.append(f)
fph = open('%s/SgRes/SgRes.h' % (d), 'w')
fpc = open('%s/SgRes/SgRes.c' % (d), 'w')
fpc.write(__hh__)
fpc.write('#include "Sg.h"\n')
fpc.write('#include "SgRes.h"\n\n')
fph.write(__hh__)
fph.write('\n#ifndef SGRES_H\n#define SGRES_H\n\n')
fph.write('#define __SG_WIDTH__ %s\n' % (root.attrib['w']))
fph.write('#define __SG_HEIGHT__ %s\n\n' % (root.attrib['h']))
fph.write('#define __SG_PIXEL__ %s\n\n' % (root.attrib['p']))
fpc.write('SgWidget SGWidget[%s] = \n{\n' % (len(widgets)))
for w in widgets:
GenerateWidget(w, fph, fpc)
fpc.write('};\n\n')
# font is a special widget
for f in fonts:
GenerateWidget(f, fph, fpc)
fph.write('\n\n')
for i, w in enumerate(widgets):
fph.write('#define SGW_%-32s %s\n' % (w.attrib['name'].upper(), i))
fph.write('#define SGW_%-32s %s\n' % ('MAX', len(widgets)))
fph.write('\n\nextern SgWidget SGWidget[%s];\n\n' % (len(widgets)))
fph.write("\n\n#define SGL_MAX %s\n\n" % (__SGL_MAX+1))
fph.write('#endif\n\n')
fph.close()
fpc.close()
print(">>>>>>>> DONE! <<<<<<<<<")
pickle.dump(new_hash, open(pklpath, 'wb'))