10#include "app_config.h"
14#include "os_helpers.h"
22#define PIC_FONT(x) ((nbgl_font_t const *) PIC(x))
23#define BAGL_FONT_ID_MASK 0x0FFF
25#define IS_WORD_DELIM(c) (c == ' ' || c == '\n' || c == '\b' || c == '\f' || c == '-' || c == '_')
40static const LANGUAGE_PACK *language_pack;
42#if SMALL_FONT_HEIGHT == 24
43#include "nbgl_font_inter_regular_24.inc"
44#include "nbgl_font_inter_semibold_24.inc"
45#include "nbgl_font_inter_medium_32.inc"
46#include "nbgl_font_inter_regular_24_1bpp.inc"
47#include "nbgl_font_inter_semibold_24_1bpp.inc"
48#include "nbgl_font_inter_medium_32_1bpp.inc"
49#elif SMALL_FONT_HEIGHT == 28
50#include "nbgl_font_inter_regular_28.inc"
51#include "nbgl_font_inter_semibold_28.inc"
52#include "nbgl_font_inter_medium_36.inc"
53#include "nbgl_font_inter_regular_28_1bpp.inc"
54#include "nbgl_font_inter_semibold_28_1bpp.inc"
55#include "nbgl_font_inter_medium_36_1bpp.inc"
56#elif SMALL_FONT_HEIGHT == 18
57#include "nbgl_font_nanotext_medium_18_1bpp.inc"
58#include "nbgl_font_nanotext_bold_18_1bpp.inc"
59#include "nbgl_font_nanodisplay_semibold_24_1bpp.inc"
60#elif SMALL_FONT_HEIGHT == 11
61#include "nbgl_font_open_sans_extrabold_11.inc"
62#include "nbgl_font_open_sans_regular_11.inc"
63#include "nbgl_font_open_sans_light_16.inc"
68#include "nbgl_font_rom_struct.inc"
71__attribute__((section(
"._nbgl_fonts_"))) const
unsigned int C_nbgl_fonts_count
72 = sizeof(C_nbgl_fonts) / sizeof(C_nbgl_fonts[0]);
74#if (defined(HAVE_BOLOS) && !defined(BOLOS_OS_UPGRADER_APP))
77#ifdef BUILD_SCREENSHOTS
79uint16_t last_nb_lines = 0;
80uint16_t last_nb_pages = 0;
81bool last_bold_state =
false;
84bool hard_caesura =
false;
103 unsigned int i = C_nbgl_fonts_count;
108 if (
PIC_FONT(C_nbgl_fonts[i])->font_id == fontId) {
132 const uint8_t *txt = *text;
133 uint8_t cur_char = *txt++;
147 if ((cur_char >= 0xF0) && (*textLen >= 4)) {
148 unicode = (cur_char & 0x07) << 18;
149 unicode |= (*txt++ & 0x3F) << 12;
150 unicode |= (*txt++ & 0x3F) << 6;
151 unicode |= (*txt++ & 0x3F);
155 else if ((cur_char >= 0xE0) && (*textLen >= 3)) {
156 unicode = (cur_char & 0x0F) << 12;
157 unicode |= (*txt++ & 0x3F) << 6;
158 unicode |= (*txt++ & 0x3F);
163 else if ((cur_char >= 0xC2) && (*textLen >= 2)) {
164 unicode = (cur_char & 0x1F) << 6;
165 unicode |= (*txt++ & 0x3F);
171 *textLen = *textLen - (txt - *text);
184static uint8_t getCharWidth(
const nbgl_font_t *font, uint32_t unicode,
bool is_unicode)
189 if (!unicodeCharacter) {
193#ifdef SCREEN_SIZE_WALLET
202 if ((unicode < font->first_char) || (unicode > font->
last_char)) {
226#ifdef BUILD_SCREENSHOTS
227 uint16_t nb_lines = 0;
229 uint16_t line_width = 0;
230 uint16_t max_width = 0;
232 uint16_t textLen =
MIN(strlen(text), maxLen);
235#ifdef BUILD_SCREENSHOTS
237 bool next_bold_state = last_bold_state;
251 if (is_unicode && !unicode_ctx) {
254 if (unicode ==
'\n') {
255 if (breakOnLineEnd) {
263 else if (unicode ==
'\b') {
268#ifdef BUILD_SCREENSHOTS
269 next_bold_state =
true;
276#ifdef BUILD_SCREENSHOTS
277 next_bold_state =
false;
282 char_width = getCharWidth(font, unicode, is_unicode);
283#ifdef BUILD_SCREENSHOTS
284 if (char_width != 0) {
286 last_bold_state = next_bold_state;
289 line_width += char_width;
291 if (line_width > max_width) {
292 max_width = line_width;
295#ifdef BUILD_SCREENSHOTS
296 if (line_width != 0) {
299 last_nb_lines = nb_lines;
314 return getTextWidth(fontId, text,
true, 0xFFFF);
328 return getTextWidth(fontId, text,
true, maxLen);
340 return getTextWidth(fontId, text,
false, 0xFFFF);
355 uint16_t textLen = 4;
359 if (is_unicode && !unicode_ctx) {
363 return getCharWidth(font, unicode, is_unicode);
412 uint16_t nbLines = 1;
444 const char *origText = text;
449 else if (*text ==
'\n') {
454 return text - origText;
479 uint32_t lenAtLastDelimiter = 0, widthAtlastDelimiter = 0;
488 uint16_t curTextLen = textLen;
491 if (is_unicode && !unicode_ctx) {
495 if (!unicode || (unicode ==
'\f') || (unicode ==
'\n')) {
496 *len += curTextLen - textLen;
500 else if (unicode ==
'\b') {
511 *len += curTextLen - textLen;
515 char_width = getCharWidth(font, unicode, is_unicode);
516 if (char_width == 0) {
517 *len += curTextLen - textLen;
523 lenAtLastDelimiter = *len;
524 widthAtlastDelimiter = *width;
526 if ((*width + char_width) > maxWidth) {
527 if ((wrapping ==
true) && (widthAtlastDelimiter > 0)) {
528 *len = lenAtLastDelimiter + 1;
529 *width = widthAtlastDelimiter;
533 *len += curTextLen - textLen;
534 *width = *width + char_width;
560 uint16_t textLen = strlen(text);
562 const char *lastDelimiter = NULL;
563 uint32_t lenAtLastDelimiter = 0;
564 const char *origText = text;
565 const char *previousText;
568 while ((textLen) && (maxNbLines > 0)) {
575 if (is_unicode && !unicode_ctx) {
580 lastDelimiter = text;
581 lenAtLastDelimiter = textLen;
585 if (unicode ==
'\n') {
588 if (maxNbLines == 0) {
591 lastDelimiter = NULL;
596 else if (unicode ==
'\f') {
601 else if (unicode ==
'\b') {
615 char_width = getCharWidth(font, unicode, is_unicode);
616 if (char_width == 0) {
620 if ((width + char_width) > maxWidth) {
621 if ((wrapping ==
true) && (lastDelimiter != NULL)) {
622 text = lastDelimiter;
623 lastDelimiter = NULL;
624 textLen = lenAtLastDelimiter;
627 textLen += text - previousText;
636 *len = text - origText;
637 return (maxNbLines == 0);
671 cur_char = text[textLen];
673 if (cur_char ==
'\n') {
679 if ((cur_char < font->first_char) || (cur_char > font->
last_char)) {
684 char_width = character->
width;
686 if ((*width + char_width) > maxWidth) {
690 *width = *width + char_width;
711#ifdef SCREEN_SIZE_NANO
712 uint16_t nbLines = 0;
714 uint16_t nbLines = 1;
716 uint16_t textLen = strlen(text);
717 const char *lastDelimiter = NULL;
718 uint32_t lenAtLastDelimiter = 0;
719 const char *prevText = NULL;
722#ifdef BUILD_SCREENSHOTS
723 hard_caesura =
false;
727 bool next_bold_state = last_bold_state;
739 if (is_unicode && !unicode_ctx) {
745 lastDelimiter = prevText;
746 lenAtLastDelimiter = textLen;
749 if (unicode ==
'\f') {
750#ifdef BUILD_SCREENSHOTS
758 else if (unicode ==
'\n') {
760#if defined(BUILD_SCREENSHOTS) && defined(SCREEN_SIZE_NANO)
761 if (!(nbLines % 4)) {
768 lastDelimiter = NULL;
772 else if (unicode ==
'\b') {
777#ifdef BUILD_SCREENSHOTS
778 next_bold_state =
true;
785#ifdef BUILD_SCREENSHOTS
786 next_bold_state =
false;
792 char_width = getCharWidth(font, unicode, is_unicode);
793 if (char_width == 0) {
796#ifdef BUILD_SCREENSHOTS
798 last_bold_state = next_bold_state;
802 if ((width + char_width) > maxWidth) {
803 if ((wrapping ==
true) && (lastDelimiter != NULL)) {
804 text = lastDelimiter + 1;
805 lastDelimiter = NULL;
806 textLen = lenAtLastDelimiter;
810#ifdef BUILD_SCREENSHOTS
817#if defined(BUILD_SCREENSHOTS) && defined(SCREEN_SIZE_NANO)
818 if (!(nbLines % 4)) {
829#ifdef SCREEN_SIZE_NANO
834#ifdef BUILD_SCREENSHOTS
835 last_nb_lines = nbLines;
852 uint8_t nbLinesPerPage,
857 uint16_t nbLines = 0;
859 uint16_t textLen = strlen(text);
860 const char *lastDelimiter = NULL;
861 uint32_t lenAtLastDelimiter = 0;
862 const char *prevText = NULL;
865#ifdef BUILD_SCREENSHOTS
866 last_nb_lines = nbLines;
867 last_nb_pages = nbPages;
869 bool next_bold_state = last_bold_state;
880 if (is_unicode && !unicode_ctx) {
886 lastDelimiter = prevText;
887 lenAtLastDelimiter = textLen;
890 if (unicode ==
'\f') {
892#ifdef BUILD_SCREENSHOTS
893#ifdef SCREEN_SIZE_NANO
897#ifdef SCREEN_SIZE_NANO
900 if (last_nb_lines < nbLines) {
901 last_nb_lines = nbLines;
909 else if (unicode ==
'\n') {
911#ifdef BUILD_SCREENSHOTS
912 if (last_nb_lines < nbLines) {
913 last_nb_lines = nbLines;
916 if (nbLines == nbLinesPerPage && textLen) {
922 lastDelimiter = NULL;
926 else if (unicode ==
'\b') {
931#ifdef BUILD_SCREENSHOTS
932 next_bold_state =
true;
939#ifdef BUILD_SCREENSHOTS
940 next_bold_state =
false;
946 char_width = getCharWidth(font, unicode, is_unicode);
947 if (char_width == 0) {
951#ifdef BUILD_SCREENSHOTS
953 last_bold_state = next_bold_state;
957 if ((width + char_width) > maxWidth) {
958 if (lastDelimiter != NULL) {
959 text = lastDelimiter + 1;
960 lastDelimiter = NULL;
961 textLen = lenAtLastDelimiter;
968#ifdef BUILD_SCREENSHOTS
969 if (last_nb_lines < nbLines) {
970 last_nb_lines = nbLines;
973 if (nbLines == nbLinesPerPage) {
982#ifdef BUILD_SCREENSHOTS
983#ifdef SCREEN_SIZE_NANO
988 if (last_nb_lines < nbLines) {
989 last_nb_lines = nbLines;
991 last_nb_pages = nbPages;
1030 uint8_t currentNbLines = 1;
1031 char *lastDelimiter = NULL;
1032 uint32_t lenAtLastDelimiter = 0;
1033 char *prevText[4] = {0};
1034 uint16_t prevWidth[2] = {0};
1044 prevText[3] = prevText[2];
1045 prevText[2] = prevText[1];
1046 prevText[1] = prevText[0];
1050 if (unicode ==
'\n') {
1053 lastDelimiter = NULL;
1056 if (is_unicode && !unicode_ctx) {
1059 char_width = getCharWidth(font, unicode, is_unicode);
1060 if (char_width == 0) {
1066 lastDelimiter = prevText[0];
1067 lenAtLastDelimiter = textLen + 1;
1070 if ((width + char_width) > maxWidth) {
1073 if (currentNbLines < nbLines) {
1076 if (lastDelimiter != NULL) {
1077 *lastDelimiter++ =
'\n';
1078 text = lastDelimiter;
1079 lastDelimiter = NULL;
1080 textLen = lenAtLastDelimiter - 1;
1083 textLen += text - prevText[0];
1094 for (i = 0; i < 2; i++) {
1095 if ((prevText[i + 1] != NULL)
1096 && ((prevWidth[i] + (3 * getCharWidth(font,
'.',
false))) <= maxWidth)) {
1101 if (prevText[i + 1] != NULL) {
1102 memcpy(prevText[i + 1],
"...", 4);
1108 prevWidth[1] = prevWidth[0];
1109 prevWidth[0] = width;
1110 width += char_width;
1130 const char *origText,
1134 uint16_t reducedTextLen)
1137 uint16_t textLen = strlen(origText);
1139 uint8_t currentNbLines = 1;
1140 uint32_t i = 0, j = 0;
1143 if ((nbLines & 0x1) == 0) {
1147 while ((i < textLen) && (j < reducedTextLen)) {
1151 curChar = origText[i];
1153 char_width = getCharWidth(font, curChar,
false);
1154 if (char_width == 0) {
1155 reducedText[j] = curChar;
1162 if ((width + char_width) > maxWidth) {
1168 else if ((currentNbLines == ((nbLines / 2) + 1)) && ((width + char_width) > halfWidth)) {
1169 uint32_t halfFullWidth = (nbLines / 2) * maxWidth + halfWidth;
1170 uint32_t countDown = textLen;
1172 reducedText[j - 1] =
'.';
1173 reducedText[j] =
'.';
1174 reducedText[j + 1] =
'.';
1177 while (width < halfFullWidth) {
1179 curChar = origText[countDown];
1180 char_width = getCharWidth(font, curChar,
false);
1181 width += char_width;
1183 memcpy(&reducedText[j + 2], &origText[countDown + 1], textLen - countDown + 1);
1187 reducedText[j] = curChar;
1191 width += char_width;
1193 reducedText[j] =
'\0';
1206 if (os_sched_current_task() != TASK_BOLOS_UX) {
1209 if ((unicodeCtx.
font != NULL) && (unicodeCtx.
font->
font_id == fontId)) {
1213 const uint8_t *ptr = (
const uint8_t *) language_pack;
1217 unicodeCtx.
bitmap = (
const uint8_t *) (ptr + language_pack->bitmaps_offset);
1219 for (uint32_t i = 0; i < language_pack->nb_fonts; i++) {
1220 if (font->
font_id == fontId) {
1222 unicodeCtx.
font = font;
1229 unicodeCtx.
characters += language_pack->nb_characters;
1248 if (os_sched_current_task() != TASK_BOLOS_UX) {
1253 uint32_t n = language_pack->nb_characters;
1254 if (characters == NULL) {
1259 for (
unsigned i = 0; i < n - 1; i++, characters++) {
1263 = (characters + 1)->bitmap_offset - characters->
bitmap_offset;
1285 if (os_sched_current_task() != TASK_BOLOS_UX) {
1301 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...
const nbgl_font_unicode_character_t * nbgl_getUnicodeFontCharacter(uint32_t unicode)
Get the unicode character object matching the given unicode (a unicode character is encoded on max of...
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
void nbgl_refreshUnicodeFont(const LANGUAGE_PACK *lp)
Function to be called when a change on the current language pack is notified by the OS,...
nbgl_unicode_ctx_t * nbgl_getUnicodeFont(nbgl_font_id_e fontId)
Get the font entry for the given font id (sparse font array support)
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.
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,...
uint32_t nbgl_getUnicodeFontCharacterByteCount(void)
Get the bitmap byte count of the latest used unicode character. (the one returned by nbgl_getUnicodeF...
__attribute__((section("._nbgl_fonts_"))) const
return the non-unicode font corresponding to the given font ID
@ BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp
@ BAGL_FONT_OPEN_SANS_EXTRABOLD_11px_1bpp
const nbgl_font_t * nbgl_getFont(nbgl_font_id_e fontId)
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 over_previous
flag set to 1 when displayed over previous char
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 height
height of all characters in pixels
uint8_t font_id
ID of the font, from nbgl_font_id_e.
uint8_t line_height
height of a line for all characters in pixels
const nbgl_font_unicode_character_t * characters
const nbgl_font_unicode_t * font
uint32_t unicode_character_byte_count