summaryrefslogtreecommitdiff
path: root/src/entry_backend
diff options
context:
space:
mode:
authorPhil Jones <philj56@gmail.com>2022-07-30 09:17:52 +0100
committerPhil Jones <philj56@gmail.com>2022-07-30 09:17:52 +0100
commit655bde52896b4d6995d0c1b349a7f34d0bb221b1 (patch)
tree3ae9c5ef8e7138bdb9356ce0a237427d5c843a0c /src/entry_backend
parent87757b4f0fe9dad041546d0c86a83d918c0aae92 (diff)
Add option to colour matching portion of results.
This was done by breaking the selected result into three parts, and rendering each separately. A side-effect is that ligatures split when a match ends inside them, but I think that's the correct behaviour (rather than highlighting the whole ligature). There may be issues with some non-latin languages that make much more extensive use of ligatures / combining characters, however.
Diffstat (limited to 'src/entry_backend')
-rw-r--r--src/entry_backend/harfbuzz.c87
-rw-r--r--src/entry_backend/pango.c52
2 files changed, 124 insertions, 15 deletions
diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c
index cba1fd8..e3217a7 100644
--- a/src/entry_backend/harfbuzz.c
+++ b/src/entry_backend/harfbuzz.c
@@ -242,17 +242,88 @@ void entry_backend_harfbuzz_update(struct entry *entry)
break;
}
- hb_buffer_clear_contents(buffer);
- setup_hb_buffer(buffer);
- hb_buffer_add_utf8(buffer, entry->results.buf[i].string, -1, 0, -1);
- hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0);
- if (i == entry->selection) {
+ /* If this isn't the selected result, just print as normal. */
+ if (i != entry->selection) {
+ hb_buffer_clear_contents(buffer);
+ setup_hb_buffer(buffer);
+ hb_buffer_add_utf8(buffer, entry->results.buf[i].string, -1, 0, -1);
+ hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0);
+ width = render_hb_buffer(cr, buffer);
+ } else {
+ /*
+ * For the selected result, there's a bit more to do.
+ *
+ * First, we need to use a different foreground color -
+ * simple enough.
+ *
+ * Next, we may need to draw a background box - this
+ * involves rendering to a cairo group, measuring the
+ * size of the text, drawing the background on the main
+ * canvas, then finally drawing the group on top of
+ * that.
+ *
+ * Finally, we may need to highlight the matching
+ * portion of text - this is achieved simply by
+ * splitting the text into prematch, match and
+ * postmatch chunks, and drawing each separately.
+ */
+ size_t prematch_len;
+ char *prematch = xstrdup(entry->results.buf[i].string);
+ char *match = NULL;
+ char *postmatch = NULL;
+ uint32_t subwidth;
+ if (entry->input_mb_length > 0 && entry->selection_highlight_color.a != 0) {
+ char *match_pos = strcasestr(prematch, entry->input_mb);
+ if (match_pos != NULL) {
+ match = xstrdup(entry->results.buf[i].string);
+ postmatch = xstrdup(entry->results.buf[i].string);
+ prematch_len = (match_pos - prematch);
+ prematch[prematch_len] = '\0';
+ match[entry->input_mb_length + prematch_len] = '\0';
+ }
+ }
+
cairo_push_group(cr);
color = entry->selection_foreground_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
- }
- width = render_hb_buffer(cr, buffer);
- if (i == entry->selection) {
+
+ hb_buffer_clear_contents(buffer);
+ setup_hb_buffer(buffer);
+ hb_buffer_add_utf8(buffer, prematch, -1, 0, -1);
+ hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0);
+ subwidth = render_hb_buffer(cr, buffer);
+ width = subwidth;
+
+ if (match != NULL) {
+ cairo_translate(cr, subwidth, 0);
+ color = entry->selection_highlight_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+ hb_buffer_clear_contents(buffer);
+ setup_hb_buffer(buffer);
+ hb_buffer_add_utf8(buffer, &match[prematch_len], -1, 0, -1);
+ hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0);
+ subwidth = render_hb_buffer(cr, buffer);
+ width += subwidth;
+
+ cairo_translate(cr, subwidth, 0);
+ color = entry->selection_foreground_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+ hb_buffer_clear_contents(buffer);
+ setup_hb_buffer(buffer);
+ hb_buffer_add_utf8(buffer, &postmatch[entry->input_mb_length + prematch_len], -1, 0, -1);
+ hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0);
+ subwidth = render_hb_buffer(cr, buffer);
+ width += subwidth;
+
+ free(match);
+ free(postmatch);
+ match = NULL;
+ postmatch = NULL;
+ }
+
+ free(prematch);
+ prematch = NULL;
+
cairo_pop_group_to_source(cr);
cairo_save(cr);
color = entry->selection_background_color;
diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c
index 73eeb49..c182e4c 100644
--- a/src/entry_backend/pango.c
+++ b/src/entry_backend/pango.c
@@ -4,6 +4,7 @@
#include "../entry.h"
#include "../log.h"
#include "../nelem.h"
+#include "../xmalloc.h"
#undef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
@@ -95,16 +96,53 @@ void entry_backend_pango_update(struct entry *entry)
} else {
str = "";
}
- pango_layout_set_text(layout, str, -1);
- pango_cairo_update_layout(cr, layout);
- if (i == entry->selection) {
+ if (i != entry->selection) {
+ pango_layout_set_text(layout, str, -1);
+ pango_cairo_update_layout(cr, layout);
+ pango_cairo_show_layout(cr, layout);
+ pango_layout_get_size(layout, &width, &height);
+ } else {
+ ssize_t prematch_len = -1;
+ size_t match_len = entry->input_mb_length;
+ int32_t subwidth;
+ if (entry->input_mb_length > 0 && entry->selection_highlight_color.a != 0) {
+ char *match_pos = strcasestr(str, entry->input_mb);
+ if (match_pos != NULL) {
+ prematch_len = (match_pos - str);
+ }
+ }
+
cairo_push_group(cr);
color = entry->selection_foreground_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
- }
- pango_cairo_show_layout(cr, layout);
- pango_layout_get_size(layout, &width, &height);
- if (i == entry->selection) {
+
+ pango_layout_set_text(layout, str, prematch_len);
+ pango_cairo_update_layout(cr, layout);
+ pango_cairo_show_layout(cr, layout);
+ pango_layout_get_size(layout, &subwidth, &height);
+ width = subwidth;
+
+ if (prematch_len != -1) {
+ cairo_translate(cr, (int)(subwidth / PANGO_SCALE), 0);
+ color = entry->selection_highlight_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+ pango_layout_set_text(layout, &str[prematch_len], match_len);
+ pango_cairo_update_layout(cr, layout);
+ pango_cairo_show_layout(cr, layout);
+ pango_layout_get_size(layout, &subwidth, &height);
+ width += subwidth;
+
+ cairo_translate(cr, (int)(subwidth / PANGO_SCALE), 0);
+ color = entry->selection_foreground_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+ pango_layout_set_text(layout, &str[prematch_len + match_len], -1);
+ pango_cairo_update_layout(cr, layout);
+ pango_cairo_show_layout(cr, layout);
+ pango_layout_get_size(layout, &subwidth, &height);
+ width += subwidth;
+
+ }
+
cairo_pop_group_to_source(cr);
cairo_save(cr);
color = entry->selection_background_color;