summaryrefslogtreecommitdiff
path: root/src/lib/evas/cserve2/evas_cs2_utils.c
blob: cc4a99b5d544b4c3126e3836775be7ac573bad93 (plain) (blame)
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
/* THIS FILE TO BE SHARED WITH THE BIN PART. KEEP IT CLEAN. THERE BE DRAGONS */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <Eina.h>

#include "evas_cs2_utils.h"

/* fash */
typedef struct _Fash_Glyph_Map  Fash_Glyph_Map;
typedef struct _Fash_Glyph_Map2 Fash_Glyph_Map2;

struct _Fash_Glyph_Map
{
   void *item[256];
};

struct _Fash_Glyph_Map2
{
   Fash_Glyph_Map *bucket[256];
};

struct _Fash_Glyph2
{
   Fash_Glyph_Map2 *bucket[256];
   void (*free_cb)(void *glyph);
};

static void
_fash_item_free(Fash_Glyph2 *fash, Fash_Glyph_Map *map)
{
   int i;

   if (fash->free_cb)
     for (i = 0; i < 256; i++)
       if (map->item[i])
         fash->free_cb(map->item[i]);
   free(map);
}

static void
_fash_gl2_free(Fash_Glyph2 *fash, Fash_Glyph_Map2 *fash2)
{
   int i;

   for (i = 0; i < 256; i++)
     if (fash2->bucket[i]) _fash_item_free(fash, fash2->bucket[i]);
   free(fash2);
}

void
fash_gl_free(Fash_Glyph2 *fash)
{
   int i;

   if (!fash) return;

   for (i = 0; i < 256; i++)
     if (fash->bucket[i]) _fash_gl2_free(fash, fash->bucket[i]);
   free(fash);
}

Fash_Glyph2 *
fash_gl_new(void (*free_cb)(void *glyph))
{
   Fash_Glyph2 *fash = calloc(1, sizeof(Fash_Glyph2));
   fash->free_cb = free_cb;
   return fash;
}

void *
fash_gl_find(Fash_Glyph2 *fash, int item)
{
   int grp, maj, min;

   EINA_SAFETY_ON_NULL_RETURN_VAL(fash, NULL);

   // 24bits for unicode - v6 up to E01EF (chrs) & 10FFFD for private use (plane 16)
   grp = (item >> 16) & 0xff;
   maj = (item >> 8) & 0xff;
   min = item & 0xff;
   if (!fash->bucket[grp]) return NULL;
   if (!fash->bucket[grp]->bucket[maj]) return NULL;
   return fash->bucket[grp]->bucket[maj]->item[min];
}

void
fash_gl_add(Fash_Glyph2 *fash, int item, void *glyph)
{
   int grp, maj, min;

   EINA_SAFETY_ON_NULL_RETURN(fash);

   // 24bits for unicode - v6 up to E01EF (chrs) & 10FFFD for private use (plane 16)
   grp = (item >> 16) & 0xff;
   maj = (item >> 8) & 0xff;
   min = item & 0xff;
   if (!fash->bucket[grp])
     fash->bucket[grp] = calloc(1, sizeof(Fash_Glyph_Map2));
   EINA_SAFETY_ON_NULL_RETURN(fash->bucket[grp]);
   if (!fash->bucket[grp]->bucket[maj])
     fash->bucket[grp]->bucket[maj] = calloc(1, sizeof(Fash_Glyph_Map));
   EINA_SAFETY_ON_NULL_RETURN(fash->bucket[grp]->bucket[maj]);
   fash->bucket[grp]->bucket[maj]->item[min] = glyph;
}

void
fash_gl_del(Fash_Glyph2 *fash, int item)
{
   int grp, maj, min;
   void *data;

   EINA_SAFETY_ON_NULL_RETURN(fash);

   // 24bits for unicode - v6 up to E01EF (chrs) & 10FFFD for private use (plane 16)
   grp = (item >> 16) & 0xff;
   maj = (item >> 8) & 0xff;
   min = item & 0xff;
   if (!fash->bucket[grp]) return;
   if (!fash->bucket[grp]->bucket[maj]) return;
   if (!fash->bucket[grp]->bucket[maj]->item[min]) return;

   data = fash->bucket[grp]->bucket[maj]->item[min];
   if (fash->free_cb)
     fash->free_cb(data);
   fash->bucket[grp]->bucket[maj]->item[min] = NULL;
}