2018-02-21 19:36:38 +08:00
|
|
|
/**
|
|
|
|
* File: xml_gen.c
|
2018-05-15 09:31:58 +08:00
|
|
|
* Author: AWTK Develop Team
|
2018-02-21 19:36:38 +08:00
|
|
|
* Brief: generate theme date from xml
|
|
|
|
*
|
2018-05-08 10:22:32 +08:00
|
|
|
* Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
2018-02-21 19:36:38 +08:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* License file for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History:
|
|
|
|
* ================================================================
|
|
|
|
* 2018-01-19 Li XianJing <xianjimli@hotmail.com> adapted from ftk.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-03-18 11:29:31 +08:00
|
|
|
#include "theme_gen.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
#include "base/enums.h"
|
2018-02-22 08:36:54 +08:00
|
|
|
#include "base/theme.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
#include "base/widget.h"
|
|
|
|
#include "common/utils.h"
|
2018-03-18 11:29:31 +08:00
|
|
|
#include "xml_theme_gen.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
#include "xml/xml_parser.h"
|
2018-12-15 17:22:05 +08:00
|
|
|
#include "tkc/color_parser.h"
|
2018-08-24 07:45:37 +08:00
|
|
|
#include "base/assets_manager.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
typedef struct _xml_builder_t {
|
|
|
|
XmlBuilder builder;
|
|
|
|
ThemeGen gen;
|
2018-03-31 19:48:49 +08:00
|
|
|
Style widget_style;
|
|
|
|
Style share_style;
|
|
|
|
|
2018-03-31 13:48:18 +08:00
|
|
|
uint16_t level;
|
2018-07-17 11:27:14 +08:00
|
|
|
string style_name;
|
2018-06-25 17:27:17 +08:00
|
|
|
string widget_type;
|
2018-02-21 19:36:38 +08:00
|
|
|
} xml_builder_t;
|
|
|
|
|
|
|
|
static color_t parse_color(const char* name) {
|
2018-12-27 17:39:26 +08:00
|
|
|
color_t c = color_parse(name);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-06-29 19:12:04 +08:00
|
|
|
static uint32_t to_border(const char* value) {
|
|
|
|
uint32_t border = 0;
|
|
|
|
if (strstr(value, "left")) {
|
|
|
|
border |= BORDER_LEFT;
|
|
|
|
}
|
|
|
|
if (strstr(value, "right")) {
|
|
|
|
border |= BORDER_RIGHT;
|
|
|
|
}
|
|
|
|
if (strstr(value, "top")) {
|
|
|
|
border |= BORDER_TOP;
|
|
|
|
}
|
|
|
|
if (strstr(value, "bottom")) {
|
|
|
|
border |= BORDER_BOTTOM;
|
|
|
|
}
|
|
|
|
if (strstr(value, "all")) {
|
|
|
|
border |= BORDER_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return border;
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:26:52 +08:00
|
|
|
static uint32_t to_icon_at(const char* value) {
|
|
|
|
uint32_t icon_at = ICON_AT_AUTO;
|
|
|
|
|
|
|
|
if (strstr(value, "left")) {
|
|
|
|
icon_at = ICON_AT_LEFT;
|
|
|
|
}
|
|
|
|
if (strstr(value, "right")) {
|
|
|
|
icon_at = ICON_AT_RIGHT;
|
|
|
|
}
|
|
|
|
if (strstr(value, "top")) {
|
|
|
|
icon_at = ICON_AT_TOP;
|
|
|
|
}
|
|
|
|
if (strstr(value, "bottom")) {
|
|
|
|
icon_at = ICON_AT_BOTTOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon_at;
|
|
|
|
}
|
|
|
|
|
2018-03-31 19:48:49 +08:00
|
|
|
static void xml_gen_style(xml_builder_t* b, Style& s, const char** attrs) {
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
|
|
|
while (attrs[i]) {
|
|
|
|
const char* name = attrs[i];
|
|
|
|
const char* value = attrs[i + 1];
|
|
|
|
|
2018-12-25 18:00:46 +08:00
|
|
|
if (strcmp(name, "name") == 0) {
|
|
|
|
} else if (strcmp(name, "bg_image_draw_type") == 0 || strcmp(name, "fg_image_draw_type") == 0) {
|
|
|
|
const key_type_value_t* dt = image_draw_type_find(value);
|
|
|
|
s.AddInt(name, dt->value);
|
|
|
|
} else if (strcmp(name, "text_align_h") == 0) {
|
|
|
|
const key_type_value_t* dt = align_h_type_find(value);
|
|
|
|
s.AddInt(name, dt->value);
|
|
|
|
} else if (strcmp(name, "text_align_v") == 0) {
|
|
|
|
const key_type_value_t* dt = align_v_type_find(value);
|
|
|
|
s.AddInt(name, dt->value);
|
|
|
|
} else if (strcmp(name, "border") == 0) {
|
|
|
|
uint32_t border = to_border(value);
|
|
|
|
s.AddInt(name, border);
|
|
|
|
} else if (strcmp(name, "icon_at") == 0) {
|
|
|
|
uint32_t icon_at = to_icon_at(value);
|
|
|
|
s.AddInt(name, icon_at);
|
|
|
|
} else if (strstr(name, "color") != NULL) {
|
|
|
|
s.AddInt(name, parse_color(value).color);
|
|
|
|
} else if (strstr(name, "image") != NULL || strstr(name, "name") != NULL ||
|
|
|
|
strstr(name, "icon") != NULL) {
|
|
|
|
s.AddString(name, value);
|
|
|
|
} else {
|
|
|
|
s.AddInt(name, tk_atoi(value));
|
2018-03-31 19:48:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:48:18 +08:00
|
|
|
static void xml_gen_on_widget(xml_builder_t* b, const char* tag, const char** attrs) {
|
2018-03-31 19:48:49 +08:00
|
|
|
b->widget_style.Reset();
|
|
|
|
xml_gen_style(b, b->widget_style, attrs);
|
|
|
|
|
2018-06-25 17:27:17 +08:00
|
|
|
b->widget_type = tag;
|
2018-07-17 11:27:14 +08:00
|
|
|
b->style_name = TK_DEFAULT_STYLE;
|
2018-03-31 13:48:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_style(xml_builder_t* b, const char* tag, const char** attrs) {
|
2018-02-21 19:36:38 +08:00
|
|
|
uint32_t i = 0;
|
2018-07-17 11:27:14 +08:00
|
|
|
b->style_name = TK_DEFAULT_STYLE;
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2018-03-31 13:48:18 +08:00
|
|
|
while (attrs[i]) {
|
|
|
|
const char* name = attrs[i];
|
|
|
|
const char* value = attrs[i + 1];
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2018-03-31 19:48:49 +08:00
|
|
|
if (strcmp(name, "name") == 0) {
|
2018-07-17 11:27:14 +08:00
|
|
|
b->style_name = value;
|
2018-03-31 13:48:18 +08:00
|
|
|
}
|
2018-03-31 19:48:49 +08:00
|
|
|
|
2018-03-31 13:48:18 +08:00
|
|
|
i += 2;
|
|
|
|
}
|
2018-03-31 19:48:49 +08:00
|
|
|
|
|
|
|
b->share_style.Reset();
|
|
|
|
xml_gen_style(b, b->share_style, attrs);
|
2018-03-31 13:48:18 +08:00
|
|
|
}
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2018-03-31 13:48:18 +08:00
|
|
|
static void xml_gen_on_state(xml_builder_t* b, const char* tag, const char** attrs) {
|
2018-12-25 15:33:44 +08:00
|
|
|
const char* state = tag;
|
|
|
|
Style s(b->widget_type, b->style_name, state);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2018-03-31 19:48:49 +08:00
|
|
|
s.Merge(b->widget_style);
|
|
|
|
s.Merge(b->share_style);
|
|
|
|
xml_gen_style(b, s, attrs);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
b->gen.AddStyle(s);
|
2018-03-31 13:48:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_start(XmlBuilder* thiz, const char* tag, const char** attrs) {
|
|
|
|
xml_builder_t* b = (xml_builder_t*)thiz;
|
|
|
|
|
2018-03-31 19:48:49 +08:00
|
|
|
if (b->level == 0) {
|
|
|
|
xml_gen_on_widget(b, tag, attrs);
|
|
|
|
} else if (b->level == 1) {
|
|
|
|
xml_gen_on_style(b, tag, attrs);
|
2018-03-31 13:48:18 +08:00
|
|
|
} else {
|
2018-03-31 19:48:49 +08:00
|
|
|
xml_gen_on_state(b, tag, attrs);
|
2018-03-31 13:48:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
b->level++;
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_end(XmlBuilder* thiz, const char* tag) {
|
|
|
|
(void)thiz;
|
|
|
|
(void)tag;
|
2018-03-31 13:48:18 +08:00
|
|
|
xml_builder_t* b = (xml_builder_t*)thiz;
|
|
|
|
b->level--;
|
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_text(XmlBuilder* thiz, const char* text, size_t length) {
|
|
|
|
(void)thiz;
|
|
|
|
(void)text;
|
|
|
|
(void)length;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_comment(XmlBuilder* thiz, const char* text, size_t length) {
|
|
|
|
(void)thiz;
|
|
|
|
(void)text;
|
|
|
|
(void)length;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_pi(XmlBuilder* thiz, const char* tag, const char** attrs) {
|
|
|
|
(void)thiz;
|
|
|
|
(void)tag;
|
|
|
|
(void)attrs;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_on_error(XmlBuilder* thiz, int line, int row, const char* message) {
|
|
|
|
(void)thiz;
|
|
|
|
printf("parse error: %d:%d %s\n", line, row, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xml_gen_destroy(XmlBuilder* thiz) {
|
|
|
|
(void)thiz;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static XmlBuilder* builder_init(xml_builder_t& b) {
|
|
|
|
b.builder.on_start = xml_gen_on_start;
|
|
|
|
b.builder.on_end = xml_gen_on_end;
|
|
|
|
b.builder.on_text = xml_gen_on_text;
|
|
|
|
b.builder.on_error = xml_gen_on_error;
|
|
|
|
b.builder.on_comment = xml_gen_on_comment;
|
|
|
|
b.builder.on_pi = xml_gen_on_pi;
|
|
|
|
b.builder.destroy = xml_gen_destroy;
|
2018-03-31 19:48:49 +08:00
|
|
|
b.level = 0;
|
2018-07-17 11:27:14 +08:00
|
|
|
b.style_name = TK_DEFAULT_STYLE;
|
2018-06-25 17:27:17 +08:00
|
|
|
b.widget_type = "";
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
return &(b.builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t xml_gen_buff(const char* xml, uint8_t* output, uint32_t max_size) {
|
|
|
|
xml_builder_t b;
|
|
|
|
return_value_if_fail(xml != NULL && output != NULL, 0);
|
|
|
|
|
|
|
|
XmlParser* parser = xml_parser_create();
|
|
|
|
xml_parser_set_builder(parser, builder_init(b));
|
|
|
|
xml_parser_parse(parser, xml, strlen(xml));
|
|
|
|
|
|
|
|
uint8_t* end = b.gen.Output(output, max_size);
|
|
|
|
return_value_if_fail(end != NULL, 0);
|
|
|
|
|
|
|
|
uint32_t size = end - output;
|
|
|
|
|
|
|
|
xml_parser_destroy(parser);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:12:12 +08:00
|
|
|
bool xml_gen(const char* input_file, const char* output_file, bool_t output_bin) {
|
2018-02-21 19:36:38 +08:00
|
|
|
xml_builder_t b;
|
2018-12-25 18:00:46 +08:00
|
|
|
uint8_t buff[500 * 1024];
|
2018-04-29 18:06:40 +08:00
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
return_value_if_fail(input_file != NULL && output_file != NULL, false);
|
|
|
|
|
|
|
|
XmlParser* parser = xml_parser_create();
|
|
|
|
xml_parser_set_builder(parser, builder_init(b));
|
|
|
|
xml_parser_parse_file(parser, input_file);
|
|
|
|
|
|
|
|
uint8_t* end = b.gen.Output(buff, sizeof(buff));
|
|
|
|
return_value_if_fail(end != NULL, false);
|
|
|
|
|
|
|
|
uint32_t size = end - buff;
|
2018-04-27 18:12:12 +08:00
|
|
|
|
2018-04-29 16:51:54 +08:00
|
|
|
if (output_bin) {
|
2018-04-27 18:12:12 +08:00
|
|
|
write_file(output_file, buff, size);
|
|
|
|
} else {
|
2018-08-24 07:45:37 +08:00
|
|
|
output_res_c_source(output_file, ASSET_TYPE_STYLE, 0, buff, size);
|
2018-04-27 18:12:12 +08:00
|
|
|
}
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
xml_parser_destroy(parser);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|