Skip to content

Commit 8bbf8f3

Browse files
Merge pull request #759 from LedgerHQ/fix-too-long-text-shortening
Fix replacement of last char by "..." when using nbgl_textWrapOnNbLines()
2 parents f56e29d + c39ba4f commit 8bbf8f3

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

lib_nbgl/src/nbgl_fonts.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,9 @@ void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth
10701070
char *lastDelimiter = NULL;
10711071
uint32_t lenAtLastDelimiter = 0;
10721072
char *prevText = NULL;
1073+
char *prevPrevText = NULL;
1074+
uint16_t prevWidth = 0;
1075+
10731076
#ifdef HAVE_UNICODE_SUPPORT
10741077
nbgl_unicode_ctx_t *unicode_ctx = NULL;
10751078
#endif // HAVE_UNICODE_SUPPORT
@@ -1078,12 +1081,13 @@ void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth
10781081
uint8_t char_width;
10791082
uint32_t unicode;
10801083
bool is_unicode;
1081-
char *prevPrevText;
1084+
char *prevPrevPrevText;
10821085

1083-
// memorize the two last chars
1084-
prevPrevText = prevText;
1085-
prevText = text;
1086-
unicode = nbgl_popUnicodeChar((const uint8_t **) &text, &textLen, &is_unicode);
1086+
// memorize the three last chars
1087+
prevPrevPrevText = prevPrevText;
1088+
prevPrevText = prevText;
1089+
prevText = text;
1090+
unicode = nbgl_popUnicodeChar((const uint8_t **) &text, &textLen, &is_unicode);
10871091
// if \n, reset width
10881092
if (unicode == '\n') {
10891093
width = 0;
@@ -1117,26 +1121,38 @@ void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth
11171121
*lastDelimiter++ = '\n';
11181122
text = lastDelimiter;
11191123
lastDelimiter = NULL;
1120-
textLen = lenAtLastDelimiter;
1124+
textLen = lenAtLastDelimiter - 1;
11211125
}
11221126
else {
11231127
textLen += text - prevText;
11241128
text = prevText;
11251129
}
11261130
// reset width for next line
11271131
width = 0;
1132+
continue;
11281133
}
11291134
else {
1130-
// replace the 2 last chars by '...' (should be same width)
1135+
// replace the 2 or 3 last chars by '...'
1136+
// try at first with 2, if it fits
11311137
if (prevPrevText != NULL) {
1132-
*prevPrevText++ = '.';
1133-
*prevPrevText++ = '.';
1134-
*prevPrevText++ = '.';
1135-
*prevPrevText = '\0';
1138+
if ((prevWidth + (3 * getCharWidth(font, '.', false))) <= maxWidth) {
1139+
*prevPrevText++ = '.';
1140+
*prevPrevText++ = '.';
1141+
*prevPrevText++ = '.';
1142+
*prevPrevText = '\0';
1143+
}
1144+
else if (prevPrevPrevText != NULL) {
1145+
*prevPrevPrevText++ = '.';
1146+
*prevPrevPrevText++ = '.';
1147+
*prevPrevPrevText++ = '.';
1148+
*prevPrevPrevText = '\0';
1149+
}
11361150
}
11371151
return;
11381152
}
11391153
}
1154+
// memorize the last width
1155+
prevWidth = width;
11401156
width += char_width;
11411157
}
11421158
}

unit-tests/lib_nbgl/test_nbgl_fonts.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@ static void test_get_length(void **state __attribute__((unused)))
104104
nbgl_textWrapOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px, textToWrap, 156, 2);
105105
assert_string_equal(textToWrap, "toto");
106106

107-
strcpy(textToWrap, "bonjour tu aimes les mois");
107+
width = nbgl_getTextWidth(BAGL_FONT_INTER_SEMIBOLD_24px, "aimesllesn...");
108+
assert_int_equal(width, 146);
109+
width = nbgl_getTextWidth(BAGL_FONT_INTER_SEMIBOLD_24px, "aimesllesno");
110+
assert_int_equal(width, 140);
111+
112+
strcpy(textToWrap, "bonjour tu aimesllesnois");
108113
nbgl_textWrapOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px, textToWrap, 156, 2);
109-
assert_string_equal(textToWrap, "bonjour tu\naimes les...");
114+
assert_string_equal(textToWrap, "bonjour tu\naimesllesn...");
110115

111116
strcpy(textToWrap, "bonjourtuaimestr les mois");
112117
nbgl_textWrapOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px, textToWrap, 156, 2);
113-
assert_string_equal(textToWrap, "bonjourtuaimestr les...");
118+
assert_string_equal(textToWrap, "bonjourtuaimestr les ...");
114119

115120
nbgl_textReduceOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px,
116121
"bc1pkdcufjh6dxjaEZFZEFZFGGEaa05hudxqgfffggghhhhhhffffffff5fhspfmZAFEZ"
@@ -225,6 +230,14 @@ static void test_get_length(void **state __attribute__((unused)))
225230
len,
226231
strlen("\bFCC Notes\b\nThis device complies with Part 15 of the FCC Rules. Operation "));
227232

233+
nbgl_getTextMaxLenInNbLines(BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp,
234+
"DED80D16B34ED27A0A92E6554FEAD680B40238BFFCB44FA5185D8B4BF9C45696",
235+
114,
236+
3,
237+
&len,
238+
true);
239+
assert_int_equal(len, 51);
240+
228241
uint8_t nbPages = nbgl_getTextNbPagesInWidth(
229242
BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp,
230243
"Pour sélectionner ou confirmer, appuyez sur les deux boutons\fTéléchargez\nLedger "

0 commit comments

Comments
 (0)