diff --git a/core/SConscript.boardloader b/core/SConscript.boardloader index ff8b5d2bcc..e4b548bd46 100644 --- a/core/SConscript.boardloader +++ b/core/SConscript.boardloader @@ -50,7 +50,8 @@ CPPPATH_MOD += [ ] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/font_bitmap.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', + 'embed/extmod/modtrezorui/fonts/font_bitmap.c', ] SOURCE_STMHAL = [ diff --git a/core/SConscript.bootloader b/core/SConscript.bootloader index edf4da926f..d5471ddb9c 100644 --- a/core/SConscript.bootloader +++ b/core/SConscript.bootloader @@ -1,6 +1,7 @@ # pylint: disable=E0602 import os +import tools TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T') @@ -21,6 +22,17 @@ CPPPATH_MOD = [] CPPDEFINES_MOD = [] SOURCE_MOD = [] +if TREZOR_MODEL in ('1', 'R'): + FONT_NORMAL='Font_PixelOperator_Regular_8' + FONT_MEDIUM=None + FONT_BOLD=None + FONT_MONO='Font_PixelOperatorMono_Regular_8' +if TREZOR_MODEL in ('T', ): + FONT_NORMAL='Font_Roboto_Regular_20' + FONT_MEDIUM=None + FONT_BOLD=None + FONT_MONO='Font_RobotoMono_Regular_20' + # modtrezorcrypto CCFLAGS_MOD += '-Wno-sequence-point ' CPPPATH_MOD += [ @@ -51,18 +63,11 @@ SOURCE_MOD += [ CPPPATH_MOD += [ 'vendor/micropython/lib/uzlib', ] -CPPDEFINES_MOD += [ - 'TREZOR_FONT_NORMAL_ENABLE', - 'TREZOR_FONT_MONO_ENABLE', -] + SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/font_bitmap.c', - 'embed/extmod/modtrezorui/font_roboto_regular_20.c', - 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', - 'embed/extmod/modtrezorui/font_pixeloperator_bold_8.c', - 'embed/extmod/modtrezorui/font_pixeloperator_regular_8.c', - 'embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', + 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', 'vendor/micropython/lib/uzlib/tinflate.c', @@ -131,6 +136,13 @@ if TREZOR_MODEL in ('T',): SOURCE_TREZORHAL.append('embed/trezorhal/touch.c') +# fonts +tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MEDIUM', FONT_MEDIUM, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD) + + env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'))) env.Replace( diff --git a/core/SConscript.bootloader_ci b/core/SConscript.bootloader_ci index 6c358cee71..5807c26b5c 100644 --- a/core/SConscript.bootloader_ci +++ b/core/SConscript.bootloader_ci @@ -1,6 +1,7 @@ # pylint: disable=E0602 import os +import tools TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T') @@ -21,6 +22,17 @@ CPPPATH_MOD = [] CPPDEFINES_MOD = [] SOURCE_MOD = [] +if TREZOR_MODEL in ('1', 'R'): + FONT_NORMAL='Font_PixelOperator_Regular_8' + FONT_MEDIUM=None + FONT_BOLD=None + FONT_MONO='Font_PixelOperatorMono_Regular_8' +if TREZOR_MODEL in ('T', ): + FONT_NORMAL='Font_Roboto_Regular_20' + FONT_MEDIUM=None + FONT_BOLD=None + FONT_MONO='Font_RobotoMono_Regular_20' + # modtrezorcrypto CCFLAGS_MOD += '-Wno-sequence-point ' CPPPATH_MOD += [ @@ -51,15 +63,10 @@ SOURCE_MOD += [ CPPPATH_MOD += [ 'vendor/micropython/lib/uzlib', ] -CPPDEFINES_MOD += [ - 'TREZOR_FONT_NORMAL_ENABLE', - 'TREZOR_FONT_MONO_ENABLE', -] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/font_bitmap.c', - 'embed/extmod/modtrezorui/font_roboto_regular_20.c', - 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', + 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', 'vendor/micropython/lib/uzlib/tinflate.c', @@ -121,6 +128,12 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/vectortable.s', ] +# fonts +tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MEDIUM', FONT_MEDIUM, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD) + env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'))) env.Replace( diff --git a/core/SConscript.firmware b/core/SConscript.firmware index 96ffec17d1..3308220803 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -2,6 +2,7 @@ # fmt: off import os +import tools BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0') EVERYTHING = BITCOIN_ONLY != '1' @@ -22,6 +23,23 @@ SOURCE_MOD = [] PYOPT = ARGUMENTS.get('PYOPT', '1') FROZEN = True +if TREZOR_MODEL in ('1', 'R'): + FONT_NORMAL='Font_PixelOperator_Regular_8' + FONT_MEDIUM='Font_PixelOperator_Regular_8' + FONT_BOLD='Font_PixelOperator_Bold_8' + FONT_MONO='Font_PixelOperatorMono_Regular_8' +if TREZOR_MODEL in ('T', ): + if UI2: + FONT_NORMAL='Font_TTHoves_Regular_18' + FONT_MEDIUM='Font_TTHoves_Medium_20' + FONT_BOLD='Font_TTHoves_Bold_16' + FONT_MONO='Font_RobotoMono_Regular_20' + else: + FONT_NORMAL='Font_Roboto_Regular_20' + FONT_MEDIUM=None + FONT_BOLD='Font_Roboto_Bold_20' + FONT_MONO='Font_RobotoMono_Regular_20' + # modtrezorconfig CPPPATH_MOD += [ 'embed/extmod/modtrezorconfig', @@ -158,20 +176,10 @@ if TREZOR_MODEL in ('T',): CPPPATH_MOD += [ 'vendor/micropython/lib/uzlib', ] -CPPDEFINES_MOD += [ - 'TREZOR_FONT_BOLD_ENABLE', - 'TREZOR_FONT_NORMAL_ENABLE', - 'TREZOR_FONT_MONO_ENABLE', -] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/font_bitmap.c', - 'embed/extmod/modtrezorui/font_roboto_bold_20.c', - 'embed/extmod/modtrezorui/font_roboto_regular_20.c', - 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', - 'embed/extmod/modtrezorui/font_pixeloperator_bold_8.c', - 'embed/extmod/modtrezorui/font_pixeloperator_regular_8.c', - 'embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', + 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'embed/extmod/modtrezorui/modtrezorui.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', 'vendor/micropython/lib/uzlib/adler32.c', @@ -180,14 +188,8 @@ SOURCE_MOD += [ ] if UI2: CPPDEFINES_MOD += [ - 'TREZOR_FONT_MEDIUM_ENABLE', 'TREZOR_UI2', ] - SOURCE_MOD += [ - 'embed/extmod/modtrezorui/font_tthoves_bold_16.c', - 'embed/extmod/modtrezorui/font_tthoves_medium_20.c', - 'embed/extmod/modtrezorui/font_tthoves_regular_18.c', - ] # modtrezorutils SOURCE_MOD += [ @@ -415,6 +417,12 @@ if FEATURE_FLAGS["SYSTEM_VIEW"]: CPPDEFINES_MOD += ['SYSTEM_VIEW'] CCFLAGS_MOD += '-DSYSTEM_VIEW ' +# fonts +tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MEDIUM', FONT_MEDIUM, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD) + SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_MICROPYTHON_SPEED env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s -DPYOPT=%s -DBITCOIN_ONLY=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'), PYOPT, BITCOIN_ONLY)) diff --git a/core/SConscript.prodtest b/core/SConscript.prodtest index 081de7bde4..dc0226ca27 100644 --- a/core/SConscript.prodtest +++ b/core/SConscript.prodtest @@ -1,6 +1,7 @@ # pylint: disable=E0602 import os +import tools TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T') @@ -9,6 +10,17 @@ CPPPATH_MOD = [] CPPDEFINES_MOD = [] SOURCE_MOD = [] +if TREZOR_MODEL in ('1', 'R'): + FONT_NORMAL=None + FONT_MEDIUM=None + FONT_BOLD='Font_PixelOperator_Bold_8' + FONT_MONO=None +if TREZOR_MODEL in ('T', ): + FONT_NORMAL=None + FONT_MEDIUM=None + FONT_BOLD='Font_Roboto_Bold_20' + FONT_MONO=None + # modtrezorcrypto CPPPATH_MOD += [ 'vendor/trezor-crypto', @@ -25,14 +37,11 @@ SOURCE_MOD += [ CPPPATH_MOD += [ 'vendor/micropython/lib/uzlib', ] -CPPDEFINES_MOD += [ - 'TREZOR_FONT_BOLD_ENABLE', -] + SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/font_bitmap.c', - 'embed/extmod/modtrezorui/font_roboto_bold_20.c', - 'embed/extmod/modtrezorui/font_pixeloperator_bold_8.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', + 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', @@ -88,6 +97,12 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/vectortable.s', ] +# fonts +tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MEDIUM', FONT_MEDIUM, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD) + env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'))) env.Replace( diff --git a/core/SConscript.reflash b/core/SConscript.reflash index 98d44f7d90..774aebe99a 100644 --- a/core/SConscript.reflash +++ b/core/SConscript.reflash @@ -1,6 +1,7 @@ # pylint: disable=E0602 import os +import tools TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T') @@ -9,6 +10,17 @@ CPPPATH_MOD = [] CPPDEFINES_MOD = [] SOURCE_MOD = [] +if TREZOR_MODEL in ('1', 'R'): + FONT_NORMAL=None + FONT_MEDIUM=None + FONT_BOLD='Font_PixelOperator_Bold_8' + FONT_MONO=None +if TREZOR_MODEL in ('T', ): + FONT_NORMAL=None + FONT_MEDIUM=None + FONT_BOLD='Font_Roboto_Bold_20' + FONT_MONO=None + # modtrezorcrypto CPPPATH_MOD += [ 'vendor/trezor-crypto', @@ -21,13 +33,10 @@ SOURCE_MOD += [ CPPPATH_MOD += [ 'vendor/micropython/lib/uzlib', ] -CPPDEFINES_MOD += [ - 'TREZOR_FONT_BOLD_ENABLE', -] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', 'embed/extmod/modtrezorui/font_bitmap.c', - 'embed/extmod/modtrezorui/font_roboto_bold_20.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', 'vendor/micropython/lib/uzlib/tinflate.c', @@ -81,6 +90,12 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/vectortable.s', ] +# fonts +tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MEDIUM', FONT_MEDIUM, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD) + env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'))) env.Replace( diff --git a/core/SConscript.unix b/core/SConscript.unix index d129adcd8b..8b101f18db 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -2,6 +2,7 @@ # fmt: off import os +import tools BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0') EVERYTHING = BITCOIN_ONLY != '1' @@ -23,6 +24,23 @@ PYOPT = ARGUMENTS.get('PYOPT', '1') FROZEN = ARGUMENTS.get('TREZOR_EMULATOR_FROZEN', 0) RASPI = os.getenv('TREZOR_EMULATOR_RASPI') == '1' +if TREZOR_MODEL in ('1', 'R'): + FONT_NORMAL='Font_PixelOperator_Regular_8' + FONT_MEDIUM='Font_PixelOperator_Regular_8' + FONT_BOLD='Font_PixelOperator_Bold_8' + FONT_MONO='Font_PixelOperatorMono_Regular_8' +if TREZOR_MODEL in ('T', ): + if UI2: + FONT_NORMAL='Font_TTHoves_Regular_18' + FONT_MEDIUM='Font_TTHoves_Medium_20' + FONT_BOLD='Font_TTHoves_Bold_16' + FONT_MONO='Font_RobotoMono_Regular_20' + else: + FONT_NORMAL='Font_Roboto_Regular_20' + FONT_MEDIUM=None + FONT_BOLD='Font_Roboto_Bold_20' + FONT_MONO='Font_RobotoMono_Regular_20' + # modtrezorconfig CPPPATH_MOD += [ 'embed/extmod/modtrezorconfig', @@ -154,20 +172,10 @@ if TREZOR_MODEL in ('T',): CPPPATH_MOD += [ 'vendor/micropython/lib/uzlib', ] -CPPDEFINES_MOD += [ - 'TREZOR_FONT_BOLD_ENABLE', - 'TREZOR_FONT_NORMAL_ENABLE', - 'TREZOR_FONT_MONO_ENABLE', -] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/font_bitmap.c', - 'embed/extmod/modtrezorui/font_roboto_bold_20.c', - 'embed/extmod/modtrezorui/font_roboto_regular_20.c', - 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', - 'embed/extmod/modtrezorui/font_pixeloperator_bold_8.c', - 'embed/extmod/modtrezorui/font_pixeloperator_regular_8.c', - 'embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.c', + 'embed/extmod/modtrezorui/fonts/fonts.c', + 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'embed/extmod/modtrezorui/modtrezorui.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', 'vendor/micropython/lib/uzlib/adler32.c', @@ -176,14 +184,8 @@ SOURCE_MOD += [ ] if UI2: CPPDEFINES_MOD += [ - 'TREZOR_FONT_MEDIUM_ENABLE', 'TREZOR_UI2', ] - SOURCE_MOD += [ - 'embed/extmod/modtrezorui/font_tthoves_bold_16.c', - 'embed/extmod/modtrezorui/font_tthoves_medium_20.c', - 'embed/extmod/modtrezorui/font_tthoves_regular_18.c', - ] if FROZEN: CPPDEFINES_MOD += ['TREZOR_EMULATOR_FROZEN'] if RASPI: @@ -356,6 +358,13 @@ if TREZOR_MODEL in ('T',): 'embed/unix/sdcard.c', ] +# fonts +tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MEDIUM', FONT_MEDIUM, CPPDEFINES_MOD, SOURCE_MOD) +tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD) + + SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_UNIX if PYOPT == '0' or not FROZEN: diff --git a/core/embed/extmod/modtrezorui/display.c b/core/embed/extmod/modtrezorui/display.c index d6fc8c2361..979303128a 100644 --- a/core/embed/extmod/modtrezorui/display.c +++ b/core/embed/extmod/modtrezorui/display.c @@ -26,81 +26,7 @@ #include "common.h" #include "display.h" -#include "font_bitmap.h" - -#if defined TREZOR_MODEL_T - -// TT new rust UI -#if TREZOR_UI2 - -#ifdef TREZOR_FONT_NORMAL_ENABLE -#include "font_tthoves_regular_18.h" -#define FONT_NORMAL_DATA Font_TTHoves_Regular_18 -#define FONT_NORMAL_HEIGHT 18 -#endif -#ifdef TREZOR_FONT_MEDIUM_ENABLE -#include "font_tthoves_medium_20.h" -#define FONT_MEDIUM_DATA Font_TTHoves_Medium_20 -#define FONT_MEDIUM_HEIGHT 20 -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE -#include "font_tthoves_bold_16.h" -#define FONT_BOLD_DATA Font_TTHoves_Bold_16 -#define FONT_BOLD_HEIGHT 16 -#endif -#ifdef TREZOR_FONT_MONO_ENABLE -#include "font_robotomono_regular_20.h" -#define FONT_MONO_DATA Font_RobotoMono_Regular_20 -#define FONT_MONO_HEIGHT 20 -#endif - -// TT old python UI -#else - -#ifdef TREZOR_FONT_NORMAL_ENABLE -#include "font_roboto_regular_20.h" -#define FONT_NORMAL_DATA Font_Roboto_Regular_20 -#define FONT_NORMAL_HEIGHT 20 -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE -#include "font_roboto_bold_20.h" -#define FONT_BOLD_DATA Font_Roboto_Bold_20 -#define FONT_BOLD_HEIGHT 20 -#endif -#ifdef TREZOR_FONT_MONO_ENABLE -#include "font_robotomono_regular_20.h" -#define FONT_MONO_DATA Font_RobotoMono_Regular_20 -#define FONT_MONO_HEIGHT 20 -#endif - -#endif - -#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R - -#ifdef TREZOR_FONT_NORMAL_ENABLE -#include "font_pixeloperator_regular_8.h" -#define FONT_NORMAL_DATA Font_PixelOperator_Regular_8 -#define FONT_NORMAL_HEIGHT 8 -#endif -#ifdef TREZOR_FONT_MEDIUM_ENABLE -#include "font_pixeloperator_regular_8.h" -#define FONT_MEDIUM_DATA Font_PixelOperator_Regular_8 -#define FONT_MEDIUM_HEIGHT 8 -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE -#include "font_pixeloperator_bold_8.h" -#define FONT_BOLD_DATA Font_PixelOperator_Bold_8 -#define FONT_BOLD_HEIGHT 8 -#endif -#ifdef TREZOR_FONT_MONO_ENABLE -#include "font_pixeloperatormono_regular_8.h" -#define FONT_MONO_DATA Font_PixelOperatorMono_Regular_8 -#define FONT_MONO_HEIGHT 8 -#endif - -#else -#error Unknown Trezor model -#endif +#include "fonts/fonts.h" #include #include @@ -634,91 +560,6 @@ void display_printf(const char *fmt, ...) { #endif // TREZOR_PRINT_DISABLE -static uint8_t convert_char(const uint8_t c) { - static char last_was_utf8 = 0; - - // non-printable ASCII character - if (c < ' ') { - last_was_utf8 = 0; - return 0x7F; - } - - // regular ASCII character - if (c < 0x80) { - last_was_utf8 = 0; - return c; - } - - // UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Encoding - - // bytes 11xxxxxx are first bytes of UTF-8 characters - if (c >= 0xC0) { - last_was_utf8 = 1; - return 0x7F; - } - - if (last_was_utf8) { - // bytes 10xxxxxx can be successive UTF-8 characters ... - return 0; // skip glyph - } else { - // ... or they are just non-printable ASCII characters - return 0x7F; - } - - return 0; -} - -const uint8_t *display_get_glyph(int font, uint8_t c) { - c = convert_char(c); - if (!c) return 0; - - // printable ASCII character - if (c >= ' ' && c < 0x7F) { - switch (font) { -#ifdef TREZOR_FONT_NORMAL_ENABLE - case FONT_NORMAL: - return FONT_NORMAL_DATA[c - ' ']; -#endif -#ifdef TREZOR_FONT_MEDIUM_ENABLE - case FONT_MEDIUM: - return FONT_MEDIUM_DATA[c - ' ']; -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE - case FONT_BOLD: - return FONT_BOLD_DATA[c - ' ']; -#endif -#ifdef TREZOR_FONT_MONO_ENABLE - case FONT_MONO: - return FONT_MONO_DATA[c - ' ']; -#endif - } - return 0; - } - -// non-printable character -#define PASTER(s) s##_glyph_nonprintable -#define NONPRINTABLE_GLYPH(s) PASTER(s) - - switch (font) { -#ifdef TREZOR_FONT_NORMAL_ENABLE - case FONT_NORMAL: - return NONPRINTABLE_GLYPH(FONT_NORMAL_DATA); -#endif -#ifdef TREZOR_FONT_MEDIUM_ENABLE - case FONT_MEDIUM: - return NONPRINTABLE_GLYPH(FONT_MEDIUM_DATA); -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE - case FONT_BOLD: - return NONPRINTABLE_GLYPH(FONT_BOLD_DATA); -#endif -#ifdef TREZOR_FONT_MONO_ENABLE - case FONT_MONO: - return NONPRINTABLE_GLYPH(FONT_MONO_DATA); -#endif - } - return 0; -} static void display_text_render(int x, int y, const char *text, int textlen, int font, uint16_t fgcolor, uint16_t bgcolor) { @@ -732,7 +573,7 @@ static void display_text_render(int x, int y, const char *text, int textlen, // render glyphs for (int i = 0; i < textlen; i++) { - const uint8_t *g = display_get_glyph(font, (uint8_t)text[i]); + const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]); if (!g) continue; const uint8_t w = g[0]; // width const uint8_t h = g[1]; // height @@ -801,7 +642,7 @@ int display_text_width(const char *text, int textlen, int font) { textlen = strlen(text); } for (int i = 0; i < textlen; i++) { - const uint8_t *g = display_get_glyph(font, (uint8_t)text[i]); + const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]); if (!g) continue; const uint8_t adv = g[2]; // advance width += adv; @@ -833,7 +674,7 @@ int display_text_split(const char *text, int textlen, int font, if (text[i] == ' ') { lastspace = i; } - const uint8_t *g = display_get_glyph(font, (uint8_t)text[i]); + const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]); if (!g) continue; const uint8_t adv = g[2]; // advance width += adv; @@ -848,28 +689,6 @@ int display_text_split(const char *text, int textlen, int font, return textlen; } -int display_text_height(int font) { - switch (font) { -#ifdef TREZOR_FONT_NORMAL_ENABLE - case FONT_NORMAL: - return FONT_NORMAL_HEIGHT; -#endif -#ifdef TREZOR_FONT_MEDIUM_ENABLE - case FONT_MEDIUM: - return FONT_MEDIUM_HEIGHT; -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE - case FONT_BOLD: - return FONT_BOLD_HEIGHT; -#endif -#ifdef TREZOR_FONT_MONO_ENABLE - case FONT_MONO: - return FONT_MONO_HEIGHT; -#endif - } - return 0; -} - #define QR_MAX_VERSION 9 void display_qrcode(int x, int y, const char *data, uint8_t scale) { diff --git a/core/embed/extmod/modtrezorui/display.h b/core/embed/extmod/modtrezorui/display.h index 7a9455c149..bfcf4ad17a 100644 --- a/core/embed/extmod/modtrezorui/display.h +++ b/core/embed/extmod/modtrezorui/display.h @@ -24,6 +24,7 @@ #include #include +#include "fonts/fonts.h" #if defined TREZOR_MODEL_T // ILI9341V, GC9307 and ST7789V drivers support 240px x 320px display resolution @@ -69,19 +70,6 @@ #define COLOR_WHITE 0xFFFF #define COLOR_BLACK 0x0000 -#ifdef TREZOR_FONT_NORMAL_ENABLE -#define FONT_NORMAL (-1) -#endif -#ifdef TREZOR_FONT_MEDIUM_ENABLE -#define FONT_MEDIUM (-5) -#endif -#ifdef TREZOR_FONT_BOLD_ENABLE -#define FONT_BOLD (-2) -#endif -#ifdef TREZOR_FONT_MONO_ENABLE -#define FONT_MONO (-3) -#endif - // provided by port void display_init(void); @@ -126,7 +114,6 @@ void display_text_right(int x, int y, const char *text, int textlen, int font, int display_text_width(const char *text, int textlen, int font); int display_text_split(const char *text, int textlen, int font, int requested_width); -int display_text_height(int font); void display_qrcode(int x, int y, const char *data, uint8_t scale); @@ -144,8 +131,6 @@ void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void display_pixeldata(uint16_t c); void display_pixeldata_dirty(); -const uint8_t *display_get_glyph(int font, uint8_t c); - #if !(defined EMULATOR) && (defined TREZOR_MODEL_T) extern volatile uint8_t *const DISPLAY_CMD_ADDRESS; extern volatile uint8_t *const DISPLAY_DATA_ADDRESS; diff --git a/core/embed/extmod/modtrezorui/font_bitmap.c b/core/embed/extmod/modtrezorui/fonts/font_bitmap.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_bitmap.c rename to core/embed/extmod/modtrezorui/fonts/font_bitmap.c diff --git a/core/embed/extmod/modtrezorui/font_bitmap.h b/core/embed/extmod/modtrezorui/fonts/font_bitmap.h similarity index 100% rename from core/embed/extmod/modtrezorui/font_bitmap.h rename to core/embed/extmod/modtrezorui/fonts/font_bitmap.h diff --git a/core/embed/extmod/modtrezorui/font_pixeloperator_bold_8.c b/core/embed/extmod/modtrezorui/fonts/font_pixeloperator_bold_8.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_pixeloperator_bold_8.c rename to core/embed/extmod/modtrezorui/fonts/font_pixeloperator_bold_8.c diff --git a/core/embed/extmod/modtrezorui/font_pixeloperator_bold_8.h b/core/embed/extmod/modtrezorui/fonts/font_pixeloperator_bold_8.h similarity index 63% rename from core/embed/extmod/modtrezorui/font_pixeloperator_bold_8.h rename to core/embed/extmod/modtrezorui/fonts/font_pixeloperator_bold_8.h index ce3f121819..198ebd4a43 100644 --- a/core/embed/extmod/modtrezorui/font_pixeloperator_bold_8.h +++ b/core/embed/extmod/modtrezorui/fonts/font_pixeloperator_bold_8.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 1 #error Wrong TREZOR_FONT_BPP (expected 1) #endif +#define Font_PixelOperator_Bold_8_HEIGHT 8 +#define Font_PixelOperator_Bold_8_MAX_HEIGHT 8 +#define Font_PixelOperator_Bold_8_BASELINE 1 extern const uint8_t* const Font_PixelOperator_Bold_8[126 + 1 - 32]; extern const uint8_t Font_PixelOperator_Bold_8_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_pixeloperator_regular_8.c b/core/embed/extmod/modtrezorui/fonts/font_pixeloperator_regular_8.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_pixeloperator_regular_8.c rename to core/embed/extmod/modtrezorui/fonts/font_pixeloperator_regular_8.c diff --git a/core/embed/extmod/modtrezorui/font_pixeloperator_regular_8.h b/core/embed/extmod/modtrezorui/fonts/font_pixeloperator_regular_8.h similarity index 62% rename from core/embed/extmod/modtrezorui/font_pixeloperator_regular_8.h rename to core/embed/extmod/modtrezorui/fonts/font_pixeloperator_regular_8.h index d7233cf5dd..ddd01b0ee7 100644 --- a/core/embed/extmod/modtrezorui/font_pixeloperator_regular_8.h +++ b/core/embed/extmod/modtrezorui/fonts/font_pixeloperator_regular_8.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 1 #error Wrong TREZOR_FONT_BPP (expected 1) #endif +#define Font_PixelOperator_Regular_8_HEIGHT 8 +#define Font_PixelOperator_Regular_8_MAX_HEIGHT 8 +#define Font_PixelOperator_Regular_8_BASELINE 1 extern const uint8_t* const Font_PixelOperator_Regular_8[126 + 1 - 32]; extern const uint8_t Font_PixelOperator_Regular_8_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.c b/core/embed/extmod/modtrezorui/fonts/font_pixeloperatormono_regular_8.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.c rename to core/embed/extmod/modtrezorui/fonts/font_pixeloperatormono_regular_8.c diff --git a/core/embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.h b/core/embed/extmod/modtrezorui/fonts/font_pixeloperatormono_regular_8.h similarity index 61% rename from core/embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.h rename to core/embed/extmod/modtrezorui/fonts/font_pixeloperatormono_regular_8.h index e6a82988a2..27a730c8bf 100644 --- a/core/embed/extmod/modtrezorui/font_pixeloperatormono_regular_8.h +++ b/core/embed/extmod/modtrezorui/fonts/font_pixeloperatormono_regular_8.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 1 #error Wrong TREZOR_FONT_BPP (expected 1) #endif +#define Font_PixelOperatorMono_Regular_8_HEIGHT 8 +#define Font_PixelOperatorMono_Regular_8_MAX_HEIGHT 8 +#define Font_PixelOperatorMono_Regular_8_BASELINE 1 extern const uint8_t* const Font_PixelOperatorMono_Regular_8[126 + 1 - 32]; extern const uint8_t Font_PixelOperatorMono_Regular_8_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_roboto_bold_20.c b/core/embed/extmod/modtrezorui/fonts/font_roboto_bold_20.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_roboto_bold_20.c rename to core/embed/extmod/modtrezorui/fonts/font_roboto_bold_20.c diff --git a/core/embed/extmod/modtrezorui/font_roboto_bold_20.h b/core/embed/extmod/modtrezorui/fonts/font_roboto_bold_20.h similarity index 65% rename from core/embed/extmod/modtrezorui/font_roboto_bold_20.h rename to core/embed/extmod/modtrezorui/fonts/font_roboto_bold_20.h index c04432fcd8..3e6093001e 100644 --- a/core/embed/extmod/modtrezorui/font_roboto_bold_20.h +++ b/core/embed/extmod/modtrezorui/fonts/font_roboto_bold_20.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 4 #error Wrong TREZOR_FONT_BPP (expected 4) #endif +#define Font_Roboto_Bold_20_HEIGHT 20 +#define Font_Roboto_Bold_20_MAX_HEIGHT 22 +#define Font_Roboto_Bold_20_BASELINE 5 extern const uint8_t* const Font_Roboto_Bold_20[126 + 1 - 32]; extern const uint8_t Font_Roboto_Bold_20_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_roboto_regular_20.c b/core/embed/extmod/modtrezorui/fonts/font_roboto_regular_20.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_roboto_regular_20.c rename to core/embed/extmod/modtrezorui/fonts/font_roboto_regular_20.c diff --git a/core/embed/extmod/modtrezorui/font_roboto_regular_20.h b/core/embed/extmod/modtrezorui/fonts/font_roboto_regular_20.h similarity index 63% rename from core/embed/extmod/modtrezorui/font_roboto_regular_20.h rename to core/embed/extmod/modtrezorui/fonts/font_roboto_regular_20.h index a651b9a6b7..ac18ed1f33 100644 --- a/core/embed/extmod/modtrezorui/font_roboto_regular_20.h +++ b/core/embed/extmod/modtrezorui/fonts/font_roboto_regular_20.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 4 #error Wrong TREZOR_FONT_BPP (expected 4) #endif +#define Font_Roboto_Regular_20_HEIGHT 20 +#define Font_Roboto_Regular_20_MAX_HEIGHT 22 +#define Font_Roboto_Regular_20_BASELINE 5 extern const uint8_t* const Font_Roboto_Regular_20[126 + 1 - 32]; extern const uint8_t Font_Roboto_Regular_20_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_robotomono_regular_20.c b/core/embed/extmod/modtrezorui/fonts/font_robotomono_regular_20.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_robotomono_regular_20.c rename to core/embed/extmod/modtrezorui/fonts/font_robotomono_regular_20.c diff --git a/core/embed/extmod/modtrezorui/font_robotomono_regular_20.h b/core/embed/extmod/modtrezorui/fonts/font_robotomono_regular_20.h similarity index 62% rename from core/embed/extmod/modtrezorui/font_robotomono_regular_20.h rename to core/embed/extmod/modtrezorui/fonts/font_robotomono_regular_20.h index d6af8eec08..94a5b273e7 100644 --- a/core/embed/extmod/modtrezorui/font_robotomono_regular_20.h +++ b/core/embed/extmod/modtrezorui/fonts/font_robotomono_regular_20.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 4 #error Wrong TREZOR_FONT_BPP (expected 4) #endif +#define Font_RobotoMono_Regular_20_HEIGHT 20 +#define Font_RobotoMono_Regular_20_MAX_HEIGHT 22 +#define Font_RobotoMono_Regular_20_BASELINE 5 extern const uint8_t* const Font_RobotoMono_Regular_20[126 + 1 - 32]; extern const uint8_t Font_RobotoMono_Regular_20_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_tthoves_bold_16.c b/core/embed/extmod/modtrezorui/fonts/font_tthoves_bold_16.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_tthoves_bold_16.c rename to core/embed/extmod/modtrezorui/fonts/font_tthoves_bold_16.c diff --git a/core/embed/extmod/modtrezorui/font_tthoves_bold_16.h b/core/embed/extmod/modtrezorui/fonts/font_tthoves_bold_16.h similarity index 64% rename from core/embed/extmod/modtrezorui/font_tthoves_bold_16.h rename to core/embed/extmod/modtrezorui/fonts/font_tthoves_bold_16.h index 467402d605..1182b0cef7 100644 --- a/core/embed/extmod/modtrezorui/font_tthoves_bold_16.h +++ b/core/embed/extmod/modtrezorui/fonts/font_tthoves_bold_16.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 4 #error Wrong TREZOR_FONT_BPP (expected 4) #endif +#define Font_TTHoves_Bold_16_HEIGHT 16 +#define Font_TTHoves_Bold_16_MAX_HEIGHT 17 +#define Font_TTHoves_Bold_16_BASELINE 4 extern const uint8_t* const Font_TTHoves_Bold_16[126 + 1 - 32]; extern const uint8_t Font_TTHoves_Bold_16_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_tthoves_medium_20.c b/core/embed/extmod/modtrezorui/fonts/font_tthoves_medium_20.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_tthoves_medium_20.c rename to core/embed/extmod/modtrezorui/fonts/font_tthoves_medium_20.c diff --git a/core/embed/extmod/modtrezorui/font_tthoves_medium_20.h b/core/embed/extmod/modtrezorui/fonts/font_tthoves_medium_20.h similarity index 63% rename from core/embed/extmod/modtrezorui/font_tthoves_medium_20.h rename to core/embed/extmod/modtrezorui/fonts/font_tthoves_medium_20.h index aa0684106f..457d66a7de 100644 --- a/core/embed/extmod/modtrezorui/font_tthoves_medium_20.h +++ b/core/embed/extmod/modtrezorui/fonts/font_tthoves_medium_20.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 4 #error Wrong TREZOR_FONT_BPP (expected 4) #endif +#define Font_TTHoves_Medium_20_HEIGHT 20 +#define Font_TTHoves_Medium_20_MAX_HEIGHT 20 +#define Font_TTHoves_Medium_20_BASELINE 4 extern const uint8_t* const Font_TTHoves_Medium_20[126 + 1 - 32]; extern const uint8_t Font_TTHoves_Medium_20_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/font_tthoves_regular_18.c b/core/embed/extmod/modtrezorui/fonts/font_tthoves_regular_18.c similarity index 100% rename from core/embed/extmod/modtrezorui/font_tthoves_regular_18.c rename to core/embed/extmod/modtrezorui/fonts/font_tthoves_regular_18.c diff --git a/core/embed/extmod/modtrezorui/font_tthoves_regular_18.h b/core/embed/extmod/modtrezorui/fonts/font_tthoves_regular_18.h similarity index 63% rename from core/embed/extmod/modtrezorui/font_tthoves_regular_18.h rename to core/embed/extmod/modtrezorui/fonts/font_tthoves_regular_18.h index fecb2840ec..f110d411ba 100644 --- a/core/embed/extmod/modtrezorui/font_tthoves_regular_18.h +++ b/core/embed/extmod/modtrezorui/fonts/font_tthoves_regular_18.h @@ -3,5 +3,8 @@ #if TREZOR_FONT_BPP != 4 #error Wrong TREZOR_FONT_BPP (expected 4) #endif +#define Font_TTHoves_Regular_18_HEIGHT 18 +#define Font_TTHoves_Regular_18_MAX_HEIGHT 19 +#define Font_TTHoves_Regular_18_BASELINE 4 extern const uint8_t* const Font_TTHoves_Regular_18[126 + 1 - 32]; extern const uint8_t Font_TTHoves_Regular_18_glyph_nonprintable[]; diff --git a/core/embed/extmod/modtrezorui/fonts/fonts.c b/core/embed/extmod/modtrezorui/fonts/fonts.c new file mode 100644 index 0000000000..66fec6c17a --- /dev/null +++ b/core/embed/extmod/modtrezorui/fonts/fonts.c @@ -0,0 +1,170 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fonts.h" + +static uint8_t convert_char(const uint8_t c) { + static char last_was_utf8 = 0; + + // non-printable ASCII character + if (c < ' ') { + last_was_utf8 = 0; + return 0x7F; + } + + // regular ASCII character + if (c < 0x80) { + last_was_utf8 = 0; + return c; + } + + // UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Encoding + + // bytes 11xxxxxx are first bytes of UTF-8 characters + if (c >= 0xC0) { + last_was_utf8 = 1; + return 0x7F; + } + + if (last_was_utf8) { + // bytes 10xxxxxx can be successive UTF-8 characters ... + return 0; // skip glyph + } else { + // ... or they are just non-printable ASCII characters + return 0x7F; + } +} + +int font_height(int font) { + switch (font) { +#ifdef TREZOR_FONT_NORMAL_ENABLE + case FONT_NORMAL: + return FONT_NORMAL_HEIGHT; +#endif +#ifdef TREZOR_FONT_MEDIUM_ENABLE + case FONT_MEDIUM: + return FONT_MEDIUM_HEIGHT; +#endif +#ifdef TREZOR_FONT_BOLD_ENABLE + case FONT_BOLD: + return FONT_BOLD_HEIGHT; +#endif +#ifdef TREZOR_FONT_MONO_ENABLE + case FONT_MONO: + return FONT_MONO_HEIGHT; +#endif + } + return 0; +} + +int font_max_height(int font) { + switch (font) { +#ifdef TREZOR_FONT_NORMAL_ENABLE + case FONT_NORMAL: + return FONT_NORMAL_MAX_HEIGHT; +#endif +#ifdef TREZOR_FONT_MEDIUM_ENABLE + case FONT_MEDIUM: + return FONT_MEDIUM_MAX_HEIGHT; +#endif +#ifdef TREZOR_FONT_BOLD_ENABLE + case FONT_BOLD: + return FONT_BOLD_MAX_HEIGHT; +#endif +#ifdef TREZOR_FONT_MONO_ENABLE + case FONT_MONO: + return FONT_MONO_MAX_HEIGHT; +#endif + } + return 0; +} + +int font_baseline(int font) { + switch (font) { +#ifdef TREZOR_FONT_NORMAL_ENABLE + case FONT_NORMAL: + return FONT_NORMAL_BASELINE; +#endif +#ifdef TREZOR_FONT_MEDIUM_ENABLE + case FONT_MEDIUM: + return FONT_MEDIUM_BASELINE; +#endif +#ifdef TREZOR_FONT_BOLD_ENABLE + case FONT_BOLD: + return FONT_BOLD_BASELINE; +#endif +#ifdef TREZOR_FONT_MONO_ENABLE + case FONT_MONO: + return FONT_MONO_BASELINE; +#endif + } + return 0; +} + +const uint8_t *font_get_glyph(int font, uint8_t c) { + c = convert_char(c); + if (!c) return 0; + + // printable ASCII character + if (c >= ' ' && c < 0x7F) { + switch (font) { +#ifdef TREZOR_FONT_NORMAL_ENABLE + case FONT_NORMAL: + return FONT_NORMAL_DATA[c - ' ']; +#endif +#ifdef TREZOR_FONT_MEDIUM_ENABLE + case FONT_MEDIUM: + return FONT_MEDIUM_DATA[c - ' ']; +#endif +#ifdef TREZOR_FONT_BOLD_ENABLE + case FONT_BOLD: + return FONT_BOLD_DATA[c - ' ']; +#endif +#ifdef TREZOR_FONT_MONO_ENABLE + case FONT_MONO: + return FONT_MONO_DATA[c - ' ']; +#endif + } + return 0; + } + +// non-printable character +#define PASTER(s) s##_glyph_nonprintable +#define NONPRINTABLE_GLYPH(s) PASTER(s) + + switch (font) { +#ifdef TREZOR_FONT_NORMAL_ENABLE + case FONT_NORMAL: + return NONPRINTABLE_GLYPH(FONT_NORMAL_DATA); +#endif +#ifdef TREZOR_FONT_MEDIUM_ENABLE + case FONT_MEDIUM: + return NONPRINTABLE_GLYPH(FONT_MEDIUM_DATA); +#endif +#ifdef TREZOR_FONT_BOLD_ENABLE + case FONT_BOLD: + return NONPRINTABLE_GLYPH(FONT_BOLD_DATA); +#endif +#ifdef TREZOR_FONT_MONO_ENABLE + case FONT_MONO: + return NONPRINTABLE_GLYPH(FONT_MONO_DATA); +#endif + } + return 0; +} diff --git a/core/embed/extmod/modtrezorui/fonts/fonts.h b/core/embed/extmod/modtrezorui/fonts/fonts.h new file mode 100644 index 0000000000..90b74893c6 --- /dev/null +++ b/core/embed/extmod/modtrezorui/fonts/fonts.h @@ -0,0 +1,108 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _FONTS_H +#define _FONTS_H + +#include "fonts/font_bitmap.h" + +#if defined TREZOR_MODEL_T +#define TREZOR_FONT_BPP 4 +#elif defined TREZOR_MODEL_1 +#define TREZOR_FONT_BPP 1 +#elif defined TREZOR_MODEL_R +#define TREZOR_FONT_BPP 1 +#else +#error Unknown Trezor model +#endif + +#define COMPOSE(font_name, suffix) font_name##suffix +#define FONT_DEFINE(s, suffix) COMPOSE(s, suffix) + +#ifdef TREZOR_FONT_NORMAL_ENABLE +#define FONT_NORMAL (-1) +#include TREZOR_FONT_NORMAL_INCLUDE +#define FONT_NORMAL_DATA TREZOR_FONT_NORMAL_ENABLE +#define FONT_NORMAL_HEIGHT FONT_DEFINE(TREZOR_FONT_NORMAL_ENABLE, _HEIGHT) +#define FONT_NORMAL_MAX_HEIGHT \ + FONT_DEFINE(TREZOR_FONT_NORMAL_ENABLE, _MAX_HEIGHT) +#define FONT_NORMAL_BASELINE FONT_DEFINE(TREZOR_FONT_NORMAL_ENABLE, _BASELINE) +#endif + +#ifdef TREZOR_FONT_MEDIUM_ENABLE +#include TREZOR_FONT_MEDIUM_INCLUDE +#define FONT_MEDIUM (-5) +#define FONT_MEDIUM_DATA TREZOR_FONT_MEDIUM_ENABLE +#define FONT_MEDIUM_HEIGHT FONT_DEFINE(TREZOR_FONT_MEDIUM_ENABLE, _HEIGHT) +#define FONT_MEDIUM_MAX_HEIGHT \ + FONT_DEFINE(TREZOR_FONT_MEDIUM_ENABLE, _MAX_HEIGHT) +#define FONT_MEDIUM_BASELINE FONT_DEFINE(TREZOR_FONT_MEDIUM_ENABLE, _BASELINE) +#endif + +#ifdef TREZOR_FONT_MONO_ENABLE +#include TREZOR_FONT_MONO_INCLUDE +#define FONT_MONO (-3) +#define FONT_MONO_DATA TREZOR_FONT_MONO_ENABLE +#define FONT_MONO_HEIGHT FONT_DEFINE(TREZOR_FONT_MONO_ENABLE, _HEIGHT) +#define FONT_MONO_MAX_HEIGHT FONT_DEFINE(TREZOR_FONT_MONO_ENABLE, _MAX_HEIGHT) +#define FONT_MONO_BASELINE FONT_DEFINE(TREZOR_FONT_MONO_ENABLE, _BASELINE) +#endif + +#ifdef TREZOR_FONT_BOLD_ENABLE +#include TREZOR_FONT_BOLD_INCLUDE +#define FONT_BOLD (-2) +#define FONT_BOLD_DATA TREZOR_FONT_BOLD_ENABLE +#define FONT_BOLD_HEIGHT FONT_DEFINE(TREZOR_FONT_BOLD_ENABLE, _HEIGHT) +#define FONT_BOLD_MAX_HEIGHT FONT_DEFINE(TREZOR_FONT_BOLD_ENABLE, _MAX_HEIGHT) +#define FONT_BOLD_BASELINE FONT_DEFINE(TREZOR_FONT_BOLD_ENABLE, _BASELINE) +#endif + +#define MAX_FONT_H(A, B) ((A) > (B) ? (A) : (B)) + +#define FONT_MAX_HEIGHT_1 0 +#ifdef TREZOR_FONT_NORMAL_ENABLE +#define FONT_MAX_HEIGHT_2 MAX_FONT_H(FONT_NORMAL_MAX_HEIGHT, FONT_MAX_HEIGHT_1) +#else +#define FONT_MAX_HEIGHT_2 FONT_MAX_HEIGHT_1 +#endif + +#ifdef TREZOR_FONT_BOLD_ENABLE +#define FONT_MAX_HEIGHT_3 MAX_FONT_H(FONT_BOLD_MAX_HEIGHT, FONT_MAX_HEIGHT_2) +#else +#define FONT_MAX_HEIGHT_3 FONT_MAX_HEIGHT_2 +#endif + +#ifdef TREZOR_FONT_MEDIUM_ENABLE +#define FONT_MAX_HEIGHT_4 MAX_FONT_H(FONT_MEDIUM_MAX_HEIGHT, FONT_MAX_HEIGHT_3) +#else +#define FONT_MAX_HEIGHT_4 FONT_MAX_HEIGHT_3 +#endif + +#ifdef TREZOR_FONT_MONO_ENABLE +#define FONT_MAX_HEIGHT MAX_FONT_H(FONT_MONO_MAX_HEIGHT, FONT_MAX_HEIGHT_4) +#else +#define FONT_MAX_HEIGHT FONT_MAX_HEIGHT_4 +#endif + +int font_height(int font); +int font_max_height(int font); +int font_baseline(int font); +const uint8_t *font_get_glyph(int font, uint8_t c); + +#endif //_FONTS_H diff --git a/core/embed/rust/build.rs b/core/embed/rust/build.rs index 97dec1db9e..fe003f1699 100644 --- a/core/embed/rust/build.rs +++ b/core/embed/rust/build.rs @@ -259,7 +259,6 @@ fn generate_trezorhal_bindings() { .allowlist_function("display_backlight") .allowlist_function("display_text") .allowlist_function("display_text_width") - .allowlist_function("display_text_height") .allowlist_function("display_bar") .allowlist_function("display_bar_radius") .allowlist_function("display_icon") @@ -269,9 +268,11 @@ fn generate_trezorhal_bindings() { .allowlist_function("display_pixeldata") .allowlist_function("display_pixeldata_dirty") .allowlist_function("display_set_window") - .allowlist_function("display_get_glyph") .allowlist_var("DISPLAY_CMD_ADDRESS") .allowlist_var("DISPLAY_DATA_ADDRESS") + // fonts + .allowlist_function("font_height") + .allowlist_function("font_get_glyph") // uzlib .allowlist_function("uzlib_uncompress_init") .allowlist_function("uzlib_uncompress") diff --git a/core/embed/rust/src/trezorhal/display.rs b/core/embed/rust/src/trezorhal/display.rs index b5bed0e530..78b810d778 100644 --- a/core/embed/rust/src/trezorhal/display.rs +++ b/core/embed/rust/src/trezorhal/display.rs @@ -41,12 +41,12 @@ pub fn char_width(ch: char, font: i32) -> i16 { } pub fn get_char_glyph(ch: u8, font: i32) -> *const u8 { - unsafe { ffi::display_get_glyph(font, ch) } + unsafe { ffi::font_get_glyph(font, ch) } } pub fn text_height(font: i32) -> i16 { unsafe { - ffi::display_text_height(font) + ffi::font_height(font) .try_into() .unwrap_or(i16::MAX) } diff --git a/core/embed/rust/trezorhal.h b/core/embed/rust/trezorhal.h index 949a44632d..6a9a5f68e8 100644 --- a/core/embed/rust/trezorhal.h +++ b/core/embed/rust/trezorhal.h @@ -1,5 +1,6 @@ #include "common.h" #include "display.h" +#include "fonts/fonts.h" #include "rgb_led.h" #include "secbool.h" #include "storage.h" diff --git a/core/site_scons/tools.py b/core/site_scons/tools.py new file mode 100644 index 0000000000..3c2aeec6db --- /dev/null +++ b/core/site_scons/tools.py @@ -0,0 +1,11 @@ + +def add_font(font_name, font, defines, sources): + if font is not None: + defines += [ + 'TREZOR_FONT_' + font_name + '_ENABLE=' + font, + 'TREZOR_FONT_' + font_name + '_INCLUDE=\\"' + font.lower() + '.h\\"', + ] + sourcefile = 'embed/extmod/modtrezorui/fonts/' + font.lower() + '.c' + if sourcefile not in sources: + sources.append(sourcefile) + diff --git a/core/tools/codegen/gen_font.py b/core/tools/codegen/gen_font.py index 7b49a157ba..cfe12536a7 100755 --- a/core/tools/codegen/gen_font.py +++ b/core/tools/codegen/gen_font.py @@ -57,19 +57,9 @@ def process_face(name, style, size, bpp=4, shave_bearingX=0, ext="ttf"): face = freetype.Face("fonts/%s-%s.%s" % (name, style, ext)) face.set_pixel_sizes(0, size) fontname = "%s_%s_%d" % (name.lower(), style.lower(), size) - with open("font_%s.h" % fontname, "wt") as f: - f.write("#include \n\n") - f.write("#if TREZOR_FONT_BPP != %d\n" % bpp) - f.write("#error Wrong TREZOR_FONT_BPP (expected %d)\n" % bpp) - f.write("#endif\n") - f.write( - "extern const uint8_t* const Font_%s_%s_%d[%d + 1 - %d];\n" - % (name, style, size, MAX_GLYPH, MIN_GLYPH) - ) - f.write( - "extern const uint8_t Font_%s_%s_%d_glyph_nonprintable[];\n" - % (name, style, size) - ) + font_ymin = 0 + font_ymax = 0 + with open("font_%s.c" % fontname, "wt") as f: f.write("#include \n\n") f.write("// clang-format off\n\n") @@ -94,6 +84,7 @@ def process_face(name, style, size, bpp=4, shave_bearingX=0, ext="ttf"): rows = bitmap.rows advance = metrics.horiAdvance // 64 bearingX = metrics.horiBearingX // 64 + # discard space on the left side if shave_bearingX > 0: advance -= min(advance, bearingX, shave_bearingX) @@ -148,6 +139,12 @@ def process_face(name, style, size, bpp=4, shave_bearingX=0, ext="ttf"): ) nonprintable += " };\n" + + yMin = bearingY - rows + yMax = yMin + rows + font_ymin = min(font_ymin, yMin) + font_ymax = max(font_ymax, yMax) + f.write(nonprintable) f.write( @@ -158,6 +155,24 @@ def process_face(name, style, size, bpp=4, shave_bearingX=0, ext="ttf"): f.write(" Font_%s_%s_%d_glyph_%d,\n" % (name, style, size, i)) f.write("};\n") + with open("font_%s.h" % fontname, "wt") as f: + f.write("#include \n\n") + f.write("#if TREZOR_FONT_BPP != %d\n" % bpp) + f.write("#error Wrong TREZOR_FONT_BPP (expected %d)\n" % bpp) + f.write("#endif\n") + + f.write("#define Font_%s_%s_%d_HEIGHT %d\n" % (name, style, size, size)) + f.write("#define Font_%s_%s_%d_MAX_HEIGHT %d\n" % (name, style, size, font_ymax - font_ymin)) + f.write("#define Font_%s_%s_%d_BASELINE %d\n" % (name, style, size, -font_ymin)) + f.write( + "extern const uint8_t* const Font_%s_%s_%d[%d + 1 - %d];\n" + % (name, style, size, MAX_GLYPH, MIN_GLYPH) + ) + f.write( + "extern const uint8_t Font_%s_%s_%d_glyph_nonprintable[];\n" + % (name, style, size) + ) + process_face("Roboto", "Regular", 20) process_face("Roboto", "Bold", 20)