aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/edje/src/lib/edje_textblock_styles.c
blob: 13a68ada917c112daf1e8f6853259bee1ccb71f8 (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
#include "edje_private.h"

static int
_edje_font_is_embedded(Edje_File *edf, char *font)
{
   if (!eina_hash_find(edf->fonts, font)) return 0;
   return 1;
}

static void
_edje_format_param_parse(char *item, char **key, char **val)
{
   char *p, *k, *v;

   p = strchr(item, '=');
   k = malloc(p - item + 1);
   strncpy(k, item, p - item);
   k[p - item] = 0;
   *key = k;
   p++;
   v = strdup(p);
   *val = v;
}

static char *
_edje_format_parse(const char **s)
{
   char *item, *ds;
   const char *p, *ss;
   const char *s1 = NULL;
   const char *s2 = NULL;

   p = *s;
   if ((!p) || (*p == 0)) return NULL;
   for (;;)
     {
	if (!s1)
	  {
	     if (*p != ' ') s1 = p;
	     if (*p == 0) break;
	  }
	else if (!s2)
	  {
	     if ((p > *s) && (p[-1] != '\\'))
	       {
		  if (*p == ' ') s2 = p;
	       }
	     if (*p == 0) s2 = p;
	  }
	p++;
	if (s1 && s2)
	  {
	     item = malloc(s2 - s1 + 1);
	     if (item)
	       {
		  for (ds = item, ss = s1; ss < s2; ss++, ds++)
		    {
		       if ((*ss == '\\') && (ss < (s2 - 1))) ss++;
		       *ds = *ss;
		    }
		  *ds = 0;
	       }
	     *s = s2;
	     return item;
	  }
     }
   *s = p;
   return NULL;
}

static int
_edje_format_is_param(char *item)
{
   if (strchr(item, '=')) return 1;
   return 0;
}

static char *
_edje_format_reparse(Edje_File *edf, const char *str, Edje_Style_Tag **tag_ret)
{
   Eina_Strbuf *txt, *tmp = NULL;
   char *s2, *item, *ret;
   const char *s;

   txt = eina_strbuf_new();
   s = str;
   while ((item = _edje_format_parse(&s)))
     {
	if (_edje_format_is_param(item))
	  {
	     char *key = NULL, *val = NULL;

	     _edje_format_param_parse(item, &key, &val);
	     if (!strcmp(key, "font_source"))
	       {
		  /* dont allow font sources */
	       }
	     else if (!strcmp(key, "text_class"))
	       {
		  if (tag_ret)
		    (*tag_ret)->text_class = eina_stringshare_add(val);
	       }
	     else if (!strcmp(key, "font_size"))
	       {
		  if (tag_ret)
		    (*tag_ret)->font_size = atof(val);
	       }
	     else if (!strcmp(key, "font")) /* Fix fonts */
	       {
		  if (tag_ret)
		    {
		       if (_edje_font_is_embedded(edf, val))
			 {
			    if (!tmp)
			      tmp = eina_strbuf_new();
			    eina_strbuf_append(tmp, "edje/fonts/");
			    eina_strbuf_append(tmp, val);
			    (*tag_ret)->font = eina_stringshare_add(eina_strbuf_string_get(tmp));
			    eina_strbuf_reset(tmp);
			 }
		       else
			 {
			    (*tag_ret)->font = eina_stringshare_add(val);
			 }
		    }
	       }
	     else /* Otherwise add to tag buffer */
	       {
		  s2 = eina_str_escape(item);
		  if (s2)
		    {
		       if (eina_strbuf_length_get(txt)) eina_strbuf_append(txt, " ");
		       eina_strbuf_append(txt, s2);
		       free(s2);
		    }
	       }
	     free(key);
	     free(val);
	  }
	else
	  {
	     if (eina_strbuf_length_get(txt)) eina_strbuf_append(txt, " ");
	     eina_strbuf_append(txt, item);
	  }
	free(item);
     }
   if (tmp)
     eina_strbuf_free(tmp);
   ret = eina_strbuf_string_steal(txt);
   eina_strbuf_free(txt);
   return ret;
}

/* Update all evas_styles which are in an edje
 *
 * @param ed	The edje containing styles which need to be updated
 */
void
_edje_textblock_style_all_update(Edje *ed)
{
   Eina_List *l, *ll;
   Edje_Style *stl;
   Eina_Strbuf *txt = NULL;

   if (!ed->file) return;

   EINA_LIST_FOREACH(ed->file->styles, l, stl)
     {
	Edje_Style_Tag *tag;
	Edje_Text_Class *tc;
	int found = 0;
	char *fontset = NULL, *fontsource = NULL;

	/* Make sure the style is already defined */
	if (!stl->style) break;

	/* Make sure the style contains a text_class */
	EINA_LIST_FOREACH(stl->tags, ll, tag)
	  if (tag->text_class) found = 1;

	/* No text classes , goto next style */
	if (!found) continue;
	found = 0;
	if (!txt)
	  txt = eina_strbuf_new();

	if (_edje_fontset_append)
	  fontset = eina_str_escape(_edje_fontset_append);
	fontsource = eina_str_escape(ed->file->path);

	/* Build the style from each tag */
	EINA_LIST_FOREACH(stl->tags, ll, tag)
	  {
	     if (!tag->key) continue;

	     /* Add Tag Key */
	     eina_strbuf_append(txt, tag->key);
	     eina_strbuf_append(txt, "='");

	     /* Configure fonts from text class if it exists */
	     if ((tc = _edje_text_class_find(ed, tag->text_class)))
	       {
		  /* Only update if not clearing, If clear leave it at zero */
		  if (tc->font) found = 1;
	       }

	     /* Add and Ha`ndle tag parsed data */
	     eina_strbuf_append(txt, tag->value);

	     if (!strcmp(tag->key, "DEFAULT"))
	       {
		  if (fontset)
		    {
		       eina_strbuf_append(txt, " ");
		       eina_strbuf_append(txt, "font_fallbacks=");
		       eina_strbuf_append(txt, fontset);
		    }
		  eina_strbuf_append(txt, " ");
		  eina_strbuf_append(txt, "font_source=");
		  eina_strbuf_append(txt, fontsource);
	       }
	     if (tag->font_size != 0)
	       {
		  char font_size[32];

		  if (found)
		    snprintf(font_size, sizeof(font_size), "%f", (double) _edje_text_size_calc(tag->font_size, tc));
		  else
		    snprintf(font_size, sizeof(font_size), "%f", tag->font_size);

		  eina_strbuf_append(txt, " ");
		  eina_strbuf_append(txt, "font_size=");
		  eina_strbuf_append(txt, font_size);
	       }
	     /* Add font name last to save evas from multiple loads */
	     if (tag->font)
	       {
		  const char *f;

		  eina_strbuf_append(txt, " ");
		  eina_strbuf_append(txt, "font=");

		  f = (found) ? tc->font : tag->font;
		  eina_strbuf_append_escaped(txt, f);
	       }
	     found = 0;

	     eina_strbuf_append(txt, "'");
	  }
	if (fontset) free(fontset);
	if (fontsource) free(fontsource);

	/* Configure the style */
	evas_textblock_style_set(stl->style, eina_strbuf_string_get(txt));
	eina_strbuf_reset(txt);
     }
   if (txt)
     eina_strbuf_free(txt);
}

void
_edje_textblock_styles_add(Edje *ed)
{
   Eina_List *l, *ll;
   Edje_Style *stl;

   if (!ed->file) return;

   EINA_LIST_FOREACH(ed->file->styles, l, stl)
     {
	Edje_Style_Tag *tag;

	/* Make sure the style contains the text_class */
	EINA_LIST_FOREACH(stl->tags, ll, tag)
	  {
	    if (!tag->text_class) continue;
	    _edje_text_class_member_add(ed, tag->text_class);
	  }
     }
}

void
_edje_textblock_styles_del(Edje *ed)
{
   Eina_List *l, *ll;
   Edje_Style *stl;

   if (!ed->file) return;

   EINA_LIST_FOREACH(ed->file->styles, l, stl)
     {
	Edje_Style_Tag *tag;

	/* Make sure the style contains the text_class */
	EINA_LIST_FOREACH(stl->tags, ll, tag)
	  {
	     if (!tag->text_class) continue;
	     _edje_text_class_member_del(ed, tag->text_class);
	  }
     }
}

/* When we get to here the edje file had been read into memory
 * the name of the style is established as well as the name and
 * data for the tags.  This function will create the Evas_Style
 * object for each style. The style is composed of a base style
 * followed by a list of tags.
 */
void
_edje_textblock_style_parse_and_fix(Edje_File *edf)
{
   Eina_Strbuf *txt = NULL;
   Eina_List *l, *ll;
   Edje_Style *stl;

   EINA_LIST_FOREACH(edf->styles, l, stl)
     {
	Edje_Style_Tag *tag;
	char *fontset = NULL, *fontsource = NULL, *ts;

	if (stl->style) break;

	if (!txt)
	  txt = eina_strbuf_new();

	stl->style = evas_textblock_style_new();
	evas_textblock_style_set(stl->style, NULL);

	if (_edje_fontset_append)
	  fontset = eina_str_escape(_edje_fontset_append);
	fontsource = eina_str_escape(edf->path);

	/* Build the style from each tag */
	EINA_LIST_FOREACH(stl->tags, ll, tag)
	  {
	     if (!tag->key) continue;

	     /* Add Tag Key */
	     eina_strbuf_append(txt, tag->key);
	     eina_strbuf_append(txt, "='");

	     ts = _edje_format_reparse(edf, tag->value, &(tag));

	     /* Add and Handle tag parsed data */
	     if (ts)
	       {
		  if (eet_dictionary_string_check(eet_dictionary_get(edf->ef), tag->value) == 0)
		    eina_stringshare_del(tag->value);
		  tag->value = eina_stringshare_add(ts);
		  eina_strbuf_append(txt, tag->value);
		  free(ts);
	       }

	     if (!strcmp(tag->key, "DEFAULT"))
	       {
		  if (fontset)
		    {
		       eina_strbuf_append(txt, " ");
		       eina_strbuf_append(txt, "font_fallbacks=");
		       eina_strbuf_append(txt, fontset);
		    }
		  eina_strbuf_append(txt, " ");
		  eina_strbuf_append(txt, "font_source=");
		  eina_strbuf_append(txt, fontsource);
	       }
	     if (tag->font_size > 0)
	       {
		  char font_size[32];

		  snprintf(font_size, sizeof(font_size), "%f", tag->font_size);
		  eina_strbuf_append(txt, " ");
		  eina_strbuf_append(txt, "font_size=");
		  eina_strbuf_append(txt, font_size);
	       }
	     /* Add font name last to save evas from multiple loads */
	     if (tag->font)
	       {
		  eina_strbuf_append(txt, " ");
		  eina_strbuf_append(txt, "font=");
		  eina_strbuf_append_escaped(txt, tag->font);
	       }
	     eina_strbuf_append(txt, "'");
	  }
	if (fontset) free(fontset);
	if (fontsource) free(fontsource);

	/* Configure the style */
	evas_textblock_style_set(stl->style, eina_strbuf_string_get(txt));
	eina_strbuf_reset(txt);
     }
   if (txt)
     eina_strbuf_free(txt);
}

void
_edje_textblock_style_cleanup(Edje_File *edf)
{
   while (edf->styles)
     {
	Edje_Style *stl;

	stl = edf->styles->data;
	edf->styles = eina_list_remove_list(edf->styles, edf->styles);
	while (stl->tags)
	  {
	     Edje_Style_Tag *tag;

	     tag = stl->tags->data;
	     stl->tags = eina_list_remove_list(stl->tags, stl->tags);
             if (edf->free_strings)
               {
                  if (tag->key) eina_stringshare_del(tag->key);
/*                FIXME: Find a proper way to handle it. */
                  if (tag->value) eina_stringshare_del(tag->value);
                  if (tag->text_class) eina_stringshare_del(tag->text_class);
                  if (tag->font) eina_stringshare_del(tag->font);
               }
             free(tag);
	  }
        if (edf->free_strings && stl->name) eina_stringshare_del(stl->name);
	if (stl->style) evas_textblock_style_free(stl->style);
	free(stl);
     }
}