dont die if u dont rm your cfg
[platform/upstream/enlightenment.git] / src / bin / e_font.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4 #include "e.h"
5 #include "config.h"
6
7 /* TODO List:
8  * - export to libe
9  * - use e_path to search for available fonts
10  */
11
12 static Evas_List *_e_font_defaults = NULL;      /* MRU <E_Font_Default> */
13 static Evas_List *_e_font_fallbacks = NULL;     /* <E_Font_Fallback> */
14
15 static Evas_List *_e_font_font_dir_available_get (Evas_List * available_fonts, const char *font_dir);
16
17 int
18 e_font_init(void)
19 {
20    /* just get the pointers into the config */
21    _e_font_defaults = e_config->font_defaults;
22    _e_font_fallbacks = e_config->font_fallbacks;
23    return 1;
24 }
25
26 int
27 e_font_shutdown(void)
28 {
29    /* e_config will do this */
30    return 1;
31 }
32
33 void
34 e_font_apply(void)
35 {
36    char buf[PATH_MAX];
37    Evas_List *next;
38    E_Font_Fallback *eff;
39    E_Font_Default *efd;
40    
41    /* setup edje fallback list */
42    next = _e_font_fallbacks;
43    if (next)
44      {
45         eff = evas_list_data(next);
46         strncpy(buf, eff->name, PATH_MAX - 1);
47         next = evas_list_next(next);
48      }
49    else
50      {
51         edje_fontset_append_set(NULL);
52      }
53
54    buf[0] = 0;
55    while (next)
56      {
57         eff = evas_list_data(next);
58         strcat(buf, ",");
59         strcat(buf, eff->name);
60         next = evas_list_next(next);
61      }
62    if (buf[0] != 0)
63      edje_fontset_append_set(buf);
64    
65    /* setup edje text classes */
66    next = _e_font_defaults;
67    while (next)
68      {
69         efd = evas_list_data(next);
70         edje_text_class_set(efd->text_class, efd->font, efd->size);
71         next = evas_list_next(next);
72      }
73 }
74
75 Evas_List *
76 e_font_available_list(void)
77 {
78    Evas_List *available;
79    
80    available = NULL;
81    /* use e_path for this */
82    available = _e_font_font_dir_available_get(available, "~/.e/e/fonts");
83    available = _e_font_font_dir_available_get(available, PACKAGE_DATA_DIR "/data/fonts");
84    return available;
85 }
86
87 void
88 e_font_available_list_free(Evas_List * available)
89 {
90    char *font_name;
91    Evas_List *l;
92    
93    for (l = available; l; l = l->next)
94      {
95         font_name = evas_list_data(l);
96         available = evas_list_remove(available, l);
97         free(font_name);
98      }
99 }
100
101 void
102 e_font_fallback_clear(void)
103 {
104    Evas_List *next;
105    
106    next = _e_font_fallbacks;
107    while (next)
108      {
109         E_Font_Fallback *eff;
110         
111         eff = evas_list_data(next);
112         _e_font_fallbacks = evas_list_remove_list(_e_font_fallbacks, next);
113         E_FREE(eff->name);
114         E_FREE(eff);
115         next = evas_list_next(next);
116     }
117 }
118
119 void
120 e_font_fallback_append(const char *font)
121 {
122    E_Font_Fallback *eff;
123    
124    e_font_fallback_remove (font);
125    
126    eff = E_NEW(E_Font_Fallback, 1);
127    eff->name = strdup(font);
128    _e_font_fallbacks = evas_list_append(_e_font_fallbacks, eff);
129 }
130
131 void
132 e_font_fallback_prepend(const char *font)
133 {
134    E_Font_Fallback *eff;
135    
136    e_font_fallback_remove (font);
137    
138    eff = E_NEW(E_Font_Fallback, 1);
139    eff->name = strdup(font);
140    _e_font_fallbacks = evas_list_prepend(_e_font_fallbacks, eff);
141 }
142
143 void
144 e_font_fallback_remove(const char *font)
145 {
146    Evas_List *next;
147    
148    next = _e_font_fallbacks;
149    while (next)
150      {
151         E_Font_Fallback *eff;
152         
153         eff = evas_list_data(next);
154         if (!strcmp(eff->name, font))
155           {
156              _e_font_fallbacks = evas_list_remove_list(_e_font_fallbacks, next);
157              E_FREE(eff->name);
158              E_FREE(eff);
159              break;
160           }
161         next = evas_list_next(next);
162      }
163 }
164
165 Evas_List *
166 e_font_fallback_list(void)
167 {
168    return _e_font_fallbacks;
169 }
170
171 void
172 e_font_default_set(const char *text_class, const char *font, int size)
173 {
174    E_Font_Default *efd;
175    Evas_List *next;
176
177    /* search for the text class */
178    next = _e_font_defaults;
179    while (next)
180      {
181         efd = evas_list_data(next);
182         if (!strcmp(efd->text_class, text_class))
183           {
184              E_FREE(efd->font);
185              efd->font = strdup(font);
186              efd->size = size;
187              /* move to the front of the list */
188              _e_font_defaults = evas_list_remove_list(_e_font_defaults, next);
189              _e_font_defaults = evas_list_prepend(_e_font_defaults, efd);
190              return;
191           }
192         next = evas_list_next(next);
193      }
194
195    /* the text class doesnt exist */
196    efd = E_NEW(E_Font_Default, 1);
197    efd->text_class = strdup(text_class);
198    efd->font = strdup(font);
199    efd->size = size;
200    
201    _e_font_defaults = evas_list_prepend(_e_font_defaults, efd);
202 }
203
204 /*
205  * returns a pointer to the data, return null if nothing if found.
206  */
207 E_Font_Default *
208 e_font_default_get(const char *text_class)
209 {
210    E_Font_Default *efd;
211    Evas_List *next;
212
213    /* search for the text class */
214    next = _e_font_defaults;
215    while (next)
216      {
217         efd = evas_list_data(next);
218         if (!strcmp(efd->text_class, text_class))
219           {
220              /* move to the front of the list */
221              _e_font_defaults = evas_list_remove_list(_e_font_defaults, next);
222              _e_font_defaults = evas_list_prepend(_e_font_defaults, efd);
223              return efd;
224           }
225         next = evas_list_next(next);
226      }
227    return NULL;
228 }
229
230 void
231 e_font_default_remove(const char *text_class)
232 {
233    E_Font_Default *efd;
234    Evas_List *next;
235    
236    /* search for the text class */
237    next = _e_font_defaults;
238    while (next)
239      {
240         efd = evas_list_data(next);
241         if (!strcmp(efd->text_class, text_class))
242           {
243              _e_font_defaults = evas_list_remove_list(_e_font_defaults, next);
244              E_FREE(efd->text_class);
245              E_FREE(efd->font);
246              E_FREE(efd);
247              return;
248           }
249         next = evas_list_next(next);
250     }
251 }
252
253 Evas_List *
254 e_font_default_list(void)
255 {
256    return _e_font_defaults;
257 }
258
259 static Evas_List *
260 _e_font_font_dir_available_get(Evas_List * available_fonts, const char *font_dir)
261 {
262    char buf[4096];
263    FILE *f;
264    
265    sprintf (buf, "%s/fonts.alias", font_dir);
266    f = fopen (buf, "r");
267    if (f)
268      {
269         char fname[4096], fdef[4096];
270         Evas_List *next;
271         
272         /* read font alias lines */
273         while (fscanf(f, "%4090s %[^\n]\n", fname, fdef) == 2)
274           {
275              /* skip comments */
276              if ((fdef[0] == '!') || (fdef[0] == '#'))
277                continue;
278              
279              /* skip duplicates */
280              next = available_fonts;
281              while (next)
282                {
283                   if (!strcmp((char *)evas_list_data(next), fname))
284                     continue;
285                   next = evas_list_next(next);
286                }
287              available_fonts = evas_list_append(available_fonts, strdup(fname));
288           }
289         fclose (f);
290      }
291    return available_fonts;
292 }