Skip to content

Commit

Permalink
Merge pull request #371 from LedgerHQ/API_LEVEL_11_ocr
Browse files Browse the repository at this point in the history
Changes needed to support fake OCR on speculos for Stax
  • Loading branch information
dmorais-ledger authored Jul 28, 2023
2 parents 6b2d5ec + 6d8a3cf commit 88626d2
Show file tree
Hide file tree
Showing 9 changed files with 7,089 additions and 37 deletions.
666 changes: 666 additions & 0 deletions lib_nbgl/fonts/nbgl_font_hmalpha_mono_medium_32.json

Large diffs are not rendered by default.

1,062 changes: 1,062 additions & 0 deletions lib_nbgl/fonts/nbgl_font_inter_medium_32.json

Large diffs are not rendered by default.

1,062 changes: 1,062 additions & 0 deletions lib_nbgl/fonts/nbgl_font_inter_medium_32_1bpp.json

Large diffs are not rendered by default.

1,062 changes: 1,062 additions & 0 deletions lib_nbgl/fonts/nbgl_font_inter_regular_24.json

Large diffs are not rendered by default.

1,062 changes: 1,062 additions & 0 deletions lib_nbgl/fonts/nbgl_font_inter_regular_24_1bpp.json

Large diffs are not rendered by default.

1,062 changes: 1,062 additions & 0 deletions lib_nbgl/fonts/nbgl_font_inter_semibold_24.json

Large diffs are not rendered by default.

1,062 changes: 1,062 additions & 0 deletions lib_nbgl/fonts/nbgl_font_inter_semibold_24_1bpp.json

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions lib_nbgl/src/nbgl_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,13 @@ void nbgl_drawText(const nbgl_area_t *area, const char* text, uint16_t textLen,
rectArea.height = (char_y_max - char_y_min);
rectArea.width = (char_x_max -char_x_min);

if (char_byte_cnt) {
switch(encoding) {
case 0:
nbgl_frontDrawImage(&rectArea, char_buffer, NO_TRANSFORMATION, fontColor);
break;
case 1:
nbgl_frontDrawImageRle(&rectArea, char_buffer, char_byte_cnt, fontColor);
break;
}
// If char_byte_cnt = 0, call nbgl_frontDrawImageRle to let speculos notice
// a space character was 'displayed'
if (!char_byte_cnt || encoding == 1) {
nbgl_frontDrawImageRle(
&rectArea, char_buffer, char_byte_cnt, fontColor);
} else {
nbgl_frontDrawImage(&rectArea, char_buffer, NO_TRANSFORMATION, fontColor);
}
x+=char_width;
}
Expand Down
72 changes: 44 additions & 28 deletions lib_nbgl/tools/ttf2inc.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,7 @@ def main(args):
inc_filename = change_ext(inc_filename, ".inc")

# Build the corresponding .json file, if we need to
if ttf.unicode_needed:
inc_json = change_ext(inc_filename, ".json")
else:
inc_json = None
inc_json = change_ext(inc_filename, ".json")

if args.suffix:
suffix = args.suffix
Expand Down Expand Up @@ -953,15 +950,15 @@ def main(args):
# Write the array containing information about characters:
if ttf.unicode_needed:
typedef = "nbgl_font_unicode_character_t"
ttf_info_dictionary["nbgl_font_unicode_character"] = []
else:
typedef = "nbgl_font_character_t"
ttf_info_dictionary["nbgl_font_character"] = []

inc.write(
f"\n __attribute__ ((section(\"._nbgl_fonts_\"))) const {typedef} characters"
f"{ttf.basename.upper()}{suffix}[{len(char_info)}] = {{\n")

ttf_info_dictionary["nbgl_font_unicode_character"] = []

for char, info in sorted(char_info.items()):
width = info["width"]
size = info["size"]
Expand All @@ -988,6 +985,17 @@ def main(args):
inc.write(f" {{ 0x{ord(char):06X}, {size:3}, {offset:4}, {width:3}, "
f"{x_min}, {y_min}, {x_max}, {y_max}, {encoding} }}, //unicode {unicode}\n")
else:
ttf_info_dictionary["nbgl_font_character"].append({
"char": ord(char),
"bitmap_byte_count": size,
"bitmap_offset": offset,
"char_width": width,
"x_min": x_min,
"y_min": y_min,
"x_max": x_max,
"y_max": y_max,
"encoding": encoding
})
# We'll use bitfieds to store x_min x_max y_min y_max
# => we need to change a little bit the meaning:
# - y_min = Y offset in pixels*4 (ie 3=>12)
Expand Down Expand Up @@ -1078,6 +1086,16 @@ def main(args):
}
else:
typedef = "nbgl_font_t"
ttf_info_dictionary["nbgl_font"] = {
"font_id": ttf.get_font_id(),
"bpp": ttf.bpp,
"char_height": ttf.font_size,
"baseline_height": baseline,
"line_height": ttf.line_size,
"char_kerning": 0,
"first_char": first_char,
"last_char" : last_char
}
inc.write(
f"\n __attribute__ ((section(\"._nbgl_fonts_\"))) const {typedef} font{ttf.basename.upper()}{suffix} = {{\n")
inc.write(f" {bitmap_len}, // bitmap len\n")
Expand All @@ -1094,29 +1112,27 @@ def main(args):
inc.write(f" bitmap{ttf.basename.upper()}\n")
inc.write("};\n")

# Do we need to generate a JSON file with unicode related info?
if ttf.unicode_needed:
ttf_info_list.append(ttf_info_dictionary)
with open(inc_json, "w") as json_file:
json.dump(ttf_info_list, json_file)
# Be sure there is a newline at the end of the file
json_file.write("\n")

if args.test_align != None:
string_width = 0
for char, info in sorted(char_info.items()):
string_width+=info['width']
img = Image.new('1', (string_width, ttf.font_size), color='black')
current_width = 0
for char, info in sorted(char_info.items()):
img.paste(info['img'], (current_width, 0))
current_width += info['width']
draw = ImageDraw.Draw(img)
shape = [(0,args.test_align), (string_width, args.test_align)]
draw.line(shape, fill='white',width=0)

img.show()
# Generate a JSON file with all font related info?
ttf_info_list.append(ttf_info_dictionary)
with open(inc_json, "w") as json_file:
json.dump(ttf_info_list, json_file, indent=2)
# Be sure there is a newline at the end of the file
json_file.write("\n")

if args.test_align != None:
string_width = 0
for char, info in sorted(char_info.items()):
string_width+=info['width']
img = Image.new('1', (string_width, ttf.font_size), color='black')
current_width = 0
for char, info in sorted(char_info.items()):
img.paste(info['img'], (current_width, 0))
current_width += info['width']
draw = ImageDraw.Draw(img)
shape = [(0,args.test_align), (string_width, args.test_align)]
draw.line(shape, fill='white',width=0)

img.show()


return 0
Expand Down

0 comments on commit 88626d2

Please sign in to comment.