Evas font-ot: Fixed mistakes that caused ilegal reads.
[framework/uifw/evas.git] / src / lib / engines / common / evas_font_ot.c
1 #include "evas_font_ot.h"
2
3 #ifdef OT_SUPPORT
4 # include <hb.h>
5 # include <hb-ft.h>
6 #endif
7
8 #include "evas_common.h"
9
10 #include <Eina.h>
11 #include "evas_font_private.h"
12
13 EAPI Eina_Bool
14 evas_common_font_ot_is_enabled(void)
15 {
16 #ifdef OT_SUPPORT
17    static int ret = -1;
18    const char *env;
19    if (ret != -1)
20      {
21         return ret;
22      }
23
24    env = getenv("EVAS_USE_OT");
25    if (env && atoi(env))
26      {
27         ret = EINA_TRUE;
28         return ret;
29      }
30 #endif
31    return EINA_FALSE;
32 }
33
34 #ifdef OT_SUPPORT
35 /* FIXME: doc. returns #items */
36 EAPI int
37 evas_common_font_ot_cluster_size_get(const Evas_Text_Props *props, size_t char_index, int orig_len)
38 {
39    int i;
40    int items;
41    int left_bound, right_bound;
42    size_t base_cluster = EVAS_FONT_OT_POS_GET(props->ot_data->items[char_index]);
43    for (i = (int) char_index ;
44          (i >= 0) &&
45          (EVAS_FONT_OT_POS_GET(props->ot_data->items[i]) == base_cluster) ;
46          i--)
47      ;
48    left_bound = i;
49    for (i = (int) char_index + 1;
50          (i < (int) props->ot_data->len) &&
51          (EVAS_FONT_OT_POS_GET(props->ot_data->items[i]) == base_cluster) ;
52          i++)
53      ;
54    right_bound = i;
55    if (right_bound == left_bound)
56      {
57         items = 1;
58      }
59    else if (props->bidi.dir == EVAS_BIDI_DIRECTION_RTL)
60      {
61         if (left_bound < 0)
62           {
63              items = orig_len -
64                 props->ot_data->items[left_bound + 1].source_cluster;
65           }
66         else
67           {
68              items = props->ot_data->items[left_bound].source_cluster -
69                 props->ot_data->items[left_bound + 1].source_cluster;
70           }
71      }
72    else
73      {
74         if (right_bound == (int) props->ot_data->len)
75           {
76              items = orig_len -
77                 props->ot_data->items[right_bound - 1].source_cluster;
78           }
79         else
80           {
81              items = props->ot_data->items[right_bound].source_cluster -
82                 props->ot_data->items[right_bound - 1].source_cluster;
83           }
84      }
85    return (items > 0) ? items : 1;
86 }
87
88 static void
89 _evas_common_font_ot_shape(hb_buffer_t *buffer, FT_Face face)
90 {
91    hb_face_t   *hb_face;
92    hb_font_t   *hb_font;
93
94    hb_face = hb_ft_face_create(face, NULL);
95    hb_font = hb_ft_font_create(face, NULL);
96
97    hb_shape(hb_font, hb_face, buffer, NULL, 0);
98    hb_font_destroy(hb_font);
99    hb_face_destroy(hb_face);
100 }
101
102 EAPI Eina_Bool
103 evas_common_font_ot_populate_text_props(void *_fn, const Eina_Unicode *text,
104       Evas_Text_Props *props, int len)
105 {
106    RGBA_Font *fn = (RGBA_Font *) _fn;
107    RGBA_Font_Int *fi;
108    hb_buffer_t *buffer;
109    hb_glyph_position_t *positions;
110    hb_glyph_info_t *infos;
111    int slen;
112    unsigned int i;
113    if (!evas_common_font_ot_is_enabled()) return EINA_TRUE;
114    if (props->ot_data)
115      {
116         evas_common_font_ot_props_unref(props->ot_data);
117      }
118    props->ot_data = calloc(1, sizeof(Evas_Font_OT_Data));
119    props->ot_data->refcount = 1;
120
121    fi = fn->fonts->data;
122    if (fi->src->current_size != fi->size)
123      {
124         FTLOCK();
125         FT_Activate_Size(fi->ft.size);
126         FTUNLOCK();
127         fi->src->current_size = fi->size;
128      }
129    /* Load the font needed for this script */
130    evas_common_font_glyph_search(fn, &fi, *text);
131
132    if (len < 0)
133      {
134         slen = eina_unicode_strlen(text);
135      }
136    else
137      {
138         slen = len;
139      }
140
141    buffer = hb_buffer_create(slen);
142    hb_buffer_set_unicode_funcs(buffer, evas_common_language_unicode_funcs_get());
143    hb_buffer_set_language(buffer, hb_language_from_string(
144             evas_common_language_from_locale_get()));
145    hb_buffer_set_script(buffer, props->script);
146    hb_buffer_set_direction(buffer,
147          (props->bidi.dir == EVAS_BIDI_DIRECTION_RTL) ?
148          HB_DIRECTION_RTL : HB_DIRECTION_LTR);
149    /* FIXME: add run-time conversions if needed, which is very unlikely */
150    hb_buffer_add_utf32(buffer, (const uint32_t *) text, slen, 0, slen);
151
152    _evas_common_font_ot_shape(buffer, fi->src->ft.face);
153
154    props->ot_data->len = hb_buffer_get_length(buffer);
155    props->ot_data->items = calloc(props->ot_data->len,
156          sizeof(Evas_Font_OT_Data_Item));
157    positions = hb_buffer_get_glyph_positions(buffer);
158    infos = hb_buffer_get_glyph_infos(buffer);
159    for (i = 0 ; i < props->ot_data->len ; i++)
160      {
161         props->ot_data->items[i].index = infos[i].codepoint;
162         props->ot_data->items[i].source_cluster = infos[i].cluster;
163         props->ot_data->items[i].x_advance = positions[i].x_advance;
164         props->ot_data->items[i].x_offset = positions[i].x_offset;
165         props->ot_data->items[i].y_offset = positions[i].y_offset;
166      }
167
168    hb_buffer_destroy(buffer);
169
170    return EINA_FALSE;
171 }
172
173 EAPI void
174 evas_common_font_ot_props_ref(Evas_Font_OT_Data *data)
175 {
176    data->refcount++;
177 }
178
179 EAPI void
180 evas_common_font_ot_props_unref(Evas_Font_OT_Data *data)
181 {
182    OTLOCK();
183    if (--data->refcount == 0)
184      {
185         if (data->items)
186           free(data->items);
187         free(data);
188      }
189    OTUNLOCK();
190 }
191 #endif
192