Initialize Tizen 2.3
[framework/uifw/elementary.git] / mobile / src / lib / elm_font.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4
5 #ifdef HAVE_EVIL
6 # include <Evil.h>
7 #endif
8
9 #include <Elementary.h>
10 #include "elm_priv.h"
11
12 Elm_Font_Properties *
13 _elm_font_properties_get(Eina_Hash **font_hash,
14                          const char *font)
15 {
16    Elm_Font_Properties *efp = NULL;
17    char *token = strchr(font, ':');
18
19    if (token &&
20        !strncmp(token, ELM_FONT_TOKEN_STYLE, strlen(ELM_FONT_TOKEN_STYLE)))
21      {
22         char *name, *subname, *style, *substyle;
23         int len;
24
25         /* get font name */
26         len = token - font;
27         name = calloc(sizeof(char), len + 1);
28         if (!name) return NULL;
29         strncpy(name, font, len);
30
31         /* remove subnames from the font name (should be english)  */
32         subname = strchr(name, ',');
33         if (subname)
34           {
35              len = subname - name;
36              name = realloc(name, sizeof(char) * len + 1);
37              if (name)
38                {
39                   memset(name, 0, sizeof(char) * len + 1);
40                   strncpy(name, font, len);
41                }
42           }
43
44         /* add a font name */
45         if (font_hash)
46           efp = eina_hash_find(*font_hash, name);
47         if (!efp)
48           {
49              efp = calloc(1, sizeof(Elm_Font_Properties));
50              if (!efp)
51                {
52                   free(name);
53                   return NULL;
54                }
55
56              efp->name = eina_stringshare_add(name);
57              if (font_hash)
58                {
59                   if (!*font_hash)
60                     *font_hash = eina_hash_string_superfast_new(NULL);
61                   eina_hash_add(*font_hash, name, efp);
62                }
63           }
64
65         free(name);
66
67         style = token + strlen(ELM_FONT_TOKEN_STYLE);
68         substyle = strchr(style, ',');
69
70         //TODO: Seems to need to add all styles. not only one.
71         if (substyle)
72           {
73              char *style_old = style;
74
75              len = substyle - style;
76              style = calloc(sizeof(char), len + 1);
77              if (style)
78                {
79                   strncpy(style, style_old, len);
80                   efp->styles = eina_list_append(efp->styles,
81                                                  eina_stringshare_add(style));
82                   free(style);
83                }
84           }
85         else
86           efp->styles = eina_list_append(efp->styles,
87                                          eina_stringshare_add(style));
88      }
89    else if ((font_hash) && (!eina_hash_find(*font_hash, font)))
90      {
91         efp = calloc(1, sizeof(Elm_Font_Properties));
92         if (!efp) return NULL;
93
94         efp->name = eina_stringshare_add(font);
95         if (!*font_hash) *font_hash = eina_hash_string_superfast_new(NULL);
96         eina_hash_add(*font_hash, font, efp);
97      }
98
99    return efp;
100 }
101
102 Eina_Hash *
103 _elm_font_available_hash_add(Eina_Hash  *font_hash,
104                              const char *full_name)
105 {
106    _elm_font_properties_get(&font_hash, full_name);
107    return font_hash;
108 }
109
110 static void
111 _elm_font_properties_free(Elm_Font_Properties *efp)
112 {
113    const char *str;
114
115    EINA_LIST_FREE(efp->styles, str)
116      if (str) eina_stringshare_del(str);
117
118    if (efp->name) eina_stringshare_del(efp->name);
119    free(efp);
120 }
121
122 static Eina_Bool
123 _font_hash_free_cb(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__)
124 {
125    Elm_Font_Properties *efp;
126
127    efp = data;
128    _elm_font_properties_free(efp);
129    return EINA_TRUE;
130 }
131
132 void
133 _elm_font_available_hash_del(Eina_Hash *hash)
134 {
135    if (!hash) return;
136
137    eina_hash_foreach(hash, _font_hash_free_cb, NULL);
138    eina_hash_free(hash);
139 }
140
141 EAPI Elm_Font_Properties *
142 elm_font_properties_get(const char *font)
143 {
144    EINA_SAFETY_ON_NULL_RETURN_VAL(font, NULL);
145    return _elm_font_properties_get(NULL, font);
146 }
147
148 EAPI void
149 elm_font_properties_free(Elm_Font_Properties *efp)
150 {
151    const char *str;
152
153    EINA_SAFETY_ON_NULL_RETURN(efp);
154    EINA_LIST_FREE(efp->styles, str)
155      if (str) eina_stringshare_del(str);
156    if (efp->name) eina_stringshare_del(efp->name);
157    free(efp);
158 }
159
160 EAPI char *
161 elm_font_fontconfig_name_get(const char *name,
162                              const char *style)
163 {
164    char buf[256];
165
166    EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
167    if (!style || style[0] == 0) return (char *) eina_stringshare_add(name);
168    snprintf(buf, 256, "%s" ELM_FONT_TOKEN_STYLE "%s", name, style);
169    return (char *) eina_stringshare_add(buf);
170 }
171
172 EAPI void
173 elm_font_fontconfig_name_free(char *name)
174 {
175    eina_stringshare_del(name);
176 }
177
178 EAPI Eina_Hash *
179 elm_font_available_hash_add(Eina_List *list)
180 {
181    Eina_Hash *font_hash;
182    Eina_List *l;
183    void *key;
184
185    font_hash = NULL;
186
187    /* populate with default font families */
188    //FIXME: Need to check whether fonts are being added multiple times.
189    font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Regular");
190    font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Bold");
191    font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Oblique");
192    font_hash = _elm_font_available_hash_add(font_hash, "Sans:style=Bold Oblique");
193    font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Regular");
194    font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Bold");
195    font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Oblique");
196    font_hash = _elm_font_available_hash_add(font_hash, "Serif:style=Bold Oblique");
197    font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Regular");
198    font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Bold");
199    font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Oblique");
200    font_hash = _elm_font_available_hash_add(font_hash, "Monospace:style=Bold Oblique");
201
202    EINA_LIST_FOREACH(list, l, key)
203      if (key) _elm_font_available_hash_add(font_hash, key);
204
205    return font_hash;
206 }
207
208 EAPI void
209 elm_font_available_hash_del(Eina_Hash *hash)
210 {
211    _elm_font_available_hash_del(hash);
212 }