|
| 1 | +""" |
| 2 | +========== |
| 3 | +Font table |
| 4 | +========== |
| 5 | +
|
| 6 | +Matplotlib's font support is provided by the FreeType library. |
| 7 | +
|
| 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. |
| 10 | +
|
| 11 | +Usage:: |
| 12 | + python font_table_sgskip.py /path/to/font/file |
| 13 | +""" |
| 14 | + |
| 15 | +import unicodedata |
| 16 | + |
| 17 | +from matplotlib import ( |
| 18 | + font_manager as fm, |
| 19 | + pyplot as plt, |
| 20 | +) |
| 21 | +from matplotlib.ft2font import FT2Font |
| 22 | +import numpy as np |
| 23 | + |
| 24 | + |
| 25 | +def main(path): |
| 26 | + """ |
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + path : str or None |
| 30 | + The path to the font file. If None, use Matplotlib's default font. |
| 31 | + """ |
| 32 | + |
| 33 | + if path is None: |
| 34 | + path = fm.findfont(fm.FontProperties()) # The default font. |
| 35 | + |
| 36 | + font = FT2Font(path) |
| 37 | + # A charmap is a mapping of "character codes" (in the sense of a character |
| 38 | + # encoding, e.g. latin-1) to glyph indices (i.e. the internal storage table |
| 39 | + # of the font face). |
| 40 | + # In FreeType>=2.1, a Unicode charmap (i.e. mapping Unicode codepoints) |
| 41 | + # is selected by default. Moreover, recent versions of FreeType will |
| 42 | + # automatically synthesize such a charmap if the font does not include one |
| 43 | + # (this behavior depends on the font format; for example it is present |
| 44 | + # since FreeType 2.0 for Type 1 fonts but only since FreeType 2.8 for |
| 45 | + # TrueType (actually, SFNT) fonts). |
| 46 | + # The code below (specifically, the ``chr(char_code)`` call) assumes that |
| 47 | + # we have indeed selected a Unicode charmap. |
| 48 | + codes = font.get_charmap().items() |
| 49 | + |
| 50 | + labelc = ["{:X}".format(i) for i in range(16)] |
| 51 | + labelr = ["{:02X}".format(16 * i) for i in range(16)] |
| 52 | + chars = [["" for c in range(16)] for r in range(16)] |
| 53 | + non_8bit = [] |
| 54 | + |
| 55 | + for char_code, glyph_index in codes: |
| 56 | + char = chr(char_code) |
| 57 | + if char_code >= 256: |
| 58 | + non_8bit.append(( |
| 59 | + str(glyph_index), |
| 60 | + char, |
| 61 | + unicodedata.name( |
| 62 | + char, |
| 63 | + f"{char_code:#x} ({font.get_glyph_name(glyph_index)})"), |
| 64 | + )) |
| 65 | + continue |
| 66 | + r, c = divmod(char_code, 16) |
| 67 | + chars[r][c] = char |
| 68 | + if non_8bit: |
| 69 | + indices, *_ = zip(*non_8bit) |
| 70 | + max_indices_len = max(map(len, indices)) |
| 71 | + print("The font face contains the following glyphs corresponding to " |
| 72 | + "code points beyond 0xff:") |
| 73 | + for index, char, name in non_8bit: |
| 74 | + print(f"{index:>{max_indices_len}} {char} {name}") |
| 75 | + |
| 76 | + ax = plt.figure(figsize=(8, 4), dpi=120).subplots() |
| 77 | + ax.set_title(path) |
| 78 | + ax.set_axis_off() |
| 79 | + |
| 80 | + table = ax.table( |
| 81 | + cellText=chars, |
| 82 | + rowLabels=labelr, |
| 83 | + colLabels=labelc, |
| 84 | + rowColours=["palegreen"] * 16, |
| 85 | + colColours=["palegreen"] * 16, |
| 86 | + cellColours=[[".95" for c in range(16)] for r in range(16)], |
| 87 | + cellLoc='center', |
| 88 | + loc='upper left', |
| 89 | + ) |
| 90 | + for key, cell in table.get_celld().items(): |
| 91 | + row, col = key |
| 92 | + if row > 0 and col > -1: # Beware of table's idiosyncratic indexing... |
| 93 | + cell.set_text_props(fontproperties=fm.FontProperties(fname=path)) |
| 94 | + |
| 95 | + plt.show() |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + from argparse import ArgumentParser |
| 100 | + |
| 101 | + parser = ArgumentParser() |
| 102 | + parser.add_argument("path", nargs="?", help="Path to the font file.") |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + main(args.path) |
0 commit comments