First throw at some utf8 support

This commit is contained in:
Qball Cow 2014-02-25 09:13:34 +01:00
parent 13d1036efb
commit 5619c856f1
2 changed files with 40 additions and 11 deletions

View File

@ -150,6 +150,7 @@ static char **tokenize( const char *input )
retv[1] = NULL;
// Iterate over tokens.
// strtok should still be valid for utf8.
for (
token = strtok_r( retv[0], " ", &saveptr );
token != NULL;
@ -751,8 +752,24 @@ static int calculate_common_prefix( char **filtered, int max_lines )
p++;
} while ( found );
// cut off to be valid utf8.
for ( j = 0; j < length_prefix; ) {
if((filtered[0][j]&0x80) == 0){j++;}
else if ((filtered[0][j]&0xf0) == 0xc0) {
// 2 bytes
if((j+2) >= length_prefix) length_prefix = j;
j+=2;
}else if ((filtered[0][j]&0xf0) == 0xe0) {
// 3 bytes
if((j+3) >= length_prefix) length_prefix = j;
j+=3;
}else if ((filtered[0][j]&0xf0) == 0xf0) {
// 4 bytes
if((j+4) >= length_prefix) length_prefix = j;
j+=4;
}else{j++;};
}
}
return length_prefix;
}
@ -1095,10 +1112,13 @@ MenuReturn menu( char **lines, char **input, char *prompt, Time *time, int *shif
int length_prefix = calculate_common_prefix( filtered, num_lines );
if ( length_prefix && strncasecmp( filtered[0], text->text, length_prefix ) ) {
// TODO: memcmp to utf8 aware cmp.
if ( length_prefix && memcmp( filtered[0], text->text, length_prefix ) ) {
// Do not want to modify original string, so make copy.
// not eff..
char * str = strndup( filtered[0], length_prefix );
char * str = allocate (sizeof(char)*(length_prefix+1));
memcpy( str,filtered[0], length_prefix );
str[length_prefix] = '\0';
textbox_text( text, str );
textbox_cursor_end( text );
free( str );

View File

@ -115,7 +115,7 @@ void textbox_extents( textbox *tb )
int length = strlen( tb->text ) + strlen( tb->prompt ) +1;
char *line = alloca( length + 1 );
sprintf( line, "%s %s", tb->prompt, tb->text );
XftTextExtents8( display, tb->font, ( unsigned char* )line, length, &tb->extents );
XftTextExtentsUtf8( display, tb->font, ( unsigned char* )line, length, &tb->extents );
}
// set the default text to display
@ -231,7 +231,7 @@ void textbox_draw( textbox *tb )
for ( i = 0; i < length; i++ ) if ( isspace( line[i] ) ) line[i] = '_';
// calc cursor position
XftTextExtents8( display, tb->font, ( unsigned char* )line, cursor_offset, &extents );
XftTextExtentsUtf8( display, tb->font, ( unsigned char* )line, cursor_offset, &extents );
cursor_x = extents.width;
// restore correct text string with spaces
@ -242,12 +242,12 @@ void textbox_draw( textbox *tb )
// Calculate the right size, so no characters are cut off.
// TODO: Check performance of this.
while ( 1 ) {
XftTextExtents8( display, tb->font, ( unsigned char* )line, length, &extents );
XftTextExtentsUtf8( display, tb->font, ( unsigned char* )line, length, &extents );
line_width = extents.width;
if ( line_width < ( tb->w-2*SIDE_MARGIN ) ) break;
length--;
for(length-=1; length > 0 && (line[length]&0xc0) == 0x80; length -=1);
}
int x = SIDE_MARGIN, y = tb->font->ascent;
@ -257,7 +257,7 @@ void textbox_draw( textbox *tb )
if ( tb->flags & TB_CENTER ) x = ( tb->w - line_width ) / 2;
// draw the text, including any prompt in edit mode
XftDrawString8( draw, &tb->color_fg, tb->font, x, y, ( unsigned char* )line, length );
XftDrawStringUtf8( draw, &tb->color_fg, tb->font, x, y, ( unsigned char* )line, length );
// draw the cursor
if ( tb->flags & TB_EDITABLE )
@ -272,6 +272,14 @@ void textbox_draw( textbox *tb )
XFreePixmap( display, canvas );
}
static size_t nextrune(textbox *tb, int inc) {
ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */
for(n = tb->cursor + inc; n + inc >= 0 && (tb->text[n] & 0xc0) == 0x80; n += inc);
return n;
}
// cursor handling for edit mode
void textbox_cursor( textbox *tb, int pos )
{
@ -281,13 +289,13 @@ void textbox_cursor( textbox *tb, int pos )
// move right
void textbox_cursor_inc( textbox *tb )
{
textbox_cursor( tb, tb->cursor+1 );
textbox_cursor( tb, nextrune(tb, 1) );
}
// move left
void textbox_cursor_dec( textbox *tb )
{
textbox_cursor( tb, tb->cursor-1 );
textbox_cursor( tb, nextrune(tb,-1) );
}
// beginning of line
@ -339,7 +347,8 @@ void textbox_cursor_ins( textbox *tb, char c )
// delete on character
void textbox_cursor_del( textbox *tb )
{
textbox_delete( tb, tb->cursor, 1 );
int del_r = nextrune(tb, 1);
textbox_delete( tb, tb->cursor, del_r-tb->cursor );
}
// back up and delete one character