[pango/attribute-list: 3/4] Add a function to update attribute lists



commit 78416326bc49700daa87ee940c8ebcf5280520dc
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jul 13 23:04:02 2019 -0400

    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 | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
 pango/pango-attributes.h |  5 +++
 3 files changed, 87 insertions(+)
---
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..fa731df2 100644
--- a/pango/pango-attributes.c
+++ b/pango/pango-attributes.c
@@ -1598,6 +1598,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->end_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
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,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]