1 #include "evas_font_ot.h"
8 #include "evas_common.h"
11 #include "evas_font_private.h"
14 static const hb_script_t
15 _evas_script_to_harfbuzz[] =
57 HB_SCRIPT_CANADIAN_ABORIGINAL,
64 /* Unicode-4.0 additions */
74 /* Unicode-4.1 additions */
75 HB_SCRIPT_NEW_TAI_LUE,
79 HB_SCRIPT_SYLOTI_NAGRI,
80 HB_SCRIPT_OLD_PERSIAN,
83 /* Unicode-5.0 additions */
91 /* Unicode-5.1 additions */
104 /* Unicode-5.2 additions */
107 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
108 HB_SCRIPT_IMPERIAL_ARAMAIC,
109 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
110 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
115 HB_SCRIPT_MEETEI_MAYEK,
116 HB_SCRIPT_OLD_SOUTH_ARABIAN,
117 HB_SCRIPT_OLD_TURKIC,
121 /* Unicode-6.0 additions */
129 /* FIXME: doc. returns #items */
131 evas_common_font_ot_cluster_size_get(const Evas_Text_Props *props, size_t char_index)
135 int left_bound, right_bound;
137 char_index += props->start;
138 base_cluster = EVAS_FONT_OT_POS_GET(props->info->ot[char_index]);
139 for (i = (int) char_index ;
140 (i >= (int) props->start) &&
141 (EVAS_FONT_OT_POS_GET(props->info->ot[i]) == base_cluster) ;
145 for (i = (int) char_index + 1;
146 (i < (int) (props->start + props->len)) &&
147 (EVAS_FONT_OT_POS_GET(props->info->ot[i]) == base_cluster) ;
152 if (right_bound == left_bound)
156 else if (props->bidi.dir == EVAS_BIDI_DIRECTION_RTL)
160 items = props->text_offset + props->text_len - base_cluster;
164 items = props->info->ot[left_bound].source_cluster - base_cluster;
169 if (right_bound >= (int) (props->text_offset + props->text_len))
171 items = props->text_offset + props->text_len - base_cluster;
175 items = props->info->ot[right_bound].source_cluster - base_cluster;
178 return (items > 0) ? items : 1;
182 evas_common_font_ot_load_face(void *_font)
184 RGBA_Font_Source *font = (RGBA_Font_Source *) _font;
185 /* Unload the face if by any chance it's already loaded */
186 evas_common_font_ot_unload_face(font);
187 font->hb.face = hb_ft_face_create(font->ft.face, NULL);
191 evas_common_font_ot_unload_face(void *_font)
193 RGBA_Font_Source *font = (RGBA_Font_Source *) _font;
194 if (!font->hb.face) return;
195 hb_face_destroy(font->hb.face);
196 font->hb.face = NULL;
199 /* Harfbuzz font functions */
200 static hb_font_funcs_t *_ft_font_funcs = NULL;
202 static hb_codepoint_t
203 _evas_common_font_ot_hb_get_glyph(hb_font_t *font, hb_face_t *face,
204 const void *user_data, hb_codepoint_t unicode,
205 hb_codepoint_t variation_selector)
207 RGBA_Font_Int *fi = (RGBA_Font_Int *) user_data;
208 return hb_font_funcs_get_glyph_func(_ft_font_funcs)(font, face,
209 fi->src->ft.face, unicode, variation_selector);
213 _evas_common_font_ot_hb_get_glyph_advance(hb_font_t *font, hb_face_t *face,
214 const void *user_data, hb_codepoint_t glyph,
215 hb_position_t *x_advance, hb_position_t *y_advance)
218 RGBA_Font_Int *fi = (RGBA_Font_Int *) user_data;
222 fg = evas_common_font_int_cache_glyph_get(fi, glyph);
225 *x_advance = fg->glyph->advance.x >> 10;
226 *y_advance = fg->glyph->advance.y >> 10;
231 _evas_common_font_ot_hb_get_glyph_extents(hb_font_t *font, hb_face_t *face,
232 const void *user_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents)
234 RGBA_Font_Int *fi = (RGBA_Font_Int *) user_data;
235 hb_font_funcs_get_glyph_extents_func(_ft_font_funcs)(font, face,
236 fi->src->ft.face, glyph, extents);
240 _evas_common_font_ot_hb_get_contour_point(hb_font_t *font, hb_face_t *face,
241 const void *user_data, unsigned int point_index, hb_codepoint_t glyph,
242 hb_position_t *x, hb_position_t *y)
244 RGBA_Font_Int *fi = (RGBA_Font_Int *) user_data;
245 return hb_font_funcs_get_contour_point_func(_ft_font_funcs)(font, face,
246 fi->src->ft.face, point_index, glyph, x, y);
250 _evas_common_font_ot_hb_get_kerning(hb_font_t *font, hb_face_t *face,
251 const void *user_data, hb_codepoint_t first_glyph,
252 hb_codepoint_t second_glyph)
254 RGBA_Font_Int *fi = (RGBA_Font_Int *) user_data;
258 if (evas_common_font_query_kerning(fi, first_glyph, second_glyph, &kern))
264 /* End of harfbuzz font funcs */
266 static inline hb_font_funcs_t *
267 _evas_common_font_ot_font_funcs_get(void)
269 static hb_font_funcs_t *font_funcs = NULL;
272 _ft_font_funcs = hb_ft_get_font_funcs();
273 font_funcs = hb_font_funcs_create();
274 hb_font_funcs_set_glyph_func(font_funcs,
275 _evas_common_font_ot_hb_get_glyph);
276 hb_font_funcs_set_glyph_advance_func(font_funcs,
277 _evas_common_font_ot_hb_get_glyph_advance);
278 hb_font_funcs_set_glyph_extents_func(font_funcs,
279 _evas_common_font_ot_hb_get_glyph_extents);
280 hb_font_funcs_set_contour_point_func(font_funcs,
281 _evas_common_font_ot_hb_get_contour_point);
282 hb_font_funcs_set_kerning_func(font_funcs,
283 _evas_common_font_ot_hb_get_kerning);
289 static inline hb_unicode_funcs_t *
290 _evas_common_font_ot_unicode_funcs_get(void)
292 static hb_unicode_funcs_t *unicode_funcs = NULL;
295 unicode_funcs = hb_unicode_funcs_get_default();
298 return unicode_funcs;
302 _evas_common_font_ot_shape(hb_buffer_t *buffer, RGBA_Font_Int *fi)
306 hb_font = hb_ft_font_create(fi->src->ft.face, NULL);
307 hb_font_set_funcs(hb_font, _evas_common_font_ot_font_funcs_get(), fi, NULL);
309 hb_shape(hb_font, fi->src->hb.face, buffer, NULL, 0);
310 hb_font_destroy(hb_font);
314 evas_common_font_ot_populate_text_props(void *_fn, const Eina_Unicode *text,
315 Evas_Text_Props *props, int len)
317 RGBA_Font *fn = (RGBA_Font *) _fn;
320 hb_glyph_position_t *positions;
321 hb_glyph_info_t *infos;
324 Evas_Font_Glyph_Info *gl_itr;
325 Evas_Font_OT_Info *ot_itr;
326 Evas_Coord pen_x = 0;
328 fi = fn->fonts->data;
329 /* Load the font needed for this script */
331 /* Skip common chars */
332 const Eina_Unicode *tmp;
335 evas_common_language_char_script_get(*tmp) == EVAS_SCRIPT_COMMON ;
338 if (!*tmp && (tmp > text)) tmp--;
339 evas_common_font_glyph_search(fn, &fi, *tmp);
341 evas_common_font_int_reload(fi);
342 if (fi->src->current_size != fi->size)
345 FT_Activate_Size(fi->ft.size);
347 fi->src->current_size = fi->size;
352 slen = eina_unicode_strlen(text);
359 buffer = hb_buffer_create(slen);
360 hb_buffer_set_unicode_funcs(buffer, _evas_common_font_ot_unicode_funcs_get());
361 hb_buffer_set_language(buffer, hb_language_from_string(
362 evas_common_language_from_locale_get()));
363 hb_buffer_set_script(buffer, _evas_script_to_harfbuzz[props->script]);
364 hb_buffer_set_direction(buffer,
365 (props->bidi.dir == EVAS_BIDI_DIRECTION_RTL) ?
366 HB_DIRECTION_RTL : HB_DIRECTION_LTR);
367 /* FIXME: add run-time conversions if needed, which is very unlikely */
368 hb_buffer_add_utf32(buffer, (const uint32_t *) text, slen, 0, slen);
370 _evas_common_font_ot_shape(buffer, fi);
372 props->len = hb_buffer_get_length(buffer);
373 props->info->ot = calloc(props->len,
374 sizeof(Evas_Font_OT_Info));
375 props->info->glyph = calloc(props->len,
376 sizeof(Evas_Font_Glyph_Info));
377 positions = hb_buffer_get_glyph_positions(buffer, NULL);
378 infos = hb_buffer_get_glyph_infos(buffer, NULL);
379 gl_itr = props->info->glyph;
380 ot_itr = props->info->ot;
381 for (i = 0 ; i < props->len ; i++)
384 ot_itr->source_cluster = infos->cluster;
385 ot_itr->x_offset = positions->x_offset;
386 ot_itr->y_offset = positions->y_offset;
387 gl_itr->index = infos->codepoint;
388 adv = positions->x_advance;
391 gl_itr->pen_after = EVAS_FONT_ROUND_26_6_TO_INT(pen_x);
399 hb_buffer_destroy(buffer);
400 evas_common_font_int_use_trim();