awtk/scripts/update_res_common.py

838 lines
27 KiB
Python
Raw Normal View History

2018-04-27 10:02:25 +08:00
import os
2019-03-31 11:13:43 +08:00
import sys
2018-11-02 11:51:14 +08:00
import copy
2018-04-27 10:02:25 +08:00
import glob
import shutil
import platform
2019-04-23 17:18:22 +08:00
from PIL import Image
2018-11-02 11:51:14 +08:00
###########################
2019-03-31 11:13:43 +08:00
DPI = ''
ACTION = 'all'
ASSET_C = ''
BIN_DIR = ''
2019-10-19 07:09:19 +08:00
THEME = 'default'
2019-03-31 11:13:43 +08:00
ASSETS_ROOT = ''
2020-03-16 21:19:20 +08:00
DEFAULT_THEME_ASSETS_ROOT = ''
2019-03-31 11:13:43 +08:00
AWTK_ROOT = ''
INPUT_DIR = ''
OUTPUT_DIR = ''
2020-03-16 21:19:20 +08:00
DEFAULT_THEME_OUTPUT_DIR = ''
2019-08-13 10:37:40 +08:00
FONT_OPTIONS = ''
2019-03-31 11:13:43 +08:00
IMAGEGEN_OPTIONS = ''
2020-03-16 21:19:20 +08:00
IS_DEFAULT_THEME = False
2020-04-30 09:15:22 +08:00
IS_FILE_EXCLUDED = None
2018-11-02 11:51:14 +08:00
###########################
2018-04-27 10:02:25 +08:00
2019-03-31 11:13:43 +08:00
2019-03-22 12:02:10 +08:00
def to_var_name(s):
2019-03-31 11:13:43 +08:00
out = ''
2019-03-22 12:02:10 +08:00
for c in s:
if(c.isalpha() or c.isdigit()):
2019-03-31 11:13:43 +08:00
out += c
2019-03-22 12:02:10 +08:00
else:
2019-03-31 11:13:43 +08:00
out += '_'
return out
2019-03-22 12:02:10 +08:00
2020-03-16 21:19:20 +08:00
def fix_output_file_name(filename):
2019-03-31 11:13:43 +08:00
basename = os.path.basename(filename)
dirname = os.path.dirname(filename)
2020-03-16 21:19:20 +08:00
varname = to_var_name(basename)
newname = os.path.normpath(os.path.join(dirname, varname))
2019-03-31 11:13:43 +08:00
return newname
2019-03-22 12:02:10 +08:00
2020-03-16 21:19:20 +08:00
def to_assets_basename(filename, output_dir):
basename = os.path.splitext(filename)[0]
basename = basename.replace(output_dir, '.')
basename = basename.replace('\\', '/')
basename = basename.replace('/fonts/', '/font/')
basename = basename.replace('/images/', '/image/')
basename = basename.replace('/styles/', '/style/')
basename = basename.replace('/scripts/', '/script/')
basename = basename.replace('./', '')
basename = basename.replace('/', '_')
return basename
def fix_inc_file(filename):
with open(filename, "r") as fr:
content = fr.read()
2020-03-18 11:21:07 +08:00
end = content.find('\n')
if end <= 0:
end = len(content)
2020-03-16 21:19:20 +08:00
basename = to_assets_basename(filename, OUTPUT_DIR)
2020-03-18 11:21:07 +08:00
index = content.find(basename + '[])', 0, end)
2020-03-16 21:19:20 +08:00
if index > 0:
with open(filename, 'w') as fw:
content = content.replace(basename + '[])', basename + '_' + THEME + '[])', 1)
fw.write(content)
2018-04-27 10:02:25 +08:00
def joinPath(root, subdir):
2019-03-31 11:13:43 +08:00
return os.path.normpath(os.path.join(root, subdir))
OS_NAME = platform.system()
2018-04-27 10:02:25 +08:00
2018-04-29 18:06:40 +08:00
def toExe(name):
2019-03-31 11:13:43 +08:00
if OS_NAME == 'Windows':
return joinPath(BIN_DIR, name+'.exe')
else:
return joinPath(BIN_DIR, name)
2018-04-27 10:02:25 +08:00
def buildAll():
2019-03-31 11:13:43 +08:00
os.system('scons')
2018-04-27 10:02:25 +08:00
def removeDir(path):
2019-03-31 11:13:43 +08:00
if os.path.isdir(path):
print('rmdir:' + path)
shutil.rmtree(path)
2018-04-27 10:02:25 +08:00
2018-11-02 11:51:14 +08:00
def prepareOutputDir(name):
2019-03-31 11:13:43 +08:00
fullpath = joinPath(OUTPUT_DIR, name)
2019-10-19 07:09:19 +08:00
if(not os.path.exists(fullpath)):
2019-03-31 11:13:43 +08:00
os.makedirs(fullpath)
2018-11-02 11:51:14 +08:00
2018-04-27 10:02:25 +08:00
def prepare():
2019-03-31 11:13:43 +08:00
prepareOutputDir('styles')
prepareOutputDir('images')
prepareOutputDir('fonts')
prepareOutputDir('strings')
prepareOutputDir('ui')
prepareOutputDir('scripts')
prepareOutputDir('data')
prepareOutputDir('xml')
2018-04-27 10:02:25 +08:00
2018-11-02 11:51:14 +08:00
def execCmd(cmd):
2019-03-31 11:13:43 +08:00
print(cmd)
os.system(cmd)
2018-04-27 10:02:25 +08:00
2018-11-02 11:51:14 +08:00
def themegen(raw, inc):
2019-10-19 07:09:19 +08:00
input=joinPath(INPUT_DIR, raw)
output=joinPath(OUTPUT_DIR, inc)
if(os.path.isfile(input)):
execCmd(toExe('themegen') + ' ' + input + ' ' + output)
2019-03-31 11:13:43 +08:00
def themegen_bin(raw, bin):
2019-10-19 07:09:19 +08:00
input=joinPath(INPUT_DIR, raw)
2019-10-19 07:42:18 +08:00
output=joinPath(INPUT_DIR, bin)
2019-10-19 07:09:19 +08:00
if(os.path.isfile(input)):
execCmd(toExe('themegen') + ' ' + input + ' ' + output + ' bin')
2019-03-31 11:13:43 +08:00
2018-05-04 14:52:15 +08:00
def strgen(raw, inc):
2019-10-19 07:09:19 +08:00
input=joinPath(INPUT_DIR, raw)
output=joinPath(OUTPUT_DIR, inc)
if(os.path.isfile(input)):
execCmd(toExe('strgen') + ' ' + input + ' ' + output)
2019-03-31 11:13:43 +08:00
2018-05-04 14:52:15 +08:00
def strgen_bin(raw, bin):
2019-10-19 07:09:19 +08:00
input=joinPath(INPUT_DIR, raw)
2019-10-19 07:42:18 +08:00
output=joinPath(INPUT_DIR, bin)
2019-10-19 07:09:19 +08:00
if(os.path.isfile(input)):
execCmd(toExe('strgen') + ' ' + input + ' ' + output + ' bin')
2018-05-04 14:52:15 +08:00
2018-04-27 10:02:25 +08:00
def resgen(raw, inc):
2019-10-19 07:09:19 +08:00
input=joinPath(INPUT_DIR, raw)
output=joinPath(OUTPUT_DIR, inc)
if(os.path.isfile(input)):
execCmd(toExe('resgen') + ' ' + input + ' ' + output)
2019-03-31 11:13:43 +08:00
2018-04-27 10:02:25 +08:00
def fontgen(raw, text, inc, size):
2019-10-19 07:09:19 +08:00
inputFont=joinPath(INPUT_DIR, raw);
inputText=joinPath(INPUT_DIR, text);
output=joinPath(OUTPUT_DIR, inc);
if(os.path.isfile(inputFont)):
execCmd(toExe('fontgen') + ' ' + inputFont + ' ' +
inputText + ' ' + output + ' ' +
2019-08-13 10:37:40 +08:00
str(size) + ' ' + FONT_OPTIONS)
2019-03-31 11:13:43 +08:00
2018-04-27 10:02:25 +08:00
def imagegen(raw, inc):
2019-10-19 07:09:19 +08:00
if(os.path.isfile(raw)):
execCmd(toExe('imagegen') + ' ' + raw + ' ' + inc + ' ' + IMAGEGEN_OPTIONS)
inc = inc.replace('.data', '.res')
resgen(raw, inc)
2019-03-31 11:13:43 +08:00
2018-04-27 10:02:25 +08:00
2018-11-20 16:25:28 +08:00
def svggen(raw, inc, bin):
2019-10-19 07:09:19 +08:00
if(os.path.isfile(raw)):
execCmd(toExe('bsvggen') + ' ' + raw + ' ' + inc)
execCmd(toExe('bsvggen') + ' ' + raw + ' ' + bin + ' bin')
2019-03-31 11:13:43 +08:00
2018-11-20 16:25:28 +08:00
2018-04-27 10:02:25 +08:00
def xml_to_ui(raw, inc):
2019-10-19 07:09:19 +08:00
if(os.path.isfile(raw)):
execCmd(toExe('xml_to_ui') + ' ' + raw + ' ' + inc)
2019-03-31 11:13:43 +08:00
2018-04-27 10:02:25 +08:00
def xml_to_ui_bin(raw, bin):
2019-10-19 07:09:19 +08:00
if(os.path.isfile(raw)):
execCmd(toExe('xml_to_ui') + ' ' + raw + ' ' + bin + ' bin')
2019-03-31 11:13:43 +08:00
2018-11-02 11:51:14 +08:00
def gen_res_all_style():
2019-03-31 11:13:43 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'styles/*.xml')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
bin = filename + '.bin'
2019-03-31 11:13:43 +08:00
themegen_bin(raw, bin)
2020-03-16 21:19:20 +08:00
filename = filename.replace(INPUT_DIR, OUTPUT_DIR)
inc = fix_output_file_name(filename) + '.data'
themegen(raw, inc)
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2019-03-31 11:13:43 +08:00
def gen_res_svg():
for f in glob.glob(joinPath(INPUT_DIR, 'images/svg/*.svg')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
bin = filename + '.bsvg'
basename = os.path.basename(filename)
filename = joinPath(OUTPUT_DIR, 'images/'+basename)
inc = fix_output_file_name(filename) + '.bsvg'
2019-03-31 11:13:43 +08:00
svggen(raw, inc, bin)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2019-03-31 11:13:43 +08:00
def gen_res_png_jpg():
2020-03-16 21:19:20 +08:00
formats = ['.png', '.jpg', '.jpeg', '.bmp', '.gif']
files = glob.glob(joinPath(INPUT_DIR, 'images/'+DPI+'/*.*')) \
+ glob.glob(joinPath(INPUT_DIR, 'images/xx/*.*'))
for f in files:
filename, extname = os.path.splitext(f)
for fm in formats:
if fm == extname:
raw = f
basename = os.path.basename(filename)
filename = joinPath(OUTPUT_DIR, 'images/'+basename)
2020-03-18 11:21:07 +08:00
filename = fix_output_file_name(filename)
inc = filename + '.data'
2020-03-16 21:19:20 +08:00
imagegen(raw, inc)
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2020-03-18 11:21:07 +08:00
fix_inc_file(filename + '.res')
2020-03-16 21:19:20 +08:00
break
2019-03-31 11:13:43 +08:00
def gen_res_all_image():
2018-11-20 16:25:28 +08:00
gen_res_png_jpg()
2018-11-21 09:34:23 +08:00
gen_res_svg()
2018-11-20 16:25:28 +08:00
2019-03-31 11:13:43 +08:00
2018-11-02 11:51:14 +08:00
def gen_res_all_ui():
2019-03-31 11:13:43 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'ui/*.xml')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
bin = filename + '.bin'
2019-03-31 11:13:43 +08:00
xml_to_ui_bin(raw, bin)
2020-03-16 21:19:20 +08:00
filename = filename.replace(INPUT_DIR, OUTPUT_DIR)
inc = fix_output_file_name(filename) + '.data'
xml_to_ui(raw, inc)
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2019-03-31 11:13:43 +08:00
2018-04-27 10:02:25 +08:00
2019-03-15 17:44:48 +08:00
def gen_res_all_data():
2019-03-31 11:13:43 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'data/*.*')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
filename = filename.replace(INPUT_DIR, OUTPUT_DIR) + extname.replace('.', '_')
inc = fix_output_file_name(filename) + '.data'
2019-03-31 11:13:43 +08:00
resgen(raw, inc)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2019-03-31 11:13:43 +08:00
2019-03-15 17:44:48 +08:00
def gen_res_all_xml():
2019-03-31 11:13:43 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'xml/*.xml')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
filename = filename.replace(INPUT_DIR, OUTPUT_DIR)
inc = fix_output_file_name(filename) + '.data'
2019-03-31 11:13:43 +08:00
resgen(raw, inc)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2019-03-31 11:13:43 +08:00
2019-03-15 17:44:48 +08:00
2018-11-02 11:51:14 +08:00
def gen_res_all_font():
2019-03-31 11:13:43 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'fonts/*.ttf')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
filename = filename.replace(INPUT_DIR, OUTPUT_DIR)
inc = fix_output_file_name(filename) + '.res'
resgen(raw, inc)
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2020-03-18 14:22:23 +08:00
if os.path.exists(joinPath(INPUT_DIR, 'fonts/default_full.ttf')):
if os.path.exists(joinPath(INPUT_DIR, 'fonts/text.txt')):
font_sizes = [16, 18, 20, 24, 32, 96]
for sz in font_sizes:
inc = 'fonts/default_%d.data' % sz
fontgen('fonts/default_full.ttf', 'fonts/text.txt', inc, sz)
if not IS_DEFAULT_THEME:
fix_inc_file(joinPath(OUTPUT_DIR, inc))
2020-03-16 21:19:20 +08:00
2019-03-31 11:13:43 +08:00
2018-07-05 15:22:04 +08:00
2019-02-07 17:27:19 +08:00
def gen_res_all_script():
2019-03-31 11:13:43 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'scripts/*.js')):
2020-03-16 21:19:20 +08:00
filename, extname = os.path.splitext(f)
raw = f
filename = filename.replace(INPUT_DIR, OUTPUT_DIR)
inc = fix_output_file_name(filename) + '.res'
resgen(raw, inc)
if not IS_DEFAULT_THEME:
fix_inc_file(inc)
2019-03-31 11:13:43 +08:00
2019-02-07 17:27:19 +08:00
2018-11-02 11:51:14 +08:00
def gen_res_all_string():
2019-10-19 07:09:19 +08:00
print('gen_res_all_string')
2019-03-31 11:13:43 +08:00
strgen('strings/strings.xml', 'strings')
strgen_bin('strings/strings.xml', 'strings')
2020-03-18 11:21:07 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(OUTPUT_DIR, 'strings/*.data'))
for f in files:
fix_inc_file(f)
2019-03-31 11:13:43 +08:00
2018-07-05 15:22:04 +08:00
2018-07-22 14:22:09 +08:00
def gen_gpinyin():
2019-03-31 11:13:43 +08:00
execCmd(toExe('resgen') + ' ' + joinPath('3rd', 'gpinyin/data/gpinyin.dat') +
' ' + joinPath('3rd', 'gpinyin/src/gpinyin.inc'))
execCmd(toExe('resgen') + ' ' + joinPath('tools', 'word_gen/words.bin') +
' ' + joinPath('src', 'input_methods/suggest_words.inc'))
execCmd(toExe('resgen') + ' ' + joinPath('tools',
'word_gen/words.bin') + ' ' + joinPath('tests', 'suggest_test.inc'))
2018-11-02 11:51:14 +08:00
def gen_res_all():
2019-03-31 11:13:43 +08:00
gen_res_all_string()
gen_res_all_font()
gen_res_all_script()
gen_res_all_image()
gen_res_all_ui()
gen_res_all_style()
gen_res_all_data()
gen_res_all_xml()
2020-03-16 21:19:20 +08:00
def writeResult(filename, str):
with open(filename, "w") as text_file:
2019-10-19 07:09:19 +08:00
text_file.write(str)
2019-03-31 11:13:43 +08:00
2019-04-07 18:31:16 +08:00
def writeResultJSON(str):
2019-04-23 16:00:02 +08:00
with open('assets.js', "w") as text_file:
2019-10-19 07:09:19 +08:00
text_file.write(str)
2018-04-27 10:02:25 +08:00
2018-06-08 19:19:06 +08:00
def genIncludes(files):
2020-04-30 09:15:22 +08:00
global IS_FILE_EXCLUDED
2020-03-16 21:19:20 +08:00
result = ""
assets_root = os.path.dirname(os.path.dirname(ASSETS_ROOT))
2019-03-31 11:13:43 +08:00
for f in files:
2020-04-30 09:15:22 +08:00
if (IS_FILE_EXCLUDED and IS_FILE_EXCLUDED(f)):
2020-04-28 18:04:52 +08:00
continue
2019-03-31 11:13:43 +08:00
incf = copy.copy(f)
2019-10-18 18:18:12 +08:00
incf = incf.replace(assets_root, ".")
2019-03-31 11:13:43 +08:00
incf = incf.replace('\\', '/')
incf = incf.replace('./', '')
2020-03-16 21:19:20 +08:00
result += '#include "'+incf+'"\n'
2019-03-31 11:13:43 +08:00
2020-03-16 21:19:20 +08:00
return result
2018-06-08 19:19:06 +08:00
2020-03-16 21:19:20 +08:00
def genExternOfDefaultTheme(files):
2020-04-30 09:15:22 +08:00
global IS_FILE_EXCLUDED
2020-03-16 21:19:20 +08:00
result = ""
for f in files:
2020-04-30 09:15:22 +08:00
if (IS_FILE_EXCLUDED and IS_FILE_EXCLUDED(f)):
2020-04-28 18:04:52 +08:00
continue
2020-03-16 21:19:20 +08:00
if(not os.path.exists(f.replace(DEFAULT_THEME_ASSETS_ROOT, ASSETS_ROOT))):
basename = to_assets_basename(f, DEFAULT_THEME_OUTPUT_DIR)
result += 'extern TK_CONST_DATA_ALIGN(const unsigned char '+basename+'[]);\n'
return result
2018-06-08 19:19:06 +08:00
2018-11-21 09:34:23 +08:00
def gen_add_assets(files):
2020-04-30 09:15:22 +08:00
global IS_FILE_EXCLUDED
2019-03-31 11:13:43 +08:00
result = ""
for f in files:
2020-04-30 09:15:22 +08:00
if (IS_FILE_EXCLUDED and IS_FILE_EXCLUDED(f)):
2020-04-28 18:04:52 +08:00
continue
2020-03-16 21:19:20 +08:00
basename = to_assets_basename(f, OUTPUT_DIR)
if IS_DEFAULT_THEME:
2019-09-13 07:55:19 +08:00
result += ' assets_manager_add(am, '+basename+');\n'
2020-03-16 21:19:20 +08:00
else:
result += ' assets_manager_add(am, '+basename+'_'+THEME+');\n'
2019-03-31 11:13:43 +08:00
return result
2020-03-16 21:19:20 +08:00
def gen_add_assets_of_default_theme(files):
2020-04-30 09:15:22 +08:00
global IS_FILE_EXCLUDED
2020-03-16 21:19:20 +08:00
result = ""
for f in files:
2020-04-30 09:15:22 +08:00
if (IS_FILE_EXCLUDED and IS_FILE_EXCLUDED(f)):
2020-04-28 18:04:52 +08:00
continue
2020-03-16 21:19:20 +08:00
if(not os.path.exists(f.replace(DEFAULT_THEME_ASSETS_ROOT, ASSETS_ROOT))):
basename = to_assets_basename(f, DEFAULT_THEME_OUTPUT_DIR)
result += ' assets_manager_add(am, '+basename+');\n'
2018-11-21 09:34:23 +08:00
2020-03-16 21:19:20 +08:00
return result
2019-10-19 07:09:19 +08:00
2020-03-16 21:19:20 +08:00
def gen_res_c():
2019-03-31 11:13:43 +08:00
result = '#include "awtk.h"\n'
result += '#include "base/assets_manager.h"\n'
result += '#ifndef WITH_FS_RES\n'
2020-03-16 21:19:20 +08:00
2019-03-31 11:13:43 +08:00
files = glob.glob(joinPath(OUTPUT_DIR, 'strings/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'styles/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'ui/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'xml/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'data/*.data'))
result += genIncludes(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'strings/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'styles/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'ui/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'xml/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'data/*.data'))
result += genExternOfDefaultTheme(files)
2019-03-31 11:13:43 +08:00
result += "#ifdef WITH_STB_IMAGE\n"
files = glob.glob(joinPath(OUTPUT_DIR, 'images/*.res'))
result += genIncludes(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'images/*.res'))
result += genExternOfDefaultTheme(files)
2019-03-31 11:13:43 +08:00
result += "#else\n"
files = glob.glob(joinPath(OUTPUT_DIR, 'images/*.data'))
result += genIncludes(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'images/*.data'))
result += genExternOfDefaultTheme(files)
2020-03-18 21:36:24 +08:00
result += '#endif /*WITH_STB_IMAGE*/\n'
2019-03-31 11:13:43 +08:00
result += "#ifdef WITH_VGCANVAS\n"
files = glob.glob(joinPath(OUTPUT_DIR, 'images/*.bsvg'))
result += genIncludes(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'images/*.bsvg'))
result += genExternOfDefaultTheme(files)
2020-03-18 21:36:24 +08:00
result += '#endif /*WITH_VGCANVAS*/\n'
2019-03-31 11:13:43 +08:00
2020-03-16 21:19:20 +08:00
result += "#if defined(WITH_STB_FONT) || defined(WITH_FT_FONT)\n"
files = glob.glob(joinPath(OUTPUT_DIR, 'fonts/*.res'))
2019-03-31 11:13:43 +08:00
result += genIncludes(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'fonts/*.res'))
result += genExternOfDefaultTheme(files)
result += "#else/*WITH_STB_FONT or WITH_FT_FONT*/\n"
2019-03-31 11:13:43 +08:00
files = glob.glob(joinPath(OUTPUT_DIR, 'fonts/*.data'))
result += genIncludes(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'fonts/*.data'))
result += genExternOfDefaultTheme(files)
2020-03-18 21:36:24 +08:00
result += '#endif /*WITH_STB_FONT or WITH_FT_FONT*/\n'
2020-03-16 21:19:20 +08:00
files = glob.glob(joinPath(OUTPUT_DIR, 'scripts/*.res'))
result += genIncludes(files)
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'scripts/*.res'))
result += genExternOfDefaultTheme(files)
2019-03-31 11:13:43 +08:00
2020-03-18 21:36:24 +08:00
result += '#endif /*WITH_FS_RES*/\n'
2019-03-31 11:13:43 +08:00
result += '\n'
2020-03-16 21:19:20 +08:00
result += 'ret_t assets_init_' + THEME + '(void) {\n'
2019-09-13 07:55:19 +08:00
result += ' assets_manager_t* am = assets_manager();\n\n'
2019-03-31 11:13:43 +08:00
result += ''
result += '#ifdef WITH_FS_RES\n'
2019-09-13 07:55:19 +08:00
result += ' assets_manager_preload(am, ASSET_TYPE_FONT, "default");\n'
result += ' assets_manager_preload(am, ASSET_TYPE_STYLE, "default");\n'
2019-03-31 11:13:43 +08:00
result += '#else\n'
2020-03-16 21:19:20 +08:00
files = glob.glob(joinPath(OUTPUT_DIR, 'strings/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'styles/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'ui/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'xml/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'data/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'images/*.data'))
result += gen_add_assets(files)
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'strings/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'styles/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'ui/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'xml/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'data/*.data')) \
+ glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'images/*.data'))
result += gen_add_assets_of_default_theme(files)
result += "#if defined(WITH_STB_FONT) || defined(WITH_FT_FONT)\n"
files = glob.glob(joinPath(OUTPUT_DIR, 'fonts/*.res'))
result += gen_add_assets(files)
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'fonts/*.res'))
result += gen_add_assets_of_default_theme(files)
2020-03-18 21:36:24 +08:00
result += "#else /*WITH_STB_FONT or WITH_FT_FONT*/\n"
2020-03-16 21:19:20 +08:00
files = glob.glob(joinPath(OUTPUT_DIR, 'fonts/*.data'))
2019-03-31 11:13:43 +08:00
result += gen_add_assets(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'fonts/*.data'))
result += gen_add_assets_of_default_theme(files)
2020-03-18 21:36:24 +08:00
result += '#endif /*WITH_STB_FONT or WITH_FT_FONT*/\n'
2019-03-31 11:13:43 +08:00
result += "#ifdef WITH_VGCANVAS\n"
files = glob.glob(joinPath(OUTPUT_DIR, 'images/*.bsvg'))
result += gen_add_assets(files)
2020-03-16 21:19:20 +08:00
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'images/*.bsvg'))
result += gen_add_assets_of_default_theme(files)
2020-03-18 21:36:24 +08:00
result += '#endif /*WITH_VGCANVAS*/\n'
2019-03-31 11:13:43 +08:00
2020-03-16 21:19:20 +08:00
files = glob.glob(joinPath(OUTPUT_DIR, 'scripts/*.res'))
result += gen_add_assets(files)
if not IS_DEFAULT_THEME:
files = glob.glob(joinPath(DEFAULT_THEME_OUTPUT_DIR, 'scripts/*.res'))
result += gen_add_assets_of_default_theme(files)
2019-03-31 11:13:43 +08:00
result += '#endif\n'
result += '\n'
result += ' tk_init_assets();\n'
result += ' return RET_OK;\n'
result += '}\n'
2020-03-16 21:19:20 +08:00
2020-03-18 21:36:24 +08:00
writeResult(ASSET_C.replace('.c', '_' + THEME + '.inc'), result)
2019-03-31 11:13:43 +08:00
def gen_res_web_c():
result = '#include "awtk.h"\n'
result += '#include "base/assets_manager.h"\n'
files = glob.glob(joinPath(OUTPUT_DIR, 'images/*.bsvg')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'strings/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'styles/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'ui/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'xml/*.data')) \
+ glob.glob(joinPath(OUTPUT_DIR, 'data/*.data'))
result += genIncludes(files)
result += '\n'
2020-03-18 21:36:24 +08:00
result += 'ret_t assets_init(void) {\n'
2019-09-13 07:55:19 +08:00
result += ' assets_manager_t* am = assets_manager();\n\n'
2019-03-31 11:13:43 +08:00
result += ''
result += gen_add_assets(files)
result += '\n'
result += ' tk_init_assets();\n'
result += ' return RET_OK;\n'
2020-04-08 12:23:29 +08:00
result += '}\n\n'
result += 'bool_t assets_has_theme(const char* name) {\n'
result += ' return_value_if_fail(name != NULL, FALSE);\n\n'
result += ' if (tk_str_eq(name, "default")) {\n'
result += ' return TRUE;\n'
result += ' } else {\n'
result += ' return FALSE;\n'
result += ' }\n}\n\n'
result += 'ret_t assets_set_global_theme(const char* name) {\n'
result += ' log_debug(\"not support to change theme.\\n\");\n'
result += ' return RET_NOT_IMPL;\n'
2019-03-31 11:13:43 +08:00
result += '}\n'
2019-04-14 19:14:16 +08:00
2020-03-16 21:19:20 +08:00
writeResult(ASSET_C.replace('.c', '_web.c'), result)
2018-04-27 10:02:25 +08:00
2019-10-19 07:09:19 +08:00
2019-04-07 18:31:16 +08:00
def gen_res_json_one(res_type, files):
2019-10-19 07:09:19 +08:00
result = "\n " + res_type + ': [\n'
2019-04-07 18:31:16 +08:00
for f in files:
2019-04-15 11:29:36 +08:00
uri = f.replace(os.getcwd(), "")[1:]
2019-10-19 07:09:19 +08:00
uri = uri.replace('\\', '/')
2019-04-07 18:31:16 +08:00
filename, extname = os.path.splitext(uri)
basename = os.path.basename(filename)
2019-10-19 07:09:19 +08:00
result = result + ' {name:"' + basename + '\", uri:"' + uri
2019-04-07 19:19:50 +08:00
if res_type == 'image' and extname != '.svg' and extname != '.bsvg':
img = Image.open(f)
w, h = img.size
2019-10-19 07:09:19 +08:00
result = result + '", w:' + str(w) + ', h:' + str(h) + '},\n'
2019-04-07 19:19:50 +08:00
else:
2019-10-19 07:09:19 +08:00
result = result + '"},\n'
2019-04-07 18:31:16 +08:00
result = result + ' ],'
2019-10-19 07:09:19 +08:00
return result
2019-04-07 18:31:16 +08:00
def gen_res_json():
2019-10-19 07:09:19 +08:00
result = 'const g_awtk_assets = {'
result = result + \
gen_res_json_one("image", glob.glob(
joinPath(INPUT_DIR, 'images/*/*.*')))
result = result + \
gen_res_json_one("ui", glob.glob(joinPath(INPUT_DIR, 'ui/*.bin')))
result = result + \
gen_res_json_one("style", glob.glob(
joinPath(INPUT_DIR, 'styles/*.bin')))
result = result + \
gen_res_json_one("string", glob.glob(
joinPath(INPUT_DIR, 'strings/*.bin')))
result = result + \
gen_res_json_one("xml", glob.glob(joinPath(INPUT_DIR, 'xml/*.xml')))
result = result + \
gen_res_json_one("data", glob.glob(joinPath(INPUT_DIR, 'data/*.*')))
result = result + \
gen_res_json_one("script", glob.glob(
joinPath(INPUT_DIR, 'scripts/*.*')))
result = result + \
gen_res_json_one("font", glob.glob(joinPath(INPUT_DIR, 'fonts/*.ttf')))
result = result + '\n};'
2019-04-07 18:31:16 +08:00
2020-03-16 21:19:20 +08:00
writeResult(ASSET_C.replace('.c', '_web.js'), result)
2019-10-19 07:09:19 +08:00
2019-04-03 09:28:49 +08:00
2020-03-16 21:19:20 +08:00
def genAssetC(themes, asset_c):
result = '#include "awtk.h"\n'
result += '#include "base/assets_manager.h"\n\n'
for theme in themes:
2020-03-18 21:36:24 +08:00
result += '#include "assets_'+theme+'.inc"\n'
2020-03-16 21:19:20 +08:00
result += '\n'
2020-03-18 21:36:24 +08:00
result += '#ifndef APP_THEME\n'
result += '#define APP_THEME "default"\n'
result += '#endif /*APP_THEME*/\n\n'
2020-03-19 16:44:31 +08:00
2020-04-02 16:55:11 +08:00
result += 'bool_t assets_has_theme(const char* name) {\n'
2020-03-19 16:44:31 +08:00
result += ' return_value_if_fail(name != NULL, FALSE);\n\n'
result += ' '
for theme in themes:
result += 'if (tk_str_eq(name, "'+theme+'")) {\n'
result += ' return TRUE;\n'
result += ' } else '
result += '{\n'
result += ' return FALSE;\n }\n}\n\n'
result += 'static ret_t assets_init_internal(const char* theme) {\n'
2020-03-16 21:19:20 +08:00
result += ' assets_manager_t* am = assets_manager();\n'
result += ' return_value_if_fail(theme != NULL && am != NULL, RET_BAD_PARAMS);\n\n'
result += ' assets_manager_set_theme(am, theme);\n\n'
result += ' '
for theme in themes:
result += 'if (tk_str_eq(theme, "'+theme+'")) {\n'
result += ' return assets_init_'+theme+'();\n'
result += ' } else '
2020-03-18 21:36:24 +08:00
result += '{\n'
2020-03-16 21:19:20 +08:00
result += ' log_debug(\"%s not support.\\n\", theme);\n'
2020-03-18 21:36:24 +08:00
result += ' return RET_NOT_IMPL;\n }\n}\n\n'
result += 'ret_t assets_init(void) {\n'
result += ' return assets_init_internal(APP_THEME);\n}\n\n'
2020-03-19 16:44:31 +08:00
result += 'static ret_t widget_set_theme_without_file_system(widget_t* widget, const char* name) {\n'
2020-03-18 21:36:24 +08:00
result += '#ifndef WITH_FS_RES\n'
result += ' const asset_info_t* info = NULL;\n'
result += ' event_t e = event_init(EVT_THEME_CHANGED, NULL);\n'
result += ' widget_t* wm = widget_get_window_manager(widget);\n'
result += ' font_manager_t* fm = widget_get_font_manager(widget);\n'
result += ' image_manager_t* imm = widget_get_image_manager(widget);\n'
result += ' assets_manager_t* am = widget_get_assets_manager(widget);\n'
result += ' locale_info_t* locale_info = widget_get_locale_info(widget);\n\n'
2020-03-19 16:44:31 +08:00
result += ' return_value_if_fail(am != NULL && name != NULL, RET_BAD_PARAMS);\n'
result += ' return_value_if_fail(assets_has_theme(name), RET_BAD_PARAMS);\n\n'
2020-03-18 21:36:24 +08:00
result += ' font_manager_unload_all(fm);\n'
result += ' image_manager_unload_all(imm);\n'
result += ' assets_manager_clear_all(am);\n'
result += ' widget_reset_canvas(widget);\n\n'
result += ' assets_init_internal(name);\n'
result += ' locale_info_reload(locale_info);\n\n'
result += ' info = assets_manager_ref(am, ASSET_TYPE_STYLE, "default");\n'
result += ' theme_init(theme(), info->data);\n'
result += ' assets_manager_unref(assets_manager(), info);\n\n'
result += ' widget_dispatch(wm, &e);\n'
result += ' widget_invalidate_force(wm, NULL);\n\n'
result += ' log_debug("theme changed: %s\\n", name);\n\n'
result += ' return RET_OK;\n'
2020-03-19 16:44:31 +08:00
result += '#else /*WITH_FS_RES*/\n'
2020-03-18 21:36:24 +08:00
result += ' return RET_NOT_IMPL;\n'
result += '#endif /*WITH_FS_RES*/\n'
2020-03-19 16:44:31 +08:00
result += '}\n\n'
result += 'ret_t assets_set_global_theme(const char* name) {\n'
result += '#ifdef WITH_FS_RES\n'
result += ' return widget_set_theme(window_manager(), name);\n'
result += '#else /*WITH_FS_RES*/\n'
result += ' return widget_set_theme_without_file_system(window_manager(), name);\n'
result += '#endif /*WITH_FS_RES*/\n'
2020-03-18 21:36:24 +08:00
result += '}\n'
2020-03-16 21:19:20 +08:00
writeResult(asset_c, result)
2020-04-30 09:15:22 +08:00
def init(awtk_root, assets_root, theme, asset_c, is_file_excluded = None):
2019-03-31 11:13:43 +08:00
global DPI
2019-10-19 07:09:19 +08:00
global THEME
2019-03-31 11:13:43 +08:00
global ASSET_C
global BIN_DIR
global ASSETS_ROOT
2020-03-16 21:19:20 +08:00
global DEFAULT_THEME_ASSETS_ROOT
2019-03-31 11:13:43 +08:00
global AWTK_ROOT
global INPUT_DIR
global OUTPUT_DIR
2020-03-16 21:19:20 +08:00
global DEFAULT_THEME_OUTPUT_DIR
2019-03-31 11:13:43 +08:00
global IMAGEGEN_OPTIONS
2020-03-16 21:19:20 +08:00
global IS_DEFAULT_THEME
2020-04-30 09:15:22 +08:00
global IS_FILE_EXCLUDED
2019-03-31 11:13:43 +08:00
2020-04-30 09:15:22 +08:00
IS_FILE_EXCLUDED = is_file_excluded
2020-03-16 21:19:20 +08:00
IS_DEFAULT_THEME = theme == 'default'
2019-10-19 07:09:19 +08:00
THEME = theme
2019-03-31 11:13:43 +08:00
ASSET_C = asset_c
AWTK_ROOT = awtk_root
2019-10-19 07:09:19 +08:00
ASSETS_ROOT = joinPath(assets_root, theme)
2020-03-16 21:19:20 +08:00
DEFAULT_THEME_ASSETS_ROOT = joinPath(assets_root, 'default')
2019-03-31 11:13:43 +08:00
BIN_DIR = joinPath(AWTK_ROOT, 'bin')
INPUT_DIR = joinPath(ASSETS_ROOT, 'raw')
OUTPUT_DIR = joinPath(ASSETS_ROOT, 'inc')
2020-03-16 21:19:20 +08:00
DEFAULT_THEME_OUTPUT_DIR = joinPath(DEFAULT_THEME_ASSETS_ROOT, 'inc')
2019-10-19 07:09:19 +08:00
print(INPUT_DIR)
print(OUTPUT_DIR)
2018-11-02 11:51:14 +08:00
def dumpArgs():
2019-10-19 07:09:19 +08:00
print('-------------------------------------------------------')
print('DPI='+DPI)
print('THEME='+THEME)
print('IMAGEGEN_OPTIONS='+IMAGEGEN_OPTIONS)
2019-03-31 11:13:43 +08:00
print('AWTK_ROOT='+AWTK_ROOT)
2019-10-19 07:09:19 +08:00
print('ASSETS_ROOT='+ASSETS_ROOT)
2019-03-31 11:13:43 +08:00
print('INPUT_DIR='+INPUT_DIR)
print('OUTPUT_DIR='+OUTPUT_DIR)
print('ASSET_C='+ASSET_C)
print('BIN_DIR='+BIN_DIR)
2018-11-02 11:51:14 +08:00
def updateRes():
2019-03-31 11:13:43 +08:00
global ACTION
if ACTION == 'all':
removeDir(OUTPUT_DIR)
2020-04-30 09:15:22 +08:00
prepare()
gen_res_all()
gen_res_c()
2019-03-31 11:13:43 +08:00
elif ACTION == 'clean':
cleanRes()
elif ACTION == 'web':
gen_res_web_c()
2019-04-07 18:31:16 +08:00
elif ACTION == 'json':
gen_res_json()
2019-03-31 11:13:43 +08:00
elif ACTION == 'string':
prepare()
gen_res_all_string()
gen_res_c()
elif ACTION == "font":
prepare()
gen_res_all_font()
gen_res_c()
elif ACTION == "script":
prepare()
gen_res_all_script()
gen_res_c()
elif ACTION == 'image':
prepare()
gen_res_all_image()
gen_res_c()
elif ACTION == 'ui':
prepare()
gen_res_all_ui()
gen_res_c()
elif ACTION == 'style':
prepare()
gen_res_all_style()
gen_res_c()
elif ACTION == 'data':
prepare()
gen_res_all_data()
gen_res_c()
elif ACTION == 'xml':
prepare()
gen_res_all_xml()
gen_res_c()
elif ACTION == 'pinyin':
prepare()
gen_gpinyin()
gen_res_c()
2020-04-30 09:15:22 +08:00
elif ACTION == 'assets.c':
gen_res_c()
2019-03-31 11:13:43 +08:00
dumpArgs()
2018-11-02 11:51:14 +08:00
2018-11-07 18:17:00 +08:00
def cleanRes():
2019-03-31 11:13:43 +08:00
print("==================================================================")
resFiles = glob.glob(joinPath(INPUT_DIR, '*/*.bin')) + \
glob.glob(joinPath(INPUT_DIR, '*/*/*.bin'))
for f in resFiles:
print("remove: " + f)
os.remove(f)
resFiles = glob.glob(joinPath(INPUT_DIR, '*/*.bin')) + \
glob.glob(joinPath(INPUT_DIR, '*/*/*.bsvg'))
for f in resFiles:
print("remove: " + f)
os.remove(f)
removeDir(OUTPUT_DIR)
print("==================================================================")
2018-11-07 18:17:00 +08:00
2018-11-02 11:51:14 +08:00
2019-03-31 11:13:43 +08:00
def showUsage():
global DPI
global ACTION
global IMAGEGEN_OPTIONS
2019-08-13 10:37:40 +08:00
global FONT_OPTIONS
2020-04-30 09:15:22 +08:00
args = ' action[clean|web|json|all|font|image|ui|style|string|script|data|xml|assets.c] dpi[x1|x2] image_options[rgba|bgra+bgr565|mono]'
2019-03-31 11:13:43 +08:00
if len(sys.argv) == 1:
print('=========================================================')
2019-11-20 14:39:40 +08:00
print('Usage: python '+sys.argv[0] + args)
2019-03-31 11:13:43 +08:00
print('Example:')
2019-11-20 14:39:40 +08:00
print('python ' + sys.argv[0] + ' all')
print('python ' + sys.argv[0] + ' clean')
print('python ' + sys.argv[0] + ' style')
print('python ' + sys.argv[0] + ' all x1 bgra+bgr565')
2019-03-31 11:13:43 +08:00
print('=========================================================')
sys.exit(0)
2018-11-02 11:51:14 +08:00
else:
2019-03-31 11:13:43 +08:00
ACTION = sys.argv[1]
if len(sys.argv) > 2:
DPI = sys.argv[2]
else:
DPI = 'x1'
2018-11-02 11:51:14 +08:00
2019-03-31 11:13:43 +08:00
if len(sys.argv) > 3:
IMAGEGEN_OPTIONS = sys.argv[3]
else:
IMAGEGEN_OPTIONS = 'bgra+bgr565'
2018-11-02 11:51:14 +08:00
2019-08-13 10:37:40 +08:00
if IMAGEGEN_OPTIONS == 'mono':
FONT_OPTIONS = 'mono'
2018-11-02 11:51:14 +08:00
2019-10-19 07:09:19 +08:00
2019-03-31 11:13:43 +08:00
showUsage()