1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

plain renderer: rewritten change_colors, handling 256 color ESC sequences.

TODO: mono mode
This commit is contained in:
Witold Filipczyk 2006-05-10 17:46:31 +02:00 committed by Witold Filipczyk
parent 47f3dd0421
commit 82d38ff2be

View File

@ -226,11 +226,23 @@ print_document_link(struct plain_renderer *renderer, int lineno,
return len; return len;
} }
static inline int enum mode_16_256 {
change_colors(struct screen_char *template, unsigned char *text, struct document *document) COLOR_NONE,
COLOR_16_FOREGROUND,
COLOR_16_BACKGROUND,
COLOR_38,
COLOR_48,
COLOR_256_FOREGROUND,
COLOR_256_BACKGROUND
};
static int
change_colors(struct screen_char *template, unsigned char *line, int line_pos, int width, struct document *document)
{ {
unsigned char fg, bg, bold; unsigned char fg, bg, bold;
unsigned char *start = text; enum mode_16_256 color = COLOR_NONE;
unsigned char value = 0;
int start = 0;
#if defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS) #if defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS)
fg = template->color[0]; fg = template->color[0];
@ -240,39 +252,177 @@ change_colors(struct screen_char *template, unsigned char *text, struct document
bg = template->color[0] >> 4; bg = template->color[0] >> 4;
#endif #endif
bold = template->attr & SCREEN_ATTR_BOLD; bold = template->attr & SCREEN_ATTR_BOLD;
for (start = text; *text && *text != 'm'; text++) { for (; line_pos < width; line_pos++) {
switch (*text) { unsigned char ch = line[line_pos];
case ';':
break; switch (color) {
case '3': case COLOR_NONE:
text++; switch (ch) {
if (*text >= '0' && *text < '9') case '0':
fg = *text - '0'; bold = 0;
else
fg = 7;
break;
case '4':
text++;
if (*text >= '0' && *text < '9')
bg = *text - '0';
else
bg = 0;
break; break;
case '1': case '1':
bold = SCREEN_ATTR_BOLD; bold = SCREEN_ATTR_BOLD;
break; break;
case '0': case '3':
bold = 0; color = COLOR_16_FOREGROUND;
break; break;
case '4':
color = COLOR_16_BACKGROUND;
break;
case 'm':
goto end;
default: default:
break; break;
}
break;
case COLOR_16_FOREGROUND:
switch (ch) {
case '9':
fg = 7;
color = COLOR_NONE;
break;
case '8':
color = COLOR_38;
break;
case 'm':
goto end;
default:
if (ch >= '0' && ch <= '7') fg = ch - '0';
color = COLOR_NONE;
break;
}
break;
case COLOR_16_BACKGROUND:
switch (ch) {
case '9':
bg = 0;
color = COLOR_NONE;
break;
case '8':
color = COLOR_48;
break;
case 'm':
goto end;
default:
if (ch >= '0' && ch <= '7') bg = ch - '0';
color = COLOR_NONE;
break;
}
break;
case COLOR_38:
switch (ch) {
case '5':
start = 1;
value = 0;
color = COLOR_256_FOREGROUND;
break;
case 'm':
goto end;
case ';':
break;
default:
color = COLOR_NONE;
break;
}
break;
case COLOR_48:
switch (ch) {
case '5':
start = 1;
value = 0;
color = COLOR_256_BACKGROUND;
break;
case 'm':
goto end;
case ';':
break;
default:
color = COLOR_NONE;
break;
}
break;
case COLOR_256_FOREGROUND:
switch (ch) {
case ';':
case 'm':
if (start) {
start = 0;
value = 0;
} else {
#if defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS)
#ifdef CONFIG_88_COLORS
if (document->options.color_mode == COLOR_MODE_88) fg = value;
#endif
#ifdef CONFIG_256_COLORS
if (document->options.color_mode == COLOR_MODE_256) fg = value;
#endif
#endif
color = COLOR_NONE;
value = 0;
}
if (ch == 'm') goto end;
break;
default:
if (ch >= '0' && ch <= '9') value *= 10 + ch - '0';
break;
}
break;
case COLOR_256_BACKGROUND:
switch (ch) {
case ';':
case 'm':
if (start) {
start = 0;
value = 0;
} else {
#if defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS)
#ifdef CONFIG_88_COLORS
if (document->options.color_mode == COLOR_MODE_88) bg = value;
#endif
#ifdef CONFIG_256_COLORS
if (document->options.color_mode == COLOR_MODE_256) bg = value;
#endif
#endif
color = COLOR_NONE;
value = 0;
}
if (ch == 'm') goto end;
break;
default:
if (ch >= '0' && ch <= '9') value *= 10 + ch - '0';
break;
}
break;
} }
} }
fg |= bold; end:
set_term_color16(template, document->options.color_flags, fg, bg); #if defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS)
return (int)(text - start); #ifdef CONFIG_88_COLORS
if (document->options.color_mode == COLOR_MODE_88) {
TERM_COLOR_FOREGROUND(template->color) = fg;
TERM_COLOR_BACKGROUND(template->color) = bg;
}
#endif
#ifdef CONFIG_256_COLORS
if (document->options.color_mode == COLOR_MODE_256) {
TERM_COLOR_FOREGROUND(template->color) = fg;
TERM_COLOR_BACKGROUND(template->color) = bg;
}
#endif
#endif
if (document->options.color_mode == COLOR_MODE_16) {
fg |= bold;
set_term_color16(template, document->options.color_flags, fg, bg);
}
return line_pos;
} }
static inline int static inline int
@ -344,7 +494,7 @@ add_document_line(struct plain_renderer *renderer,
case 27: case 27:
if (next_char != '[') goto normal; if (next_char != '[') goto normal;
line_pos += 2; line_pos += 2;
line_pos += change_colors(&saved_renderer_template, &line[line_pos], document); line_pos = change_colors(&saved_renderer_template, line, line_pos, width, document);
*template = saved_renderer_template; *template = saved_renderer_template;
break; break;
case ASCII_BS: case ASCII_BS: