From: yu.dongliang <18588496441@163.com> Date: Wed, 4 Mar 2026 15:13:26 +0000 (+0800) Subject: css: a simple css example ok X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=64363851112d832fddf9b4a7c890da1f9ab3a99f;p=abc.git css: a simple css example ok --- diff --git a/examples/css.html b/examples/css.html index 7e1dda0..bf8095a 100644 --- a/examples/css.html +++ b/examples/css.html @@ -4,7 +4,7 @@ diff --git a/html/abc_css.c b/html/abc_css.c index 4ddf3b1..ebcbb13 100644 --- a/html/abc_css.c +++ b/html/abc_css.c @@ -298,3 +298,29 @@ int abc_css_parse(abc_obj_t* css) return -1; } + +int abc_css_use(abc_html_t* html, abc_obj_t* obj) +{ + if (!html->root || !obj) + return -EINVAL; + + scf_list_t* l; + abc_obj_t* css; + abc_obj_t* label; + abc_obj_t* attr; + + css = abc_obj_find_type(html->root, ABC_HTML_STYLE); + if (!css) + return 0; + + label = abc_obj_find_type(css, obj->type); + if (label) { + for (l = scf_list_head(&label->attrs); l != scf_list_sentinel(&label->attrs); l = scf_list_next(l)) { + attr = scf_list_data(l, abc_obj_t, list); + + abc_obj_set_attr(obj, attr->type, attr->value->data); + } + } + + return 0; +} diff --git a/html/abc_html.c b/html/abc_html.c index 664da6b..63a68fb 100644 --- a/html/abc_html.c +++ b/html/abc_html.c @@ -1153,8 +1153,10 @@ static int __html_parse_obj(abc_html_t* html, abc_char_t* c) case ABC_HTML_AUDIO: if (__html_add_controls(obj) < 0) return -1; - break; + default: + if (abc_css_use(html, obj) < 0) + return -1; break; }; diff --git a/html/abc_html.h b/html/abc_html.h index 81f7891..4ecb379 100644 --- a/html/abc_html.h +++ b/html/abc_html.h @@ -50,5 +50,6 @@ void abc_html_close(abc_html_t* html); int abc_html_parse(abc_html_t* html); int abc_css_parse (abc_obj_t* css); +int abc_css_use (abc_html_t* html, abc_obj_t* obj); #endif diff --git a/html/abc_obj.c b/html/abc_obj.c index 4d4bb2a..b2ada1f 100644 --- a/html/abc_obj.c +++ b/html/abc_obj.c @@ -86,6 +86,26 @@ abc_obj_t* abc_obj_find(abc_obj_t* root, int x, int y) return root; } +abc_obj_t* abc_obj_find_type(abc_obj_t* root, int type) +{ + scf_list_t* l; + abc_obj_t* child; + abc_obj_t* obj; + + if (type == root->type) + return root; + + for (l = scf_list_head(&root->childs); l != scf_list_sentinel(&root->childs); l = scf_list_next(l)) { + child = scf_list_data(l, abc_obj_t, list); + + obj = abc_obj_find_type(child, type); + if (obj) + return obj; + } + + return NULL; +} + int abc_obj_set_attr(abc_obj_t* obj, int key, const char* value) { scf_string_t* s; diff --git a/html/abc_obj.h b/html/abc_obj.h index c1a74f3..66663b7 100644 --- a/html/abc_obj.h +++ b/html/abc_obj.h @@ -154,6 +154,8 @@ int abc_obj_set_attr (abc_obj_t* obj, int key, const char* value); abc_obj_t* abc_obj_get_attr (abc_obj_t* obj, int key); abc_obj_t* abc_obj_find_attr(abc_obj_t* obj, int key); +abc_obj_t* abc_obj_find_type(abc_obj_t* root, int type); + scf_string_t* abc_obj_to_string(abc_obj_t* obj); #endif diff --git a/ui/Makefile b/ui/Makefile index 8624094..ed0856e 100644 --- a/ui/Makefile +++ b/ui/Makefile @@ -43,6 +43,7 @@ CFILES += abc_render_video.c CFILES += abc_render_audio.c CFILES += ../html/abc_html.c +CFILES += ../html/abc_css.c CFILES += ../html/abc_obj.c CFILES += ../html/abc_io_util.c CFILES += ../html/abc_io_file.c diff --git a/ui/abc_layout.c b/ui/abc_layout.c index 02b4deb..63c0a94 100644 --- a/ui/abc_layout.c +++ b/ui/abc_layout.c @@ -63,6 +63,7 @@ static abc_layout_pt abc_layouts[ABC_HTML_NB] = [ABC_HTML_PROGRESS] = abc_layout_empty, [ABC_HTML_SCRIPT] = abc_layout_empty, + [ABC_HTML_STYLE] = abc_layout_empty, }; int abc_layout_obj(abc_layout_t* layout, abc_obj_t* obj, int width, int height) diff --git a/ui/abc_render.c b/ui/abc_render.c index 4e5be5a..2784190 100644 --- a/ui/abc_render.c +++ b/ui/abc_render.c @@ -65,6 +65,7 @@ static abc_render_t* abc_renders[ABC_HTML_NB] = [ABC_HTML_PROGRESS] = &abc_render_empty, [ABC_HTML_SCRIPT] = &abc_render_empty, + [ABC_HTML_STYLE] = &abc_render_empty, }; int abc_renders_fini() diff --git a/ui/abc_render_h1.c b/ui/abc_render_h1.c index 300e84c..810f6ef 100644 --- a/ui/abc_render_h1.c +++ b/ui/abc_render_h1.c @@ -79,6 +79,18 @@ static int _render_draw_h1(abc_render_t* render, abc_obj_t* obj, int width, int cairo_set_font_size(cr, atoi(attr->value->data)); cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); + attr = abc_obj_find_attr(obj, ABC_HTML_ATTR_FONT_COLOR); + if (attr) { + if (!strcmp(attr->value->data, "red")) + cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0); + + else if (!strcmp(attr->value->data, "green")) + cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0); + + else if (!strcmp(attr->value->data, "blue")) + cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0); + } + cairo_text_extents(cr, obj->text->data, &extents); cairo_move_to (cr, extents.x_bearing, -extents.y_bearing);