Matplotlib Font Table Visualization

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will guide you through an example of how font tables relate to one another using Python's Matplotlib library.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/for_loops -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/lists -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/tuples -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/importing_modules -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/using_packages -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/standard_libraries -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/os_system -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/data_visualization -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} python/build_in_functions -.-> lab-48744{{"`Matplotlib Font Table Visualization`"}} end

Load the Font

First, we need to load a font file. In this example, we will use DejaVuSans.ttf font file.

import os
import matplotlib
from matplotlib.ft2font import FT2Font

font = FT2Font(os.path.join(matplotlib.get_data_path(), 'fonts/ttf/DejaVuSans.ttf'))

Set the Character Map

Next, we set the character map to the standard Unicode character map.

font.set_charmap(0)

Get Character Codes and Glyphs

We will get the character codes and corresponding glyphs in the font and store them in two dictionaries, coded and glyphd.

codes = font.get_charmap().items()

coded = {}
glyphd = {}
for ccode, glyphind in codes:
    name = font.get_glyph_name(glyphind)
    coded[name] = ccode
    glyphd[name] = glyphind

Load a Glyph

Now we will load a glyph, the letter 'A', from the font and print its bounding box using the glyph.bbox attribute.

code = coded['A']
glyph = font.load_char(code)
print(glyph.bbox)

Get Kerning Values

We can get the kerning values between two glyphs by using the font.get_kerning() method. In this example, we will get the kerning value between the glyphs for 'A' and 'V' and the glyphs for 'A' and 'T'.

## kerning values for 'AV'
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))

## kerning value for 'AT'
print('AT', font.get_kerning(glyphd['A'], glyphd['T'], KERNING_UNSCALED))

Summary

In this lab, we learned how to load a font file, set the character map, get character codes and glyphs, load a glyph, and get kerning values between glyphs. These are useful techniques for anyone who wants to work with fonts and text in their Python projects.

Other Python Tutorials you may like