55
66Matplotlib's font support is provided by the FreeType library.
77
8- Here, we use `~.Axes.table` build a font table that shows the glyphs by Unicode
9- codepoint, and print the glyphs corresponding to codepoints beyond 0xff .
8+ Here, we use `~.Axes.table` to draw a table that shows the glyphs by Unicode
9+ codepoint. For brevity, the table only contains the first 256 glyphs .
1010
11- Download this script and run it to investigate a font by running ::
11+ The example is a full working script. You can download it and use it to
12+ investigate a font by running ::
1213
13- python font_table_sgskip .py /path/to/font/file
14+ python font_table .py /path/to/font/file
1415"""
1516
1617import unicodedata
1718
1819import matplotlib .font_manager as fm
1920from matplotlib .ft2font import FT2Font
2021import matplotlib .pyplot as plt
21- import numpy as np
2222
2323
24- def draw_font_table (path ):
24+ def draw_font_table (path , print_non_latin1 = False ):
2525 """
2626 Parameters
2727 ----------
2828 path : str or None
2929 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.
3033 """
3134
3235 if path is None :
@@ -62,17 +65,17 @@ def draw_font_table(path):
6265 f"{ char_code :#x} ({ font .get_glyph_name (glyph_index )} )" ),
6366 ))
6467 continue
65- r , c = divmod (char_code , 16 )
66- chars [r ][ c ] = char
67- if non_8bit :
68+ row , col = divmod (char_code , 16 )
69+ chars [row ][ col ] = char
70+ if non_8bit and print_non_latin1 :
6871 indices , * _ = zip (* non_8bit )
6972 max_indices_len = max (map (len , indices ))
7073 print ("The font face contains the following glyphs corresponding to "
7174 "code points beyond 0xff:" )
7275 for index , char , name in non_8bit :
7376 print (f"{ index :>{max_indices_len }} { char } { name } " )
7477
75- ax = plt .figure (figsize = (8 , 4 ), dpi = 120 ). subplots ( )
78+ fig , ax = plt .subplots (figsize = (8 , 4 ))
7679 ax .set_title (path )
7780 ax .set_axis_off ()
7881
@@ -91,14 +94,17 @@ def draw_font_table(path):
9194 if row > 0 and col > - 1 : # Beware of table's idiosyncratic indexing...
9295 cell .set_text_props (fontproperties = fm .FontProperties (fname = path ))
9396
97+ fig .tight_layout ()
9498 plt .show ()
9599
96100
97101if __name__ == "__main__" :
98102 from argparse import ArgumentParser
99103
100- parser = ArgumentParser ()
104+ parser = ArgumentParser (description = "Display a font table." )
101105 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." )
102108 args = parser .parse_args ()
103109
104- draw_font_table (args .path )
110+ draw_font_table (args .path , args . print_non_latin1 )
0 commit comments