From b66d2bec674f59016ca0708c20249421914b1d2b Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Tue, 28 Aug 2007 20:39:15 +0200 Subject: [PATCH] document: Unify text style -> screen attribute handling Currently, all DOM, HTML and plain renderers had their own routine for conversion from text style to screen attribute. This moves text_style and text_style_format from html/parser.h to renderer.h and introduces new generic routine get_screen_chracter_template() that is used by all the specific rendering engines. --- src/document/dom/rss.c | 3 +-- src/document/dom/source.c | 3 +-- src/document/dom/util.c | 29 ++++++++++------------------- src/document/html/parser.h | 16 +--------------- src/document/html/renderer.c | 26 +++----------------------- src/document/plain/renderer.c | 9 ++------- src/document/renderer.c | 34 ++++++++++++++++++++++++++++++++++ src/document/renderer.h | 20 ++++++++++++++++++++ 8 files changed, 72 insertions(+), 68 deletions(-) diff --git a/src/document/dom/rss.c b/src/document/dom/rss.c index 519bd65a7..195a610ad 100644 --- a/src/document/dom/rss.c +++ b/src/document/dom/rss.c @@ -216,7 +216,6 @@ dom_rss_push_document(struct dom_stack *stack, struct dom_node *root, void *xxx) struct screen_char *template = &data->styles[type]; color_T background = document->options.default_bg; color_T foreground = document->options.default_fg; - enum screen_char_attr attr = 0; static int i_want_struct_module_for_dom; static unsigned char *names[RSS_STYLES] = { "title", "aux" }; @@ -238,7 +237,7 @@ dom_rss_push_document(struct dom_stack *stack, struct dom_node *root, void *xxx) selector = find_css_selector(&css->selectors, CST_ELEMENT, CSR_ROOT, names[type], strlen(names[type])); - init_template_by_style(template, &document->options, background, foreground, attr, + init_template_by_style(template, &document->options, 0, foreground, background, selector ? &selector->properties : NULL); } diff --git a/src/document/dom/source.c b/src/document/dom/source.c index 55f6a7e69..69ca2afef 100644 --- a/src/document/dom/source.c +++ b/src/document/dom/source.c @@ -344,7 +344,6 @@ render_dom_document_start(struct dom_stack *stack, struct dom_node *node, void * struct screen_char *template = &data->styles[type]; color_T background = document->options.default_bg; color_T foreground = document->options.default_fg; - enum screen_char_attr attr = 0; static int i_want_struct_module_for_dom; struct dom_string *name = get_dom_node_type_name(type); @@ -373,7 +372,7 @@ render_dom_document_start(struct dom_stack *stack, struct dom_node *node, void * selector = find_css_selector(&css->selectors, CST_ELEMENT, CSR_ROOT, name->string, name->length); - init_template_by_style(template, &document->options, background, foreground, attr, + init_template_by_style(template, &document->options, 0, foreground, background, selector ? &selector->properties : NULL); } diff --git a/src/document/dom/util.c b/src/document/dom/util.c index 76c7f6027..9611e9e61 100644 --- a/src/document/dom/util.c +++ b/src/document/dom/util.c @@ -28,18 +28,14 @@ static inline void init_template(struct screen_char *template, struct document_options *options, - color_T background, color_T foreground, enum screen_char_attr attr) + enum screen_char_attr attr, color_T foreground, color_T background) { - struct color_pair colors = INIT_COLOR_PAIR(background, foreground); + struct text_style style = { attr, foreground, background }; - template->attr = attr; - template->data = ' '; - set_term_color(template, &colors, - options->color_flags, options->color_mode); + get_screen_char_template(template, options, style); } - -inline void +void init_template_by_style(struct screen_char *template, struct document_options *options, color_T background, color_T foreground, enum screen_char_attr attr, LIST_OF(struct css_property) *properties) @@ -47,6 +43,7 @@ init_template_by_style(struct screen_char *template, struct document_options *op struct css_property *property; if (properties) { + /* TODO: Use the CSS appliers. */ foreach (property, *properties) { switch (property->type) { case CSS_PT_BACKGROUND_COLOR: @@ -58,19 +55,13 @@ init_template_by_style(struct screen_char *template, struct document_options *op foreground = property->value.color; break; case CSS_PT_FONT_WEIGHT: - if (property->value.font_attribute.add & AT_BOLD) - attr |= SCREEN_ATTR_BOLD; + attr |= property->value.font_attribute.add; break; case CSS_PT_FONT_STYLE: - if (property->value.font_attribute.add & AT_UNDERLINE) - attr |= SCREEN_ATTR_UNDERLINE; - - if (property->value.font_attribute.add & AT_ITALIC) - attr |= SCREEN_ATTR_ITALIC; + attr |= property->value.font_attribute.add; break; case CSS_PT_TEXT_DECORATION: - if (property->value.font_attribute.add & AT_UNDERLINE) - attr |= SCREEN_ATTR_UNDERLINE; + attr |= property->value.font_attribute.add; break; case CSS_PT_DISPLAY: case CSS_PT_NONE: @@ -82,7 +73,7 @@ init_template_by_style(struct screen_char *template, struct document_options *op } } - init_template(template, options, background, foreground, attr); + init_template(template, options, attr, foreground, background); } @@ -305,7 +296,7 @@ add_dom_link(struct dom_renderer *renderer, unsigned char *string, int length, link->number = document->nlinks; init_template(&template, &document->options, - link->color.background, link->color.foreground, 0); + 0, link->color.foreground, link->color.background); render_dom_text(renderer, &template, string, length); diff --git a/src/document/html/parser.h b/src/document/html/parser.h index fdb7c5292..855aee37c 100644 --- a/src/document/html/parser.h +++ b/src/document/html/parser.h @@ -2,6 +2,7 @@ #ifndef EL__DOCUMENT_HTML_PARSER_H #define EL__DOCUMENT_HTML_PARSER_H +#include "document/renderer.h" #include "intl/charsets.h" /* unicode_val_T */ #include "util/align.h" #include "util/color.h" @@ -21,21 +22,6 @@ struct uri; * files - there's lack of any well defined interface and it's all randomly * mixed up :/. */ -enum text_style_format { - AT_BOLD = 1, - AT_ITALIC = 2, - AT_UNDERLINE = 4, - AT_FIXED = 8, - AT_GRAPHICS = 16, - AT_PREFORMATTED = 32, -}; - -struct text_style { - enum text_style_format attr; - color_T fg; - color_T bg; -}; - struct text_attrib { struct text_style style; diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 4a7a6c1e8..af766d058 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -345,34 +345,14 @@ get_format_screen_char(struct html_context *html_context, if (memcmp(&ta_cache, &format.style, sizeof(ta_cache))) { copy_struct(&ta_cache, &format.style); - - schar_cache.attr = 0; - if (format.style.attr) { - if (format.style.attr & AT_UNDERLINE) { - schar_cache.attr |= SCREEN_ATTR_UNDERLINE; - } - - if (format.style.attr & AT_BOLD) { - schar_cache.attr |= SCREEN_ATTR_BOLD; - } - - if (format.style.attr & AT_ITALIC) { - schar_cache.attr |= SCREEN_ATTR_ITALIC; - } - - if (format.style.attr & AT_GRAPHICS) { - schar_cache.attr |= SCREEN_ATTR_FRAME; - } - } + struct text_style final_style = format.style; if (link_state != LINK_STATE_NONE && html_context->options->underline_links) { - schar_cache.attr |= SCREEN_ATTR_UNDERLINE; + final_style.attr |= AT_UNDERLINE; } - set_screen_char_color(&schar_cache, format.style.bg, format.style.fg, - html_context->options->color_flags, - html_context->options->color_mode); + get_screen_char_template(&schar_cache, html_context->options, final_style); } if (!!(schar_cache.attr & SCREEN_ATTR_UNSEARCHABLE) diff --git a/src/document/plain/renderer.c b/src/document/plain/renderer.c index 3d4cde914..43c10cfa5 100644 --- a/src/document/plain/renderer.c +++ b/src/document/plain/renderer.c @@ -478,14 +478,9 @@ next: static void init_template(struct screen_char *template, struct document_options *options) { - color_T background = options->default_bg; - color_T foreground = options->default_fg; - struct color_pair colors = INIT_COLOR_PAIR(background, foreground); + struct text_style style = { 0, options->default_fg, options->default_bg }; - template->attr = 0; - template->data = ' '; - set_term_color(template, &colors, - options->color_flags, options->color_mode); + get_screen_char_template(template, options, style); } static struct node * diff --git a/src/document/renderer.c b/src/document/renderer.c index 40f2f58ff..efca6a3da 100644 --- a/src/document/renderer.c +++ b/src/document/renderer.c @@ -31,6 +31,7 @@ #include "protocol/uri.h" #include "session/location.h" #include "session/session.h" +#include "terminal/draw.h" #include "terminal/terminal.h" #include "terminal/window.h" #include "util/error.h" @@ -624,3 +625,36 @@ get_convert_table(unsigned char *head, int to_cp, return get_translation_table(cp_index, to_cp); } + + +void +get_screen_char_template(struct screen_char *template, + struct document_options *options, + struct text_style style) +{ + template->attr = 0; + template->data = ' '; + + if (style.attr) { + if (style.attr & AT_UNDERLINE) { + template->attr |= SCREEN_ATTR_UNDERLINE; + } + + if (style.attr & AT_BOLD) { + template->attr |= SCREEN_ATTR_BOLD; + } + + if (style.attr & AT_ITALIC) { + template->attr |= SCREEN_ATTR_ITALIC; + } + + if (style.attr & AT_GRAPHICS) { + template->attr |= SCREEN_ATTR_FRAME; + } + } + + { + struct color_pair colors = INIT_COLOR_PAIR(style.bg, style.fg); + set_term_color(template, &colors, options->color_flags, options->color_mode); + } +} diff --git a/src/document/renderer.h b/src/document/renderer.h index 8cc828d73..4055ae6f4 100644 --- a/src/document/renderer.h +++ b/src/document/renderer.h @@ -8,6 +8,26 @@ struct document_options; struct document_view; struct session; struct view_state; +struct screen_char; + + +enum text_style_format { + AT_BOLD = 1, + AT_ITALIC = 2, + AT_UNDERLINE = 4, + AT_FIXED = 8, + AT_GRAPHICS = 16, + AT_PREFORMATTED = 32, +}; + +struct text_style { + enum text_style_format attr; + color_T fg; + color_T bg; +}; + +void get_screen_char_template(struct screen_char *template, struct document_options *options, struct text_style style); + void render_document(struct view_state *, struct document_view *, struct document_options *); void render_document_frames(struct session *ses, int no_cache);