From 8e28a79e88c344023a036758bb9793549f29829f Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 25 Apr 2023 17:50:05 +0300 Subject: [PATCH] greatly improve performance by caching text size --- src/text-node.c | 33 ++++++++++++++++++--------------- src/text-node.h | 3 +++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/text-node.c b/src/text-node.c index 1b7684a..aed844a 100644 --- a/src/text-node.c +++ b/src/text-node.c @@ -17,6 +17,9 @@ UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA col n->color = color; n->caret_index = -1; n->caret_node = NULL; + n->computed_dimensions_invalid = true; + n->computed_text_width = 0; + n->computed_text_height = 0; n->node.text = "text"; n->node.handle_proto = text_node_handle; node_attach(parent, (UINode*)n); @@ -47,6 +50,7 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p) n->layout = layout; n->pending_text = NULL; } + n->computed_dimensions_invalid = true; } switch (ev) { @@ -59,23 +63,22 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p) cairo_fill(n->node.drw); break; } - case UI_EVENT_GET_WIDTH: { - if (!n->layout) { - break; - } - int w; - pango_layout_get_size(n->layout, &w, NULL); - *(double*)p = pango_units_to_double(w); - return 1; - } + case UI_EVENT_GET_WIDTH: /* through */ case UI_EVENT_GET_HEIGHT: { - if (!n->layout) { - break; + if (n->computed_dimensions_invalid) { + int w, h = 0; + pango_layout_get_size(n->layout, &w, &h); + n->computed_text_width = pango_units_to_double(w); + n->computed_text_height = pango_units_to_double(h); + n->computed_dimensions_invalid = false; } - int h; - pango_layout_get_size(n->layout, NULL, &h); - *(double*)p = pango_units_to_double(h); - return 1; + if (ev == UI_EVENT_GET_WIDTH) { + *(double*)p = n->computed_text_width; + } + if (ev == UI_EVENT_GET_HEIGHT) { + *(double*)p = n->computed_text_height; + } + break; } case UI_EVENT_RELAYOUT: { if (!n->layout) { diff --git a/src/text-node.h b/src/text-node.h index dc5b9de..ffacd9d 100644 --- a/src/text-node.h +++ b/src/text-node.h @@ -15,6 +15,9 @@ typedef struct UITextNode { UIRGBA color; ssize_t caret_index; UINode *caret_node; + double computed_text_width; + double computed_text_height; + bool computed_dimensions_invalid; } UITextNode; int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p);