8686start_x = 0
8787start_y = 0
8888
89- def get_char_list ():
89+ def get_char_list (ranges ):
90+ if isinstance (ranges , str ):
91+ ranges = ranges .replace (" " , "" ).split (',' )
9092 list_char = []
91- for intervals in list_char_ranges . get (). split ( ',' ) :
93+ for intervals in ranges :
9294 first = intervals .split ('-' )[0 ]
9395 # we check if we the user input is a single char or an interval
9496 try :
@@ -99,7 +101,18 @@ def get_char_list():
99101 second = intervals .split ('-' )[1 ]
100102 for char in range (int (first ), int (second ) + 1 ):
101103 list_char .append (char )
102- return list_char
104+ return sorted (set (list_char ))
105+
106+ def get_ranges_list (char_codes ):
107+ char_codes = sorted (set (char_codes ))
108+ ranges = []
109+ start = char_codes [0 ]
110+ for i in range (1 , len (char_codes )):
111+ if char_codes [i ] != char_codes [i - 1 ] + 1 :
112+ ranges .append (f"{ start } -{ char_codes [i - 1 ]} " if start != char_codes [i - 1 ] else str (start ))
113+ start = char_codes [i ]
114+ ranges .append (f"{ start } -{ char_codes [- 1 ]} " if start != char_codes [- 1 ] else str (start ))
115+ return ranges
103116
104117def find_bounding_box (image ):
105118 pixels = image .load ()
@@ -127,7 +140,7 @@ def load_ttf_font(font_path, font_size):
127140 font_name = ' ' .join (pil_font .getname ())
128141 font_data = []
129142
130- for char_code in get_char_list ():
143+ for char_code in get_char_list (list_char_ranges . get () ):
131144 char = chr (char_code )
132145
133146 image = Image .new ("1" , (font_size * 2 , font_size * 2 ), 0 ) # generate mono bmp, 0 = black color
@@ -194,7 +207,7 @@ def load_ttf_font(font_path, font_size):
194207 font_data .append (glyph_data )
195208
196209 # The font render glyphs at font_size but they can shift them up or down which will cause the max_height
197- # to exceed font_size. It's not desirable to remove the padding entirely (the "enforce" option above),
210+ # to exceed font_size. It's not desirable to remove the padding entirely (the "enforce" option above),
198211 # but there are some things we can do to reduce the discrepency without affecting the look.
199212 max_height = max (g ["ofs_y" ] + g ["box_h" ] for g in font_data )
200213 if max_height > font_size :
@@ -325,13 +338,14 @@ def generate_c_font(font_name, font_size, font_data):
325338 normalized_name = f"{ font_name .replace ('-' , '_' ).replace (' ' , '' )} { font_size } "
326339 max_height = max (font_size , max (g ["ofs_y" ] + g ["box_h" ] for g in font_data ))
327340 memory_usage = sum (len (g ["bitmap" ]) + 8 for g in font_data )
341+ char_ranges = ", " .join (get_ranges_list ([g ["char_code" ] for g in font_data ]))
328342
329343 file_data = "#include \" ../rg_gui.h\" \n \n "
330344 file_data += "// File generated with font_converter.py (https://github.com/ducalex/retro-go/tree/dev/tools)\n \n "
331345 file_data += f"// Font : { font_name } \n "
332346 file_data += f"// Point Size : { font_size } \n "
333347 file_data += f"// Memory usage : { memory_usage } bytes\n "
334- file_data += f"// # characters : { len (font_data )} \n \n "
348+ file_data += f"// Characters : { len (font_data )} ( { char_ranges } ) \n \n "
335349 file_data += f"const rg_font_t font_{ normalized_name } = {{\n "
336350 file_data += f" .name = \" { font_name } \" ,\n "
337351 file_data += f" .type = 1,\n "
0 commit comments