Browse Source

[TextServer] Fix space trimming around mandatory line breaks.

Pāvels Nadtočajevs 7 months ago
parent
commit
7c182a1544
2 changed files with 41 additions and 8 deletions
  1. 6 6
      servers/text_server.cpp
  2. 35 2
      tests/servers/test_text_server.h

+ 6 - 6
servers/text_server.cpp

@@ -890,10 +890,10 @@ PackedInt32Array TextServer::shaped_text_get_line_breaks_adv(const RID &p_shaped
 						if (last_end <= l_gl[start_pos].start) {
 							lines.push_back(l_gl[start_pos].start);
 							lines.push_back(l_gl[end_pos].end);
-							last_end = l_gl[end_pos].end;
-							cur_safe_brk = end_pos;
+							last_end = l_gl[i].end;
+							cur_safe_brk = i;
 						}
-						trim_next = false;
+						trim_next = true;
 					} else {
 						if (last_end <= line_start) {
 							lines.push_back(line_start);
@@ -1057,15 +1057,15 @@ PackedInt32Array TextServer::shaped_text_get_line_breaks(const RID &p_shaped, do
 						while ((start_pos < end_pos) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
 							end_pos -= l_gl[end_pos].count;
 						}
-						trim_next = false;
+						trim_next = true;
 						if (last_end <= l_gl[start_pos].start) {
 							lines.push_back(l_gl[start_pos].start);
 							lines.push_back(l_gl[end_pos].end);
 							if (p_width > indent) {
 								l_width = p_width - indent;
 							}
-							last_end = l_gl[end_pos].end;
-							cur_safe_brk = end_pos;
+							last_end = l_gl[i].end;
+							cur_safe_brk = i;
 						}
 					} else {
 						if (last_end <= line_start) {

+ 35 - 2
tests/servers/test_text_server.h

@@ -461,7 +461,7 @@ TEST_SUITE("[TextServer]") {
 					ts->free_rid(ctx);
 				}
 
-				if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
+				if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) { // Line breaking opportunities.
 					String test = U"เป็นภาษาราชการและภาษา";
 					RID ctx = ts->create_shaped_text();
 					CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
@@ -489,7 +489,7 @@ TEST_SUITE("[TextServer]") {
 					ts->free_rid(ctx);
 				}
 
-				if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
+				if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) { // Break line.
 					struct TestCase {
 						String text;
 						PackedInt32Array breaks;
@@ -504,15 +504,48 @@ TEST_SUITE("[TextServer]") {
 						{ U"الحمدا لحمدا لحمـــد", { 0, 13, 13, 20 } },
 						{ U"         الحمد test", { 0, 15, 15, 19 } },
 						{ U"الحمـد الرياضي العربي", { 0, 7, 7, 15, 15, 21 } },
+						{ U"test \rtest", { 0, 6, 6, 10 } },
+						{ U"test\r test", { 0, 5, 5, 10 } },
+						{ U"test\r test \r test", { 0, 5, 5, 12, 12, 17 } },
 					};
 					for (size_t j = 0; j < sizeof(cases) / sizeof(TestCase); j++) {
 						RID ctx = ts->create_shaped_text();
 						CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
 						bool ok = ts->shaped_text_add_string(ctx, cases[j].text, font, 16);
 						CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
+
 						PackedInt32Array breaks = ts->shaped_text_get_line_breaks(ctx, 90.0);
+						CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
 
+						breaks = ts->shaped_text_get_line_breaks_adv(ctx, { 90.0 }, 0, false);
 						CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
+
+						ts->free_rid(ctx);
+					}
+				}
+
+				if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) { // Break line and trim spaces.
+					struct TestCase {
+						String text;
+						PackedInt32Array breaks;
+					};
+					TestCase cases[] = {
+						{ U"test \rtest", { 0, 4, 6, 10 } },
+						{ U"test\r test", { 0, 4, 6, 10 } },
+						{ U"test\r test \r test", { 0, 4, 6, 10, 13, 17 } },
+					};
+					for (size_t j = 0; j < sizeof(cases) / sizeof(TestCase); j++) {
+						RID ctx = ts->create_shaped_text();
+						CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
+						bool ok = ts->shaped_text_add_string(ctx, cases[j].text, font, 16);
+						CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
+
+						PackedInt32Array breaks = ts->shaped_text_get_line_breaks(ctx, 90.0, 0, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_EDGE_SPACES);
+						CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
+
+						breaks = ts->shaped_text_get_line_breaks_adv(ctx, { 90.0 }, 0, false, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_EDGE_SPACES);
+						CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
+
 						ts->free_rid(ctx);
 					}
 				}