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 | |
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.
-rw-r--r-- | docs/pango-sections.txt | 1 | ||||
-rw-r--r-- | pango/pango-attributes.c | 81 | ||||
-rw-r--r-- | pango/pango-attributes.h | 5 |
3 files changed, 87 insertions, 0 deletions
diff --git a/docs/pango-sections.txt b/docs/pango-sections.txt index 3b70e980..11aabcae 100644 --- a/docs/pango-sections.txt +++ b/docs/pango-sections.txt @@ -425,6 +425,7 @@ pango_attr_list_insert_before pango_attr_list_change pango_attr_list_splice pango_attr_list_filter +pango_attr_list_update PangoAttrFilterFunc pango_attr_list_get_attributes pango_attr_list_get_iterator 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 diff --git a/pango/pango-attributes.h b/pango/pango-attributes.h index 1f43c27f..6f021c06 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_update (PangoAttrList *list, + int pos, + int remove, + int add); PANGO_AVAILABLE_IN_1_2 PangoAttrList *pango_attr_list_filter (PangoAttrList *list, |