summaryrefslogtreecommitdiff
path: root/src/bin/e_font.c
blob: 6801af5b310bfdb634e53dcd2d684bd0a443e0e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "e.h"

#define E_TOK_STYLE ":style="

static Eina_Bool _font_hash_free_cb(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__);
static Eina_Hash *_e_font_available_hash_add(Eina_Hash *font_hash, const char *full_name);
static E_Font_Properties *_e_font_fontconfig_name_parse(Eina_Hash **font_hash, E_Font_Properties *efp, const char *font);
static char _fn_buf[1024];

EINTERN int
e_font_init(void)
{
   /* all init stuff is in e_config */
   return 1;
}

EINTERN int
e_font_shutdown(void)
{
   /* e_config will do this */
   return 1;
}

EAPI void
e_font_apply(void)
{
   char buf[1024];
   Eina_List *l;
   E_Border *bd;
   E_Font_Default *efd;
   E_Font_Fallback *eff;
   int blen, len;

   /* setup edje fallback list */
   blen = sizeof(buf) - 1;
   buf[0] = 0;
   buf[blen] = 0;
   l = e_config->font_fallbacks;
   if (l)
     {
	eff = eina_list_data_get(l);
	len = strlen(eff->name);
	if (len < blen)
	  {
	     strcpy(buf, eff->name);
	     blen -= len;
	  }
	EINA_LIST_FOREACH(eina_list_next(l), l, eff)
	  {
	     len = 1;
	     if (len < blen)
	       {
		  strcat(buf, ",");
		  blen -= len;
	       }
	     len = strlen(eff->name);
	     if (len < blen)
	       {
		  strcat(buf, eff->name);
		  blen -= len;
	       }
	  }
	edje_fontset_append_set(buf);
     }
   else
     edje_fontset_append_set(NULL);

   /* setup edje text classes */
   EINA_LIST_FOREACH(e_config->font_defaults, l, efd)
     {
	edje_text_class_set(efd->text_class, efd->font, efd->size);
     }

   /* Update borders */
   EINA_LIST_FOREACH(e_border_client_list(), l, bd)
     {
	e_border_frame_recalc(bd);
     }
}

EAPI Eina_List *
e_font_available_list(void)
{
   Eina_List *evas_fonts;
   Eina_List *e_fonts;
   Eina_List *l;
   const char *evas_font;
   E_Manager *man;
   E_Container *con;

   man = e_manager_current_get();
   if (!man) return NULL;
   con = e_container_current_get(man);
   if (!con) con = e_container_number_get(man, 0);
   if (!con) return NULL;

   evas_fonts = evas_font_available_list(con->bg_evas);

   e_fonts = NULL;
   EINA_LIST_FOREACH(evas_fonts, l, evas_font)
     {
	E_Font_Available *efa;
	
	efa = E_NEW(E_Font_Available, 1);
	efa->name = eina_stringshare_add(evas_font);
	e_fonts = eina_list_append(e_fonts, efa);
     }

   evas_font_available_list_free(con->bg_evas, evas_fonts);

   return e_fonts;
}

EAPI void
e_font_available_list_free(Eina_List *available)
{
   E_Font_Available *efa;

   EINA_LIST_FREE(available, efa)
     {	
	if (efa->name) eina_stringshare_del(efa->name);
	E_FREE(efa);
    }
}

EAPI void
e_font_properties_free(E_Font_Properties *efp)
{
   const char *str;

   EINA_LIST_FREE(efp->styles, str)
     {
        if (str) eina_stringshare_del(str);
     }
   if (efp->name) eina_stringshare_del(efp->name);
   free(efp);
}

static Eina_Bool
_font_hash_free_cb(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__)
{
   E_Font_Properties *efp;

   efp = data;
   e_font_properties_free(efp);
   return 1;
}

EAPI void
e_font_available_hash_free(Eina_Hash *hash)
{
   eina_hash_foreach(hash, _font_hash_free_cb, NULL);
   eina_hash_free(hash);
}

EAPI E_Font_Properties *
e_font_fontconfig_name_parse(const char *font)
{
   if (!font) return NULL;
   return _e_font_fontconfig_name_parse(NULL, NULL, font);
}

static E_Font_Properties *
_e_font_fontconfig_name_parse(Eina_Hash **font_hash, E_Font_Properties *efp, const char *font)
{
   char *s1;

   s1 = strchr(font, ':');
   if (s1)
     {
        char *s2, *name, *style;
        int len;

        len = s1 - font;
        name = calloc(sizeof(char), len + 1);
        strncpy(name, font, len);

        /* Get subname (should be english)  */
        s2 = strchr(name, ',');
        if (s2)
          {
             len = s2 - name;
             name = realloc(name, sizeof(char) * len + 1);
             memset(name, 0, sizeof(char) * len + 1);
             strncpy(name, font, len);
          }

        if (strncmp(s1, E_TOK_STYLE, strlen(E_TOK_STYLE)) == 0)
          {
             style = s1 + strlen(E_TOK_STYLE);

             if (font_hash) efp = eina_hash_find(*font_hash, name);
             if (!efp)
               {
                  efp = calloc(1, sizeof(E_Font_Properties));
                  efp->name = eina_stringshare_add(name);
                  if (font_hash) 
		    {
		      if (!*font_hash) *font_hash = eina_hash_string_superfast_new(NULL);
		      eina_hash_add(*font_hash, name, efp);
		    }
               }
             s2 = strchr(style, ',');
             if (s2)
               {
                  char *style_old;

                  len = s2 - style;
                  style_old = style;
                  style = calloc(sizeof(char), len + 1);
                  strncpy(style, style_old, len);
                  efp->styles = eina_list_append(efp->styles, eina_stringshare_add(style));
                  free(style);
               }
             else
               efp->styles = eina_list_append(efp->styles, eina_stringshare_add(style));
          }
        free(name);
     }
   else 
     {
        if (font_hash) efp = eina_hash_find(*font_hash, font);
        if (!efp)
          {
             efp = calloc(1, sizeof(E_Font_Properties));
             efp->name = eina_stringshare_add(font);
             if (font_hash)
	       {
		 if (!*font_hash) *font_hash = eina_hash_string_superfast_new(NULL);
		 eina_hash_add(*font_hash, font, efp);
	       }
          }
     }
   return efp;
}


static Eina_Hash *
_e_font_available_hash_add(Eina_Hash *font_hash, const char *full_name)
{
   _e_font_fontconfig_name_parse(&font_hash, NULL, full_name);
   return font_hash;
}

EAPI Eina_Hash *
e_font_available_list_parse(Eina_List *list)
{
   Eina_Hash *font_hash;
   Eina_List *next;
   void *key;

   font_hash = NULL;

   /* Populate Default Font Families */
   font_hash = _e_font_available_hash_add(font_hash, "Sans:style=Regular");
   font_hash = _e_font_available_hash_add(font_hash, "Sans:style=Bold");
   font_hash = _e_font_available_hash_add(font_hash, "Sans:style=Oblique");
   font_hash = _e_font_available_hash_add(font_hash, "Sans:style=Bold Oblique");

   font_hash = _e_font_available_hash_add(font_hash, "Serif:style=Regular");
   font_hash = _e_font_available_hash_add(font_hash, "Serif:style=Bold");
   font_hash = _e_font_available_hash_add(font_hash, "Serif:style=Oblique");
   font_hash = _e_font_available_hash_add(font_hash, "Serif:style=Bold Oblique");

   font_hash = _e_font_available_hash_add(font_hash, "Monospace:style=Regular");
   font_hash = _e_font_available_hash_add(font_hash, "Monospace:style=Bold");
   font_hash = _e_font_available_hash_add(font_hash, "Monospace:style=Oblique");
   font_hash = _e_font_available_hash_add(font_hash, "Monospace:style=Bold Oblique");

   EINA_LIST_FOREACH(list, next, key)
     font_hash = _e_font_available_hash_add(font_hash, key);

   return font_hash;
}


EAPI const char *
e_font_fontconfig_name_get(const char *name, const char *style)
{
   char buf[256];

   if (!name) return NULL;
   if (!style || style[0] == 0) return eina_stringshare_add(name);
   snprintf(buf, 256, "%s"E_TOK_STYLE"%s", name, style);
   return eina_stringshare_add(buf);
}

EAPI void
e_font_fallback_clear(void)
{
   E_Font_Fallback *eff;

   EINA_LIST_FREE(e_config->font_fallbacks, eff)
     {	
	if (eff->name) eina_stringshare_del(eff->name);
	E_FREE(eff);
    }
}

EAPI void
e_font_fallback_append(const char *font)
{
   E_Font_Fallback *eff;

   e_font_fallback_remove(font);

   eff = E_NEW(E_Font_Fallback, 1);
   eff->name = eina_stringshare_add(font);
   e_config->font_fallbacks = eina_list_append(e_config->font_fallbacks, eff);
}

EAPI void
e_font_fallback_prepend(const char *font)
{
   E_Font_Fallback *eff;

   e_font_fallback_remove(font);

   eff = E_NEW(E_Font_Fallback, 1);
   eff->name = eina_stringshare_add(font);
   e_config->font_fallbacks = eina_list_prepend(e_config->font_fallbacks, eff);
}

EAPI void
e_font_fallback_remove(const char *font)
{
   Eina_List *next;
   E_Font_Fallback *eff;

   EINA_LIST_FOREACH(e_config->font_fallbacks, next, eff)
     {
	if (!strcmp(eff->name, font))
	  {
	     e_config->font_fallbacks = 
               eina_list_remove_list(e_config->font_fallbacks, next);
	     if (eff->name) eina_stringshare_del(eff->name);
	     E_FREE(eff);
	     break;
	  }
     }
}

EAPI Eina_List *
e_font_fallback_list(void)
{
   return e_config->font_fallbacks;
}

EAPI void
e_font_default_set(const char *text_class, const char *font, Evas_Font_Size size)
{
   E_Font_Default *efd;
   Eina_List *next;

   /* search for the text class */
   EINA_LIST_FOREACH(e_config->font_defaults, next, efd)
     {
	if (!strcmp(efd->text_class, text_class))
	  {
	     if (efd->font) eina_stringshare_del(efd->font);
	     efd->font = eina_stringshare_add(font);
	     efd->size = size;
	     /* move to the front of the list */
	     e_config->font_defaults = 
               eina_list_remove_list(e_config->font_defaults, next);
	     e_config->font_defaults = 
               eina_list_prepend(e_config->font_defaults, efd);
	     return;
	  }
     }

   /* the text class doesn't exist */
   efd = E_NEW(E_Font_Default, 1);
   efd->text_class = eina_stringshare_add(text_class);
   efd->font = eina_stringshare_add(font);
   efd->size = size;

   e_config->font_defaults = eina_list_prepend(e_config->font_defaults, efd);
}

/*
 * returns a pointer to the data, return null if nothing if found.
 */
EAPI E_Font_Default *
e_font_default_get(const char *text_class)
{
   E_Font_Default *efd = NULL, *defd = NULL;
   Eina_List *next;

   /* search for the text class */
   EINA_LIST_FOREACH(e_config->font_defaults, next, efd)
     {
	if (!strcmp(efd->text_class, text_class))
	  {
	     /* move to the front of the list */
	     e_config->font_defaults = 
               eina_list_remove_list(e_config->font_defaults, next);
	     e_config->font_defaults = 
               eina_list_prepend(e_config->font_defaults, efd);
	     return efd;
	  }
	if (!strcmp(efd->text_class, "default")) defd = efd;
     }
   if (!defd) defd  = efd;
   return defd;
}

EAPI void
e_font_default_remove(const char *text_class)
{
   E_Font_Default *efd;
   Eina_List *next;

   /* search for the text class */
   EINA_LIST_FOREACH(e_config->font_defaults, next, efd)
     {
	if (!strcmp(efd->text_class, text_class))
	  {
	     e_config->font_defaults = 
               eina_list_remove_list(e_config->font_defaults, next);
	     if (efd->text_class) eina_stringshare_del(efd->text_class);
	     if (efd->font) eina_stringshare_del(efd->font);
	     E_FREE(efd);
	     edje_text_class_del(text_class);
	     return;
	  }
    }
}

EAPI Eina_List *
e_font_default_list(void)
{
   return e_config->font_defaults;
}

/* return the default font name with fallbacks, font size is returned
 * in size_ret. This function is needed when all hell breaks loose and
 * we need a font name and size.
 */
EAPI const char *
e_font_default_string_get(const char *text_class, Evas_Font_Size *size_ret)
{
   E_Font_Default *efd;
   Eina_List *next;
   E_Font_Fallback *eff;
   int blen, len;

   _fn_buf[0] = 0;
   efd = e_font_default_get(text_class);
   if (!efd)
     {
	if (size_ret) *size_ret = 0;
	return "";
     }
   blen = sizeof(_fn_buf) - 1;

   len = strlen(efd->font);
   if (len < blen)
     {
	strcpy(_fn_buf, efd->font);
	blen -= len;
     }

   EINA_LIST_FOREACH(e_config->font_fallbacks, next, eff)
     {
	len = 1;
	if (len < blen)
	  {
	     strcat(_fn_buf, ",");
	     blen -= len;
	  }
	len = strlen(eff->name);
	if (len < blen)
	  {
	     strcat(_fn_buf, eff->name);
	     blen -= len;
	  }
     }

   if (size_ret) *size_ret = efd->size;
   return _fn_buf;
}