Fix image checker to resolve C# crash
[platform/core/api/system-settings.git] / libutil / sstu.c
1 /*
2  * Copyright (c) 2011-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 #include "sst_utils_wrapper.h"
17
18 #include <string.h>
19 #include <vconf.h>
20 #include <fontconfig/fontconfig.h>
21 #include <Ecore_Evas.h>
22 #include <efl_extension.h>
23 #include "system_settings.h"
24 #include "sst.h"
25
26 #ifdef TIZEN_WEARABLE
27 #define SMALL_FONT_DPI                                          (-90)
28 #else
29 #define SMALL_FONT_DPI                                          (-80)
30 #endif
31
32 #define MIDDLE_FONT_DPI                                         (-100)
33
34 #ifdef TIZEN_WEARABLE
35 #define LARGE_FONT_DPI                                          (-110)
36 #else
37 #define LARGE_FONT_DPI                                          (-150)
38 #endif
39
40 #define HUGE_FONT_DPI                                           (-190)
41 #define GIANT_FONT_DPI                                          (-250)
42
43 #define SETTING_FONT_PRELOAD_FONT_PATH _TZ_SYS_RO_SHARE"/fonts"
44 #define SETTING_FONT_DOWNLOADED_FONT_PATH _TZ_SYS_SHARE"/fonts"
45
46 #define SETTING_FONT_TIZEN_FONT_ALIAS "Tizen"
47 #define SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS "TizenDefaultFont"
48
49 static char* _get_main_font_family(char *alias)
50 {
51         FcFontSet *set;
52         FcPattern *pat;
53         FcConfig *font_config = NULL;
54         FcChar8 *family = NULL;
55         char *ret = NULL;
56         FcResult res = 0;
57
58         font_config = FcInitLoadConfigAndFonts();
59         if (NULL == font_config) {
60                 ERR("FcInitLoadConfigAndFonts() Fail");
61                 return NULL;
62         }
63
64         pat = FcPatternBuild(0, FC_FAMILY, FcTypeString, alias, NULL);
65         if (NULL == pat) {
66                 ERR("FcPatternBuild() Fail");
67                 FcConfigDestroy(font_config);
68                 return NULL;
69         }
70
71         FcConfigSubstitute(font_config, pat, FcMatchPattern);
72         FcDefaultSubstitute(pat);
73
74         /* do matching */
75         set = FcFontSort(font_config, pat, FcTrue, NULL, &res);
76         do {
77                 if (set && 0 < set->nfont) {
78                         int result = FcPatternGetString(set->fonts[0], FC_FAMILY, 0, &family);
79                         if (FcResultMatch != result) {
80                                 ERR("FcPatternGetString() Fail(%d)", result);
81                                 ret = NULL;
82                                 break;
83                         }
84                         if (family)
85                                 ret = strdup((char*)family);
86                 }
87         } while(0);
88
89         if (set)
90                 FcFontSetDestroy(set);
91
92         FcPatternDestroy(pat);
93         FcConfigDestroy(font_config);
94
95         return ret;
96 }
97
98 API bool sstu_is_valid_image(const char *path)
99 {
100         void *ee;
101         void *evas;
102
103         if (0 == ecore_evas_init()) {
104                 ERR("ecore_evas_init() Fail");
105                 return false;
106         }
107
108         ee = ecore_evas_buffer_new(100, 100);
109         evas = ecore_evas_get(ee);
110
111         void *img = evas_object_image_add(evas);
112         evas_object_image_file_set(img, path, NULL);
113         int ret = evas_object_image_load_error_get(img);
114
115         bool result = false;
116         if (ret == 0) {
117                 result = true;
118         } else {
119                 ERR("Invalid(%s)", path);
120                 result = false;
121         }
122         ecore_evas_free(ee);
123         ecore_evas_shutdown();
124         return result;
125 }
126
127 API void sstu_font_config_set_notification()
128 {
129 }
130
131 API int sstu_is_available_font(const char *font_name)
132 {
133         FcObjectSet *os;
134         FcFontSet *fs;
135         FcPattern *pat;
136         FcConfig *font_config;
137         int ret = 0;
138
139         RETV_IF(NULL == font_name, -1);
140
141         font_config = FcInitLoadConfigAndFonts();
142         if (NULL == font_config)
143                 ERR("FcInitLoadConfigAndFonts() Fail");
144
145         char *locale = setlocale(0, NULL);
146         os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FAMILYLANG, NULL);
147         if (NULL == os) {
148                 ERR("FcObjectSetBuild() Fail");
149                 FcConfigDestroy(font_config);
150                 return 0;
151         }
152
153         pat = FcPatternCreate();
154         if (NULL == pat) {
155                 ERR("FcPatternCreate() Fail");
156                 FcObjectSetDestroy(os);
157                 FcConfigDestroy(font_config);
158                 return 0;
159         }
160
161         fs = FcFontList(font_config, pat, os);
162         FcPatternDestroy(pat);
163         FcObjectSetDestroy(os);
164         if (NULL == fs) {
165                 ERR("FcFontList() Fail");
166                 FcConfigDestroy(font_config);
167                 return 0;
168         }
169
170         INFO("fs->nfont = %d", fs->nfont);
171
172         int i;
173         for (i = 0; i < fs->nfont; i++) {
174                 FcChar8 *file = NULL;
175                 FcChar8 *family = NULL;
176
177                 FcResult result;
178                 result = FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file);
179                 if (FcResultMatch != result || NULL == file) {
180                         ERR("FcPatternGetString() Fail(%d)", result);
181                         FcFontSetDestroy(fs);
182                         FcConfigDestroy(font_config);
183                         return 0;
184                 }
185                 const char *preloaded = SETTING_FONT_PRELOAD_FONT_PATH;
186                 const char *downloaded = SETTING_FONT_DOWNLOADED_FONT_PATH;
187                 int preload_len = sizeof(SETTING_FONT_PRELOAD_FONT_PATH) - 1;
188                 int download_len = sizeof(SETTING_FONT_DOWNLOADED_FONT_PATH) - 1;
189
190                 if (SST_EQUAL == strncmp((char*)file, preloaded, preload_len)
191                         || SST_EQUAL == strncmp((char*)file, downloaded, download_len)) {
192                         char *family_result = NULL;
193                         FcChar8 *lang = NULL;
194                         int id = 0;
195                         if (FcPatternGetString(fs->fonts[i], FC_FAMILY, id, &family) != FcResultMatch)
196                                 break;
197                         if (FcPatternGetString(fs->fonts[i], FC_FAMILYLANG, id, &lang) != FcResultMatch)
198                                 break;
199                         family_result = (char*)family;
200
201                         /* Find proper family name for current locale. */
202                         while (locale && family && lang) {
203                                 if (SST_EQUAL == strncmp(locale, (char*)lang, strlen((char*)lang))) {
204                                         family_result = (char*)family;
205                                         break;
206                                 }
207
208                                 /* I will set english as default family language. */
209                                 /* If there is no proper family language for current locale, */
210                                 /* we have to show the english family name. */
211                                 if (SST_EQUAL == strcmp((char*)lang, "en"))
212                                         family_result = (char*)family;
213
214                                 id++;
215                                 if (FcPatternGetString(fs->fonts[i], FC_FAMILY, id, &family) != FcResultMatch)
216                                         break;
217                                 if (FcPatternGetString(fs->fonts[i], FC_FAMILYLANG, id, &lang) != FcResultMatch)
218                                         break;
219                         }
220
221                         if (family_result) {
222                                 DBG("family = %s", family_result);
223                                 if (strcmp(family_result, font_name) == 0) {
224                                         ret = 1;
225                                         break;
226                                 }
227                         }
228                 }
229         }
230         FcFontSetDestroy(fs);
231         FcConfigDestroy(font_config);
232         return ret;
233 }
234
235 API char* sstu_get_default_font()
236 {
237         return _get_main_font_family(SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS);
238 }
239
240 static int _get_font_size()
241 {
242         int font_size = -1;
243
244         int vconf_value = -1;
245         if (vconf_get_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &vconf_value)) {
246                 ERR("vconf_get_int() Fail");
247                 return -1;
248         }
249
250         switch (vconf_value) {
251         case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
252                 font_size = SMALL_FONT_DPI;
253                 break;
254         case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
255                 font_size = MIDDLE_FONT_DPI;
256                 break;
257         case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
258                 font_size = LARGE_FONT_DPI;
259                 break;
260         case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
261                 font_size = HUGE_FONT_DPI;
262                 break;
263         case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
264                 font_size = GIANT_FONT_DPI;
265                 break;
266         default:
267                 font_size = MIDDLE_FONT_DPI;
268                 break;
269         }
270         return font_size;
271 }
272
273 API bool sstu_set_font_config(const char *font_name)
274 {
275         int font_size = _get_font_size();
276         return eext_config_font_set((char*)font_name, font_size);
277 }
278
279 API void sstu_set_font_size()
280 {
281         int font_size = _get_font_size();
282         char *font_name = NULL;
283         font_name = vconf_get_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME);
284         eext_config_font_set(font_name, font_size);
285         free(font_name);
286 }