Snapshot.
[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)
38 {
39    int i;
40    int items;
41    int left_bound, right_bound;
42    char_index += props->start;
43    size_t base_cluster = EVAS_FONT_OT_POS_GET(props->info->ot[char_index]);
44    for (i = (int) char_index ;
45          (i >= (int) props->start) &&
46          (EVAS_FONT_OT_POS_GET(props->info->ot[i]) == base_cluster) ;
47          i--)
48      ;
49    left_bound = i;
50    for (i = (int) char_index + 1;
51          (i < (int) (props->start + props->len)) &&
52          (EVAS_FONT_OT_POS_GET(props->info->ot[i]) == base_cluster) ;
53          i++)
54      ;
55    right_bound = i;
56    if (right_bound == left_bound)
57      {
58         items = 1;
59      }
60    else if (props->bidi.dir == EVAS_BIDI_DIRECTION_RTL)
61      {
62         if (left_bound < 0)
63           {
64              items = props->start + props->len -
65                 props->info->ot[left_bound + 1].source_cluster;
66           }
67         else
68           {
69              items = props->info->ot[left_bound].source_cluster -
70                 props->info->ot[left_bound + 1].source_cluster;
71           }
72      }
73    else
74      {
75         if (right_bound == (int) props->len)
76           {
77              items = props->start + props->len -
78                 props->info->ot[right_bound - 1].source_cluster;
79           }
80         else
81           {
82              items = props->info->ot[right_bound].source_cluster -
83                 props->info->ot[right_bound - 1].source_cluster;
84           }
85      }
86    return (items > 0) ? items : 1;
87 }
88
89 EAPI void
90 evas_common_font_ot_load_face(void *_font)
91 {
92    RGBA_Font_Source *font = (RGBA_Font_Source *) _font;
93    /* Unload the face if by any chance it's already loaded */
94    evas_common_font_ot_unload_face(font);
95    font->hb.face = hb_ft_face_create(font->ft.face, NULL);
96 }
97
98 EAPI void
99 evas_common_font_ot_unload_face(void *_font)
100 {
101    RGBA_Font_Source *font = (RGBA_Font_Source *) _font;
102    if (!font->hb.face) return;
103    hb_face_destroy(font->hb.face);
104    font->hb.face = NULL;
105 }
106
107 static void
108 _evas_common_font_ot_shape(hb_buffer_t *buffer, RGBA_Font_Source *src)
109 {
110    hb_font_t   *hb_font;
111
112    hb_font = hb_ft_font_create(src->ft.face, NULL);
113
114    hb_shape(hb_font, src->hb.face, buffer, NULL, 0);
115    hb_font_destroy(hb_font);
116 }
117
118 EAPI Eina_Bool
119 evas_common_font_ot_populate_text_props(void *_fn, const Eina_Unicode *text,
120       Evas_Text_Props *props, int len)
121 {
122    RGBA_Font *fn = (RGBA_Font *) _fn;
123    RGBA_Font_Int *fi;
124    hb_buffer_t *buffer;
125    hb_glyph_position_t *positions;
126    hb_glyph_info_t *infos;
127    int slen;
128    unsigned int i;
129    if (!evas_common_font_ot_is_enabled()) return EINA_TRUE;
130
131    fi = fn->fonts->data;
132    /* Load the font needed for this script */
133      {
134         /* Skip common chars */
135         const Eina_Unicode *tmp;
136         for (tmp = text ;
137               *tmp &&
138               evas_common_language_char_script_get(*tmp) == EVAS_SCRIPT_COMMON ;
139               tmp++)
140           ;
141         if (!*tmp && (tmp > text)) tmp--;
142         evas_common_font_glyph_search(fn, &fi, *tmp);
143      }
144    evas_common_font_int_reload(fi);
145    if (fi->src->current_size != fi->size)
146      {
147         FTLOCK();
148         FT_Activate_Size(fi->ft.size);
149         FTUNLOCK();
150         fi->src->current_size = fi->size;
151      }
152
153    if (len < 0)
154      {
155         slen = eina_unicode_strlen(text);
156      }
157    else
158      {
159         slen = len;
160      }
161
162    buffer = hb_buffer_create(slen);
163    hb_buffer_set_unicode_funcs(buffer, evas_common_language_unicode_funcs_get());
164    hb_buffer_set_language(buffer, hb_language_from_string(
165             evas_common_language_from_locale_get()));
166    hb_buffer_set_script(buffer, props->script);
167    hb_buffer_set_direction(buffer,
168          (props->bidi.dir == EVAS_BIDI_DIRECTION_RTL) ?
169          HB_DIRECTION_RTL : HB_DIRECTION_LTR);
170    /* FIXME: add run-time conversions if needed, which is very unlikely */
171    hb_buffer_add_utf32(buffer, (const uint32_t *) text, slen, 0, slen);
172
173    _evas_common_font_ot_shape(buffer, fi->src);
174
175    props->len = hb_buffer_get_length(buffer);
176    props->info->ot = calloc(props->len,
177          sizeof(Evas_Font_OT_Info));
178    props->info->glyph = calloc(props->len,
179               sizeof(Evas_Font_Glyph_Info));
180    positions = hb_buffer_get_glyph_positions(buffer);
181    infos = hb_buffer_get_glyph_infos(buffer);
182    for (i = 0 ; i < props->len ; i++)
183      {
184         props->info->ot[i].source_cluster = infos[i].cluster;
185         props->info->ot[i].x_offset = positions[i].x_offset;
186         props->info->ot[i].y_offset = positions[i].y_offset;
187         props->info->glyph[i].index = infos[i].codepoint;
188         props->info->glyph[i].advance = positions[i].x_advance;
189      }
190
191    hb_buffer_destroy(buffer);
192    evas_common_font_int_use_trim();
193
194    return EINA_FALSE;
195 }
196
197 #endif
198