summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2020-06-18 02:29:01 +0000
committerMatthias Clasen <mclasen@redhat.com>2020-06-18 02:29:01 +0000
commit2a44077091bfe8680a12194d79f90e0443a06ad8 (patch)
treef4b21871323f077c47f1c5987a8263d373532753
parentb1411194d196471fad7984a73044a3c19e7d884d (diff)
parent1ef6e557805733ba7c995e5a16ad261afa6608f2 (diff)
downloadpango-2a44077091bfe8680a12194d79f90e0443a06ad8.tar.gz
Merge branch 'fix-attr-list-change' into 'master'
Fix attr list change See merge request GNOME/pango!205
-rw-r--r--pango/pango-attributes.c18
-rw-r--r--tests/testattributes.c68
2 files changed, 78 insertions, 8 deletions
diff --git a/pango/pango-attributes.c b/pango/pango-attributes.c
index 1fa8c399..37443346 100644
--- a/pango/pango-attributes.c
+++ b/pango/pango-attributes.c
@@ -1523,11 +1523,12 @@ pango_attr_list_insert_before (PangoAttrList *list,
**/
void
pango_attr_list_change (PangoAttrList *list,
- PangoAttribute *attr)
+ PangoAttribute *attr)
{
guint i, p;
guint start_index = attr->start_index;
guint end_index = attr->end_index;
+ gboolean inserted;
g_return_if_fail (list != NULL);
@@ -1543,6 +1544,7 @@ pango_attr_list_change (PangoAttrList *list,
return;
}
+ inserted = FALSE;
for (i = 0, p = list->attributes->len; i < p; i++)
{
PangoAttribute *tmp_attr = g_ptr_array_index (list->attributes, i);
@@ -1550,6 +1552,7 @@ pango_attr_list_change (PangoAttrList *list,
if (tmp_attr->start_index > start_index)
{
g_ptr_array_insert (list->attributes, i, attr);
+ inserted = TRUE;
break;
}
@@ -1559,8 +1562,8 @@ pango_attr_list_change (PangoAttrList *list,
if (tmp_attr->end_index < start_index)
continue; /* This attr does not overlap with the new one */
+ g_assert (tmp_attr->start_index <= start_index);
g_assert (tmp_attr->end_index >= start_index);
- g_assert (start_index <= tmp_attr->end_index);
if (pango_attribute_equal (tmp_attr, attr))
{
@@ -1579,21 +1582,22 @@ pango_attr_list_change (PangoAttrList *list,
pango_attribute_destroy (attr);
attr = tmp_attr;
+ inserted = TRUE;
break;
}
else
{
/* Split, truncate, or remove the old attribute
*/
- if (tmp_attr->end_index > attr->end_index)
+ if (tmp_attr->end_index > end_index)
{
PangoAttribute *end_attr = pango_attribute_copy (tmp_attr);
- end_attr->start_index = attr->end_index;
+ end_attr->start_index = end_index;
pango_attr_list_insert (list, end_attr);
}
- if (tmp_attr->start_index == attr->start_index)
+ if (tmp_attr->start_index == start_index)
{
pango_attribute_destroy (tmp_attr);
g_ptr_array_remove_index (list->attributes, i);
@@ -1601,12 +1605,12 @@ pango_attr_list_change (PangoAttrList *list,
}
else
{
- tmp_attr->end_index = attr->start_index;
+ tmp_attr->end_index = start_index;
}
}
}
- if (i == p)
+ if (!inserted)
{
/* we didn't insert attr yet */
pango_attr_list_insert (list, attr);
diff --git a/tests/testattributes.c b/tests/testattributes.c
index d6c8c87c..4a8bc577 100644
--- a/tests/testattributes.c
+++ b/tests/testattributes.c
@@ -99,7 +99,12 @@ assert_attributes (GSList *attrs,
s = g_string_new ("");
print_attributes (attrs, s);
- g_assert_cmpstr (s->str, ==, expected);
+ if (strcmp (s->str, expected) != 0)
+ {
+ g_print ("-----\nattribute list mismatch\nexpected:\n%s-----\nreceived:\n%s-----\n",
+ expected, s->str);
+ g_assert_not_reached ();
+ }
g_string_free (s, TRUE);
}
@@ -830,6 +835,66 @@ test_merge (void)
pango_attr_list_unref (list2);
}
+/* reproduce what the links example in gtk4-demo does
+ * with the colored Google link
+ */
+static void
+test_merge2 (void)
+{
+ PangoAttrList *list;
+ PangoAttribute *attr;
+
+ list = pango_attr_list_new ();
+ attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
+ attr->start_index = 0;
+ attr->end_index = 10;
+ pango_attr_list_insert (list, attr);
+ attr = pango_attr_foreground_new (0, 0, 0xffff);
+ attr->start_index = 0;
+ attr->end_index = 10;
+ pango_attr_list_insert (list, attr);
+
+ assert_attr_list (list, "[0,10]underline=1\n"
+ "[0,10]foreground=#00000000ffff\n");
+
+ attr = pango_attr_foreground_new (0xffff, 0, 0);
+ attr->start_index = 2;
+ attr->end_index = 3;
+
+ pango_attr_list_change (list, attr);
+
+ assert_attr_list (list, "[0,10]underline=1\n"
+ "[0,2]foreground=#00000000ffff\n"
+ "[2,3]foreground=#ffff00000000\n"
+ "[3,10]foreground=#00000000ffff\n");
+
+ attr = pango_attr_foreground_new (0, 0xffff, 0);
+ attr->start_index = 3;
+ attr->end_index = 4;
+
+ pango_attr_list_change (list, attr);
+
+ assert_attr_list (list, "[0,10]underline=1\n"
+ "[0,2]foreground=#00000000ffff\n"
+ "[2,3]foreground=#ffff00000000\n"
+ "[3,4]foreground=#0000ffff0000\n"
+ "[4,10]foreground=#00000000ffff\n");
+
+ attr = pango_attr_foreground_new (0, 0, 0xffff);
+ attr->start_index = 4;
+ attr->end_index = 5;
+
+ pango_attr_list_change (list, attr);
+
+ assert_attr_list (list, "[0,10]underline=1\n"
+ "[0,2]foreground=#00000000ffff\n"
+ "[2,3]foreground=#ffff00000000\n"
+ "[3,4]foreground=#0000ffff0000\n"
+ "[4,10]foreground=#00000000ffff\n");
+
+ pango_attr_list_unref (list);
+}
+
int
main (int argc, char *argv[])
{
@@ -845,6 +910,7 @@ main (int argc, char *argv[])
g_test_add_func ("/attributes/list/equal", test_list_equal);
g_test_add_func ("/attributes/list/insert", test_insert);
g_test_add_func ("/attributes/list/merge", test_merge);
+ g_test_add_func ("/attributes/list/merge2", test_merge2);
g_test_add_func ("/attributes/iter/basic", test_iter);
g_test_add_func ("/attributes/iter/get", test_iter_get);
g_test_add_func ("/attributes/iter/get_font", test_iter_get_font);