10 #include "app_config.h"
14 #include "os_helpers.h"
16 #if defined(HAVE_LANGUAGE_PACK)
23 #define PIC_FONT(x) ((nbgl_font_t const *) PIC(x))
24 #define BAGL_FONT_ID_MASK 0x0FFF
26 #define IS_WORD_DELIM(c) (c == ' ' || c == '\n' || c == '\b' || c == '\f' || c == '-' || c == '_')
35 #ifdef HAVE_UNICODE_SUPPORT
43 #ifdef HAVE_LANGUAGE_PACK
47 #if defined(BOLOS_OS_UPGRADER_APP)
48 #ifdef SCREEN_SIZE_WALLET
50 #include "nbgl_font_inter_regular_24.inc"
51 #include "nbgl_font_inter_semibold_24.inc"
52 #include "nbgl_font_inter_medium_32.inc"
53 #include "nbgl_font_inter_regular_24_1bpp.inc"
54 #include "nbgl_font_inter_semibold_24_1bpp.inc"
55 #include "nbgl_font_inter_medium_32_1bpp.inc"
57 #include "nbgl_font_inter_regular_28.inc"
58 #include "nbgl_font_inter_semibold_28.inc"
59 #include "nbgl_font_inter_medium_36.inc"
60 #include "nbgl_font_inter_regular_28_1bpp.inc"
61 #include "nbgl_font_inter_semibold_28_1bpp.inc"
62 #include "nbgl_font_inter_medium_36_1bpp.inc"
65 #include "nbgl_font_open_sans_extrabold_11.inc"
66 #include "nbgl_font_open_sans_regular_11.inc"
67 #include "nbgl_font_open_sans_light_16.inc"
72 #include "nbgl_font_rom_struct.inc"
75 const unsigned int C_nbgl_fonts_count =
sizeof(C_nbgl_fonts) /
sizeof(C_nbgl_fonts[0]);
79 #if (defined(HAVE_BOLOS) && !defined(BOLOS_OS_UPGRADER_APP))
80 #if !defined(HAVE_LANGUAGE_PACK)
83 #include "nbgl_font_unicode_rom_struct.inc"
88 const unsigned int C_unicode_characters_count
89 = (
sizeof(charactersOPEN_SANS_REGULAR_11PX_UNICODE)
90 /
sizeof(charactersOPEN_SANS_REGULAR_11PX_UNICODE[0]));
95 #ifdef BUILD_SCREENSHOTS
99 bool last_bold_state =
false;
102 bool hard_caesura =
false;
119 #if defined(BOLOS_OS_UPGRADER_APP)
122 unsigned int i = C_nbgl_fonts_count;
127 if (
PIC_FONT(C_nbgl_fonts[i])->font_id == fontId) {
150 uint32_t nbgl_popUnicodeChar(const uint8_t **text, uint16_t *textLen, bool *is_unicode)
167 if ((cur_char >= 0xF0) && (*textLen >= 4)) {
168 unicode = (cur_char & 0x07) << 18;
169 unicode |= (*txt++ & 0x3F) << 12;
170 unicode |= (*txt++ & 0x3F) << 6;
171 unicode |= (*txt++ & 0x3F);
175 else if ((cur_char >= 0xE0) && (*textLen >= 3)) {
176 unicode = (cur_char & 0x0F) << 12;
177 unicode |= (*txt++ & 0x3F) << 6;
178 unicode |= (*txt++ & 0x3F);
183 else if ((cur_char >= 0xC2) && (*textLen >= 2)) {
184 unicode = (cur_char & 0x1F) << 6;
185 unicode |= (*txt++ & 0x3F);
191 *textLen = *textLen - (txt - *text);
204 static uint8_t getCharWidth(
const nbgl_font_t *font, uint32_t unicode,
bool is_unicode)
207 #ifdef HAVE_UNICODE_SUPPORT
209 = nbgl_getUnicodeFontCharacter(unicode);
210 if (!unicodeCharacter) {
220 if ((unicode < font->first_char) || (unicode > font->
last_char)) {
244 #ifdef BUILD_SCREENSHOTS
251 #ifdef HAVE_UNICODE_SUPPORT
255 #ifdef BUILD_SCREENSHOTS
257 bool next_bold_state = last_bold_state;
267 #ifdef HAVE_UNICODE_SUPPORT
268 if (is_unicode && !unicode_ctx) {
269 unicode_ctx = nbgl_getUnicodeFont(fontId);
272 if (unicode ==
'\n') {
273 if (breakOnLineEnd) {
281 else if (unicode ==
'\b') {
284 #ifdef HAVE_UNICODE_SUPPORT
288 #ifdef BUILD_SCREENSHOTS
289 next_bold_state =
true;
294 #ifdef HAVE_UNICODE_SUPPORT
298 #ifdef BUILD_SCREENSHOTS
299 next_bold_state =
false;
304 char_width = getCharWidth(font, unicode, is_unicode);
305 #ifdef BUILD_SCREENSHOTS
306 if (char_width != 0) {
308 last_bold_state = next_bold_state;
311 line_width += char_width;
313 if (line_width > max_width) {
314 max_width = line_width;
317 #ifdef BUILD_SCREENSHOTS
318 if (line_width != 0) {
321 last_nb_lines = nb_lines;
336 return getTextWidth(fontId, text,
true, 0xFFFF);
350 return getTextWidth(fontId, text,
true, maxLen);
362 return getTextWidth(fontId, text,
false, 0xFFFF);
378 #ifdef HAVE_UNICODE_SUPPORT
383 #ifdef HAVE_UNICODE_SUPPORT
384 if (is_unicode && !unicode_ctx) {
385 unicode_ctx = nbgl_getUnicodeFont(fontId);
389 return getCharWidth(font, unicode, is_unicode);
456 const char *origText = text;
461 else if (*text ==
'\n') {
466 return text - origText;
491 uint32_t lenAtLastDelimiter = 0, widthAtlastDelimiter = 0;
492 #ifdef HAVE_UNICODE_SUPPORT
505 #ifdef HAVE_UNICODE_SUPPORT
506 if (is_unicode && !unicode_ctx) {
507 unicode_ctx = nbgl_getUnicodeFont(fontId);
511 if ((unicode ==
'\f') || (unicode ==
'\n')) {
512 *len += curTextLen - textLen;
516 else if (unicode ==
'\b') {
519 #ifdef HAVE_UNICODE_SUPPORT
526 #ifdef HAVE_UNICODE_SUPPORT
531 *len += curTextLen - textLen;
535 char_width = getCharWidth(font, unicode, is_unicode);
536 if (char_width == 0) {
537 *len += curTextLen - textLen;
543 lenAtLastDelimiter = *len;
544 widthAtlastDelimiter = *width;
546 if ((*width + char_width) > maxWidth) {
547 if ((wrapping ==
true) && (widthAtlastDelimiter > 0)) {
548 *len = lenAtLastDelimiter + 1;
549 *width = widthAtlastDelimiter;
553 *len += curTextLen - textLen;
554 *width = *width + char_width;
582 const char *lastDelimiter = NULL;
583 uint32_t lenAtLastDelimiter = 0;
584 const char *origText = text;
585 const char *previousText;
586 #ifdef HAVE_UNICODE_SUPPORT
590 while ((textLen) && (maxNbLines > 0)) {
597 #ifdef HAVE_UNICODE_SUPPORT
598 if (is_unicode && !unicode_ctx) {
599 unicode_ctx = nbgl_getUnicodeFont(fontId);
604 lastDelimiter = text;
605 lenAtLastDelimiter = textLen;
609 if (unicode ==
'\n') {
612 if (maxNbLines == 0) {
619 else if (unicode ==
'\f') {
624 else if (unicode ==
'\b') {
627 #ifdef HAVE_UNICODE_SUPPORT
634 #ifdef HAVE_UNICODE_SUPPORT
642 char_width = getCharWidth(font, unicode, is_unicode);
643 if (char_width == 0) {
647 if ((width + char_width) > maxWidth) {
648 if ((wrapping ==
true) && (lastDelimiter != NULL)) {
649 text = lastDelimiter;
650 lastDelimiter = NULL;
651 textLen = lenAtLastDelimiter;
654 textLen += text - previousText;
663 *len = text - origText;
664 return (maxNbLines == 0);
698 cur_char = text[textLen];
700 if (cur_char ==
'\n') {
706 if ((cur_char < font->first_char) || (cur_char > font->
last_char)) {
711 char_width = character->
width;
713 if ((*width + char_width) > maxWidth) {
717 *width = *width + char_width;
738 #ifdef SCREEN_SIZE_NANO
744 const char *lastDelimiter = NULL;
745 uint32_t lenAtLastDelimiter = 0;
746 const char *prevText = NULL;
747 #ifdef HAVE_UNICODE_SUPPORT
751 #ifdef BUILD_SCREENSHOTS
752 hard_caesura =
false;
756 bool next_bold_state = last_bold_state;
768 #ifdef HAVE_UNICODE_SUPPORT
769 if (is_unicode && !unicode_ctx) {
770 unicode_ctx = nbgl_getUnicodeFont(fontId);
776 lastDelimiter = prevText;
777 lenAtLastDelimiter = textLen;
780 if (unicode ==
'\f') {
781 #ifdef BUILD_SCREENSHOTS
789 else if (unicode ==
'\n') {
791 #if defined(BUILD_SCREENSHOTS) && defined(SCREEN_SIZE_NANO)
792 if (!(nbLines % 4)) {
799 lastDelimiter = NULL;
803 else if (unicode ==
'\b') {
806 #ifdef HAVE_UNICODE_SUPPORT
810 #ifdef BUILD_SCREENSHOTS
811 next_bold_state =
true;
816 #ifdef HAVE_UNICODE_SUPPORT
820 #ifdef BUILD_SCREENSHOTS
821 next_bold_state =
false;
827 char_width = getCharWidth(font, unicode, is_unicode);
828 if (char_width == 0) {
831 #ifdef BUILD_SCREENSHOTS
833 last_bold_state = next_bold_state;
837 if ((width + char_width) > maxWidth) {
838 if ((wrapping ==
true) && (lastDelimiter != NULL)) {
839 text = lastDelimiter + 1;
840 lastDelimiter = NULL;
841 textLen = lenAtLastDelimiter;
845 #ifdef BUILD_SCREENSHOTS
852 #if defined(BUILD_SCREENSHOTS) && defined(SCREEN_SIZE_NANO)
853 if (!(nbLines % 4)) {
864 #ifdef SCREEN_SIZE_NANO
869 #ifdef BUILD_SCREENSHOTS
870 last_nb_lines = nbLines;
895 const char *lastDelimiter = NULL;
896 uint32_t lenAtLastDelimiter = 0;
897 const char *prevText = NULL;
898 #ifdef HAVE_UNICODE_SUPPORT
902 #ifdef BUILD_SCREENSHOTS
903 last_nb_lines = nbLines;
904 last_nb_pages = nbPages;
906 bool next_bold_state = last_bold_state;
917 #ifdef HAVE_UNICODE_SUPPORT
918 if (is_unicode && !unicode_ctx) {
919 unicode_ctx = nbgl_getUnicodeFont(fontId);
925 lastDelimiter = prevText;
926 lenAtLastDelimiter = textLen;
929 if (unicode ==
'\f') {
931 #ifdef BUILD_SCREENSHOTS
932 #ifdef SCREEN_SIZE_NANO
936 #ifdef SCREEN_SIZE_NANO
939 if (last_nb_lines < nbLines) {
940 last_nb_lines = nbLines;
948 else if (unicode ==
'\n') {
950 #ifdef BUILD_SCREENSHOTS
951 if (last_nb_lines < nbLines) {
952 last_nb_lines = nbLines;
955 if (nbLines == nbLinesPerPage && textLen) {
961 lastDelimiter = NULL;
965 else if (unicode ==
'\b') {
968 #ifdef HAVE_UNICODE_SUPPORT
972 #ifdef BUILD_SCREENSHOTS
973 next_bold_state =
true;
978 #ifdef HAVE_UNICODE_SUPPORT
982 #ifdef BUILD_SCREENSHOTS
983 next_bold_state =
false;
989 char_width = getCharWidth(font, unicode, is_unicode);
990 if (char_width == 0) {
994 #ifdef BUILD_SCREENSHOTS
996 last_bold_state = next_bold_state;
1000 if ((width + char_width) > maxWidth) {
1001 if (lastDelimiter != NULL) {
1002 text = lastDelimiter + 1;
1003 lastDelimiter = NULL;
1004 textLen = lenAtLastDelimiter;
1011 #ifdef BUILD_SCREENSHOTS
1012 if (last_nb_lines < nbLines) {
1013 last_nb_lines = nbLines;
1016 if (nbLines == nbLinesPerPage) {
1022 width += char_width;
1025 #ifdef BUILD_SCREENSHOTS
1026 #ifdef SCREEN_SIZE_NANO
1031 if (last_nb_lines < nbLines) {
1032 last_nb_lines = nbLines;
1034 last_nb_pages = nbPages;
1074 char *lastDelimiter = NULL;
1075 uint32_t lenAtLastDelimiter = 0;
1076 char *prevText[4] = {0};
1079 #ifdef HAVE_UNICODE_SUPPORT
1090 prevText[3] = prevText[2];
1091 prevText[2] = prevText[1];
1092 prevText[1] = prevText[0];
1096 if (unicode ==
'\n') {
1099 lastDelimiter = NULL;
1102 #ifdef HAVE_UNICODE_SUPPORT
1103 if (is_unicode && !unicode_ctx) {
1104 unicode_ctx = nbgl_getUnicodeFont(fontId);
1107 char_width = getCharWidth(font, unicode, is_unicode);
1108 if (char_width == 0) {
1114 lastDelimiter = prevText[0];
1115 lenAtLastDelimiter = textLen + 1;
1118 if ((width + char_width) > maxWidth) {
1121 if (currentNbLines < nbLines) {
1124 if (lastDelimiter != NULL) {
1125 *lastDelimiter++ =
'\n';
1126 text = lastDelimiter;
1127 lastDelimiter = NULL;
1128 textLen = lenAtLastDelimiter - 1;
1131 textLen += text - prevText[0];
1142 for (i = 0; i < 2; i++) {
1143 if ((prevText[i + 1] != NULL)
1144 && ((prevWidth[i] + (3 * getCharWidth(font,
'.',
false))) <= maxWidth)) {
1149 if (prevText[i + 1] != NULL) {
1150 memcpy(prevText[i + 1],
"...", 4);
1156 prevWidth[1] = prevWidth[0];
1157 prevWidth[0] = width;
1158 width += char_width;
1178 const char *origText,
1185 uint16_t textLen = strlen(origText);
1188 uint32_t i = 0, j = 0;
1191 if ((nbLines & 0x1) == 0) {
1195 while ((i < textLen) && (j < reducedTextLen)) {
1199 curChar = origText[i];
1201 char_width = getCharWidth(font, curChar,
false);
1202 if (char_width == 0) {
1203 reducedText[j] = curChar;
1210 if ((width + char_width) > maxWidth) {
1216 else if ((currentNbLines == ((nbLines / 2) + 1)) && ((width + char_width) > halfWidth)) {
1217 uint32_t halfFullWidth = (nbLines / 2) * maxWidth + halfWidth;
1218 uint32_t countDown = textLen;
1220 reducedText[j - 1] =
'.';
1221 reducedText[j] =
'.';
1222 reducedText[j + 1] =
'.';
1225 while (width < halfFullWidth) {
1227 curChar = origText[countDown];
1228 char_width = getCharWidth(font, curChar,
false);
1229 width += char_width;
1231 memcpy(&reducedText[j + 2], &origText[countDown + 1], textLen - countDown + 1);
1235 reducedText[j] = curChar;
1239 width += char_width;
1241 reducedText[j] =
'\0';
1244 #ifdef HAVE_UNICODE_SUPPORT
1254 if ((unicodeCtx.
font != NULL) && (unicodeCtx.
font->
font_id == fontId)) {
1257 #if defined(HAVE_LANGUAGE_PACK)
1263 unicodeCtx.
bitmap = (
const uint8_t *) (ptr + language_pack->bitmaps_offset);
1265 for (uint32_t i = 0; i < language_pack->nb_fonts; i++) {
1266 if (font->
font_id == fontId) {
1268 unicodeCtx.
font = font;
1275 unicodeCtx.
characters += language_pack->nb_characters;
1294 #if defined(HAVE_LANGUAGE_PACK)
1297 uint32_t n = language_pack->nb_characters;
1298 if (characters == NULL) {
1303 for (
unsigned i = 0; i < n - 1; i++, characters++) {
1307 = (characters + 1)->bitmap_offset - characters->
bitmap_offset;
1331 uint32_t nbgl_getUnicodeFontCharacterByteCount(
void)
1333 #ifdef HAVE_LANGUAGE_PACK
1340 #ifdef HAVE_LANGUAGE_PACK
1351 unicodeCtx.
font = NULL;
#define LOG_FATAL(__logger,...)
Middle Level API of the new BOLOS Graphical Library.
void nbgl_textWrapOnNbLines(nbgl_font_id_e fontId, char *text, uint16_t maxWidth, uint8_t nbLines)
Modifies the given text to wrap it on the given max width (in pixels), in the given nbLines If possib...
void nbgl_getTextMaxLenAndWidth(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, uint16_t *len, uint16_t *width, bool wrapping)
compute the max width of the first line of the given text fitting in maxWidth
uint16_t nbgl_getTextNbLines(const char *text)
return the number of lines in the given text, according to the found ' 's
uint8_t nbgl_getTextNbPagesInWidth(nbgl_font_id_e fontId, const char *text, uint8_t nbLinesPerPage, uint16_t maxWidth)
compute the number of pages of nbLinesPerPage lines per page of the given text fitting in the given m...
uint16_t nbgl_getSingleLineTextWidth(nbgl_font_id_e fontId, const char *text)
return the max width in pixels of the given text until the first or \0 is encountered
uint8_t nbgl_getCharWidth(nbgl_font_id_e fontId, const char *text)
return the width in pixels of the given UTF-8 character
bool nbgl_getTextMaxLenInNbLines(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, uint16_t maxNbLines, uint16_t *len, bool wrapping)
compute the len of the given text (in bytes) fitting in the given maximum nb lines,...
bool nbgl_getTextMaxLenAndWidthFromEnd(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, uint16_t *len, uint16_t *width)
compute the len and width of the given text fitting in the maxWidth, starting from end of text
uint8_t nbgl_getFontHeight(nbgl_font_id_e fontId)
return the height in pixels of the font with the given font ID
void nbgl_textReduceOnNbLines(nbgl_font_id_e fontId, const char *origText, uint16_t maxWidth, uint8_t nbLines, char *reducedText, uint16_t reducedTextLen)
Create a reduced version of given ASCII text to wrap it on the given max width (in pixels),...
uint32_t nbgl_popUnicodeChar(const uint8_t **text, uint16_t *textLen, bool *is_unicode)
Get the coming unicode value on the given UTF-8 string. If the value is a simple ASCII character,...
uint16_t nbgl_getTextWidth(nbgl_font_id_e fontId, const char *text)
return the max width in pixels of the given text (can be multiline)
uint16_t nbgl_getTextHeightInWidth(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, bool wrapping)
return the height of the given multiline text, with the given font.
#define BAGL_FONT_ID_MASK
uint16_t nbgl_getTextLength(const char *text)
return the number of bytes of the given text, excluding final ' ' or '\0'
uint16_t nbgl_getTextNbLinesInWidth(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, bool wrapping)
compute the number of lines of the given text fitting in the given maxWidth
uint16_t nbgl_getTextHeight(nbgl_font_id_e fontId, const char *text)
return the height of the given multiline text, with the given font.
const nbgl_font_t * nbgl_getFont(nbgl_font_id_e fontId)
return the non-unicode font corresponding to the given font ID
uint8_t nbgl_getFontLineHeight(nbgl_font_id_e fontId)
return the height in pixels of the line of font with the given font ID
uint16_t nbgl_getSingleLineTextWidthInLen(nbgl_font_id_e fontId, const char *text, uint16_t maxLen)
return the max width in pixels of the given text until the first or \0 is encountered,...
const nbgl_font_t * nbgl_font_getFont(unsigned int fontId)
@ BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp
@ BAGL_FONT_OPEN_SANS_EXTRABOLD_11px_1bpp
fonts nicknames to be used for various wallet size targets (non-Nano)
uint32_t width
width of character in pixels
structure defining an ASCII font
uint8_t char_kerning
kerning for the font
uint8_t first_char
ASCII code of the first character in bitmap and in characters fields.
const nbgl_font_character_t *const characters
array containing definitions of all characters
uint8_t height
height of all characters in pixels
uint8_t line_height
height of a line for all characters in pixels
uint8_t last_char
ASCII code of the last character in bitmap and in characters fields.
structure defining a unicode character (except the bitmap)
uint32_t char_unicode
plane value from 0 to 16 then 16-bit code.
uint32_t width
width of character in pixels
uint32_t bitmap_offset
offset of this character in chars buffer
structure defining a unicode font
uint16_t bitmap_len
Size in bytes of all characters bitmaps.
uint8_t font_id
ID of the font, from nbgl_font_id_e.
const nbgl_font_unicode_character_t * characters
const nbgl_font_unicode_t * font
uint32_t unicode_character_byte_count