css: a simple css example ok
authoryu.dongliang <18588496441@163.com>
Wed, 4 Mar 2026 15:13:26 +0000 (23:13 +0800)
committeryu.dongliang <18588496441@163.com>
Wed, 4 Mar 2026 15:13:26 +0000 (23:13 +0800)
examples/css.html
html/abc_css.c
html/abc_html.c
html/abc_html.h
html/abc_obj.c
html/abc_obj.h
ui/Makefile
ui/abc_layout.c
ui/abc_render.c
ui/abc_render_h1.c

index 7e1dda04d7d0de96e3e11cd3733562d62b8f9c37..bf8095a51d99916fd3d2153839724bf3b92b178f 100644 (file)
@@ -4,7 +4,7 @@
 <style>
 p
 {
-       color:red;
+       color:blue;
        text-align:center;
 } 
 </style>
index 4ddf3b1c50094164bd82764aa8d6007c9de5aaf8..ebcbb13974b26567cd7089050c5df99774c46569 100644 (file)
@@ -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;
+}
index 664da6bd46ab777c6f70b5e97b26ab71c785ae55..63a68fbd9961deb7200fc1a16ba4de4aec40f036 100644 (file)
@@ -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;
        };
 
index 81f78913b48a11e27363e39e4d670997791154fe..4ecb379b5d23516b90b7fa050d94b55155aa6834 100644 (file)
@@ -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
index 4d4bb2ac9556291f4b559ebd1ee42de1ad17b580..b2ada1f7b2badcf63470594a9a55c06a4749010b 100644 (file)
@@ -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;
index c1a74f31a06ad0f2460118b65bdf7628f88e2866..66663b70618de996ddb3abc61456bf4dee1140e8 100644 (file)
@@ -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
index 86240945ce686874496eac20181ac9188ace26fd..ed0856ec7216e43597333322f191d68af23ca96d 100644 (file)
@@ -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
index 02b4debf16d921a449918ff04d6e94aadff02965..63c0a94fc4f91d053dfb14bde421cb9e8049f929 100644 (file)
@@ -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)
index 4e5be5a8bfdda441157271fc5b48db752fe19774..2784190adeb6244e30b1373bab04d576694431c2 100644 (file)
@@ -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()
index 300e84cbfe858619b1eecff96459512afe7f3e45..810f6ef44059d4418540305b0f96511ab407bf8f 100644 (file)
@@ -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);