Repository: MyreMylar/pygame_gui
Pull request #274 (2022-06-08 16:47:55)

fix issue #273 where newline character causes crash for text_box_layo…
fix issue #273 


Patch:
diff --git a/pygame_gui/core/text/text_box_layout_row.py b/pygame_gui/core/text/text_box_layout_row.py
index 10a1def..eb3f509 100644
--- a/pygame_gui/core/text/text_box_layout_row.py
+++ b/pygame_gui/core/text/text_box_layout_row.py
@@ -354,13 +354,13 @@ class TextBoxLayoutRow(pygame.Rect):
                         cursor_draw_width += sum([char_metric[4]
                                                   for char_metric in
                                                   chunk.font.get_metrics(
-                                                      chunk.text[:letter_index])])
+                                                      chunk.text[:letter_index]) if char_metric])
                         letter_acc += letter_index
                         found_chunk = True
                     else:
                         cursor_draw_width += sum([char_metric[4]
                                                   for char_metric in
-                                                  chunk.font.get_metrics(chunk.text)])
+                                                  chunk.font.get_metrics(chunk.text) if char_metric])
                         letter_acc += chunk.letter_count
         if not found_chunk:
             # not inside chunk so move to start of line
@@ -387,14 +387,14 @@ class TextBoxLayoutRow(pygame.Rect):
                 chunk_letter_pos = cursor_pos - letter_acc
                 cursor_draw_width += sum([char_metric[4]
                                           for char_metric
-                                          in chunk.font.get_metrics(chunk.text[:chunk_letter_pos])])
+                                          in chunk.font.get_metrics(chunk.text[:chunk_letter_pos]) if char_metric])
 
                 break
 
             letter_acc += chunk.letter_count
             cursor_draw_width += sum([char_metric[4]
                                       for char_metric in
-                                      chunk.font.get_metrics(chunk.text)])
+                                      chunk.font.get_metrics(chunk.text) if char_metric])
 
         self.cursor_draw_width = cursor_draw_width
 


Test patch:
diff --git a/tests/test_elements/test_ui_text_entry_line.py b/tests/test_elements/test_ui_text_entry_line.py
index ab13370..0481dfc 100644
--- a/tests/test_elements/test_ui_text_entry_line.py
+++ b/tests/test_elements/test_ui_text_entry_line.py
@@ -764,6 +764,22 @@ class TestUITextEntryLine:
 
         text_entry.update(0.01)
 
+    def test_update_newline_after_click(self,  _init_pygame, _display_surface_return_none: None, default_ui_manager):
+        manager = UIManager((800, 600), os.path.join("tests", "data",
+                                                     "themes",
+                                                     "ui_text_entry_line_non_default.json"))
+        text_entry = UITextEntryLine(relative_rect=pygame.Rect(0, 0, 200, 30),
+                                     manager=manager)
+
+        text_entry.set_text('Wow testing is great so amazing\n\n')
+        text_entry.focus()
+
+        text_entry.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1,
+                                                                             'pos': (30, 15)}))
+        default_ui_manager.mouse_position = (70, 15)
+
+        text_entry.update(0.01)
+
     def test_update_after_long_wait(self,  _init_pygame):
         manager = UIManager((800, 600), os.path.join("tests", "data",
                                                      "themes",