From 224e9ffa3435a44e83a0fcaadef86416190b6aa1 Mon Sep 17 00:00:00 2001 From: Matt Emerick-Law Date: Thu, 27 Jun 2024 10:49:44 +0100 Subject: [PATCH 1/5] Fix split text to new line when \n is used --- modules/app_components/utils.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/modules/app_components/utils.py b/modules/app_components/utils.py index b6aac07..145480f 100644 --- a/modules/app_components/utils.py +++ b/modules/app_components/utils.py @@ -16,12 +16,10 @@ def fill_line(ctx, text, font_size, width_for_line): def wrap_text(ctx, text, font_size=None, width=None): if width is None: width = 240 - remaining_text = text - lines = [] - while remaining_text: - line, remaining_text = fill_line(ctx, remaining_text, font_size, width) - if "\n" in line: - lines += line.split("\n") - else: - lines.append(line) - return lines + lines = text.split("\n") + wrapped_lines = [] + for line in lines: + lines = fill_line(ctx, line, font_size, width) + wrapped_lines.extend(lines) + print(wrapped_lines) + return wrapped_lines From 477c817731d2f879b67c0cf992a95e3d3fd0f691 Mon Sep 17 00:00:00 2001 From: Matt Emerick-Law Date: Thu, 27 Jun 2024 10:50:20 +0100 Subject: [PATCH 2/5] Do not wrap text in the middle of a word, if a word is too long for a line then hyphenate --- modules/app_components/utils.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/modules/app_components/utils.py b/modules/app_components/utils.py index 145480f..4887035 100644 --- a/modules/app_components/utils.py +++ b/modules/app_components/utils.py @@ -1,16 +1,29 @@ def fill_line(ctx, text, font_size, width_for_line): ctx.save() ctx.font_size = font_size - extra_text = "" - text_that_fits = text - text_width = ctx.text_width(text_that_fits) - while text_width > width_for_line: - character = text_that_fits[-1] - text_that_fits = text_that_fits[:-1] - extra_text = character + extra_text - text_width = ctx.text_width(text_that_fits) + lines = [] + line = "" + words = text.split(' ') + for word in words: + remaining_word = word + while ctx.text_width(remaining_word) > width_for_line: + word = word[:-1] + check_word = (line + ' ' + word + '-').strip() + if ctx.text_width(check_word) <= width_for_line: + lines.append(check_word) + line = "" + word = remaining_word[len(word) :] + remaining_word = word + + new_line = line + ' ' + word + if ctx.text_width(new_line) > width_for_line: + lines.append(line.strip()) + line = word + else: + line = new_line.strip() + lines.append(line) ctx.restore() - return text_that_fits, extra_text + return lines def wrap_text(ctx, text, font_size=None, width=None): From 6f742b18578514d061dbaf4c6dc25a73e040b3cd Mon Sep 17 00:00:00 2001 From: Matt Emerick-Law Date: Thu, 27 Jun 2024 11:00:54 +0100 Subject: [PATCH 3/5] Remove debugging print --- modules/app_components/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/app_components/utils.py b/modules/app_components/utils.py index 4887035..a25a496 100644 --- a/modules/app_components/utils.py +++ b/modules/app_components/utils.py @@ -34,5 +34,4 @@ def wrap_text(ctx, text, font_size=None, width=None): for line in lines: lines = fill_line(ctx, line, font_size, width) wrapped_lines.extend(lines) - print(wrapped_lines) return wrapped_lines From c28d63a84feada67fb1d5a1ff92d38ad4811dc7d Mon Sep 17 00:00:00 2001 From: Matt Emerick-Law Date: Thu, 27 Jun 2024 11:20:18 +0100 Subject: [PATCH 4/5] Missed ruff --- modules/app_components/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/app_components/utils.py b/modules/app_components/utils.py index a25a496..35a0212 100644 --- a/modules/app_components/utils.py +++ b/modules/app_components/utils.py @@ -3,19 +3,19 @@ def fill_line(ctx, text, font_size, width_for_line): ctx.font_size = font_size lines = [] line = "" - words = text.split(' ') + words = text.split(" ") for word in words: remaining_word = word while ctx.text_width(remaining_word) > width_for_line: word = word[:-1] - check_word = (line + ' ' + word + '-').strip() + check_word = (line + " " + word + '-').strip() if ctx.text_width(check_word) <= width_for_line: lines.append(check_word) line = "" word = remaining_word[len(word) :] remaining_word = word - new_line = line + ' ' + word + new_line = line + " " + word if ctx.text_width(new_line) > width_for_line: lines.append(line.strip()) line = word From e14dd262467b5f2b53091bdd3b919326a1c5410d Mon Sep 17 00:00:00 2001 From: Matt Emerick-Law Date: Thu, 27 Jun 2024 12:30:07 +0100 Subject: [PATCH 5/5] Missed ruff --- modules/app_components/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/app_components/utils.py b/modules/app_components/utils.py index 35a0212..21c0a1c 100644 --- a/modules/app_components/utils.py +++ b/modules/app_components/utils.py @@ -8,7 +8,7 @@ def fill_line(ctx, text, font_size, width_for_line): remaining_word = word while ctx.text_width(remaining_word) > width_for_line: word = word[:-1] - check_word = (line + " " + word + '-').strip() + check_word = (line + " " + word + "-").strip() if ctx.text_width(check_word) <= width_for_line: lines.append(check_word) line = ""