1 /**
2  * @file lv_xml_image_parser.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_xml_image_parser.h"
10 #if LV_USE_XML
11 
12 #include "../../../lvgl.h"
13 #include "../../../lvgl_private.h"
14 
15 /*********************
16  *      DEFINES
17  *********************/
18 
19 /**********************
20  *      TYPEDEFS
21  **********************/
22 
23 /**********************
24  *  STATIC PROTOTYPES
25  **********************/
26 static lv_image_align_t image_align_to_enum(const char * txt);
27 
28 /**********************
29  *  STATIC VARIABLES
30  **********************/
31 
32 /**********************
33  *      MACROS
34  **********************/
35 
36 /**********************
37  *   GLOBAL FUNCTIONS
38  **********************/
39 
lv_xml_image_create(lv_xml_parser_state_t * state,const char ** attrs)40 void * lv_xml_image_create(lv_xml_parser_state_t * state, const char ** attrs)
41 {
42     LV_UNUSED(attrs);
43     void * item = lv_image_create(lv_xml_state_get_parent(state));
44 
45     if(item == NULL) {
46         LV_LOG_ERROR("Failed to create image");
47         return NULL;
48     }
49 
50     return item;
51 }
52 
lv_xml_image_apply(lv_xml_parser_state_t * state,const char ** attrs)53 void lv_xml_image_apply(lv_xml_parser_state_t * state, const char ** attrs)
54 {
55     void * item = lv_xml_state_get_item(state);
56 
57     lv_xml_obj_apply(state, attrs); /*Apply the common properties, e.g. width, height, styles flags etc*/
58 
59     for(int i = 0; attrs[i]; i += 2) {
60         const char * name = attrs[i];
61         const char * value = attrs[i + 1];
62 
63         if(lv_streq("src", name)) lv_image_set_src(item, lv_xml_get_image(value));
64         if(lv_streq("inner_align", name)) lv_image_set_inner_align(item, image_align_to_enum(value));
65         if(lv_streq("rotation", name)) lv_image_set_rotation(item, lv_xml_atoi(value));
66         if(lv_streq("scale_x", name)) lv_image_set_scale_x(item, lv_xml_atoi(value));
67         if(lv_streq("scale_y", name)) lv_image_set_scale_y(item, lv_xml_atoi(value));
68         if(lv_streq("pivot", name)) {
69             int32_t x = lv_xml_atoi_split(&value, ' ');
70             int32_t y = lv_xml_atoi_split(&value, ' ');
71             lv_image_set_pivot(item, x, y);
72         }
73 
74     }
75 
76 }
77 
78 /**********************
79  *   STATIC FUNCTIONS
80  **********************/
81 
image_align_to_enum(const char * txt)82 static lv_image_align_t image_align_to_enum(const char * txt)
83 {
84     if(lv_streq("top_left", txt)) return LV_IMAGE_ALIGN_TOP_LEFT;
85     if(lv_streq("top_mid", txt)) return LV_IMAGE_ALIGN_TOP_MID;
86     if(lv_streq("top_right", txt)) return LV_IMAGE_ALIGN_TOP_RIGHT;
87     if(lv_streq("bottom_left", txt)) return LV_IMAGE_ALIGN_BOTTOM_LEFT;
88     if(lv_streq("bottom_mid", txt)) return LV_IMAGE_ALIGN_BOTTOM_MID;
89     if(lv_streq("bottom_right", txt)) return LV_IMAGE_ALIGN_BOTTOM_RIGHT;
90     if(lv_streq("right_mid", txt)) return LV_IMAGE_ALIGN_RIGHT_MID;
91     if(lv_streq("left_mid", txt)) return LV_IMAGE_ALIGN_LEFT_MID;
92     if(lv_streq("center", txt)) return LV_IMAGE_ALIGN_CENTER;
93     if(lv_streq("stretch", txt)) return LV_IMAGE_ALIGN_STRETCH;
94     if(lv_streq("tile", txt)) return LV_IMAGE_ALIGN_TILE;
95 
96     LV_LOG_WARN("%s is an unknown value for image align", txt);
97     return 0; /*Return 0 in lack of a better option. */
98 }
99 
100 
101 #endif /* LV_USE_XML */
102