From c489cf77e64edc55bb583dc05352f3e24f0ae72c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 13 Jul 2019 23:04:02 -0400 Subject: Add a function to update attribute lists Add a function that can update positions of attributes as the underlying text is changing. This is meant to be used to update an attribute list as text transformations are applied, like capitalization. --- docs/pango-sections.txt | 1 + pango/pango-attributes.c | 42 ++++++++++++++++++++++++++++++++++++++++++ pango/pango-attributes.h | 5 +++++ 3 files changed, 48 insertions(+) diff --git a/docs/pango-sections.txt b/docs/pango-sections.txt index 9bf20619..60d360d6 100644 --- a/docs/pango-sections.txt +++ b/docs/pango-sections.txt @@ -410,6 +410,7 @@ pango_attr_list_insert_before pango_attr_list_change pango_attr_list_splice pango_attr_list_filter +pango_attr_list_apply_delta PangoAttrFilterFunc pango_attr_list_get_iterator PangoAttrIterator diff --git a/pango/pango-attributes.c b/pango/pango-attributes.c index 85e5a240..f641e9ac 100644 --- a/pango/pango-attributes.c +++ b/pango/pango-attributes.c @@ -1598,6 +1598,48 @@ pango_attr_list_change (PangoAttrList *list, } } +/** + * pango_attr_list_apply_delta: + * @list: a #PangoAttrList + * @pos: the position of the change + * @remove: the number of removed bytes + * @add: the number of added bytes + * + * Update indices of attributes in @list for + * a change in the text they refer to. + * + * The change that this function applies is + * removing @remove bytes at position @pos + * and inserting @add bytes instead. + * + * Attributes start and end positions are updated + * if they are behind @pos + @remove. Positions + * that fall into the (@pos, @pos + @remove) range + * are not updated, since we don't know how the + * removed and added text affects them. + */ +void +pango_attr_list_apply_delta (PangoAttrList *list, + int pos, + int remove, + int add) +{ + GSList *l; + + for (l = list->attributes; l; l = l->next) + { + PangoAttribute *attr = l->data; + + if (attr->end_index < pos) + continue; + + if (attr->start_index >= pos + remove) + attr->start_index += add - remove; + if (attr->end_index >= pos + remove) + attr->end_index += add - remove; + } +} + /** * pango_attr_list_splice: * @list: a #PangoAttrList diff --git a/pango/pango-attributes.h b/pango/pango-attributes.h index cff495b2..b933bc11 100644 --- a/pango/pango-attributes.h +++ b/pango/pango-attributes.h @@ -547,6 +547,11 @@ void pango_attr_list_splice (PangoAttrList *list, PangoAttrList *other, gint pos, gint len); +PANGO_AVAILABLE_IN_1_44 +void pango_attr_list_apply_delta (PangoAttrList *list, + int pos, + int remove, + int add); PANGO_AVAILABLE_IN_1_2 PangoAttrList *pango_attr_list_filter (PangoAttrList *list, -- cgit v1.2.1