From 32effc6657c1802cdf34098748337b0d7bc39dba Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 23 Jun 2024 17:39:15 +0200 Subject: csi: wip: styled underlines This is work in progress, and fairly untested. This adds initial tracking of styled underlines. Setting attributes seems to work (both color and underline style). Grid reflow has *not* been tested. When rendering, style is currently ignored (all styles are rendered as a plain, legacy underline). Color however, *is* applied. --- render.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index fd7e743a..78cc5325 100644 --- a/render.c +++ b/render.c @@ -847,8 +847,43 @@ render_cell(struct terminal *term, pixman_image_t *pix, pixman_region32_t *damag pixman_image_unref(clr_pix); /* Underline */ - if (cell->attrs.underline) - draw_underline(term, pix, font, &fg, x, y, cell_cols); + if (cell->attrs.underline) { + pixman_color_t underline_color = fg; + + /* Check if cell has a styled underline. This lookup is fairly + expensive... */ + if (row->extra != NULL) { + for (int i = 0; i < row->extra->curly_ranges.count; i++) { + const struct row_range *range = &row->extra->curly_ranges.v[i]; + + if (range->start <= col && col <= range->end) { + switch (range->curly.color_src) { + case COLOR_BASE256: + underline_color = color_hex_to_pixman( + term->colors.table[range->curly.color]); + break; + + case COLOR_RGB: + underline_color = + color_hex_to_pixman(range->curly.color); + break; + + case COLOR_DEFAULT: + break; + + case COLOR_BASE16: + BUG("underline color can't be base-16"); + break; + } + + break; + } + } + } + + draw_underline(term, pix, font, &underline_color, x, y, cell_cols); + + } if (cell->attrs.strikethrough) draw_strikeout(term, pix, font, &fg, x, y, cell_cols); -- cgit v1.3 From 8e2402605ec81358cd18c657c9698044598b72f0 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 23 Jun 2024 18:55:37 +0200 Subject: render: styled underlines This was originally contributed by @kraftwerk28 in https://codeberg.org/dnkl/foot/pulls/1099 Here, we re-use the rendering logic only, as attribute tracking has been completely rewritten. --- render.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 78cc5325..471ca467 100644 --- a/render.c +++ b/render.c @@ -384,6 +384,120 @@ draw_underline(const struct terminal *term, pixman_image_t *pix, x, y + y_ofs, cols * term->cell_width, thickness}); } +static void +draw_styled_underline(const struct terminal *term, pixman_image_t *pix, + const struct fcft_font *font, + const pixman_color_t *color, + enum curly_style style, int x, int y, int cols) +{ + xassert(style != CURLY_NONE); + + const int thickness = font->underline.thickness; + + int y_ofs; + + /* Make sure the line isn't positioned below the cell */ + switch (style) { + case CURLY_DOUBLE: + case CURLY_CURLY: + y_ofs = min(underline_offset(term, font), + term->cell_height - thickness * 3); + break; + + default: + y_ofs = min(underline_offset(term, font), + term->cell_height - thickness); + break; + } + + const int ceil_w = cols * term->cell_width; + + switch (style) { + case CURLY_DOUBLE: { + const pixman_rectangle16_t rects[] = { + {x, y + y_ofs, ceil_w, thickness}, + {x, y + y_ofs + thickness * 2, ceil_w, thickness}}; + pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, 2, rects); + break; + } + + case CURLY_DASHED: { + const int ceil_w = cols * term->cell_width; + const int dash_w = ceil_w / 3 + (ceil_w % 3 > 0); + const pixman_rectangle16_t rects[] = { + {x, y + y_ofs, dash_w, thickness}, + {x + dash_w * 2, y + y_ofs, dash_w, thickness}, + }; + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, pix, color, 2, rects); + break; + } + case CURLY_DOTTED: { + const int ceil_w = cols * term->cell_width; + const int nrects = min(ceil_w / thickness / 2, 16); + pixman_rectangle16_t rects[16] = {0}; + + for (int i = 0; i < nrects; i++) { + rects[i] = (pixman_rectangle16_t){ + x + i * thickness * 2, y + y_ofs, thickness, thickness}; + } + + pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, nrects, rects); + break; + } + + case CURLY_CURLY: { + const int top = y + y_ofs; + const int bot = top + thickness * 3; + const int half_x = x + ceil_w / 2.0, full_x = x + ceil_w; + + const double bt_2 = (bot - top) * (bot - top); + const double th_2 = thickness * thickness; + const double hx_2 = ceil_w * ceil_w / 4.0; + const int th = round(sqrt(th_2 + (th_2 * bt_2 / hx_2)) / 2.); + + #define I(x) pixman_int_to_fixed(x) + const pixman_trapezoid_t traps[] = { +#if 0 /* characters sit within the "dips" of the curlies */ + { + I(top), I(bot), + {{I(x), I(top + th)}, {I(half_x), I(bot + th)}}, + {{I(x), I(top - th)}, {I(half_x), I(bot - th)}}, + }, + { + I(top), I(bot), + {{I(half_x), I(bot - th)}, {I(full_x), I(top - th)}}, + {{I(half_x), I(bot + th)}, {I(full_x), I(top + th)}}, + } +#else /* characters sit on top of the curlies */ + { + I(top), I(bot), + {{I(x), I(bot - th)}, {I(half_x), I(top - th)}}, + {{I(x), I(bot + th)}, {I(half_x), I(top + th)}}, + }, + { + I(top), I(bot), + {{I(half_x), I(top + th)}, {I(full_x), I(bot + th)}}, + {{I(half_x), I(top - th)}, {I(full_x), I(bot - th)}}, + } +#endif + }; + + pixman_image_t *fill = pixman_image_create_solid_fill(color); + pixman_composite_trapezoids( + PIXMAN_OP_OVER, fill, pix, PIXMAN_a8, 0, 0, 0, 0, + sizeof(traps) / sizeof(traps[0]), traps); + + pixman_image_unref(fill); + break; + } + + default: + draw_underline(term, pix, font, color, x, y, cols); + break; + } +} + static void draw_strikeout(const struct terminal *term, pixman_image_t *pix, const struct fcft_font *font, @@ -849,6 +963,7 @@ render_cell(struct terminal *term, pixman_image_t *pix, pixman_region32_t *damag /* Underline */ if (cell->attrs.underline) { pixman_color_t underline_color = fg; + enum curly_style underline_style = CURLY_SINGLE; /* Check if cell has a styled underline. This lookup is fairly expensive... */ @@ -876,12 +991,14 @@ render_cell(struct terminal *term, pixman_image_t *pix, pixman_region32_t *damag break; } + underline_style = range->curly.style; break; } } } - draw_underline(term, pix, font, &underline_color, x, y, cell_cols); + draw_styled_underline( + term, pix, font, &underline_color, underline_style, x, y, cell_cols); } -- cgit v1.3 From 963ce45f3f29bccf7ca5aa0dc8e258a11cfbc552 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 24 Jun 2024 00:57:24 +0200 Subject: render: resize: copy styled underlines to temporary grid When doing an interactive resize, we create a small grid copy of the current viewport, and then do a non-reflow resize. When the interactive resize is done, we do a proper reflow. This is for performance reasons. When creating the viewport copy, we also need to copy the styled underlines. Otherwise, styled underlines will be rendered as plain underlines *while resizing*. --- render.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index 471ca467..b9a597a9 100644 --- a/render.c +++ b/render.c @@ -4395,6 +4395,29 @@ render_resize(struct terminal *term, int width, int height, uint8_t opts) memcpy(g.rows[i]->cells, orig->rows[j]->cells, g.num_cols * sizeof(g.rows[i]->cells[0])); + + if (orig->rows[j]->extra == NULL || + orig->rows[j]->extra->curly_ranges.count == 0) + { + continue; + } + + /* + * Copy undercurly ranges + */ + + const struct row_ranges *curly_src = &orig->rows[j]->extra->curly_ranges; + + const int count = curly_src->count; + g.rows[i]->extra = xcalloc(1, sizeof(*g.rows[i]->extra)); + g.rows[i]->extra->curly_ranges.v = xmalloc( + count * sizeof(g.rows[i]->extra->curly_ranges.v[0])); + + struct row_ranges *curly_dst = &g.rows[i]->extra->curly_ranges; + curly_dst->count = curly_dst->size = count; + + for (int k = 0; k < count; k++) + curly_dst->v[k] = curly_src->v[k]; } term->normal = g; -- cgit v1.3 From 08138e9546a00736d6dab6197070f5455d4d7f8c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 24 Jun 2024 01:50:52 +0200 Subject: render: underlines: minor perf-tweak: early break out When looking up the extender underline range, break out early if we see that there can't possibly be any ranges matching the current column. --- render.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'render.c') diff --git a/render.c b/render.c index b9a597a9..d66bce65 100644 --- a/render.c +++ b/render.c @@ -971,6 +971,9 @@ render_cell(struct terminal *term, pixman_image_t *pix, pixman_region32_t *damag for (int i = 0; i < row->extra->curly_ranges.count; i++) { const struct row_range *range = &row->extra->curly_ranges.v[i]; + if (range->start > col) + break; + if (range->start <= col && col <= range->end) { switch (range->curly.color_src) { case COLOR_BASE256: -- cgit v1.3 From 19bf558e6cee456eabe6b87d9d51e8f220e20cfa Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 27 Jun 2024 18:36:17 +0200 Subject: render: underlines: improve the appearance of the 'dotted' style Try to make the 'dotted' style appear more even, and less like each cell is rendered separately (even though they are). Algorithm: Each dot is a square; it's sides are that of the font's line thickness. The spacing (gaps) between the dots is initially the same width as the dots themselves. This means the number of dots per cell is the cell width divided by the dots' length/width, divided by two. At this point, there may be "left-over" pixels.I.e. the widths of the dots and the gaps between them may not add up to the width of the cell. These pixels are evenly (as possible) across the gaps. There are still visual inaccuracies at small font sizes. This is impossible to fix without changing the way underlines are rendered, to render an entire line in one go. This is not something we want to do, since it'll make styled underlines, for a specific cell/character, look differently, depending on the surrounding context. --- render.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index d66bce65..65fd1739 100644 --- a/render.c +++ b/render.c @@ -432,17 +432,43 @@ draw_styled_underline(const struct terminal *term, pixman_image_t *pix, PIXMAN_OP_SRC, pix, color, 2, rects); break; } + case CURLY_DOTTED: { - const int ceil_w = cols * term->cell_width; - const int nrects = min(ceil_w / thickness / 2, 16); - pixman_rectangle16_t rects[16] = {0}; + /* Number of dots per cell */ + int per_cell = (term->cell_width / thickness) / 2; + if (per_cell == 0) + per_cell = 1; + + xassert(per_cell >= 1); + + /* Spacing between dots; start with the same width as the dots + themselves, then widen them if necessary, to consume unused + pixels */ + int spacing[per_cell]; + for (int i = 0; i < per_cell; i++) + spacing[i] = thickness; + + /* Pixels remaining at the end of the cell */ + int remaining = term->cell_width - (per_cell * 2) * thickness; - for (int i = 0; i < nrects; i++) { + /* Spread out the left-over pixels across the spacing between + the dots */ + for (int i = 0; remaining > 0; i = (i + 1) % per_cell, remaining--) + spacing[i]++; + + xassert(remaining <= 0); + + pixman_rectangle16_t rects[per_cell]; + int dot_x = x; + for (int i = 0; i < per_cell; i++) { rects[i] = (pixman_rectangle16_t){ - x + i * thickness * 2, y + y_ofs, thickness, thickness}; + dot_x, y + y_ofs, thickness, thickness + }; + + dot_x += thickness + spacing[i]; } - pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, nrects, rects); + pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, per_cell, rects); break; } -- cgit v1.3 From 0c7725217a706f6df1b543c4c85f54872aaf2f21 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 27 Jun 2024 18:54:46 +0200 Subject: render: draw_styled_underline(): respect main.underline-thickness Also refactor a bit, and break out early to draw_underline() for legacy underlines. --- render.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 65fd1739..35b7c6ae 100644 --- a/render.c +++ b/render.c @@ -392,7 +392,15 @@ draw_styled_underline(const struct terminal *term, pixman_image_t *pix, { xassert(style != CURLY_NONE); - const int thickness = font->underline.thickness; + if (style == CURLY_SINGLE) { + draw_underline(term, pix, font, color, x, y, cols); + return; + } + + const int thickness = term->conf->underline_thickness.px >= 0 + ? term_pt_or_px_as_pixels( + term, &term->conf->underline_thickness) + : font->underline.thickness; int y_ofs; @@ -404,10 +412,16 @@ draw_styled_underline(const struct terminal *term, pixman_image_t *pix, term->cell_height - thickness * 3); break; - default: + case CURLY_DASHED: + case CURLY_DOTTED: y_ofs = min(underline_offset(term, font), term->cell_height - thickness); break; + + case CURLY_NONE: + case CURLY_SINGLE: + BUG("underline styles not supposed to be handled here"); + break; } const int ceil_w = cols * term->cell_width; @@ -518,8 +532,9 @@ draw_styled_underline(const struct terminal *term, pixman_image_t *pix, break; } - default: - draw_underline(term, pix, font, color, x, y, cols); + case CURLY_NONE: + case CURLY_SINGLE: + BUG("underline styles not supposed to be handled here"); break; } } -- cgit v1.3