Tizen 2.0 Release
[profile/ivi/libscl-ui.git] / xml2binary / encode_layout.cpp
1 /*
2  * Copyright 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "resource_storage_impl.h"
19 #include "encode_layout.h"
20 #include <string>
21 #include <libxml/parser.h>
22 #include "timer.h"
23 #include "xmlresource.h"
24 #include "put_record.h"
25 #include "_auto_metadata.h"
26 using namespace std;
27 using namespace xmlresource;
28 extern IString_Encoder& string_encoder;
29
30 static void _encode_color(ResourceStorage& storage, const SclColor& color, int width) {
31     if (width <= 0) return;
32
33     storage.put<sint_t>(color.r, width);
34     storage.put<sint_t>(color.g, width);
35     storage.put<sint_t>(color.b, width);
36     storage.put<sint_t>(color.a, width);
37 }
38
39 static void
40 encode_layout_record(ResourceStorage& storage, PSclLayout cur, const Layout_width& record_width) {
41     //name
42     storage.encode_string((const char*)cur->name, record_width.name);
43     //display mode
44     storage.put<sint_t>(cur->display_mode, record_width.display_mode);
45
46     //style
47     storage.put<sint_t>(cur->style, record_width.style);
48
49     //width
50     storage.put<sint_t>(cur->width, record_width.width);
51
52     //height
53     storage.put<sint_t>(cur->height, record_width.height);
54
55     //use_sw_button
56     storage.put<sint_t>(cur->use_sw_button, record_width.use_sw_button);
57
58     //use_magnifier_window
59     storage.put<sint_t>(cur->use_magnifier_window, record_width.use_magnifier_window);
60
61     //extract_background
62     storage.put<sint_t>(cur->extract_background, record_width.extract_background);
63
64     //key_width
65     storage.put<sint_t>(cur->key_width, record_width.key_width);
66
67     //key_height
68     storage.put<sint_t>(cur->key_height, record_width.key_height);
69     //key_spacing
70     storage.put<sint_t>(cur->key_spacing, record_width.key_spacing);
71     //row_spacing
72     storage.put<sint_t>(cur->row_spacing, record_width.row_spacing);
73
74     //use_sw_background
75     storage.put<sint_t>(cur->use_sw_background, record_width.use_sw_background);
76
77     _encode_color(storage, cur->bg_color, record_width.bg_color);
78
79     //bg_line_width
80     storage.put<float_t>(cur->bg_line_width, record_width.bg_line_width);
81
82     _encode_color(storage, cur->bg_line_color, record_width.bg_line_color);
83
84     //add_grab_left
85     storage.put<sint_t>(cur->add_grab_left, record_width.add_grab_left);
86
87     //add_grab_right
88     storage.put<sint_t>(cur->add_grab_right, record_width.add_grab_right);
89
90     //add_grab_top
91     storage.put<sint_t>(cur->add_grab_top, record_width.add_grab_top);
92
93     //add_grab_bottom
94     storage.put<sint_t>(cur->add_grab_bottom, record_width.add_grab_bottom);
95
96     //image_path
97     for (int i = 0; i < SCL_BUTTON_STATE_MAX; ++i) {
98         storage.encode_string(cur->image_path[i], record_width.image_path);
99     }
100
101     //key_background_image
102     for (int i = 0; i < SCL_SHIFT_STATE_MAX; ++i) {
103         for (int j = 0; j < SCL_BUTTON_STATE_MAX; ++j) {
104             storage.encode_string(cur->key_background_image[i][j], record_width.key_background_image);
105         }
106     }
107
108     //sound_style
109     storage.encode_string(cur->sound_style, record_width.sound_style);
110     //vibe_style
111     storage.encode_string(cur->vibe_style, record_width.vibe_style);
112     //label_name
113     storage.encode_string(cur->label_type, record_width.label_type);
114     //modifier_decorator
115     storage.encode_string(cur->modifier_decorator, record_width.modifier_decorator);
116 }
117
118 int
119 encode_layout_file(ResourceStorage& storage, IMetaData_Helper& md_helper) {
120     // 4 byte (range[0-4,294,967,295))
121     const int DATA_SIZE_BYTES = 4;
122     // 1 byte (range[0-128))
123     const int REC_NUM_BYTES = 1;
124     // 2 byte (range[0-65536))
125     const int LAYOUT_REC_DATA_SIZE_BYTES = 2;
126
127     const int init_size = storage.size();
128
129     XMLResource *xmlresource = XMLResource::get_instance();
130     PSclLayout layoutTable = xmlresource->get_layout_table();
131
132     int size = xmlresource->get_layout_size();
133
134 #ifdef __SCL_TXT_DEBUG
135     put_layout_table(ENCODE, layoutTable);
136 #endif
137
138     PSclLayout cur = layoutTable;
139
140     // data size
141     storage.reserve(DATA_SIZE_BYTES);
142
143     // rec_num
144     storage.put<sint_t>(size, REC_NUM_BYTES);
145
146     // rec data size
147     storage.reserve(LAYOUT_REC_DATA_SIZE_BYTES);
148
149     Layout_width record_width;
150     set_layout_width(md_helper, record_width);
151
152     int layout_rec_data_size = 0;
153     for ( int i = 0; i < size; ++i) {
154         int pre_size = storage.size();
155         encode_layout_record(storage, cur, record_width);
156
157         int aft_size = storage.size();
158         if (layout_rec_data_size == 0) {
159             layout_rec_data_size = aft_size - pre_size;
160         }
161         assert(layout_rec_data_size == aft_size - pre_size);
162
163         cur++;
164     }
165
166     // back write data size
167     int advance_size = storage.size() - init_size;
168
169     // random put advance_size
170     int data_size_offset = init_size;
171     storage.random_put<sint_t>(advance_size, DATA_SIZE_BYTES, data_size_offset);
172
173     // random put layout_rec size
174     int layout_rec_data_offset = init_size + DATA_SIZE_BYTES + REC_NUM_BYTES;
175     storage.random_put<sint_t>(layout_rec_data_size, LAYOUT_REC_DATA_SIZE_BYTES, layout_rec_data_offset);
176
177     return storage.size();
178 }
179
180 int
181 encode_layout_file(const char* file, IMetaData_Helper& md_helper) {
182     if (!file) return 0;
183
184     ResourceStorage storage;
185     encode_layout_file(storage, md_helper);
186
187     storage.toFile(file);
188
189     return storage.size();
190 }
191
192 int
193 encode_layout_file(const char* file, int& offset, IMetaData_Helper& md_helper) {
194     if (!file) return 0;
195
196     ResourceStorage storage;
197     encode_layout_file(storage, md_helper);
198
199     storage.toFile(file, offset);
200
201     return storage.size();
202 }