1111
1212parser = argparse .ArgumentParser (description = 'Generate USB descriptors.' )
1313parser .add_argument ('--font' , type = str ,
14- help = 'manufacturer of the device' , required = True )
14+ help = 'Font path' , required = True )
15+ parser .add_argument ('--extra_characters' , type = str ,
16+ help = 'Unicode string of extra characters' )
17+ parser .add_argument ('--sample_file' , type = argparse .FileType ('r' ),
18+ help = 'Text file that includes strings to support.' )
1519parser .add_argument ('--output_c_file' , type = argparse .FileType ('w' ), required = True )
1620
1721args = parser .parse_args ()
@@ -25,32 +29,44 @@ def _load_row(self, y, row):
2529 self .rows [y ] = bytes (row )
2630
2731f = bitmap_font .load_font (args .font , BitmapStub )
28- f .load_glyphs (range (0x20 , 0x7f ))
29-
30- print (f .get_bounding_box ())
3132real_bb = [0 , 0 ]
3233
34+ # Load extra characters from the sample file.
35+ sample_characters = set ()
36+ if args .sample_file :
37+ for line in args .sample_file :
38+ # Skip comments because we add additional characters in our huffman comments.
39+ if line .startswith ("//" ):
40+ continue
41+ for c in line .strip ():
42+ sample_characters .add (c )
43+
44+ # Merge visible ascii, sample characters and extra characters.
3345visible_ascii = bytes (range (0x20 , 0x7f )).decode ("utf-8" )
34- extra_characters = "üàêùéáçãÍóíαψ◌"
35- all_characters = visible_ascii + extra_characters
46+ all_characters = visible_ascii
47+ for c in sample_characters :
48+ if c not in all_characters :
49+ all_characters += c
50+ if args .extra_characters :
51+ all_characters .extend (args .extra_characters )
3652filtered_characters = all_characters
53+
54+ # Try to pre-load all of the glyphs. Misses will still be slow later.
55+ f .load_glyphs (set (all_characters ))
56+
57+ # Get each glyph.
3758for c in all_characters :
3859 g = f .get_glyph (ord (c ))
3960 if not g :
4061 print ("Font missing character:" , c , ord (c ))
4162 filtered_characters = filtered_characters .replace (c , "" )
42- extra_characters = extra_characters .replace (c , "" )
4363 continue
4464 x , y , dx , dy = g ["bounds" ]
45- #print(c, g["bounds"], g["shift"])
4665 if g ["shift" ][1 ] != 0 :
4766 raise RuntimeError ("y shift" )
4867 real_bb [0 ] = max (real_bb [0 ], x - dx )
4968 real_bb [1 ] = max (real_bb [1 ], y - dy )
5069
51- #real_bb[1] += 1
52- #print(real_bb)
53-
5470tile_x , tile_y = real_bb
5571total_bits = tile_x * len (all_characters )
5672total_bits += 32 - total_bits % 32
@@ -61,31 +77,19 @@ def _load_row(self, y, row):
6177 g = f .get_glyph (ord (c ))
6278 start_bit = x * tile_x + g ["bounds" ][2 ]
6379 start_y = (tile_y - 2 ) - (g ["bounds" ][1 ] + g ["bounds" ][3 ])
64- # print(c, g["bounds"], g["shift"], tile_y, start_y)
6580 for y , row in enumerate (g ["bitmap" ].rows ):
6681 for i in range (g ["bounds" ][0 ]):
6782 byte = i // 8
6883 bit = i % 8
6984 if row [byte ] & (1 << (7 - bit )) != 0 :
7085 overall_bit = start_bit + (start_y + y ) * bytes_per_row * 8 + i
7186 b [overall_bit // 8 ] |= 1 << (7 - (overall_bit % 8 ))
72- # print("*",end="")
73- # else:
74- # print("_",end="")
75- #print()
76-
77- # print(b)
78- # print("tile_x = {}".format(tile_x))
79- # print("tile_y = {}".format(tile_y))
80- # print("tiles = {}".format(len(all_characters)))
81- # print("font = displayio.Bitmap(tile_x * tiles, tile_y, 2)")
82- # for row in range(tile_y):
83- # print("font._load_row({}, {})".format(row, bytes(b[row*bytes_per_row:row*bytes_per_row+bytes_per_row])))
84-
85- # for row in range(tile_y):
86- # for byte in b[row*bytes_per_row:row*bytes_per_row+bytes_per_row]:
87- # print("{:08b} ".format(byte),end="")
88- # print()
87+
88+
89+ extra_characters = ""
90+ for c in filtered_characters :
91+ if c not in visible_ascii :
92+ extra_characters += c
8993
9094c_file = args .output_c_file
9195
0 commit comments