2 * Copyright (c) 2011-2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "sst_utils_wrapper.h"
20 #include <fontconfig/fontconfig.h>
21 #include <efl_extension.h>
22 #include <json-glib/json-glib.h>
23 #include "system_settings.h"
26 #define FONT_SCALE_CONF_PATH SYSCONF_DIR"/font_scale.json"
27 #define INTERNAL_API __attribute__((visibility("default")))
30 #define SMALL_FONT_DPI (-90)
32 #define SMALL_FONT_DPI (-80)
35 #define MIDDLE_FONT_DPI (-100)
38 #define LARGE_FONT_DPI (-110)
40 #define LARGE_FONT_DPI (-150)
43 #define HUGE_FONT_DPI (-190)
44 #define GIANT_FONT_DPI (-250)
46 #define SETTING_FONT_PRELOAD_FONT_PATH _TZ_SYS_RO_SHARE"/fonts"
47 #define SETTING_FONT_DOWNLOADED_FONT_PATH _TZ_SYS_SHARE"/fonts"
49 #define SETTING_FONT_TIZEN_FONT_ALIAS "Tizen"
50 #define SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS "TizenDefaultFont"
53 typedef struct __font_size_info {
61 static char* _get_main_font_family(char *alias)
65 FcConfig *font_config = NULL;
66 FcChar8 *family = NULL;
70 font_config = FcInitLoadConfigAndFonts();
71 if (NULL == font_config) {
72 ERR("FcInitLoadConfigAndFonts() Fail");
76 pat = FcPatternBuild(0, FC_FAMILY, FcTypeString, alias, NULL);
78 ERR("FcPatternBuild() Fail");
79 FcConfigDestroy(font_config);
83 FcConfigSubstitute(font_config, pat, FcMatchPattern);
84 FcDefaultSubstitute(pat);
87 set = FcFontSort(font_config, pat, FcTrue, NULL, &res);
89 if (set && 0 < set->nfont) {
90 int result = FcPatternGetString(set->fonts[0], FC_FAMILY, 0, &family);
91 if (FcResultMatch != result) {
92 ERR("FcPatternGetString() Fail(%d)", result);
97 ret = strdup((char*)family);
102 FcFontSetDestroy(set);
104 FcPatternDestroy(pat);
105 FcConfigDestroy(font_config);
110 API void sstu_font_config_set_notification()
114 API int sstu_is_available_font(const char *font_name)
119 FcConfig *font_config;
122 RETV_IF(NULL == font_name, -1);
124 font_config = FcInitLoadConfigAndFonts();
125 if (NULL == font_config)
126 ERR("FcInitLoadConfigAndFonts() Fail");
128 char *locale = setlocale(0, NULL);
129 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FAMILYLANG, NULL);
131 ERR("FcObjectSetBuild() Fail");
132 FcConfigDestroy(font_config);
136 pat = FcPatternCreate();
138 ERR("FcPatternCreate() Fail");
139 FcObjectSetDestroy(os);
140 FcConfigDestroy(font_config);
144 fs = FcFontList(font_config, pat, os);
145 FcPatternDestroy(pat);
146 FcObjectSetDestroy(os);
148 ERR("FcFontList() Fail");
149 FcConfigDestroy(font_config);
153 INFO("fs->nfont = %d", fs->nfont);
156 for (i = 0; i < fs->nfont; i++) {
157 FcChar8 *file = NULL;
158 FcChar8 *family = NULL;
161 result = FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file);
162 if (FcResultMatch != result || NULL == file) {
163 ERR("FcPatternGetString() Fail(%d)", result);
164 FcFontSetDestroy(fs);
165 FcConfigDestroy(font_config);
168 const char *preloaded = SETTING_FONT_PRELOAD_FONT_PATH;
169 const char *downloaded = SETTING_FONT_DOWNLOADED_FONT_PATH;
170 int preload_len = sizeof(SETTING_FONT_PRELOAD_FONT_PATH) - 1;
171 int download_len = sizeof(SETTING_FONT_DOWNLOADED_FONT_PATH) - 1;
173 if (SST_EQUAL == strncmp((char*)file, preloaded, preload_len)
174 || SST_EQUAL == strncmp((char*)file, downloaded, download_len)) {
175 char *family_result = NULL;
176 FcChar8 *lang = NULL;
178 if (FcPatternGetString(fs->fonts[i], FC_FAMILY, id, &family) != FcResultMatch)
180 if (FcPatternGetString(fs->fonts[i], FC_FAMILYLANG, id, &lang) != FcResultMatch)
182 family_result = (char*)family;
184 /* Find proper family name for current locale. */
185 while (locale && family && lang) {
186 if (SST_EQUAL == strncmp(locale, (char*)lang, strlen((char*)lang))) {
187 family_result = (char*)family;
191 /* I will set english as default family language. */
192 /* If there is no proper family language for current locale, */
193 /* we have to show the english family name. */
194 if (SST_EQUAL == strcmp((char*)lang, "en"))
195 family_result = (char*)family;
198 if (FcPatternGetString(fs->fonts[i], FC_FAMILY, id, &family) != FcResultMatch)
200 if (FcPatternGetString(fs->fonts[i], FC_FAMILYLANG, id, &lang) != FcResultMatch)
205 DBG("family = %s", family_result);
206 if (strcmp(family_result, font_name) == 0) {
213 FcFontSetDestroy(fs);
214 FcConfigDestroy(font_config);
218 API char* sstu_get_default_font()
220 return _get_main_font_family(SETTING_FONT_TIZEN_DEFAULT_FONT_ALIAS);
224 To support different variable product, font_scale.json is added.
225 With %config option in .spec file, the font_scale.json can be
226 uploaded only when there is no the font_scale.json in /etc.
228 In font_scale.json, there are 5 grade of font size, below.
230 $ cat /etc/font_scale.json
239 -80 means that 80% font scale of each application font size.
240 Each grade is mapping, below.
242 SYSTEM_SETTINGS_FONT_SIZE_SMALL == "SMALL" in font_scale.json value
243 SYSTEM_SETTINGS_FONT_SIZE_NORMAL == "NORMAL" in font_scale.json value
244 SYSTEM_SETTINGS_FONT_SIZE_LARGE == "LARGE" in font_scale.json value
245 SYSTEM_SETTINGS_FONT_SIZE_HUGE == "HUGE" in font_scale.json value
246 SYSTEM_SETTINGS_FONT_SIZE_GIANT == "GIANT" in font_scale.json value
249 static int get_int_from_object(JsonObject *obj, const char *key, int *data)
251 JsonNode *tmp = json_object_get_member(obj, key);
254 ERR("json_object_object_get_ex(key:%s) error", key);
258 int tmp_int = json_node_get_int(tmp);
262 ERR("%s key Wrong value : %d ", key, tmp_int);
268 INTERNAL_API int load_font_size_info(font_size_info *info, const gchar *path)
270 GError *error = NULL;
275 info->small = SMALL_FONT_DPI;
276 info->normal = MIDDLE_FONT_DPI;
277 info->large = LARGE_FONT_DPI;
278 info->huge = HUGE_FONT_DPI;
279 info->giant = GIANT_FONT_DPI;
281 JsonParser *parser = json_parser_new();
282 json_parser_load_from_file (parser, path, &error);
283 /* Parse configuration file */
285 INFO("There is no json_object file(%s), loaded default font size values", path);
289 JsonObject *obj = json_node_get_object(json_parser_get_root (parser));
290 INFO("json_object file(%s) OPENED! Try to load font size values from the file.", path);
291 get_int_from_object(obj, "SMALL", &info->small);
292 get_int_from_object(obj, "NORMAL", &info->normal);
293 get_int_from_object(obj, "LARGE", &info->large);
294 get_int_from_object(obj, "HUGE", &info->huge);
295 get_int_from_object(obj, "GIANT", &info->giant);
297 g_object_unref(parser);
302 static int _get_font_size()
307 int vconf_value = -1;
308 if (vconf_get_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &vconf_value)) {
309 ERR("vconf_get_int() Fail");
313 load_font_size_info(&info, FONT_SCALE_CONF_PATH);
315 switch (vconf_value) {
316 case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
317 font_size = info.small;
319 case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
320 font_size = info.normal;
322 case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
323 font_size = info.large;
325 case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
326 font_size = info.huge;
328 case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
329 font_size = info.giant;
332 font_size = info.normal;
338 API bool sstu_set_font_config(const char *font_name)
340 int font_size = _get_font_size();
341 return eext_config_font_set((char*)font_name, font_size);
344 API void sstu_set_font_size()
346 int font_size = _get_font_size();
347 char *font_name = NULL;
348 font_name = vconf_get_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME);
349 eext_config_font_set(font_name, font_size);