-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Update the font_table example. #12943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
========== | ||
Font table | ||
========== | ||
|
||
Matplotlib's font support is provided by the FreeType library. | ||
|
||
Here, we use `~.Axes.table` to draw a table that shows the glyphs by Unicode | ||
codepoint. For brevity, the table only contains the first 256 glyphs. | ||
|
||
The example is a full working script. You can download it and use it to | ||
investigate a font by running :: | ||
|
||
python font_table.py /path/to/font/file | ||
""" | ||
|
||
import unicodedata | ||
|
||
import matplotlib.font_manager as fm | ||
from matplotlib.ft2font import FT2Font | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def print_glyphs(font): | ||
"""Print the all the glyphs in the given FT2Font font to stdout.""" | ||
charmap = font.get_charmap() | ||
max_indices_len = len(str(max(charmap.values()))) | ||
|
||
print("The font face contains the following glyphs:") | ||
for char_code, glyph_index in charmap.items(): | ||
char = chr(char_code) | ||
name = unicodedata.name( | ||
char, | ||
f"{char_code:#x} ({font.get_glyph_name(glyph_index)})") | ||
print(f"{glyph_index:>{max_indices_len}} {char} {name}") | ||
|
||
|
||
def draw_font_table(path, print_all=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make print_all kwonly? (Not that it really matters here but it's the kind of arg that should likely be kwonly if this was a general-use function...)
|
||
""" | ||
Draw a font table of the first 255 chars of the given font. | ||
|
||
Parameters | ||
---------- | ||
path : str or None | ||
The path to the font file. If None, use Matplotlib's default font. | ||
print_all : bool | ||
Additionally print all chars to stdout. | ||
""" | ||
|
||
if path is None: | ||
path = fm.findfont(fm.FontProperties()) # The default font. | ||
|
||
font = FT2Font(path) | ||
if print_all: | ||
print_glyphs(font) | ||
|
||
# A charmap is a mapping of "character codes" (in the sense of a character | ||
# encoding, e.g. latin-1) to glyph indices (i.e. the internal storage table | ||
# of the font face). | ||
# In FreeType>=2.1, a Unicode charmap (i.e. mapping Unicode codepoints) | ||
# is selected by default. Moreover, recent versions of FreeType will | ||
# automatically synthesize such a charmap if the font does not include one | ||
# (this behavior depends on the font format; for example it is present | ||
# since FreeType 2.0 for Type 1 fonts but only since FreeType 2.8 for | ||
# TrueType (actually, SFNT) fonts). | ||
# The code below (specifically, the ``chr(char_code)`` call) assumes that | ||
# we have indeed selected a Unicode charmap. | ||
codes = font.get_charmap().items() | ||
|
||
labelc = ["{:X}".format(i) for i in range(16)] | ||
labelr = ["{:02X}".format(16 * i) for i in range(16)] | ||
chars = [["" for c in range(16)] for r in range(16)] | ||
|
||
for char_code, glyph_index in codes: | ||
if char_code >= 256: | ||
continue | ||
row, col = divmod(char_code, 16) | ||
chars[row][col] = chr(char_code) | ||
|
||
fig, ax = plt.subplots(figsize=(8, 4)) | ||
ax.set_title(path) | ||
ax.set_axis_off() | ||
|
||
table = ax.table( | ||
cellText=chars, | ||
rowLabels=labelr, | ||
colLabels=labelc, | ||
rowColours=["palegreen"] * 16, | ||
colColours=["palegreen"] * 16, | ||
cellColours=[[".95" for c in range(16)] for r in range(16)], | ||
cellLoc='center', | ||
loc='upper left', | ||
) | ||
for key, cell in table.get_celld().items(): | ||
row, col = key | ||
if row > 0 and col > -1: # Beware of table's idiosyncratic indexing... | ||
cell.set_text_props(fontproperties=fm.FontProperties(fname=path)) | ||
|
||
fig.tight_layout() | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
from argparse import ArgumentParser | ||
|
||
parser = ArgumentParser(description="Display a font table.") | ||
parser.add_argument("path", nargs="?", help="Path to the font file.") | ||
parser.add_argument("--print-all", action="store_true", | ||
help="Additionally, print all chars to stdout.") | ||
args = parser.parse_args() | ||
|
||
draw_font_table(args.path, args.print_all) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove "the"