
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/text_labels_and_annotations/font_table.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_text_labels_and_annotations_font_table.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_text_labels_and_annotations_font_table.py:


==========
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

.. GENERATED FROM PYTHON SOURCE LINES 16-124



.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_font_table_001.png
   :alt: DejaVuSans.ttf
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_font_table_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_font_table_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. code-block:: Python


    import os
    from pathlib import Path
    import unicodedata

    import matplotlib.pyplot as plt

    import matplotlib.font_manager as fm
    from matplotlib.ft2font import FT2Font


    def print_glyphs(path):
        """
        Print the all glyphs in the given font file to stdout.

        Parameters
        ----------
        path : str or None
            The path to the font file.  If None, use Matplotlib's default font.
        """
        if path is None:
            path = fm.findfont(fm.FontProperties())  # The default font.

        font = FT2Font(path)

        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):
        """
        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.
        """
        if path is None:
            path = fm.findfont(fm.FontProperties())  # The default font.

        font = FT2Font(path)
        # 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 = [f"{i:X}" for i in range(16)]
        labelr = [f"{i:02X}" for i in range(0, 16*16, 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(os.path.basename(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(font=Path(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()

        if args.print_all:
            print_glyphs(args.path)
        draw_font_table(args.path)


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.002 seconds)


.. _sphx_glr_download_gallery_text_labels_and_annotations_font_table.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: font_table.ipynb <font_table.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: font_table.py <font_table.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: font_table.zip <font_table.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
