awtk/scripts/update_res_common.py

371 lines
10 KiB
Python
Raw Normal View History

2018-04-27 10:02:25 +08:00
#!/usr/bin/python
import os
2018-11-02 11:51:14 +08:00
import sys
import copy
2018-04-27 10:02:25 +08:00
import glob
import shutil
import platform
2018-11-02 11:51:14 +08:00
###########################
DPI=''
ACTION='all'
ASSET_C=''
BIN_DIR=''
ASSETS_ROOT=''
AWTK_ROOT=''
INPUT_DIR=''
OUTPUT_DIR=''
IMAGEGEN_OPTIONS=''
###########################
2018-04-27 10:02:25 +08:00
def joinPath(root, subdir):
return os.path.normpath(os.path.join(root, subdir))
2018-04-29 18:06:40 +08:00
OS_NAME=platform.system()
def toExe(name):
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():
os.system('scons')
def removeDir(path):
if os.path.isdir(path):
print('rmdir:' + path);
shutil.rmtree(path)
2018-11-02 11:51:14 +08:00
def prepareOutputDir(name):
fullpath=joinPath(OUTPUT_DIR, name)
if os.path.exists(fullpath):
print(fullpath+" exist.")
else:
os.makedirs(fullpath);
2018-04-27 10:02:25 +08:00
def prepare():
2018-11-02 11:51:14 +08:00
prepareOutputDir('styles')
prepareOutputDir('images')
prepareOutputDir('fonts')
prepareOutputDir('strings')
prepareOutputDir('ui')
2018-04-27 10:02:25 +08:00
2018-11-02 11:51:14 +08:00
def execCmd(cmd):
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):
execCmd(toExe('themegen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(OUTPUT_DIR, inc))
def themegen_bin(raw, bin):
2018-11-02 11:51:14 +08:00
execCmd(toExe('themegen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(INPUT_DIR, bin) + ' bin')
2018-05-04 14:52:15 +08:00
def strgen(raw, inc):
2018-11-02 11:51:14 +08:00
execCmd(toExe('strgen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(OUTPUT_DIR, inc))
2018-05-04 14:52:15 +08:00
def strgen_bin(raw, bin):
2018-11-02 11:51:14 +08:00
execCmd(toExe('strgen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(INPUT_DIR, bin) + ' bin')
2018-05-04 14:52:15 +08:00
2018-04-27 10:02:25 +08:00
def resgen(raw, inc):
2018-11-02 11:51:14 +08:00
execCmd(toExe('resgen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(OUTPUT_DIR, inc))
2018-04-27 10:02:25 +08:00
def fontgen(raw, text, inc, size):
2018-11-02 11:51:14 +08:00
execCmd(toExe('fontgen') + ' ' + joinPath(INPUT_DIR, raw) + ' ' + joinPath(INPUT_DIR, text) +' ' + joinPath(OUTPUT_DIR, inc) + ' ' + str(size))
2018-04-27 10:02:25 +08:00
def imagegen(raw, inc):
2018-11-02 11:51:14 +08:00
execCmd(toExe('imagegen') + ' ' + raw + ' ' + inc + ' ' + IMAGEGEN_OPTIONS)
2018-06-08 19:19:06 +08:00
inc=inc.replace('.data', '.res')
resgen(raw, inc)
2018-04-27 10:02:25 +08:00
2018-11-20 16:25:28 +08:00
def svggen(raw, inc, bin):
execCmd(toExe('bsvggen') + ' ' + raw + ' ' + inc)
execCmd(toExe('bsvggen') + ' ' + raw + ' ' + bin + ' bin')
2018-04-27 10:02:25 +08:00
def xml_to_ui(raw, inc):
2018-11-02 11:51:14 +08:00
execCmd(toExe('xml_to_ui') + ' ' + raw + ' ' + inc)
2018-04-27 10:02:25 +08:00
def xml_to_ui_bin(raw, bin):
2018-11-02 11:51:14 +08:00
execCmd(toExe('xml_to_ui') + ' ' + raw + ' ' + bin + ' bin')
2018-11-02 11:51:14 +08:00
def gen_res_all_style():
2018-08-19 19:12:35 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'styles/*.xml')):
2018-06-15 19:12:34 +08:00
inc=copy.copy(f);
raw=copy.copy(f);
bin=copy.copy(f);
inc=inc.replace('.xml', '.data')
inc=inc.replace(INPUT_DIR, OUTPUT_DIR)
themegen(raw, inc)
bin=bin.replace('.xml', '.bin')
themegen_bin(raw, bin)
2018-04-27 10:02:25 +08:00
2018-11-20 16:25:28 +08:00
def gen_res_svg():
2018-11-21 09:34:23 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'images/svg/*.svg')):
2018-11-20 16:25:28 +08:00
inc=copy.copy(f);
bin=copy.copy(f);
raw=copy.copy(f);
basename=os.path.basename(inc);
inc=joinPath(OUTPUT_DIR, 'images/'+basename);
2018-11-21 09:34:23 +08:00
inc=inc.replace('.svg', '.bsvg')
2018-11-20 16:25:28 +08:00
bin=bin.replace('.svg', '.bsvg')
svggen(raw, inc, bin)
def gen_res_png_jpg():
2018-04-27 10:02:25 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'images/'+DPI+'/*.*')):
inc=copy.copy(f);
raw=copy.copy(f);
basename=os.path.basename(inc);
inc=joinPath(OUTPUT_DIR, 'images/'+basename);
inc=inc.replace('.png', '.data')
inc=inc.replace('.jpg', '.data')
2018-11-25 18:57:44 +08:00
inc=inc.replace('.gif', '.data')
2018-04-27 10:02:25 +08:00
imagegen(raw, inc)
2018-11-20 16:25:28 +08:00
def gen_res_all_image():
gen_res_png_jpg()
2018-11-21 09:34:23 +08:00
gen_res_svg()
2018-11-20 16:25:28 +08:00
2018-11-02 11:51:14 +08:00
def gen_res_all_ui():
2018-04-27 10:02:25 +08:00
for f in glob.glob(joinPath(INPUT_DIR, 'ui/*.xml')):
inc=copy.copy(f);
raw=copy.copy(f);
bin=copy.copy(f);
2018-04-27 10:02:25 +08:00
inc=inc.replace('.xml', '.data')
inc=inc.replace(INPUT_DIR, OUTPUT_DIR)
xml_to_ui(raw, inc)
bin=bin.replace('.xml', '.bin')
xml_to_ui_bin(raw, bin)
2018-04-27 10:02:25 +08:00
2018-11-02 11:51:14 +08:00
def gen_res_all_font():
for f in glob.glob(joinPath(INPUT_DIR, 'fonts/*.ttf')):
res=copy.copy(f);
raw=copy.copy(f);
res=res.replace(INPUT_DIR, '.')
res=res.replace('.ttf', '.res');
raw=raw.replace(INPUT_DIR, '.')
resgen(raw, res)
2018-07-05 15:22:04 +08:00
fontgen('fonts/default.ttf', 'fonts/text.txt', 'fonts/default.data', 18);
2018-11-02 11:51:14 +08:00
def gen_res_all_string():
2018-07-05 15:22:04 +08:00
strgen('strings/strings.xml', 'strings');
strgen_bin('strings/strings.xml', 'strings');
2018-07-22 14:22:09 +08:00
def gen_gpinyin():
2018-11-02 11:51:14 +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'))
def gen_res_all():
gen_res_all_string()
gen_res_all_font()
gen_res_all_image()
gen_res_all_ui()
gen_res_all_style()
2018-04-27 10:02:25 +08:00
def writeResult(str):
2018-08-24 07:45:37 +08:00
fd = os.open(ASSET_C, os.O_RDWR|os.O_CREAT|os.O_TRUNC)
2018-04-27 10:02:25 +08:00
os.write(fd, str)
os.close(fd)
2018-06-08 19:19:06 +08:00
def genIncludes(files):
str1 = ""
2018-04-27 10:02:25 +08:00
for f in files:
incf = copy.copy(f);
2018-11-02 12:42:58 +08:00
incf=incf.replace(os.path.dirname(ASSETS_ROOT), ".");
2018-04-27 10:02:25 +08:00
incf=incf.replace('\\', '/');
incf=incf.replace('./', '');
2018-06-08 19:19:06 +08:00
str1 += '#include "'+incf+'"\n'
return str1
2018-11-21 09:34:23 +08:00
def gen_add_assets(files):
result = "";
for f in files:
incf = copy.copy(f);
basename = incf.replace(OUTPUT_DIR, '.');
basename = basename.replace('\\', '/');
basename = basename.replace('/fonts/', '/font/');
basename = basename.replace('/images/', '/image/');
basename = basename.replace('/styles/', '/style/');
basename = basename.replace('./', '');
basename = basename.replace('/', '_');
basename = basename.replace('.data', '');
basename = basename.replace('.bsvg', '');
result += ' assets_manager_add(rm, '+basename+');\n'
return result;
2018-06-08 19:19:06 +08:00
def gen_res_c():
2018-08-06 12:21:12 +08:00
result = '#include "awtk.h"\n'
2018-08-24 07:45:37 +08:00
result += '#include "base/assets_manager.h"\n'
2018-06-08 19:19:06 +08:00
result += '#ifndef WITH_FS_RES\n'
2018-06-09 16:36:53 +08:00
files=glob.glob(joinPath(OUTPUT_DIR, 'strings/*.data')) \
2018-08-19 19:12:35 +08:00
+ glob.glob(joinPath(OUTPUT_DIR, 'styles/*.data')) \
2018-06-08 19:19:06 +08:00
+ glob.glob(joinPath(OUTPUT_DIR, 'ui/*.data'))
result += genIncludes(files);
result += "#ifdef WITH_STB_IMAGE\n"
files=glob.glob(joinPath(OUTPUT_DIR, 'images/*.res'))
result += genIncludes(files)
result += "#else\n"
files=glob.glob(joinPath(OUTPUT_DIR, 'images/*.data'))
result += genIncludes(files)
result += '#endif/*WITH_STB_IMAGE*/\n'
2018-11-21 09:34:23 +08:00
result += "#ifdef WITH_VGCANVAS\n"
files=glob.glob(joinPath(OUTPUT_DIR, 'images/*.bsvg'))
result += genIncludes(files)
result += '#endif/*WITH_VGCANVAS*/\n'
2018-06-08 19:19:06 +08:00
2018-12-26 18:10:38 +08:00
result += "#if defined(WITH_STB_FONT) || defined(WITH_FT_FONT)\n"
2018-06-24 18:27:27 +08:00
result += "#ifdef WITH_MINI_FONT\n"
files=glob.glob(joinPath(OUTPUT_DIR, 'fonts/default.mini.res'))
2018-06-09 16:36:53 +08:00
result += genIncludes(files)
2018-06-24 18:27:27 +08:00
result += "#else/*WITH_MINI_FONT*/\n"
files=glob.glob(joinPath(OUTPUT_DIR, 'fonts/default.res'))
result += genIncludes(files)
result += '#endif/*WITH_MINI_FONT*/\n'
2018-12-26 18:10:38 +08:00
result += "#else/*WITH_STB_FONT or WITH_FT_FONT*/\n"
2018-06-09 16:36:53 +08:00
files=glob.glob(joinPath(OUTPUT_DIR, 'fonts/*.data'))
result += genIncludes(files)
2018-12-26 18:10:38 +08:00
result += '#endif/*WITH_STB_FONT or WITH_FT_FONT*/\n'
2018-06-09 16:36:53 +08:00
2018-06-08 19:19:06 +08:00
result += '#endif/*WITH_FS_RES*/\n'
2018-04-27 10:02:25 +08:00
result += '\n';
2018-08-24 07:45:37 +08:00
result += 'ret_t assets_init(void) {\n'
result += ' assets_manager_t* rm = assets_manager();\n\n'
2018-04-27 10:02:25 +08:00
result += ''
result += '#ifdef WITH_FS_RES\n'
2018-08-24 07:45:37 +08:00
result += ' assets_manager_load(rm, ASSET_TYPE_STYLE, "default");\n'
result += ' assets_manager_load(rm, ASSET_TYPE_FONT, "default");\n'
result += '#else\n'
2018-06-08 19:19:06 +08:00
files=glob.glob(joinPath(OUTPUT_DIR, '**/*.data'))
2018-11-21 09:34:23 +08:00
result += gen_add_assets(files);
result += "#ifdef WITH_VGCANVAS\n"
files=glob.glob(joinPath(OUTPUT_DIR, 'images/*.bsvg'))
result += gen_add_assets(files);
result += '#endif/*WITH_VGCANVAS*/\n'
result += '#endif\n'
2018-04-27 10:02:25 +08:00
result += '\n'
2018-08-24 07:45:37 +08:00
result += ' tk_init_assets();\n'
2018-04-27 10:02:25 +08:00
result += ' return RET_OK;\n'
result += '}\n'
writeResult(result);
2018-11-02 11:51:14 +08:00
def gen_res():
2018-04-27 10:02:25 +08:00
prepare()
2018-11-02 11:51:14 +08:00
gen_res_all()
2018-04-27 10:02:25 +08:00
gen_res_c()
2018-11-02 11:51:14 +08:00
def init(awtk_root, assets_root, asset_c):
global DPI
global ASSET_C
global BIN_DIR
global ASSETS_ROOT
global AWTK_ROOT
global INPUT_DIR
global OUTPUT_DIR
global IMAGEGEN_OPTIONS
ASSET_C=asset_c
AWTK_ROOT=awtk_root
ASSETS_ROOT=assets_root
BIN_DIR=joinPath(AWTK_ROOT, 'bin')
INPUT_DIR=joinPath(ASSETS_ROOT, 'raw')
OUTPUT_DIR=joinPath(ASSETS_ROOT, 'inc')
def dumpArgs():
print('ASSETS_ROOT='+ASSETS_ROOT)
print('AWTK_ROOT='+AWTK_ROOT)
print('INPUT_DIR='+INPUT_DIR)
print('OUTPUT_DIR='+OUTPUT_DIR)
print('ASSET_C='+ASSET_C)
print('DPI='+DPI)
print('IMAGEGEN_OPTIONS='+IMAGEGEN_OPTIONS)
print('BIN_DIR='+BIN_DIR)
def updateRes():
global ACTION
if ACTION=='all':
removeDir(OUTPUT_DIR)
gen_res()
elif ACTION=='clean':
2018-11-07 18:17:00 +08:00
cleanRes();
2018-11-02 11:51:14 +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=='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=='pinyin':
prepare()
gen_gpinyin()
gen_res_c()
dumpArgs()
2018-11-07 18:17:00 +08:00
def cleanRes():
print("==================================================================")
resFiles=glob.glob(joinPath(INPUT_DIR, '*/*.bin')) + glob.glob(joinPath(INPUT_DIR, '*/*/*.bin'))
2018-11-20 16:25:28 +08:00
for f in resFiles:
print("remove: " + f)
os.remove(f)
resFiles=glob.glob(joinPath(INPUT_DIR, '*/*.bin')) + glob.glob(joinPath(INPUT_DIR, '*/*/*.bsvg'))
2018-11-07 18:17:00 +08:00
for f in resFiles:
print("remove: " + f)
os.remove(f)
removeDir(OUTPUT_DIR)
print("==================================================================")
2018-11-02 11:51:14 +08:00
def showUsage():
global DPI
global ACTION
global IMAGEGEN_OPTIONS
args=' action[clean|all|font|image|ui|style|string] dpi[x1|x2] image_options[rgba|bgra+bgr565]'
if len(sys.argv) == 1:
print('=========================================================');
print('Usage: '+sys.argv[0] + args)
print('Example:')
print(sys.argv[0] + ' all')
print(sys.argv[0] + ' clean')
print(sys.argv[0] + ' style')
print(sys.argv[0] + ' all x1 bgra+bgr565')
print('=========================================================');
sys.exit(0)
else:
ACTION=sys.argv[1];
if len(sys.argv) > 2:
DPI=sys.argv[2]
else:
DPI='x1'
if len(sys.argv) > 3:
IMAGEGEN_OPTIONS=sys.argv[3]
else:
IMAGEGEN_OPTIONS='bgra+bgr565'
showUsage()