[pango/log-attr-tweaks] layout: Validate external log attrs



commit e772b642027caea78b0250a9381a98c382e5706e
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 21 15:42:18 2021 -0400

    layout: Validate external log attrs
    
    These might be nonsense, better do some checks.

 pango/break.c        | 27 +++++++++++++++++++++++----
 pango/pango-layout.c |  9 ++++++++-
 2 files changed, 31 insertions(+), 5 deletions(-)
---
diff --git a/pango/break.c b/pango/break.c
index dbba48da..2a1f9b22 100644
--- a/pango/break.c
+++ b/pango/break.c
@@ -1866,6 +1866,13 @@ check_line_char (int            pos,
       return FALSE;
     }
 
+  if (attr->is_line_break && !attr->is_char_break)
+    {
+      g_set_error (error,
+                   PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_BREAK,
+                   "char %d: Line breaks must also be marked as char breaks", pos);
+      return FALSE;
+    }
   if (break_type == G_UNICODE_BREAK_OPEN_PUNCTUATION &&
       prev_break_type == G_UNICODE_BREAK_OPEN_PUNCTUATION &&
       attr->is_line_break && !attr->is_mandatory_break)
@@ -1956,6 +1963,14 @@ check_word_invariants (const char          *text,
           break;
 
         case AFTER_START:
+          if (log_attrs[i].is_word_end)
+            {
+              if (log_attrs[i].is_word_start)
+                state = AFTER_START;
+              else
+                state = AFTER_END;
+              break;
+           }
           if (log_attrs[i].is_word_start)
             {
               g_set_error (error,
@@ -1963,8 +1978,6 @@ check_word_invariants (const char          *text,
                            "char %d: Unexpected word start", i);
               return FALSE;
             }
-          if (log_attrs[i].is_word_end)
-            state = AFTER_END;
           break;
         }
     }
@@ -2007,6 +2020,14 @@ check_sentence_invariants (const char          *text,
           break;
 
         case AFTER_START:
+          if (log_attrs[i].is_sentence_end)
+            {
+              if (log_attrs[i].is_sentence_start)
+                state = AFTER_START;
+              else
+                state = AFTER_END;
+              break;
+            }
           if (log_attrs[i].is_sentence_start)
             {
               g_set_error (error,
@@ -2014,8 +2035,6 @@ check_sentence_invariants (const char          *text,
                            "char %d: Unexpected sentence start", i);
               return FALSE;
             }
-          if (log_attrs[i].is_sentence_end)
-            state = AFTER_END;
           break;
         }
     }
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index 9e86bcd5..080a60ba 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -1589,7 +1589,14 @@ pango_layout_set_log_attrs (PangoLayout        *layout,
                             const PangoLogAttr *attrs,
                             int                 n_attrs)
 {
-  g_return_if_fail (layout->n_chars + 1 == n_attrs);
+  GError *error = NULL;
+
+  if (!pango_validate_log_attrs (layout->text, layout->length, attrs, n_attrs, &error))
+    {
+      g_warning ("pango_layout_set_log_attrs: %s", error->message);
+      g_error_free (error);
+      return;
+    }
 
   if (!layout->log_attrs)
     layout->log_attrs = g_new (PangoLogAttr, layout->n_chars + 1);


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