.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_misc_font_indexing.py: ============= Font Indexing ============= A little example that shows how the various indexing into the font tables relate to one another. Mainly for mpl developers.... .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (6, 0, 519, 576) 36 57 65 86 AV 0 AV 0 AV 0 AV 0 | .. code-block:: python from __future__ import print_function import matplotlib from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, KERNING_UNFITTED, KERNING_UNSCALED fname = matplotlib.get_data_path() + '/fonts/ttf/DejaVuSans.ttf' font = FT2Font(fname) font.set_charmap(0) codes = font.get_charmap().items() #dsu = [(ccode, glyphind) for ccode, glyphind in codes] #dsu.sort() #for ccode, glyphind in dsu: # try: name = font.get_glyph_name(glyphind) # except RuntimeError: pass # else: print('% 4d % 4d %s %s' % (glyphind, ccode, hex(int(ccode)), name)) # make a charname to charcode and glyphind dictionary coded = {} glyphd = {} for ccode, glyphind in codes: name = font.get_glyph_name(glyphind) coded[name] = ccode glyphd[name] = glyphind code = coded['A'] glyph = font.load_char(code) print(glyph.bbox) print(glyphd['A'], glyphd['V'], coded['A'], coded['V']) print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_DEFAULT)) print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNFITTED)) print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNSCALED)) print('AV', font.get_kerning(glyphd['A'], glyphd['T'], KERNING_UNSCALED)) .. _sphx_glr_download_gallery_misc_font_indexing.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: font_indexing.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: font_indexing.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature Keywords: matplotlib code example, codex, python plot, pyplot `Gallery generated by Sphinx-Gallery `_