greatly improve performance by caching text size

This commit is contained in:
hippoz 2023-04-25 17:50:05 +03:00
parent 996d6fb520
commit 8e28a79e88
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
2 changed files with 21 additions and 15 deletions

View file

@ -17,6 +17,9 @@ UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA col
n->color = color; n->color = color;
n->caret_index = -1; n->caret_index = -1;
n->caret_node = NULL; 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.text = "text";
n->node.handle_proto = text_node_handle; n->node.handle_proto = text_node_handle;
node_attach(parent, (UINode*)n); 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->layout = layout;
n->pending_text = NULL; n->pending_text = NULL;
} }
n->computed_dimensions_invalid = true;
} }
switch (ev) { 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); cairo_fill(n->node.drw);
break; break;
} }
case UI_EVENT_GET_WIDTH: { case UI_EVENT_GET_WIDTH: /* through */
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_HEIGHT: { case UI_EVENT_GET_HEIGHT: {
if (!n->layout) { if (n->computed_dimensions_invalid) {
break; 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; if (ev == UI_EVENT_GET_WIDTH) {
pango_layout_get_size(n->layout, NULL, &h); *(double*)p = n->computed_text_width;
*(double*)p = pango_units_to_double(h); }
return 1; if (ev == UI_EVENT_GET_HEIGHT) {
*(double*)p = n->computed_text_height;
}
break;
} }
case UI_EVENT_RELAYOUT: { case UI_EVENT_RELAYOUT: {
if (!n->layout) { if (!n->layout) {

View file

@ -15,6 +15,9 @@ typedef struct UITextNode {
UIRGBA color; UIRGBA color;
ssize_t caret_index; ssize_t caret_index;
UINode *caret_node; UINode *caret_node;
double computed_text_width;
double computed_text_height;
bool computed_dimensions_invalid;
} UITextNode; } UITextNode;
int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p); int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p);