move elementary to trunk base. out of TMP/st.
[framework/uifw/elementary.git] / 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 *s1;
18
19    s1 = strchr(font, ':');
20    if (s1)
21      {
22         char *s2, *name, *style;
23         int len;
24
25         len = s1 - font;
26         name = calloc(sizeof(char), len + 1);
27         strncpy(name, font, len);
28
29         /* get subname (should be english)  */
30         s2 = strchr(name, ',');
31         if (s2)
32           {
33              len = s2 - name;
34              name = realloc(name, sizeof(char) * len + 1);
35              memset(name, 0, sizeof(char) * len + 1);
36              strncpy(name, font, len);
37           }
38
39         if (!strncmp(s1, ELM_FONT_TOKEN_STYLE, strlen(ELM_FONT_TOKEN_STYLE)))
40           {
41              style = s1 + strlen(ELM_FONT_TOKEN_STYLE);
42
43              if (font_hash) efp = eina_hash_find(*font_hash, name);
44              if (!efp)
45                {
46                   efp = calloc(1, sizeof(Elm_Font_Properties));
47                   efp->name = eina_stringshare_add(name);
48                   if (font_hash)
49                     {
50                        if (!*font_hash)
51                          *font_hash = eina_hash_string_superfast_new(NULL);
52                        eina_hash_add(*font_hash, name, efp);
53                     }
54                }
55              s2 = strchr(style, ',');
56              if (s2)
57                {
58                   char *style_old;
59
60                   len = s2 - style;
61                   style_old = style;
62                   style = calloc(sizeof(char), len + 1);
63                   strncpy(style, style_old, len);
64                   efp->styles = eina_list_append(efp->styles,
65                                                  eina_stringshare_add(style));
66                   free(style);
67                }
68              else
69                efp->styles = eina_list_append(efp->styles,
70                                               eina_stringshare_add(style));
71           }
72         free(name);
73      }
74    else
75      {
76         if (font_hash) efp = eina_hash_find(*font_hash, font);
77         if (!efp)
78           {
79              efp = calloc(1, sizeof(Elm_Font_Properties));
80              efp->name = eina_stringshare_add(font);
81              if (font_hash)
82                {
83                   if (!*font_hash)
84                     *font_hash = eina_hash_string_superfast_new(NULL);
85                   eina_hash_add(*font_hash, font, efp);
86                }
87           }
88      }
89    return efp;
90 }
91
92 /* FIXME: do we really need it? */
93 Eina_Hash *
94 _elm_font_available_hash_add(Eina_Hash  *font_hash,
95                              const char *full_name)
96 {
97    _elm_font_properties_get(&font_hash, full_name);
98    return font_hash;
99 }
100
101 static void
102 _elm_font_properties_free(Elm_Font_Properties *efp)
103 {
104    const char *str;
105
106    EINA_LIST_FREE(efp->styles, str)
107      if (str) eina_stringshare_del(str);
108
109    if (efp->name) eina_stringshare_del(efp->name);
110    free(efp);
111 }
112
113 static Eina_Bool
114 _font_hash_free_cb(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__)
115 {
116    Elm_Font_Properties *efp;
117
118    efp = data;
119    _elm_font_properties_free(efp);
120    return EINA_TRUE;
121 }
122
123 void
124 _elm_font_available_hash_del(Eina_Hash *hash)
125 {
126    eina_hash_foreach(hash, _font_hash_free_cb, NULL);
127    eina_hash_free(hash);
128 }