Skip to content

Commit

Permalink
WIP - loading font data from translations blob
Browse files Browse the repository at this point in the history
  • Loading branch information
grdddj committed Oct 31, 2023
1 parent 6ef99fe commit e2f2c5c
Show file tree
Hide file tree
Showing 14 changed files with 699 additions and 266 deletions.
135 changes: 6 additions & 129 deletions core/embed/lib/fonts/fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "fonts.h"
#include <stdbool.h>
#include <stdio.h>
#include "librust.h"

// TODO: make it return uint32_t (needs logic to assemble at most 4 bytes
// together)
Expand Down Expand Up @@ -133,142 +135,17 @@ int font_baseline(int font) {
return 0;
}

const uint8_t utf8_mapping(uint16_t c_2bytes) {
switch (c_2bytes) {
case 0xC381:
return 127; // Á
case 0xC48C:
return 128; // Č
case 0xC48E:
return 129; // Ď
case 0xC389:
return 130; // É
case 0xC49A:
return 131; // Ě
case 0xC38D:
return 132; // Í
case 0xC587:
return 133; // Ň
case 0xC393:
return 134; // Ó
case 0xC598:
return 135; // Ř
case 0xC5A0:
return 136; // Š
case 0xC5A4:
return 137; // Ť
case 0xC39A:
return 138; // Ú
case 0xC5AE:
return 139; // Ů
case 0xC39D:
return 140; // Ý
case 0xC5BD:
return 141; // Ž
case 0xC3A1:
return 142; // á
case 0xC48D:
return 143; // č
case 0xC48F:
return 144; // ď
case 0xC3A9:
return 145; // é
case 0xC49B:
return 146; // ě
case 0xC3AD:
return 147; // í
case 0xC588:
return 148; // ň
case 0xC3B3:
return 149; // ó
case 0xC599:
return 150; // ř
case 0xC5A1:
return 151; // š
case 0xC5A5:
return 152; // ť
case 0xC3BA:
return 153; // ú
case 0xC5AF:
return 154; // ů
case 0xC3BD:
return 155; // ý
case 0xC5BE:
return 156; // ž
case 0xC380:
return 157; // À
case 0xC382:
return 158; // Â
case 0xC386:
return 159; // Æ
case 0xC387:
return 160; // Ç
case 0xC388:
return 161; // È
case 0xC38A:
return 162; // Ê
case 0xC38B:
return 163; // Ë
case 0xC38E:
return 164; // Î
case 0xC38F:
return 165; // Ï
case 0xC394:
return 166; // Ô
case 0xC399:
return 167; // Ù
case 0xC39B:
return 168; // Û
case 0xC39C:
return 169; // Ü
case 0xC5B8:
return 170; // Ÿ
case 0xC592:
return 171; // Œ
case 0xC3A0:
return 172; // à
case 0xC3A2:
return 173; // â
case 0xC3A6:
return 174; // æ
case 0xC3A7:
return 175; // ç
case 0xC3A8:
return 176; // è
case 0xC3AA:
return 177; // ê
case 0xC3AB:
return 178; // ë
case 0xC3AE:
return 179; // î
case 0xC3AF:
return 180; // ï
case 0xC3B4:
return 181; // ô
case 0xC3B9:
return 182; // ù
case 0xC3BB:
return 183; // û
case 0xC3BC:
return 184; // ü
case 0xC3BF:
return 185; // ÿ
case 0xC593:
return 186; // œ
default:
return 0; // non-printable
}
}

const uint8_t *font_get_glyph(int font, uint8_t c) {
uint16_t c_2bytes = convert_char_utf8(c);
bool is_printable = c_2bytes != 0x7F;
if (!c_2bytes) return 0;

// found UTF8 character
if (c_2bytes > 0xFF) {
c_2bytes = utf8_mapping(c_2bytes);
if (!c_2bytes) {
PointerData glyph_data = get_utf8_glyph(c_2bytes);
if (glyph_data.ptr != NULL) {
return glyph_data.ptr;
} else {
is_printable = false;
}
}
Expand Down
7 changes: 7 additions & 0 deletions core/embed/rust/librust.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ extern mp_obj_module_t mp_module_trezortranslate;
#ifdef TREZOR_EMULATOR
mp_obj_t ui_debug_layout_type();
#endif

typedef struct {
const uint8_t* ptr;
uint32_t len;
} PointerData;

PointerData get_utf8_glyph(uint16_t char_code);
20 changes: 19 additions & 1 deletion core/embed/rust/src/trezorhal/translations.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
use super::ffi;

#[repr(C)]
pub struct PointerData {
pub ptr: *const u8,
pub len: u32,
}

pub fn translations_get() -> &'static [u8] {
let mut len: u32 = 0;
let ptr = unsafe { ffi::translations_read(&mut len) };
let ptr = unsafe { ffi::translations_read(&mut len, 0) };
if ptr.is_null() {
fatal_error!("Translations read failed", "");
}
unsafe { core::slice::from_raw_parts(ptr, len as usize) }
}

pub fn get_font_pointer(area_offset: u16, len: u16) -> PointerData {
let mut overall_len: u32 = 0;
let ptr = unsafe { ffi::translations_read(&mut overall_len, area_offset.into()) };
if ptr.is_null() {
fatal_error!("Translations read failed", "");
}
PointerData {
ptr,
len: len.into(),
}
}
3 changes: 3 additions & 0 deletions core/embed/rust/src/ui/translations/cs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"font": {
"file": "font_pixeloperator_regular_8_cs.json"
},
"header": {
"language": "cs",
"version": "2.6.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
[
{
"char": "Á",
"utf8": "C381",
"data": "0507060007111d18fe20"
},
{
"char": "Č",
"utf8": "C48C",
"data": "0507060007511d1845c0"
},
{
"char": "Ď",
"utf8": "C48E",
"data": "0507060007513d18c7c0"
},
{
"char": "É",
"utf8": "C389",
"data": "0507060007113f0e43e0"
},
{
"char": "Ě",
"utf8": "C49A",
"data": "0507060007513f0e43e0"
},
{
"char": "Í",
"utf8": "C38D",
"data": "020704010762a8"
},
{
"char": "Ň",
"utf8": "C587",
"data": "050706000751239ace20"
},
{
"char": "Ó",
"utf8": "C393",
"data": "0507060007111d18c5c0"
},
{
"char": "Ř",
"utf8": "C598",
"data": "0507060007513d18fa20"
},
{
"char": "Š",
"utf8": "C5A0",
"data": "0507060007511d0707c0"
},
{
"char": "Ť",
"utf8": "C5A4",
"data": "0507060007513e421080"
},
{
"char": "Ú",
"utf8": "C39A",
"data": "0507060007112318c5c0"
},
{
"char": "Ů",
"utf8": "C5AE",
"data": "0507060007228918c5c0"
},
{
"char": "Ý",
"utf8": "C39D",
"data": "05070600071123151080"
},
{
"char": "Ž",
"utf8": "C5BD",
"data": "0507060007513e2223e0"
},
{
"char": "á",
"utf8": "C3A1",
"data": "0507060007111c17c5e0"
},
{
"char": "č",
"utf8": "C48D",
"data": "0507060007511d1845c0"
},
{
"char": "ď",
"utf8": "C48F",
"data": "09070b000708845e51088441e0"
},
{
"char": "é",
"utf8": "C3A9",
"data": "0507060007111d1fc1c0"
},
{
"char": "ě",
"utf8": "C49B",
"data": "0507060007511d1fc1c0"
},
{
"char": "í",
"utf8": "C3AD",
"data": "020704010762a8"
},
{
"char": "ň",
"utf8": "C588",
"data": "0507060007513d18c620"
},
{
"char": "ó",
"utf8": "C3B3",
"data": "0507060007111d18c5c0"
},
{
"char": "ř",
"utf8": "C599",
"data": "050706000751274c4200"
},
{
"char": "š",
"utf8": "C5A1",
"data": "0507060007511d0707c0"
},
{
"char": "ť",
"utf8": "C5A5",
"data": "08070a00070141f24040403000"
},
{
"char": "ú",
"utf8": "C3BA",
"data": "0507060007112318c5c0"
},
{
"char": "ů",
"utf8": "C5AF",
"data": "0507060007228918c5c0"
},
{
"char": "ý",
"utf8": "C3BD",
"data": "0508060007112318bc2e00"
},
{
"char": "ž",
"utf8": "C5BE",
"data": "0507060007513e2223e0"
}
]
Loading

0 comments on commit e2f2c5c

Please sign in to comment.