diff --git a/apps/plugins/viewer.c b/apps/plugins/viewer.c old mode 100644 new mode 100755 index 299c392..d1c8656 --- a/apps/plugins/viewer.c +++ b/apps/plugins/viewer.c @@ -28,7 +28,8 @@ PLUGIN_HEADER #define BOOKMARKS_FILE VIEWERS_DIR "/viewer_bookmarks.dat" #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ -#define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ +#define NARROW_MAX_COLUMNS 64 /* Max displayable string len [narrow] (over-estimate) */ +#define WIDE_MAX_COLUMNS 128 /* Max displayable string len [wide] (over-estimate) */ #define MAX_WIDTH 910 /* Max line length in WIDE mode */ #define READ_PREV_ZONE 910 /* Arbitrary number less than SMALL_BLOCK_SIZE */ #define SMALL_BLOCK_SIZE 0x1000 /* 4k: Smallest file chunk we will read */ @@ -463,6 +464,8 @@ static bool mac_text; static long file_pos; /* Position of the top of the buffer in the file */ static unsigned char *buffer_end; /*Set to BUFFER_END() when file_pos changes*/ static int max_line_len; +static int max_width; +static int max_columns; static unsigned char *screen_top_ptr; static unsigned char *next_screen_ptr; static unsigned char *next_screen_to_draw_ptr; @@ -505,12 +508,60 @@ unsigned char* get_ucs(const unsigned char* str, unsigned short* ch) return (unsigned char*)str+1; } +static unsigned char *decode2utf8(const unsigned char *src, unsigned char *dst, + int skip_width, int disp_width) +{ + unsigned short ch; + const unsigned char *oldstr; + const unsigned char *str = src; + unsigned char *utf8 = dst; + int width = 0; + + while (*str != '\0') + { + oldstr = str; + str = get_ucs(oldstr, &ch); + width += glyph_width(ch); + if (width > skip_width) + { + str = oldstr; + break; + } + } + width = 0; + while(*str != '\0') + { + str = get_ucs(str, &ch); + width += glyph_width(ch); + if (width > disp_width) + break; + + utf8 = rb->utf8encode(ch, utf8); + } + + return utf8; +} + +static void calc_max_width(void) +{ + if (prefs.view_mode == NARROW) + { + max_columns = NARROW_MAX_COLUMNS; + max_width = draw_columns; + } + else + { + max_columns = WIDE_MAX_COLUMNS; + max_width = 2 * draw_columns; + } +} + bool done = false; int col = 0; #define ADVANCE_COUNTERS(c) { width += glyph_width(c); k++; } -#define LINE_IS_FULL ((k>=MAX_COLUMNS-1) ||( width >= draw_columns)) -#define LINE_IS_NOT_FULL ((k=max_columns-1) ||( width >= max_width)) +#define LINE_IS_NOT_FULL ((kiso_decode(scratch_buffer + col, utf8_buffer, - prefs.encoding, draw_columns/glyph_width('i')); + endptr = decode2utf8(scratch_buffer, utf8_buffer, col, draw_columns); *endptr = 0; } } @@ -1026,7 +1076,7 @@ static void viewer_draw(int col) if (spaces) { /* total number of spaces to insert between words */ - extra_spaces = (draw_columns-width)/glyph_width(' ') + extra_spaces = (max_width-width)/glyph_width(' ') - indent_spaces; /* number of spaces between each word*/ spaces_per_word = extra_spaces / spaces; @@ -1046,7 +1096,7 @@ static void viewer_draw(int col) multiple_spacing = false; for (j=k=spaces=0; j < line_len; j++) { - if (k == MAX_COLUMNS) + if (k == max_columns) break; c = line_begin[j]; @@ -1071,11 +1121,9 @@ static void viewer_draw(int col) break; } } - if (col != -1) { scratch_buffer[k] = 0; - endptr = rb->iso_decode(scratch_buffer + col, utf8_buffer, - prefs.encoding, k-col); + endptr = decode2utf8(scratch_buffer, utf8_buffer, col, draw_columns); *endptr = 0; } } @@ -1182,6 +1230,7 @@ static void init_need_scrollbar(void) { prefs.need_scrollbar = NEED_SCROLLBAR(); draw_columns = prefs.need_scrollbar? display_columns-SCROLLBAR_WIDTH : display_columns; par_indent_spaces = draw_columns/(5*glyph_width(' ')); + calc_max_width(); } #else #define init_need_scrollbar() @@ -1251,6 +1300,7 @@ static void viewer_load_settings(void) /* same name as global, but not the same } rb->memcpy(&old_prefs, &prefs, sizeof(struct preferences)); + calc_max_width(); data = (struct bookmark_file_data*)buffer; /* grab the text buffer */ data->bookmarked_files_count = 0; @@ -1379,8 +1429,8 @@ static int col_limit(int col) if (col < 0) col = 0; else - if (col > max_line_len - 2*glyph_width('o')) - col = max_line_len - 2*glyph_width('o'); + if (col >= max_width) + col = max_width - draw_columns; return col; } @@ -1439,6 +1489,7 @@ static bool view_mode_setting(void) names , 2, NULL); if (prefs.view_mode == NARROW) col = 0; + calc_max_width(); return ret; }