commit a204d241b9eaad2d6980f1d1817751ce9500d5a3
Author: Peng Wu <alexepico@gmail.com>
Date:   Wed Jul 5 15:05:16 2017 +0800

    Update pango_default_break function for Sentence Boundary
    
    Re-write the code for Sentence Boundary,
    and use the code style like Grapheme Boundary and Word Boundary.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=782813

diff --git a/pango/break.c b/pango/break.c
index 5b2128d..e50cef2 100644
--- a/pango/break.c
+++ b/pango/break.c
@@ -432,6 +432,7 @@ static const CharJamoProps HangulJamoProps[] = {
 #define HANGUL(wc) ((wc) >= 0xAC00 && (wc) <= 0xD7A3)
 #define BACKSPACE_DELETES_CHARACTER(wc) (!LATIN (wc) && !CYRILLIC (wc) && !GREEK (wc) && !KANA(wc) && !HANGUL(wc))
 
+#if 0
 /* p. 132-133 of Unicode spec table 5-6 will help understand this */
 typedef enum
 {
@@ -452,6 +453,7 @@ typedef enum
    */
   STATE_SENTENCE_POST_DOT_SEP
 } SentenceState;
+#endif
 
 /* Previously "123foo" was two words. But in UAX 29 of Unicode, 
  * we know don't break words between consecutive letters and numbers
@@ -553,17 +555,34 @@ pango_default_break (const gchar   *text,
   WordBreakType prev_prev_WB_type = WB_Other, prev_WB_type = WB_Other;
   gint prev_WB_i = -1;
 
+  /* See Sentence_Break Property Values table of UAX#29 */
+  typedef enum
+  {
+    SB_Other,
+    SB_ExtendFormat,
+    SB_ParaSep,
+    SB_Sp,
+    SB_Lower,
+    SB_Upper,
+    SB_OLetter,
+    SB_Numeric,
+    SB_ATerm,
+    SB_SContinue,
+    SB_STerm,
+    SB_Close,
+    /* Rules SB8 and SB8a */
+    SB_ATerm_Close_Sp,
+    SB_STerm_Close_Sp,
+  } SentenceBreakType;
+  SentenceBreakType prev_prev_SB_type = SB_Other, prev_SB_type = SB_Other;
+  gint prev_SB_i = -1;
+
   WordType current_word_type = WordNone;
   gunichar last_word_letter = 0;
   gunichar base_character = 0;
 
-  SentenceState sentence_state = STATE_SENTENCE_OUTSIDE;
-  /* Tracks what will be the end of the sentence if a period is
-   * determined to actually be a sentence-ending period.
-   */
-  gint possible_sentence_end = -1;
-  /* possible sentence break before Open* after a period-ended sentence */
-  gint possible_sentence_boundary = -1;
+  gint last_sentence_start = -1;
+
   gboolean almost_done = FALSE;
   gboolean done = FALSE;
 
@@ -601,6 +620,7 @@ pango_default_break (const gchar   *text,
       /* UAX#29 boundaries */
       gboolean is_grapheme_boundary;
       gboolean is_word_boundary;
+      gboolean is_sentence_boundary;
 
 
       wc = next_wc;
@@ -1078,6 +1098,279 @@ pango_default_break (const gchar   *text,
 	attrs[i].is_word_boundary = is_word_boundary;
       }
 
+      /* ---- UAX#29 Sentence Boundaries ---- */
+      {
+	is_sentence_boundary = FALSE;
+	if (is_word_boundary ||
+	    wc == '\r' || wc == '\n') /* Rules SB3 and SB5 */
+	  {
+	    PangoScript script;
+	    SentenceBreakType SB_type;
+
+	    script = g_unichar_get_script (wc);
+
+	    /* Find the SentenceBreakType of wc */
+	    SB_type = SB_Other;
+
+	    if (break_type == G_UNICODE_BREAK_NUMERIC)
+	      SB_type = SB_Numeric; /* Numeric */
+
+	    if (SB_type == SB_Other)
+	      switch ((int) type)
+		{
+		case G_UNICODE_CONTROL:
+		  if (wc == '\r' || wc == '\n')
+		    SB_type = SB_ParaSep;
+		  else if (wc == 0x0009 || wc == 0x000B || wc == 0x000C)
+		    SB_type = SB_Sp;
+		  else if (wc == 0x0085)
+		    SB_type = SB_ParaSep;
+		  break;
+
+		case G_UNICODE_SPACE_SEPARATOR:
+		  if (wc == 0x0020 || wc == 0x00A0 || wc == 0x1680 ||
+		      (wc >= 0x2000 && wc <= 0x200A) ||
+		      wc == 0x202F || wc == 0x205F || wc == 0x3000)
+		    SB_type = SB_Sp;
+		  break;
+
+		case G_UNICODE_LINE_SEPARATOR:
+		case G_UNICODE_PARAGRAPH_SEPARATOR:
+		  SB_type = SB_ParaSep;
+		  break;
+
+		case G_UNICODE_FORMAT:
+		case G_UNICODE_SPACING_MARK:
+		case G_UNICODE_ENCLOSING_MARK:
+		case G_UNICODE_NON_SPACING_MARK:
+		  SB_type = SB_ExtendFormat; /* Extend, Format */
+		  break;
+
+		case G_UNICODE_MODIFIER_LETTER:
+		  if (wc >= 0xFF9E && wc <= 0xFF9F)
+		    SB_type = SB_ExtendFormat; /* Other_Grapheme_Extend */
+		  break;
+
+		case G_UNICODE_TITLECASE_LETTER:
+		  SB_type = SB_Upper;
+		  break;
+
+		case G_UNICODE_DASH_PUNCTUATION:
+		  if (wc == 0x002D ||
+		      (wc >= 0x2013 && wc <= 0x2014) ||
+		      (wc >= 0xFE31 && wc <= 0xFE32) ||
+		      wc == 0xFE58 ||
+		      wc == 0xFE63 ||
+		      wc == 0xFF0D)
+		    SB_type = SB_SContinue;
+		  break;
+
+		case G_UNICODE_OTHER_PUNCTUATION:
+		  if (wc == 0x05F3)
+		    SB_type = SB_OLetter;
+		  else if (wc == 0x002E || wc == 0x2024 ||
+		      wc == 0xFE52 || wc == 0xFF0E)
+		    SB_type = SB_ATerm;
+
+		  if (wc == 0x002C ||
+		      wc == 0x003A ||
+		      wc == 0x055D ||
+		      (wc >= 0x060C && wc <= 0x060D) ||
+		      wc == 0x07F8 ||
+		      wc == 0x1802 ||
+		      wc == 0x1808 ||
+		      wc == 0x3001 ||
+		      (wc >= 0xFE10 && wc <= 0xFE11) ||
+		      wc == 0xFE13 ||
+		      (wc >= 0xFE50 && wc <= 0xFE51) ||
+		      wc == 0xFE55 ||
+		      wc == 0xFF0C ||
+		      wc == 0xFF1A ||
+		      wc == 0xFF64)
+		    SB_type = SB_SContinue;
+
+		  if (wc == 0x0021 ||
+		      wc == 0x003F ||
+		      wc == 0x0589 ||
+		      wc == 0x061F ||
+		      wc == 0x06D4 ||
+		      (wc >= 0x0700 && wc <= 0x0702) ||
+		      wc == 0x07F9 ||
+		      (wc >= 0x0964 && wc <= 0x0965) ||
+		      (wc >= 0x104A && wc <= 0x104B) ||
+		      wc == 0x1362 ||
+		      (wc >= 0x1367 && wc <= 0x1368) ||
+		      wc == 0x166E ||
+		      (wc >= 0x1735 && wc <= 0x1736) ||
+		      wc == 0x1803 ||
+		      wc == 0x1809 ||
+		      (wc >= 0x1944 && wc <= 0x1945) ||
+		      (wc >= 0x1AA8 && wc <= 0x1AAB) ||
+		      (wc >= 0x1B5A && wc <= 0x1B5B) ||
+		      (wc >= 0x1B5E && wc <= 0x1B5F) ||
+		      (wc >= 0x1C3B && wc <= 0x1C3C) ||
+		      (wc >= 0x1C7E && wc <= 0x1C7F) ||
+		      (wc >= 0x203C && wc <= 0x203D) ||
+		      (wc >= 0x2047 && wc <= 0x2049) ||
+		      wc == 0x2E2E ||
+		      wc == 0x2E3C ||
+		      wc == 0x3002 ||
+		      wc == 0xA4FF ||
+		      (wc >= 0xA60E && wc <= 0xA60F) ||
+		      wc == 0xA6F3 ||
+		      wc == 0xA6F7 ||
+		      (wc >= 0xA876 && wc <= 0xA877) ||
+		      (wc >= 0xA8CE && wc <= 0xA8CF) ||
+		      wc == 0xA92F ||
+		      (wc >= 0xA9C8 && wc <= 0xA9C9) ||
+		      (wc >= 0xAA5D && wc <= 0xAA5F) ||
+		      (wc >= 0xAAF0 && wc <= 0xAAF1) ||
+		      wc == 0xABEB ||
+		      (wc >= 0xFE56 && wc <= 0xFE57) ||
+		      wc == 0xFF01 ||
+		      wc == 0xFF1F ||
+		      wc == 0xFF61 ||
+		      (wc >= 0x10A56 && wc <= 0x10A57) ||
+		      (wc >= 0x11047 && wc <= 0x11048) ||
+		      (wc >= 0x110BE && wc <= 0x110C1) ||
+		      (wc >= 0x11141 && wc <= 0x11143) ||
+		      (wc >= 0x111C5 && wc <= 0x111C6) ||
+		      wc == 0x111CD ||
+		      (wc >= 0x111DE && wc <= 0x111DF) ||
+		      (wc >= 0x11238 && wc <= 0x11239) ||
+		      (wc >= 0x1123B && wc <= 0x1123C) ||
+		      wc == 0x112A9 ||
+		      (wc >= 0x1144B && wc <= 0x1144C) ||
+		      (wc >= 0x115C2 && wc <= 0x115C3) ||
+		      (wc >= 0x115C9 && wc <= 0x115D7) ||
+		      (wc >= 0x11641 && wc <= 0x11642) ||
+		      (wc >= 0x1173C && wc <= 0x1173E) ||
+		      (wc >= 0x11C41 && wc <= 0x11C42) ||
+		      (wc >= 0x16A6E && wc <= 0x16A6F) ||
+		      wc == 0x16AF5 ||
+		      (wc >= 0x16B37 && wc <= 0x16B38) ||
+		      wc == 0x16B44 ||
+		      wc == 0x1BC9F ||
+		      wc == 0x1DA88)
+		    SB_type = SB_STerm;
+
+		  break;
+		}
+
+	    if (SB_type == SB_Other)
+	      {
+		if (g_unichar_islower(wc))
+		  SB_type = SB_Lower;
+		else if (g_unichar_isupper(wc))
+		  SB_type = SB_Upper;
+		else if (g_unichar_isalpha(wc))
+		  SB_type = SB_OLetter;
+
+		if (type == G_UNICODE_OPEN_PUNCTUATION ||
+		    type == G_UNICODE_CLOSE_PUNCTUATION ||
+		    break_type == G_UNICODE_BREAK_QUOTATION)
+		  SB_type = SB_Close;
+	      }
+
+	    /* Sentence Boundary Rules */
+
+	    /* We apply Rules SB1 and SB2 at the end of the function */
+
+#define IS_OTHER_TERM(SB_type)						\
+	    /* not in (OLetter | Upper | Lower | ParaSep | SATerm) */	\
+	      !(SB_type == SB_OLetter ||				\
+		SB_type == SB_Upper || SB_type == SB_Lower ||		\
+		SB_type == SB_ParaSep ||				\
+		SB_type == SB_ATerm || SB_type == SB_STerm ||		\
+		SB_type == SB_ATerm_Close_Sp ||				\
+		SB_type == SB_STerm_Close_Sp)
+
+
+	    if (wc == '\n' && prev_wc == '\r')
+	      is_sentence_boundary = FALSE; /* Rule SB3 */
+	    else if (prev_SB_type == SB_ParaSep && prev_SB_i + 1 == i)
+	      {
+		/* The extra check for prev_SB_i is to correctly handle sequences like
+		 * ParaSep ÷ Extend × Extend
+		 * since we have not skipped ExtendFormat yet.
+		 */
+
+		is_sentence_boundary = TRUE; /* Rule SB4 */
+	      }
+	    else if (SB_type == SB_ExtendFormat)
+	      is_sentence_boundary = FALSE; /* Rule SB5? */
+	    else if (prev_SB_type == SB_ATerm && SB_type == SB_Numeric)
+	      is_sentence_boundary = FALSE; /* Rule SB6 */
+	    else if ((prev_prev_SB_type == SB_Upper ||
+		      prev_prev_SB_type == SB_Lower) &&
+		     prev_SB_type == SB_ATerm &&
+		     SB_type == SB_Upper)
+	      is_sentence_boundary = FALSE; /* Rule SB7 */
+	    else if (prev_SB_type == SB_ATerm && SB_type == SB_Close)
+		SB_type = SB_ATerm;
+	    else if (prev_SB_type == SB_STerm && SB_type == SB_Close)
+	      SB_type = SB_STerm;
+	    else if (prev_SB_type == SB_ATerm && SB_type == SB_Sp)
+	      SB_type = SB_ATerm_Close_Sp;
+	    else if (prev_SB_type == SB_STerm && SB_type == SB_Sp)
+	      SB_type = SB_STerm_Close_Sp;
+	    /* Rule SB8 */
+	    else if ((prev_SB_type == SB_ATerm ||
+		      prev_SB_type == SB_ATerm_Close_Sp) &&
+		     SB_type == SB_Lower)
+	      is_sentence_boundary = FALSE;
+	    else if ((prev_prev_SB_type == SB_ATerm ||
+		      prev_prev_SB_type == SB_ATerm_Close_Sp) &&
+		     IS_OTHER_TERM(prev_SB_type) &&
+		     SB_type == SB_Lower)
+	      attrs[prev_SB_i].is_sentence_boundary = FALSE;
+	    else if ((prev_SB_type == SB_ATerm ||
+		      prev_SB_type == SB_ATerm_Close_Sp ||
+		      prev_SB_type == SB_STerm ||
+		      prev_SB_type == SB_STerm_Close_Sp) &&
+		     (SB_type == SB_SContinue ||
+		      SB_type == SB_ATerm || SB_type == SB_STerm))
+	      is_sentence_boundary = FALSE; /* Rule SB8a */
+	    else if ((prev_SB_type == SB_ATerm ||
+		      prev_SB_type == SB_STerm) &&
+		     (SB_type == SB_Close || SB_type == SB_Sp ||
+		      SB_type == SB_ParaSep))
+	      is_sentence_boundary = FALSE; /* Rule SB9 */
+	    else if ((prev_SB_type == SB_ATerm ||
+		      prev_SB_type == SB_ATerm_Close_Sp ||
+		      prev_SB_type == SB_STerm ||
+		      prev_SB_type == SB_STerm_Close_Sp) &&
+		     (SB_type == SB_Sp || SB_type == SB_ParaSep))
+	      is_sentence_boundary = FALSE; /* Rule SB10 */
+	    else if ((prev_SB_type == SB_ATerm ||
+		      prev_SB_type == SB_ATerm_Close_Sp ||
+		      prev_SB_type == SB_STerm ||
+		      prev_SB_type == SB_STerm_Close_Sp) &&
+		     SB_type != SB_ParaSep)
+	      is_sentence_boundary = TRUE; /* Rule SB11 */
+	    else
+	      is_sentence_boundary = FALSE; /* Rule SB998 */
+
+	    if (SB_type != SB_ExtendFormat &&
+		!((prev_prev_SB_type == SB_ATerm ||
+		   prev_prev_SB_type == SB_ATerm_Close_Sp) &&
+		  IS_OTHER_TERM(prev_SB_type) &&
+		  IS_OTHER_TERM(SB_type)))
+              {
+                prev_prev_SB_type = prev_SB_type;
+                prev_SB_type = SB_type;
+                prev_SB_i = i;
+              }
+
+#undef IS_OTHER_TERM
+
+	  }
+
+	if (i == 0 || done)
+	  is_sentence_boundary = TRUE; /* Rules SB1 and SB2 */
+
+	attrs[i].is_sentence_boundary = is_sentence_boundary;
+      }
 
       /* ---- Line breaking ---- */
 
@@ -1371,6 +1664,21 @@ pango_default_break (const gchar   *text,
 
       /* ---- Sentence breaks ---- */
 
+      /* default to not a sentence start/end */
+      attrs[i].is_sentence_start = FALSE;
+      attrs[i].is_sentence_end = FALSE;
+
+      if (last_sentence_start == -1 && !is_sentence_boundary) {
+	last_sentence_start = i - 1;
+	attrs[i - 1].is_sentence_start = TRUE;
+      }
+
+      if (last_sentence_start != -1 && is_sentence_boundary) {
+	last_sentence_start = -1;
+	attrs[i].is_sentence_end = TRUE;
+      }
+
+#if 0
       /* The Unicode spec specifies sentence breakpoints, so that a piece of
        * text would be partitioned into sentences, and all characters would
        * be inside some sentence. This code implements that for is_sentence_boundary,
@@ -1787,6 +2095,7 @@ pango_default_break (const gchar   *text,
 	  g_assert_not_reached ();
 	  break;
 	}
+#endif
 
       prev_type = type;
       prev_wc = wc;
@@ -1798,6 +2107,7 @@ pango_default_break (const gchar   *text,
 	  type != G_UNICODE_NON_SPACING_MARK)
 	base_character = wc;
     }
+
   i--;
 
   attrs[i].is_cursor_position = TRUE;  /* Rule GB2 */
