Improve bidi handling in entry editor.

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-01-16 13:41:16 +01:00
parent 4542e71b8b
commit 34f71cb866
8 changed files with 453 additions and 267 deletions

View file

@ -34,6 +34,10 @@ struct term_state
int backlog_fixed_tab;
grub_size_t backlog_len;
int bidi_stack_depth;
grub_uint8_t bidi_stack[GRUB_BIDI_MAX_EXPLICIT_LEVEL];
int invalid_pushes;
void *free;
int num_lines;
char *term_name;
@ -44,7 +48,9 @@ print_ucs4_real (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term, int backlog,
int dry_run, int fixed_tab);
int dry_run, int fixed_tab, unsigned skip_lines,
unsigned max_lines,
grub_uint32_t contchar, struct grub_term_pos *pos);
static struct term_state *term_states = NULL;
@ -243,7 +249,8 @@ grub_puts_terminal (const char *str, struct grub_term_output *term)
return;
}
print_ucs4_real (unicode_str, unicode_last_position, 0, 0, term, 0, 0, 0);
print_ucs4_real (unicode_str, unicode_last_position, 0, 0, term,
0, 0, 0, 0, -1, 0, 0);
grub_free (unicode_str);
}
@ -541,13 +548,25 @@ get_startwidth (struct grub_term_output *term,
return ((term->getxy (term) >> 8) & 0xff) - margin_left;
}
static void
fill_margin (struct grub_term_output *term, int r)
{
int sp = (term->getwh (term) >> 8)
- (term->getxy (term) >> 8) - r;
if (sp > 0)
grub_print_spaces (term, sp);
}
static int
print_ucs4_terminal (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term,
struct term_state *state,
int dry_run, int fixed_tab)
int dry_run, int fixed_tab, unsigned skip_lines,
unsigned max_lines,
grub_uint32_t contchar,
int primitive_wrap, struct grub_term_pos *pos)
{
const grub_uint32_t *ptr;
grub_ssize_t startwidth = dry_run ? 0 : get_startwidth (term, margin_left);
@ -556,10 +575,40 @@ print_ucs4_terminal (const grub_uint32_t * str,
grub_ssize_t max_width = get_maxwidth (term, margin_left, margin_right);
const grub_uint32_t *line_start = str, *last_space = str - 1;
int lines = 0;
int i;
struct term_state local_state;
if (!state)
{
grub_memset (&local_state, 0, sizeof (local_state));
state = &local_state;
}
for (i = 0; i < state->bidi_stack_depth; i++)
putcode_real (state->bidi_stack[i] | (GRUB_UNICODE_LRE & ~0xff),
term, fixed_tab);
for (ptr = str; ptr < last_position; ptr++)
{
grub_ssize_t last_width = 0;
switch (*ptr)
{
case GRUB_UNICODE_LRE:
case GRUB_UNICODE_RLE:
case GRUB_UNICODE_LRO:
case GRUB_UNICODE_RLO:
if (state->bidi_stack_depth >= (int) ARRAY_SIZE (state->bidi_stack))
state->invalid_pushes++;
else
state->bidi_stack[state->bidi_stack_depth++] = *ptr;
break;
case GRUB_UNICODE_PDF:
if (state->invalid_pushes)
state->invalid_pushes--;
else if (state->bidi_stack_depth)
state->bidi_stack_depth--;
break;
}
if (grub_unicode_get_comb_type (*ptr) == GRUB_UNICODE_COMB_NONE)
{
struct grub_unicode_glyph c = {
@ -569,10 +618,16 @@ print_ucs4_terminal (const grub_uint32_t * str,
.combining = 0
};
c.base = *ptr;
if (pos)
{
pos[ptr - str].x = line_width;
pos[ptr - str].y = lines;
pos[ptr - str].valid = 1;
}
line_width += last_width = grub_term_getcharwidth (term, &c);
}
if (*ptr == ' ')
if (*ptr == ' ' && !primitive_wrap)
{
lastspacewidth = line_width;
last_space = ptr;
@ -581,6 +636,13 @@ print_ucs4_terminal (const grub_uint32_t * str,
if (line_width > max_width || *ptr == '\n')
{
const grub_uint32_t *ptr2;
int wasn = (*ptr == '\n');
if (wasn)
{
state->bidi_stack_depth = 0;
state->invalid_pushes = 0;
}
if (line_width > max_width && last_space > line_start)
ptr = last_space;
@ -595,7 +657,7 @@ print_ucs4_terminal (const grub_uint32_t * str,
lines++;
if (!dry_run)
if (!skip_lines && !dry_run)
{
for (ptr2 = line_start; ptr2 < ptr; ptr2++)
{
@ -608,9 +670,12 @@ print_ucs4_terminal (const grub_uint32_t * str,
putcode_real (*ptr2, term, fixed_tab);
}
grub_print_spaces (term, margin_right);
if (!wasn && contchar)
putcode_real (contchar, term, fixed_tab);
fill_margin (term, contchar ? margin_right : 1);
grub_putcode ('\n', term);
if (state && ++state->num_lines
if (state != &local_state && ++state->num_lines
>= (grub_ssize_t) grub_term_height (term) - 2)
{
state->backlog_ucs4 = (ptr == last_space || *ptr == '\n')
@ -622,17 +687,42 @@ print_ucs4_terminal (const grub_uint32_t * str,
}
line_width -= lastspacewidth;
if (!dry_run)
grub_print_spaces (term, margin_left);
if (ptr == last_space || *ptr == '\n')
ptr++;
line_start = ptr;
if (skip_lines)
skip_lines--;
else if (max_lines != (unsigned) -1)
{
max_lines--;
if (!max_lines)
break;
}
if (!skip_lines && !dry_run)
{
if (!contchar)
grub_print_spaces (term, margin_left);
else
grub_term_gotoxy (term, margin_left,
grub_term_getxy (term) & 0xff);
for (i = 0; i < state->bidi_stack_depth; i++)
putcode_real (state->bidi_stack[i] | (GRUB_UNICODE_LRE & ~0xff),
term, fixed_tab);
}
}
}
if (pos)
{
pos[ptr - str].x = line_width;
pos[ptr - str].y = lines;
pos[ptr - str].valid = 1;
}
if (line_start < last_position)
lines++;
if (!dry_run)
if (!dry_run && !skip_lines && max_lines)
{
const grub_uint32_t *ptr2;
for (ptr2 = line_start; ptr2 < last_position; ptr2++)
@ -717,7 +807,8 @@ print_backlog (struct grub_term_output *term,
ret = print_ucs4_terminal (state->backlog_ucs4,
state->backlog_ucs4 + state->backlog_len,
margin_left, margin_right, term, state, 0,
state->backlog_fixed_tab);
state->backlog_fixed_tab, 0, -1, 0, 0,
0);
if (!ret)
{
grub_free (state->free);
@ -753,17 +844,28 @@ print_ucs4_real (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term, int backlog,
int dry_run, int fixed_tab)
int dry_run, int fixed_tab, unsigned skip_lines,
unsigned max_lines,
grub_uint32_t contchar, struct grub_term_pos *pos)
{
struct term_state *state = NULL;
if (!dry_run)
{
int xy;
if (backlog)
state = find_term_state (term);
if (((term->getxy (term) >> 8) & 0xff) < margin_left)
grub_print_spaces (term, margin_left - ((term->getxy (term) >> 8) & 0xff));
xy = term->getxy (term);
if (((xy >> 8) & 0xff) < margin_left)
{
if (!contchar)
grub_print_spaces (term, margin_left - ((xy >> 8) & 0xff));
else
grub_term_gotoxy (term, margin_left,
xy & 0xff);
}
}
if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
@ -773,7 +875,10 @@ print_ucs4_real (const grub_uint32_t * str,
{
grub_ssize_t visual_len;
struct grub_unicode_glyph *visual;
grub_ssize_t visual_len_show;
struct grub_unicode_glyph *visual_show;
int ret;
struct grub_unicode_glyph *vptr;
auto grub_ssize_t getcharwidth (const struct grub_unicode_glyph *c);
grub_ssize_t getcharwidth (const struct grub_unicode_glyph *c)
@ -787,27 +892,45 @@ print_ucs4_real (const grub_uint32_t * str,
margin_left,
margin_right),
get_startwidth (term,
margin_left));
margin_left),
contchar, pos, !!contchar);
if (visual_len < 0)
{
grub_print_error ();
return 0;
}
visual_show = visual;
for (; skip_lines && visual_show < visual + visual_len; visual_show++)
if (visual_show->base == '\n')
skip_lines--;
if (max_lines != (unsigned) -1)
{
for (vptr = visual_show;
max_lines && vptr < visual + visual_len; vptr++)
if (visual_show->base == '\n')
max_lines--;
visual_len_show = vptr - visual_show;
}
else
visual_len_show = visual + visual_len - visual_show;
if (dry_run)
{
struct grub_unicode_glyph *vptr;
ret = 0;
for (vptr = visual; vptr < visual + visual_len; vptr++)
for (vptr = visual_show; vptr < visual_show + visual_len_show; vptr++)
if (vptr->base == '\n')
ret++;
if (visual_len && visual[visual_len - 1].base != '\n')
if (visual_len_show && visual[visual_len_show - 1].base != '\n')
ret++;
grub_free (visual);
}
else
{
ret = put_glyphs_terminal (visual, visual_len, margin_left,
margin_right, term, state, fixed_tab);
ret = put_glyphs_terminal (visual_show, visual_len_show, margin_left,
contchar ? margin_right : 1,
term, state, fixed_tab);
if (!ret)
grub_free (visual);
else
@ -816,7 +939,22 @@ print_ucs4_real (const grub_uint32_t * str,
return ret;
}
return print_ucs4_terminal (str, last_position, margin_left, margin_right,
term, state, dry_run, fixed_tab);
term, state, dry_run, fixed_tab, skip_lines,
max_lines, contchar, !!contchar, pos);
}
void
grub_print_ucs4_menu (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term,
int skip_lines, int max_lines,
grub_uint32_t contchar,
struct grub_term_pos *pos)
{
print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0, 0, 1, skip_lines, max_lines,
contchar, pos);
}
void
@ -826,7 +964,7 @@ grub_print_ucs4 (const grub_uint32_t * str,
struct grub_term_output *term)
{
print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0, 0, 1);
term, 0, 0, 1, 0, -1, 0, 0);
}
int
@ -836,7 +974,7 @@ grub_ucs4_count_lines (const grub_uint32_t * str,
struct grub_term_output *term)
{
return print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0, 1, 1);
term, 0, 1, 1, 0, -1, 0, 0);
}
void
@ -886,7 +1024,7 @@ grub_xputs_normal (const char *str)
{
int cur;
cur = print_ucs4_real (unicode_str, unicode_last_position, 0, 0,
term, grub_more, 0, 0);
term, grub_more, 0, 0, 0, -1, 0, 0);
if (cur)
backlog = 1;
}