diff options
author | Matthias Clasen <mclasen@redhat.com> | 2019-07-13 23:04:02 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2019-07-19 10:38:49 -0700 |
commit | 513e45cf8cf1cb15cc32f95cf62617fc75baa99c (patch) | |
tree | 47d3eb2da4b6babf684f73a4c18734c3df335afb /pango/pango-attributes.c | |
parent | 8df747b7d161d08dca7dc08de032bb0d30a1e9b9 (diff) | |
download | pango-513e45cf8cf1cb15cc32f95cf62617fc75baa99c.tar.gz |
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.
Diffstat (limited to 'pango/pango-attributes.c')
-rw-r--r-- | pango/pango-attributes.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/pango/pango-attributes.c b/pango/pango-attributes.c index ac27251d..17549ffd 100644 --- a/pango/pango-attributes.c +++ b/pango/pango-attributes.c @@ -1599,6 +1599,87 @@ pango_attr_list_change (PangoAttrList *list, } /** + * pango_attr_list_update: + * @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 that fall entirely in the + * (@pos, @pos + @remove) range are removed. + * + * Attributes that start or end inside the + * (@pos, @pos + @remove) range are shortened to + * reflect the removal. + * + * Attributes start and end positions are updated + * if they are behind @pos + @remove. + * + * Since: 1.44 + */ +void +pango_attr_list_update (PangoAttrList *list, + int pos, + int remove, + int add) +{ + GSList *l, *prev, *next; + + prev = NULL; + l = list->attributes; + while (l) + { + next = l->next; + PangoAttribute *attr = l->data; + + if (attr->start_index >= pos && + attr->end_index < pos + remove) + { + pango_attribute_destroy (attr); + if (prev == NULL) + list->attributes = next; + else + prev->next = next; + + g_slist_free_1 (l); + } + else + { + prev = l; + + if (attr->start_index >= pos && + attr->start_index < pos + remove) + { + attr->start_index = pos + add; + } + else if (attr->start_index >= pos + remove) + { + attr->start_index += add - remove; + } + + if (attr->end_index >= pos && + attr->end_index < pos + remove) + { + attr->end_index = pos; + } + else if (attr->end_index >= pos + remove) + { + attr->end_index += add - remove; + } + } + + l = next; + } +} + +/** * pango_attr_list_splice: * @list: a #PangoAttrList * @other: another #PangoAttrList |