From 77b06bb7b3280339669e6917fda0455c8e247715 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Fri, 1 May 2026 15:01:38 +0800 Subject: [PATCH] draw border of

, , maybe other HTML objects --- html/abc_css_length.c | 36 +++++++++++++++++++++++ html/abc_html.h | 3 ++ ui/abc_layout.c | 14 ++++----- ui/abc_layout_h1.c | 68 +++++++++++++++++++++++++------------------ ui/abc_layout_table.c | 28 ++---------------- ui/abc_render_h1.c | 41 +++++++++++++++++--------- ui/abc_render_table.c | 2 +- 7 files changed, 117 insertions(+), 75 deletions(-) diff --git a/html/abc_css_length.c b/html/abc_css_length.c index f51c989..45d2c7e 100644 --- a/html/abc_css_length.c +++ b/html/abc_css_length.c @@ -60,3 +60,39 @@ next: return x; } + +int abc_css_width(abc_obj_t* obj, int width, int margin) +{ + abc_obj_t* attr = abc_obj_get_attr(obj, ABC_HTML_ATTR_WIDTH); + + if (attr && attr->value && attr->value->len > 0) { + + obj->w = abc_css_length(obj, attr->value->data, width); + obj->w += margin * 2; + + if (obj->w > width - obj->x) + obj->w = width - obj->x; + return 1; + } + + obj->w = width - obj->x; + return 0; +} + +int abc_css_height(abc_obj_t* obj, int height, int margin) +{ + abc_obj_t* attr = abc_obj_get_attr(obj, ABC_HTML_ATTR_HEIGHT); + + if (attr && attr->value && attr->value->len > 0) { + + obj->h = abc_css_length(obj, attr->value->data, height); + obj->h += margin * 2; + + if (obj->h > height - obj->y) + obj->h = height - obj->y; + return 1; + } + + obj->h = height - obj->y; + return 0; +} diff --git a/html/abc_html.h b/html/abc_html.h index 8d5ea23..6bfb8f3 100644 --- a/html/abc_html.h +++ b/html/abc_html.h @@ -61,4 +61,7 @@ int abc_css_border(double* r, double* g, double* b, int* width, int* type, abc_ int abc_css_margin(abc_obj_t* obj); int abc_css_length(abc_obj_t* obj, const uint8_t* str, int len); +int abc_css_width (abc_obj_t* obj, int width, int margin); +int abc_css_height(abc_obj_t* obj, int height, int margin); + #endif diff --git a/ui/abc_layout.c b/ui/abc_layout.c index a8b33c5..1dd4fd5 100644 --- a/ui/abc_layout.c +++ b/ui/abc_layout.c @@ -163,8 +163,8 @@ int abc_layout_root(abc_ctx_t* abc, abc_obj_t* root, int width, int height) break; }; - int x = root->x + 4; - int y = root->y + 4; + int x = root->x + 2; + int y = root->y + 2; int h = 0; int X = 0; @@ -185,21 +185,21 @@ int abc_layout_root(abc_ctx_t* abc, abc_obj_t* root, int width, int height) case ABC_HTML_OL: case ABC_HTML_UL: case ABC_HTML_LI: - child->x = root->x + 4; + child->x = root->x + 2; child->y = y + h; ret = abc_layout_root(NULL, child, width, height); if (ret < 0) return ret; - x = root->x + 4; + x = root->x + 2; y = child->y + child->h; h = 0; break; case ABC_HTML_BR: case ABC_HTML_TR: - x = root->x + 4; + x = root->x + 2; y += h; h = 0; default: @@ -223,7 +223,7 @@ int abc_layout_root(abc_ctx_t* abc, abc_obj_t* root, int width, int height) } else { y += h; - child->x = root->x + 4; + child->x = root->x + 2; child->y = y; ret = abc_layout_root(NULL, child, width, height); @@ -235,7 +235,7 @@ int abc_layout_root(abc_ctx_t* abc, abc_obj_t* root, int width, int height) } if (ABC_HTML_P == child->type && ABC_HTML_P != root->type) { - x = root->x + 4; + x = root->x + 2; y += h; h = 0; } diff --git a/ui/abc_layout_h1.c b/ui/abc_layout_h1.c index c735cdc..a620318 100644 --- a/ui/abc_layout_h1.c +++ b/ui/abc_layout_h1.c @@ -151,44 +151,56 @@ int abc_layout_h1(abc_layout_t* layout, abc_obj_t* obj, int width, int height) cairo_set_font_size (cr, size); cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); - int ret = __layout_text(&extents, cr, obj, width); - if (ret < 0) - scf_loge("ret: %d\n", ret); + int d = abc_css_margin(obj); + int w_set = abc_css_width (obj, width, d); + int h_set = abc_css_height(obj, height, d); + + int dw = 0; + switch (obj->parent->type) { + case ABC_HTML_OL: + if (0 == obj->parent->padding) { + scf_string_t* s = abc_ol_list_style(obj->parent, obj->parent->n_childs); + if (s) { + cairo_text_extents(cr, s->data, &extents2); + + obj->parent->padding = extents2.width + extents2.x_bearing + size / 2; + + scf_string_free(s); + s = NULL; + } + } - int w = 0; - if (ABC_HTML_OL == obj->parent->type) - { - if (0 == obj->parent->padding) { - scf_string_t* s = abc_ol_list_style(obj->parent, obj->parent->n_childs); - if (s) { - cairo_text_extents(cr, s->data, &extents2); + dw = obj->parent->padding; + break; - obj->parent->padding = extents2.width + extents2.x_bearing + size / 2; + case ABC_HTML_UL: + dw = size; + obj->parent->padding = size; + break; + default: + break; + }; - scf_string_free(s); - s = NULL; - } - } + int w = (width - d * 2 - dw) / size * size; - obj->y_bearing = extents.y_bearing; + int ret = __layout_text(&extents, cr, obj, w); + if (ret < 0) + scf_loge("ret: %d\n", ret); - w = obj->parent->padding; - } else if (ABC_HTML_UL == obj->parent->type) { - w = size; - obj->parent->padding = size; - } + if (ABC_HTML_OL == obj->parent->type) + obj->y_bearing = extents.y_bearing; - w += extents.width + extents.x_bearing; + w = extents.width + extents.x_bearing + d * 2 + dw; obj->w = w; - obj->h = extents.height + extents.height / 2; - obj->w = (obj->w + 3) & ~0x3; - obj->h = (obj->h + 3) & ~0x3; + obj->h = extents.height + extents.height / 2 + d * 2; +// obj->w = (obj->w + 3) & ~0x3; +// obj->h = (obj->h + 3) & ~0x3; - scf_logi("%s, w: %d, h: %d, x_bearing: %lg, y_bearing: %lg, width: %lg, height: %lg, x_advance: %lg, y_advance: %lg\n", - obj->text->data, obj->w, obj->h, + scf_logd("%s, w: %d, h: %d, d: %d, x_bearing: %lg, y_bearing: %lg, width: %lg, height: %lg, x_advance: %lg, y_advance: %lg, width: %d, height: %d\n", + obj->text->data, obj->w, obj->h, d, extents.x_bearing, extents.y_bearing, extents.width, extents.height, - extents.x_advance, extents.y_advance); + extents.x_advance, extents.y_advance, width, height); cairo_destroy(cr); cairo_surface_destroy(surface); diff --git a/ui/abc_layout_table.c b/ui/abc_layout_table.c index b877475..e240c6b 100644 --- a/ui/abc_layout_table.c +++ b/ui/abc_layout_table.c @@ -18,33 +18,9 @@ int abc_layout_table(abc_layout_t* layout, abc_obj_t* obj, int width, int height } } - int w_set = 0; - int h_set = 0; int d = abc_css_margin(obj); - - attr = abc_obj_get_attr(obj, ABC_HTML_ATTR_WIDTH); - if (attr && attr->value && attr->value->len > 0) - { - obj->w = abc_css_length(obj, attr->value->data, width); - obj->w += d * 2; - - if (obj->w > width - obj->x) - obj->w = width - obj->x; - - w_set = 1; - } - - attr = abc_obj_get_attr(obj, ABC_HTML_ATTR_HEIGHT); - if (attr && attr->value && attr->value->len > 0) - { - obj->h = abc_css_length(obj, attr->value->data, height); - obj->h += d * 2; - - if (obj->h > height - obj->y) - obj->h = height - obj->y; - - h_set = 1; - } + int w_set = abc_css_width (obj, width, d); + int h_set = abc_css_height(obj, height, d); int x = obj->x + d; int y = obj->y + d; diff --git a/ui/abc_render_h1.c b/ui/abc_render_h1.c index d4b6a50..ed18e6a 100644 --- a/ui/abc_render_h1.c +++ b/ui/abc_render_h1.c @@ -1,5 +1,7 @@ #include"abc.h" +extern abc_render_t abc_render_table; + static const char* vert_shader = "#version 330 core\n" "layout(location = 0) in vec4 position; \n" @@ -169,6 +171,19 @@ int __init_text(cairo_t* cr, abc_obj_t* obj, int num_flag) static int _render_draw_h1(abc_render_t* render, abc_obj_t* obj, int width, int height) { + if (obj->w <= 0 || obj->h <= 0) + return 0; + + abc_render_table.draw(&abc_render_table, obj, width, height); + + int d = abc_css_margin(obj); + int x = obj->x + d; + int y = obj->y + d; + int w = obj->w - d * 2; + int h = obj->h - d * 2; + + scf_logd("obj->type: %d, obj->w: %d, obj->h: %d, w: %d, h: %d, d: %d\n", obj->type, obj->w, obj->h, w, h, d); + if (0 == program) __init_program(&program, vert_shader, frag_shader); @@ -176,13 +191,13 @@ static int _render_draw_h1(abc_render_t* render, abc_obj_t* obj, int width, int __init_buffers(&vao, buffers); if (0 == texture_rgba) - __init_texture(&texture_rgba, GL_RGBA, obj->w, obj->h, NULL); + __init_texture(&texture_rgba, GL_RGBA, w, h, NULL); abc_obj_t* attr; cairo_surface_t* surface; cairo_t* cr; - uint8_t* bgra = calloc(1, obj->w * obj->h * 4); + uint8_t* bgra = calloc(1, w * h * 4); if (!bgra) return -ENOMEM; @@ -193,12 +208,12 @@ static int _render_draw_h1(abc_render_t* render, abc_obj_t* obj, int width, int if (attr) abc_css_color(&r, &g, &b, attr->value->data); - surface = cairo_image_surface_create_for_data(bgra, CAIRO_FORMAT_ARGB32, obj->w, obj->h, obj->w * 4); + surface = cairo_image_surface_create_for_data(bgra, CAIRO_FORMAT_ARGB32, w, h, w * 4); cr = cairo_create(surface); cairo_set_line_width(cr, 1); cairo_set_source_rgb(cr, r, g, b); - cairo_rectangle(cr, 0, 0, obj->w, obj->h); + cairo_rectangle(cr, 0, 0, w, h); cairo_fill(cr); cairo_stroke(cr); @@ -218,17 +233,17 @@ static int _render_draw_h1(abc_render_t* render, abc_obj_t* obj, int width, int GLfloat vert_update[] = { - 2.0 * obj->x / (float)width - 1.0, - -2.0 * (obj->y + obj->h) / (float)height + 1.0, + 2.0 * x / (float)width - 1.0, + -2.0 * (y + h) / (float)height + 1.0, - 2.0 * (obj->x + obj->w) / (float)width - 1.0, - -2.0 * (obj->y + obj->h) / (float)height + 1.0, + 2.0 * (x + w) / (float)width - 1.0, + -2.0 * (y + h) / (float)height + 1.0, - 2.0 * obj->x / (float)width - 1.0, - -2.0 * obj->y / (float)height + 1.0, + 2.0 * x / (float)width - 1.0, + -2.0 * y / (float)height + 1.0, - 2.0 * (obj->x + obj->w) / (float)width - 1.0, - -2.0 * obj->y / (float)height + 1.0, + 2.0 * (x + w) / (float)width - 1.0, + -2.0 * y / (float)height + 1.0, }; glUseProgram(program); @@ -240,7 +255,7 @@ static int _render_draw_h1(abc_render_t* render, abc_obj_t* obj, int width, int // board glActiveTexture(GL_TEXTURE0); glBindTexture (GL_TEXTURE_2D, texture_rgba); - glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, obj->w, obj->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bgra); + glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bgra); glUniform1i(uniform_rgba, 0); // draw diff --git a/ui/abc_render_table.c b/ui/abc_render_table.c index 732d3f7..bd457f3 100644 --- a/ui/abc_render_table.c +++ b/ui/abc_render_table.c @@ -64,7 +64,7 @@ static int _render_draw_table(abc_render_t* render, abc_obj_t* obj, int width, i __init_buffers(&vao, buffers); int margin = 0; - int border = 1; + int border = 0; int border_type = ABC_BORDER_SOLID; double r0 = 0.0; -- 2.25.1