From c1ff0efad19a6b84c78283dc852f7515f68cbefa Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Sat, 1 Jun 2024 21:26:45 -0700 Subject: [PATCH] Word wrap button labels on soft hypens If the UTF-8 sequence for the Unicode Soft Hyphen ("Non-printable U+00AD") occurs in the text to be used as the label of a button, and the label is still too wide after any wrapping done based on either space and/or a visible hyphen ("-", U+002D), a visible hyphen will be introduced and the label will be wrapped. e.g. Norwegian Nynorsk "Regnbogesyklus" Regnboge- syklus (rather than all crammed on one line and impossible to read) h/t Karl for the idea. Closes https://sourceforge.net/p/tuxpaint/bugs/288/ --- docs/CHANGES.txt | 4 +- src/org.tuxpaint.Tuxpaint.appdata.xml.in | 3 +- src/tuxpaint.c | 96 ++++++++++++++++++++---- 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index 540bcc806..1eb588318 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -81,8 +81,8 @@ https://tuxpaint.org/ Closes https://sourceforge.net/p/tuxpaint/feature-requests/229/ Bill Kendrick - * Button labels word-wrap on hyphens/dashes ("-") now too. - TODO And on soft-hyphens. + * Button labels word-wrap on hyphens/dashes ("-"), and + can word-wrap (and add a hyphen) when soft-hyphens are used. Closes https://sourceforge.net/p/tuxpaint/bugs/288/ (h/t Karl Ove Hufthammer for suggesting) Bill Kendrick diff --git a/src/org.tuxpaint.Tuxpaint.appdata.xml.in b/src/org.tuxpaint.Tuxpaint.appdata.xml.in index 42f7e6b20..e0624fe6b 100644 --- a/src/org.tuxpaint.Tuxpaint.appdata.xml.in +++ b/src/org.tuxpaint.Tuxpaint.appdata.xml.in @@ -47,7 +47,7 @@ - +

Transparent Erasers.

Brushes support descriptions.

@@ -55,6 +55,7 @@

Updated Magic tools: 3D Glasses (now offers different anaglyph color combinations).

Magic tools may be ungrouped ("ungroupmagictools").

Localization updates.

+

Improved word wrapping in buttons (via hyphens and soft hyphens).

diff --git a/src/tuxpaint.c b/src/tuxpaint.c index 3a9265790..05c8b4778 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -9568,16 +9568,15 @@ static SDL_Surface *do_render_button_label(const char *const label) height_mult = 1.0; + /* If very wide, try to wrap on a space (near the end) */ if (tmp_surf1->w >= button_w * 1.5) { - int i, found = -1; - char * broken_str; - + int i, found = -1, wrapped = 0; DEBUG_PRINTF("'%s' is very wide (%d) compared to button size (%d)\n", upstr, tmp_surf1->w, button_w); + if (strstr(upstr, " ") != NULL) { - /* Try to wrap on a space */ for (i = (strlen(upstr) * 3 / 4); i >= 0 && found == -1; i--) { if (upstr[i] == ' ') @@ -9589,16 +9588,30 @@ static SDL_Surface *do_render_button_label(const char *const label) if (found != -1) { upstr[found] = '\n'; + wrapped = 1; + } + if (wrapped) + { SDL_FreeSurface(tmp_surf1); tmp_surf1 = render_text(myfont, upstr, black); height_mult = 1.5; } } - else if (strstr(upstr, "-") != NULL) + } + + /* If STILL very wide, try to wrap on visible hyphen/dash */ + if (tmp_surf1->w >= button_w * 1.5) + { + int i, found = -1, wrapped = 0; + char *broken_str; + + DEBUG_PRINTF("'%s' is STILL very wide (%d) compared to button size (%d)\n", upstr, tmp_surf1->w, button_w); + + /* Try to wrap on a visible hyphen/dash */ + if (strstr(upstr, "-") != NULL) { - /* Try to wrap on a visible hyphen/dash */ for (i = (strlen(upstr) - 1); i >= 0 && found == -1; i--) { if (upstr[i] == '-') @@ -9609,23 +9622,78 @@ static SDL_Surface *do_render_button_label(const char *const label) if (found != -1) { - SDL_FreeSurface(tmp_surf1); - broken_str = alloca(sizeof(wchar_t) * strlen(upstr) + 2); - if (broken_str != NULL) { - for (i = 0; i <= found; i++) { + if (broken_str != NULL) + { + for (i = 0; i <= found; i++) + { broken_str[i] = upstr[i]; } broken_str[i] = '\n'; - for (i = found + 1; i < (int) strlen(upstr); i++) { + for (i = found + 1; i < (int)strlen(upstr); i++) + { broken_str[i + 1] = upstr[i]; } broken_str[i + 1] = '\0'; - tmp_surf1 = render_text(myfont, broken_str, black); - } else { - tmp_surf1 = render_text(myfont, upstr, black); + wrapped = 1; + free(upstr); + upstr = strdup(broken_str); } + } + + if (wrapped) + { + SDL_FreeSurface(tmp_surf1); + tmp_surf1 = render_text(myfont, upstr, black); + + height_mult = 1.5; + } + } + } + + /* If STILL very wide, try to wrap on invisible soft hyphen */ + if (tmp_surf1->w >= button_w * 1.5) + { + int i, found = -1, wrapped = 0; + char *broken_str; + + DEBUG_PRINTF("'%s' is STILL very wide (%d) compared to button size (%d)\n", upstr, tmp_surf1->w, button_w); + + /* Try to wrap on an invisible soft hyphen */ + if (strstr(upstr, "\302\255") != NULL) + { + /* (This also _introduces_ a visible hyphen; + basically we replace the two-byte UTF-8 sequence + with ASCII '-' and '\n') */ + found = (int)(strstr(upstr, "\302\255") - upstr); + + DEBUG_PRINTF("\"%s\" has a soft hypen at %d\n", upstr, found); + + broken_str = alloca(sizeof(wchar_t) * strlen(upstr) + 3); + if (broken_str != NULL) + { + for (i = 0; i < found; i++) + { + broken_str[i] = upstr[i]; + } + broken_str[found] = '-'; + broken_str[found + 1] = '\n'; + for (i = found + 2; i < (int)strlen(upstr); i++) + { + broken_str[i] = upstr[i]; + } + broken_str[i] = '\0'; + + wrapped = 1; + free(upstr); + upstr = strdup(broken_str); + } + + if (wrapped) + { + SDL_FreeSurface(tmp_surf1); + tmp_surf1 = render_text(myfont, upstr, black); height_mult = 1.5; }