Exploring Font Attributes and Glyph Metrics

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn about the attributes of an .FT2Font object that describe global font properties. You will also learn how to use individual character metrics using the .Glyph object, as returned by .load_char.

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/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/scope("`Scope`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") 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-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} matplotlib/importing_matplotlib -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/variables_data_types -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/conditional_statements -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/for_loops -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/tuples -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/dictionaries -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/scope -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/importing_modules -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/standard_libraries -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/os_system -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/data_visualization -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} python/build_in_functions -.-> lab-48750{{"`Exploring Font Attributes and Glyph Metrics`"}} end

Import necessary libraries

In this step, we will import the necessary libraries.

import os
import matplotlib
import matplotlib.ft2font as ft

Load Font

In this step, we will load the font that we will be working with. We will use a font shipped with Matplotlib.

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

Print Font Properties

In this step, we will print the properties of the font.

print('Num faces:  ', font.num_faces)        ## number of faces in file
print('Num glyphs: ', font.num_glyphs)       ## number of glyphs in the face
print('Family name:', font.family_name)      ## face family name
print('Style name: ', font.style_name)       ## face style name
print('PS name:    ', font.postscript_name)  ## the postscript name
print('Num fixed:  ', font.num_fixed_sizes)  ## number of embedded bitmaps

Print Additional Font Properties

In this step, we will print additional font properties that are only available if the face is scalable.

if font.scalable:
    ## the face global bounding box (xmin, ymin, xmax, ymax)
    print('Bbox:               ', font.bbox)
    ## number of font units covered by the EM
    print('EM:                 ', font.units_per_EM)
    ## the ascender in 26.6 units
    print('Ascender:           ', font.ascender)
    ## the descender in 26.6 units
    print('Descender:          ', font.descender)
    ## the height in 26.6 units
    print('Height:             ', font.height)
    ## maximum horizontal cursor advance
    print('Max adv width:      ', font.max_advance_width)
    ## same for vertical layout
    print('Max adv height:     ', font.max_advance_height)
    ## vertical position of the underline bar
    print('Underline pos:      ', font.underline_position)
    ## vertical thickness of the underline
    print('Underline thickness:', font.underline_thickness)

Print Font Styles

In this step, we will print the font styles.

for style in ('Italic',
              'Bold',
              'Scalable',
              'Fixed sizes',
              'Fixed width',
              'SFNT',
              'Horizontal',
              'Vertical',
              'Kerning',
              'Fast glyphs',
              'Multiple masters',
              'Glyph names',
              'External stream'):
    bitpos = getattr(ft, style.replace(' ', '_').upper()) - 1
    print(f"{style+':':17}", bool(font.style_flags & (1 << bitpos)))

Summary

In this lab, you learned about the attributes of an .FT2Font object that describe global font properties. You also learned how to use individual character metrics using the .Glyph object, as returned by .load_char.

Other Python Tutorials you may like