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/
This commit is contained in:
Bill Kendrick 2024-06-01 21:26:45 -07:00
parent 217e653d3a
commit c1ff0efad1
3 changed files with 86 additions and 17 deletions

View file

@ -81,8 +81,8 @@ https://tuxpaint.org/
Closes https://sourceforge.net/p/tuxpaint/feature-requests/229/
Bill Kendrick <bill@newbreedsoftware.com>
* 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 <bill@newbreedsoftware.com>

View file

@ -47,7 +47,7 @@
</screenshot>
</screenshots>
<releases>
<release version="0.9.33" date="2024-05-19">
<release version="0.9.33" date="2024-06-01">
<description>
<p>Transparent Erasers.</p>
<p>Brushes support descriptions.</p>
@ -55,6 +55,7 @@
<p>Updated Magic tools: 3D Glasses (now offers different anaglyph color combinations).</p>
<p>Magic tools may be ungrouped ("ungroupmagictools").</p>
<p>Localization updates.</p>
<p>Improved word wrapping in buttons (via hyphens and soft hyphens).</p>
</description>
</release>
<release version="0.9.32" date="2024-01-28">

View file

@ -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;
}