2121import matplotlib .pyplot as plt
2222
2323
24- def draw_font_table (path , print_non_latin1 = False ):
24+ def print_glyphs (font ):
25+ """Print the all the glyphs in the given FT2Font font to stdout."""
26+ charmap = font .get_charmap ()
27+ max_indices_len = len (str (max (charmap .values ())))
28+
29+ print ("The font face contains the following glyphs:" )
30+ for char_code , glyph_index in charmap .items ():
31+ char = chr (char_code )
32+ name = unicodedata .name (
33+ char ,
34+ f"{ char_code :#x} ({ font .get_glyph_name (glyph_index )} )" )
35+ print (f"{ glyph_index :>{max_indices_len }} { char } { name } " )
36+
37+
38+ def draw_font_table (path , print_all = False ):
2539 """
40+ Draw a font table of the first 255 chars of the given font.
41+
2642 Parameters
2743 ----------
2844 path : str or None
2945 The path to the font file. If None, use Matplotlib's default font.
30- print_non_latin1 : bool
31- Print non-latin1 chars (i.e. chars with codepoints beyond 255) to
32- stdout.
46+ print_all : bool
47+ Additionally print all chars to stdout.
3348 """
3449
3550 if path is None :
3651 path = fm .findfont (fm .FontProperties ()) # The default font.
3752
3853 font = FT2Font (path )
54+ if print_all :
55+ print_glyphs (font )
56+
3957 # A charmap is a mapping of "character codes" (in the sense of a character
4058 # encoding, e.g. latin-1) to glyph indices (i.e. the internal storage table
4159 # of the font face).
@@ -52,28 +70,12 @@ def draw_font_table(path, print_non_latin1=False):
5270 labelc = ["{:X}" .format (i ) for i in range (16 )]
5371 labelr = ["{:02X}" .format (16 * i ) for i in range (16 )]
5472 chars = [["" for c in range (16 )] for r in range (16 )]
55- non_8bit = []
5673
5774 for char_code , glyph_index in codes :
58- char = chr (char_code )
5975 if char_code >= 256 :
60- non_8bit .append ((
61- str (glyph_index ),
62- char ,
63- unicodedata .name (
64- char ,
65- f"{ char_code :#x} ({ font .get_glyph_name (glyph_index )} )" ),
66- ))
6776 continue
6877 row , col = divmod (char_code , 16 )
69- chars [row ][col ] = char
70- if non_8bit and print_non_latin1 :
71- indices , * _ = zip (* non_8bit )
72- max_indices_len = max (map (len , indices ))
73- print ("The font face contains the following glyphs corresponding to "
74- "code points beyond 0xff:" )
75- for index , char , name in non_8bit :
76- print (f"{ index :>{max_indices_len }} { char } { name } " )
78+ chars [row ][col ] = chr (char_code )
7779
7880 fig , ax = plt .subplots (figsize = (8 , 4 ))
7981 ax .set_title (path )
@@ -103,8 +105,8 @@ def draw_font_table(path, print_non_latin1=False):
103105
104106 parser = ArgumentParser (description = "Display a font table." )
105107 parser .add_argument ("path" , nargs = "?" , help = "Path to the font file." )
106- parser .add_argument ("--print-non-latin1 " , action = "store_true" ,
107- help = "Print non-latin1 chars to stdout." )
108+ parser .add_argument ("--print-all " , action = "store_true" ,
109+ help = "Additionally, print all chars to stdout." )
108110 args = parser .parse_args ()
109111
110- draw_font_table (args .path , args .print_non_latin1 )
112+ draw_font_table (args .path , args .print_all )
0 commit comments