Skip to content

Commit

Permalink
Fix replacement of last char by "..." when using nbgl_textWrapOnNbLin…
Browse files Browse the repository at this point in the history
…es()
  • Loading branch information
nroggeman-ledger committed Sep 3, 2024
1 parent f56e29d commit c39ba4f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
38 changes: 27 additions & 11 deletions lib_nbgl/src/nbgl_fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,9 @@ void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth
char *lastDelimiter = NULL;
uint32_t lenAtLastDelimiter = 0;
char *prevText = NULL;
char *prevPrevText = NULL;
uint16_t prevWidth = 0;

#ifdef HAVE_UNICODE_SUPPORT
nbgl_unicode_ctx_t *unicode_ctx = NULL;
#endif // HAVE_UNICODE_SUPPORT
Expand All @@ -1078,12 +1081,13 @@ void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth
uint8_t char_width;
uint32_t unicode;
bool is_unicode;
char *prevPrevText;
char *prevPrevPrevText;

// memorize the two last chars
prevPrevText = prevText;
prevText = text;
unicode = nbgl_popUnicodeChar((const uint8_t **) &text, &textLen, &is_unicode);
// memorize the three last chars
prevPrevPrevText = prevPrevText;
prevPrevText = prevText;
prevText = text;
unicode = nbgl_popUnicodeChar((const uint8_t **) &text, &textLen, &is_unicode);
// if \n, reset width
if (unicode == '\n') {
width = 0;
Expand Down Expand Up @@ -1117,26 +1121,38 @@ void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth
*lastDelimiter++ = '\n';
text = lastDelimiter;
lastDelimiter = NULL;
textLen = lenAtLastDelimiter;
textLen = lenAtLastDelimiter - 1;
}
else {
textLen += text - prevText;
text = prevText;
}
// reset width for next line
width = 0;
continue;
}
else {
// replace the 2 last chars by '...' (should be same width)
// replace the 2 or 3 last chars by '...'
// try at first with 2, if it fits
if (prevPrevText != NULL) {
*prevPrevText++ = '.';
*prevPrevText++ = '.';
*prevPrevText++ = '.';
*prevPrevText = '\0';
if ((prevWidth + (3 * getCharWidth(font, '.', false))) <= maxWidth) {
*prevPrevText++ = '.';
*prevPrevText++ = '.';
*prevPrevText++ = '.';
*prevPrevText = '\0';
}
else if (prevPrevPrevText != NULL) {
*prevPrevPrevText++ = '.';
*prevPrevPrevText++ = '.';
*prevPrevPrevText++ = '.';
*prevPrevPrevText = '\0';
}
}
return;
}
}
// memorize the last width
prevWidth = width;
width += char_width;
}
}
Expand Down
19 changes: 16 additions & 3 deletions unit-tests/lib_nbgl/test_nbgl_fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,18 @@ static void test_get_length(void **state __attribute__((unused)))
nbgl_textWrapOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px, textToWrap, 156, 2);
assert_string_equal(textToWrap, "toto");

strcpy(textToWrap, "bonjour tu aimes les mois");
width = nbgl_getTextWidth(BAGL_FONT_INTER_SEMIBOLD_24px, "aimesllesn...");
assert_int_equal(width, 146);
width = nbgl_getTextWidth(BAGL_FONT_INTER_SEMIBOLD_24px, "aimesllesno");
assert_int_equal(width, 140);

strcpy(textToWrap, "bonjour tu aimesllesnois");
nbgl_textWrapOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px, textToWrap, 156, 2);
assert_string_equal(textToWrap, "bonjour tu\naimes les...");
assert_string_equal(textToWrap, "bonjour tu\naimesllesn...");

strcpy(textToWrap, "bonjourtuaimestr les mois");
nbgl_textWrapOnNbLines(BAGL_FONT_INTER_SEMIBOLD_24px, textToWrap, 156, 2);
assert_string_equal(textToWrap, "bonjourtuaimestr les...");
assert_string_equal(textToWrap, "bonjourtuaimestr les ...");

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

nbgl_getTextMaxLenInNbLines(BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp,
"DED80D16B34ED27A0A92E6554FEAD680B40238BFFCB44FA5185D8B4BF9C45696",
114,
3,
&len,
true);
assert_int_equal(len, 51);

uint8_t nbPages = nbgl_getTextNbPagesInWidth(
BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp,
"Pour sélectionner ou confirmer, appuyez sur les deux boutons\fTéléchargez\nLedger "
Expand Down

0 comments on commit c39ba4f

Please sign in to comment.