Let hb_shape() guess script and direction...

- Rename HB_SCRIPT_INVALID_CODE to HB_SCRIPT_INVALID

- Add HB_DIRECTION_INVALID

- Make hb_script_get_horizontal_direction() public

- Make hb_shape() guess script from buffer text (first non-common
  non-inherit script) if buffer script is set to HB_SCRIPT_INVALID (this
  is NOT the default.)

- Make hb_shape() guess direction from buffer script if buffer direction
is set to HB_DIRECTION_INVALID (this is NOT the default.)

- Make hb-view.c set INVALID script and direction on the buffer.

The above changes are meant to make hb-view fairly useful for uni-script
uni-direction text.  The guessing behavior however is NOT the default of
hb_shape() and must be asked for explicitly.  This is intended, because
the guess is not a suitable substitute to full-fledged bidi and script
segmentation.  It's just a testing tool.
This commit is contained in:
Behdad Esfahbod 2011-03-16 14:53:32 -03:00
parent b7b29684df
commit 3286fc0e9a
8 changed files with 61 additions and 17 deletions

View File

@ -78,7 +78,8 @@ typedef uint32_t hb_mask_t;
typedef void (*hb_destroy_func_t) (void *user_data);
typedef enum _hb_direction_t {
HB_DIRECTION_LTR,
HB_DIRECTION_INVALID = -1,
HB_DIRECTION_LTR = 0,
HB_DIRECTION_RTL,
HB_DIRECTION_TTB,
HB_DIRECTION_BTT

View File

@ -118,7 +118,7 @@ hb_icu_get_script (hb_codepoint_t unicode)
U_ICU_VERSION_MAJOR_NUM > (major) || (U_ICU_VERSION_MAJOR_NUM == (major) && U_ICU_VERSION_MINOR_NUM >= (minor))
#define MATCH_SCRIPT(C) case USCRIPT_##C: return HB_SCRIPT_##C
#define MATCH_SCRIPT2(C1, C2) case USCRIPT_##C1: return HB_SCRIPT_##C2
MATCH_SCRIPT (INVALID_CODE);
MATCH_SCRIPT (COMMON); /* Zyyy */
MATCH_SCRIPT (INHERITED); /* Qaai */
MATCH_SCRIPT (ARABIC); /* Arab */

View File

@ -64,6 +64,7 @@ hb_ot_shape_collect_features (hb_ot_shape_plan_t *plan,
break;
case HB_DIRECTION_TTB:
case HB_DIRECTION_BTT:
case HB_DIRECTION_INVALID:
default:
break;
}
@ -170,7 +171,7 @@ hb_ensure_native_direction (hb_ot_shape_context_t *c)
/* TODO vertical */
if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
direction != _hb_script_get_horizontal_direction (c->buffer->props.script))
direction != hb_script_get_horizontal_direction (c->buffer->props.script))
{
hb_buffer_reverse_clusters (c->buffer);
c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);

View File

@ -39,8 +39,8 @@
HB_BEGIN_DECLS
void
hb_shape (hb_font_t *font,
static void
hb_shape_internal (hb_font_t *font,
hb_face_t *face,
hb_buffer_t *buffer,
hb_feature_t *features,
@ -61,5 +61,45 @@ hb_shape (hb_font_t *font,
hb_ot_shape (font, face, buffer, features, num_features);
}
void
hb_shape (hb_font_t *font,
hb_face_t *face,
hb_buffer_t *buffer,
hb_feature_t *features,
unsigned int num_features)
{
hb_segment_properties_t orig_props;
orig_props = buffer->props;
/* If script is set to INVALID, guess from buffer contents */
if (buffer->props.script == HB_SCRIPT_INVALID) {
hb_unicode_get_script_func_t get_script = buffer->unicode->v.get_script;
unsigned int count = buffer->len;
for (unsigned int i = 0; i < count; i++) {
hb_script_t script = get_script (buffer->info[i].codepoint);
if (likely (script > HB_SCRIPT_INHERITED)) {
buffer->props.script = script;
break;
}
}
}
/* If direction is set to INVALID, guess from script */
if (buffer->props.direction == HB_DIRECTION_INVALID) {
buffer->props.direction = hb_script_get_horizontal_direction (buffer->props.script);
}
/* If language is not set, use default language from locale */
if (buffer->props.language == NULL) {
/* TODO get_default_for_script? using $LANGUAGE */
//buffer->props.language = hb_language_get_default ();
}
hb_shape_internal (font, face, buffer, features, num_features);
buffer->props = orig_props;
}
HB_END_DECLS

View File

@ -55,10 +55,6 @@ struct _hb_unicode_funcs_t {
extern HB_INTERNAL hb_unicode_funcs_t _hb_unicode_funcs_nil;
HB_INTERNAL hb_direction_t
_hb_script_get_horizontal_direction (hb_script_t script);
HB_END_DECLS
#endif /* HB_UNICODE_PRIVATE_H */

View File

@ -352,7 +352,7 @@ const hb_direction_t horiz_dir[] =
#undef RTL
hb_direction_t
_hb_script_get_horizontal_direction (hb_script_t script)
hb_script_get_horizontal_direction (hb_script_t script)
{
if (unlikely ((unsigned int) script >= ARRAY_LENGTH (horiz_dir)))
return HB_DIRECTION_LTR;

View File

@ -70,7 +70,8 @@ typedef enum
/* Unicode Script property */
typedef enum
{ /* ISO 15924 code */
HB_SCRIPT_INVALID_CODE = -1,
HB_SCRIPT_INVALID = -1,
HB_SCRIPT_COMMON = 0, /* Zyyy */
HB_SCRIPT_INHERITED, /* Qaai */
HB_SCRIPT_ARABIC, /* Arab */
@ -289,6 +290,11 @@ hb_unicode_get_eastasian_width (hb_unicode_funcs_t *ufuncs,
hb_codepoint_t unicode);
/* Misc functions */
hb_direction_t
hb_script_get_horizontal_direction (hb_script_t script);
HB_END_DECLS
#endif /* HB_UNICODE_H */

View File

@ -184,8 +184,8 @@ _hb_cr_text_glyphs (cairo_t *cr,
hb_buffer_set_unicode_funcs (hb_buffer, hb_glib_get_unicode_funcs ());
hb_buffer_add_utf8 (hb_buffer, text, len, 0, len);
/* hb_buffer_set_script (hb_buffer, HB_SCRIPT_LATIN); */
/* hb_buffer_set_direction (hb_buffer, HB_DIRECTION_LTR); */
hb_buffer_set_script (hb_buffer, HB_SCRIPT_INVALID);
hb_buffer_set_direction (hb_buffer, HB_DIRECTION_INVALID);
hb_buffer_set_language (hb_buffer, hb_language_from_string (language));
hb_shape (hb_font, hb_face, hb_buffer, NULL, 0);