1 #include "evas_font_private.h"
2 
3 #ifdef USE_HARFBUZZ
4 # include <hb.h>
5 # include <hb-ft.h>
6 #endif
7 
8 #ifdef USE_HARFBUZZ
9 static const hb_script_t
10 _evas_script_to_harfbuzz[] =
11 {
12   HB_SCRIPT_COMMON,
13   HB_SCRIPT_INHERITED,
14   HB_SCRIPT_ARABIC,
15   HB_SCRIPT_ARMENIAN,
16   HB_SCRIPT_BENGALI,
17   HB_SCRIPT_BOPOMOFO,
18   HB_SCRIPT_CHEROKEE,
19   HB_SCRIPT_COPTIC,
20   HB_SCRIPT_CYRILLIC,
21   HB_SCRIPT_DESERET,
22   HB_SCRIPT_DEVANAGARI,
23   HB_SCRIPT_ETHIOPIC,
24   HB_SCRIPT_GEORGIAN,
25   HB_SCRIPT_GOTHIC,
26   HB_SCRIPT_GREEK,
27   HB_SCRIPT_GUJARATI,
28   HB_SCRIPT_GURMUKHI,
29   HB_SCRIPT_HAN,
30   HB_SCRIPT_HANGUL,
31   HB_SCRIPT_HEBREW,
32   HB_SCRIPT_HIRAGANA,
33   HB_SCRIPT_KANNADA,
34   HB_SCRIPT_KATAKANA,
35   HB_SCRIPT_KHMER,
36   HB_SCRIPT_LAO,
37   HB_SCRIPT_LATIN,
38   HB_SCRIPT_MALAYALAM,
39   HB_SCRIPT_MONGOLIAN,
40   HB_SCRIPT_MYANMAR,
41   HB_SCRIPT_OGHAM,
42   HB_SCRIPT_OLD_ITALIC,
43   HB_SCRIPT_ORIYA,
44   HB_SCRIPT_RUNIC,
45   HB_SCRIPT_SINHALA,
46   HB_SCRIPT_SYRIAC,
47   HB_SCRIPT_TAMIL,
48   HB_SCRIPT_TELUGU,
49   HB_SCRIPT_THAANA,
50   HB_SCRIPT_THAI,
51   HB_SCRIPT_TIBETAN,
52   HB_SCRIPT_CANADIAN_ABORIGINAL,
53   HB_SCRIPT_YI,
54   HB_SCRIPT_TAGALOG,
55   HB_SCRIPT_HANUNOO,
56   HB_SCRIPT_BUHID,
57   HB_SCRIPT_TAGBANWA,
58 
59   /* Unicode-4.0 additions */
60   HB_SCRIPT_BRAILLE,
61   HB_SCRIPT_CYPRIOT,
62   HB_SCRIPT_LIMBU,
63   HB_SCRIPT_OSMANYA,
64   HB_SCRIPT_SHAVIAN,
65   HB_SCRIPT_LINEAR_B,
66   HB_SCRIPT_TAI_LE,
67   HB_SCRIPT_UGARITIC,
68 
69   /* Unicode-4.1 additions */
70   HB_SCRIPT_NEW_TAI_LUE,
71   HB_SCRIPT_BUGINESE,
72   HB_SCRIPT_GLAGOLITIC,
73   HB_SCRIPT_TIFINAGH,
74   HB_SCRIPT_SYLOTI_NAGRI,
75   HB_SCRIPT_OLD_PERSIAN,
76   HB_SCRIPT_KHAROSHTHI,
77 
78   /* Unicode-5.0 additions */
79   HB_SCRIPT_UNKNOWN,
80   HB_SCRIPT_BALINESE,
81   HB_SCRIPT_CUNEIFORM,
82   HB_SCRIPT_PHOENICIAN,
83   HB_SCRIPT_PHAGS_PA,
84   HB_SCRIPT_NKO,
85 
86   /* Unicode-5.1 additions */
87   HB_SCRIPT_KAYAH_LI,
88   HB_SCRIPT_LEPCHA,
89   HB_SCRIPT_REJANG,
90   HB_SCRIPT_SUNDANESE,
91   HB_SCRIPT_SAURASHTRA,
92   HB_SCRIPT_CHAM,
93   HB_SCRIPT_OL_CHIKI,
94   HB_SCRIPT_VAI,
95   HB_SCRIPT_CARIAN,
96   HB_SCRIPT_LYCIAN,
97   HB_SCRIPT_LYDIAN,
98 
99   /* Unicode-5.2 additions */
100   HB_SCRIPT_AVESTAN,
101   HB_SCRIPT_BAMUM,
102   HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
103   HB_SCRIPT_IMPERIAL_ARAMAIC,
104   HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
105   HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
106   HB_SCRIPT_JAVANESE,
107   HB_SCRIPT_KAITHI,
108   HB_SCRIPT_TAI_THAM,
109   HB_SCRIPT_LISU,
110   HB_SCRIPT_MEETEI_MAYEK,
111   HB_SCRIPT_OLD_SOUTH_ARABIAN,
112   HB_SCRIPT_OLD_TURKIC,
113   HB_SCRIPT_SAMARITAN,
114   HB_SCRIPT_TAI_VIET,
115 
116   /* Unicode-6.0 additions */
117   HB_SCRIPT_BATAK,
118   HB_SCRIPT_BRAHMI,
119   HB_SCRIPT_MANDAIC
120 };
121 #endif
122 
123 #ifdef OT_SUPPORT
124 /* FIXME: doc. returns #items */
125 EAPI int
evas_common_font_ot_cluster_size_get(const Evas_Text_Props * props,size_t char_index)126 evas_common_font_ot_cluster_size_get(const Evas_Text_Props *props, size_t char_index)
127 {
128    int i;
129    int items;
130    int left_bound, right_bound;
131    size_t base_cluster;
132    char_index += props->start;
133    base_cluster = EVAS_FONT_OT_POS_GET(props->info->ot[char_index]);
134    for (i = (int) char_index ;
135          (i >= (int) props->start) &&
136          (EVAS_FONT_OT_POS_GET(props->info->ot[i]) == base_cluster) ;
137          i--)
138      ;
139    left_bound = i;
140    for (i = (int) char_index + 1;
141          (i < (int) (props->start + props->len)) &&
142          (EVAS_FONT_OT_POS_GET(props->info->ot[i]) == base_cluster) ;
143          i++)
144      ;
145    right_bound = i;
146 
147    if (right_bound == left_bound)
148      {
149         items = 1;
150      }
151    else if (props->bidi_dir == EVAS_BIDI_DIRECTION_RTL)
152      {
153         if (left_bound < 0)
154           {
155              items = props->text_offset + props->text_len - base_cluster;
156           }
157         else
158           {
159              items = props->info->ot[left_bound].source_cluster - base_cluster;
160           }
161      }
162    else
163      {
164         if (right_bound >= (int) (props->start + props->len))
165           {
166              items = props->text_offset + props->text_len - base_cluster;
167           }
168         else
169           {
170              items = props->info->ot[right_bound].source_cluster - base_cluster;
171           }
172      }
173    return (items > 0) ? items : 1;
174 }
175 
176 /* Harfbuzz font functions */
177 
178 static hb_position_t
_evas_common_font_ot_hb_get_glyph_advance(hb_font_t * font,void * font_data,hb_codepoint_t glyph,void * user_data)179 _evas_common_font_ot_hb_get_glyph_advance(hb_font_t *font,
180       void *font_data, hb_codepoint_t glyph,
181       void *user_data)
182 {
183    /* Use our cache*/
184    RGBA_Font_Int *fi = (RGBA_Font_Int *) font_data;
185    RGBA_Font_Glyph *fg;
186    (void) font;
187    (void) user_data;
188    fg = evas_common_font_int_cache_glyph_get(fi, glyph);
189    if (fg)
190      {
191         return fg->advance.x >> 10;
192      }
193    return 0;
194 }
195 
196 static hb_position_t
_evas_common_font_ot_hb_get_kerning(hb_font_t * font,void * font_data,hb_codepoint_t first_glyph,hb_codepoint_t second_glyph,void * user_data)197 _evas_common_font_ot_hb_get_kerning(hb_font_t *font, void *font_data,
198    hb_codepoint_t first_glyph, hb_codepoint_t second_glyph, void *user_data)
199 {
200    RGBA_Font_Int *fi = (RGBA_Font_Int *) font_data;
201    int kern;
202    (void) font;
203    (void) user_data;
204    if (evas_common_font_query_kerning(fi, first_glyph, second_glyph, &kern))
205       return kern;
206 
207    return 0;
208 }
209 
210 /* End of harfbuzz font funcs */
211 
212 static inline hb_font_funcs_t *
_evas_common_font_ot_font_funcs_get(void)213 _evas_common_font_ot_font_funcs_get(void)
214 {
215    static hb_font_funcs_t *font_funcs = NULL;
216    if (!font_funcs)
217      {
218         font_funcs = hb_font_funcs_create();
219         hb_font_funcs_set_glyph_h_advance_func(font_funcs,
220             _evas_common_font_ot_hb_get_glyph_advance, NULL, NULL);
221         hb_font_funcs_set_glyph_h_kerning_func(font_funcs,
222             _evas_common_font_ot_hb_get_kerning, NULL, NULL);
223      }
224 
225    return font_funcs;
226 }
227 
228 static inline hb_unicode_funcs_t *
_evas_common_font_ot_unicode_funcs_get(void)229 _evas_common_font_ot_unicode_funcs_get(void)
230 {
231    static hb_unicode_funcs_t *unicode_funcs = NULL;
232    if (!unicode_funcs)
233      {
234         unicode_funcs = hb_unicode_funcs_get_default();
235      }
236 
237    return unicode_funcs;
238 }
239 
240 static void
_evas_common_font_ot_shape(hb_buffer_t * buffer,RGBA_Font_Int * fi,Evas_Text_Props_Mode mode)241 _evas_common_font_ot_shape(hb_buffer_t *buffer, RGBA_Font_Int *fi, Evas_Text_Props_Mode mode)
242 {
243    /* Create hb_font if not previously created */
244    if (!fi->ft.hb_font)
245      {
246         hb_font_t *hb_ft_font;
247 
248         hb_ft_font = hb_ft_font_create(fi->src->ft.face, NULL);
249         fi->ft.hb_font = hb_font_create_sub_font(hb_ft_font);
250         hb_font_destroy(hb_ft_font);
251 
252         hb_font_set_funcs(fi->ft.hb_font,
253               _evas_common_font_ot_font_funcs_get(), fi, NULL);
254      }
255 
256    if (mode == EVAS_TEXT_PROPS_MODE_SHAPE)
257      {
258         hb_shape(fi->ft.hb_font, buffer, NULL, 0);
259      }
260    else
261      {
262         const char *shaper_list[] = { "fallback", NULL };
263         hb_shape_full(fi->ft.hb_font, buffer, NULL, 0, shaper_list);
264      }
265 }
266 
267 EAPI Eina_Bool
evas_common_font_ot_populate_text_props(const Eina_Unicode * text,Evas_Text_Props * props,int len,Evas_Text_Props_Mode mode,const char * lang)268 evas_common_font_ot_populate_text_props(const Eina_Unicode *text,
269                                         Evas_Text_Props *props, int len,
270                                         Evas_Text_Props_Mode mode,
271                                         const char *lang)
272 {
273    RGBA_Font_Int *fi;
274    hb_buffer_t *buffer;
275    hb_glyph_position_t *positions;
276    hb_glyph_info_t *infos;
277    int slen;
278    unsigned int i;
279    Evas_Font_Glyph_Info *gl_itr;
280    Evas_Font_OT_Info *ot_itr;
281    Evas_Coord pen_x = 0;
282 
283    fi = props->font_instance;
284 
285    if (len < 0)
286      {
287         slen = eina_unicode_strlen(text);
288      }
289    else
290      {
291         slen = len;
292      }
293 
294    buffer = hb_buffer_create();
295    hb_buffer_set_unicode_funcs(buffer, _evas_common_font_ot_unicode_funcs_get());
296    hb_buffer_set_language(buffer, hb_language_from_string(lang, -1));
297    hb_buffer_set_script(buffer, _evas_script_to_harfbuzz[props->script]);
298    hb_buffer_set_direction(buffer,
299                            (props->bidi_dir == EVAS_BIDI_DIRECTION_RTL) ?
300                            HB_DIRECTION_RTL : HB_DIRECTION_LTR);
301    /* FIXME: add run-time conversions if needed, which is very unlikely */
302    hb_buffer_add_utf32(buffer, (const uint32_t *) text, slen, 0, slen);
303 
304    _evas_common_font_ot_shape(buffer, fi, mode);
305 
306    props->len = hb_buffer_get_length(buffer);
307    props->info->ot = calloc(props->len, sizeof(Evas_Font_OT_Info));
308    props->info->glyph = calloc(props->len, sizeof(Evas_Font_Glyph_Info));
309    positions = hb_buffer_get_glyph_positions(buffer, NULL);
310    infos = hb_buffer_get_glyph_infos(buffer, NULL);
311    gl_itr = props->info->glyph;
312    ot_itr = props->info->ot;
313    for (i = 0 ; i < props->len ; i++)
314      {
315         Evas_Coord adv;
316         ot_itr->source_cluster = infos->cluster;
317         ot_itr->x_offset = positions->x_offset;
318         ot_itr->y_offset = positions->y_offset;
319         gl_itr->index = infos->codepoint;
320         adv = positions->x_advance;
321 
322         pen_x += EVAS_FONT_ROUND_26_6_TO_INT(adv);
323         gl_itr->pen_after = pen_x;
324 
325         ot_itr++;
326         gl_itr++;
327         infos++;
328         positions++;
329      }
330 
331    hb_buffer_destroy(buffer);
332    evas_common_font_int_use_trim();
333 
334    return EINA_FALSE;
335 }
336 
337 #endif
338 
339