1. Products
  2.   Font
  3.   Python
  4.   FontTools
 
  

Python Library for Fonts Reading

Open Source Python API that Empowers Software Developers and Designers to manipulate, edit, and analyze fonts with ease.

FontTools plays a vital role in design, branding, and communication, and fonts are the building blocks of visual aesthetics. In the digital era, font management and manipulation have become increasingly important. One powerful tool that has gained popularity among developers and designers is the FontTools library. FontTools is a versatile open source Python library that provides a comprehensive set of tools for working with fonts and has emerged as a powerful tool for font enthusiasts, designers, developers, and typographers alike. Its extensive feature set, coupled with its Python-based interface, makes it an excellent choice for font-related tasks and projects.

FontTools is an open-source library that enables software developers to interact with font files programmatically. It offers a wide range of functionality for font manipulation, editing, and analysis. It supports features like manipulating fonts in various ways, basic glyph editing operations, enhanced fonts merging, removing singular glyphs, SFNT table support, identifying and implementing various SFNT values, modifying glyph contours, adjusting font metrics, changing font encoding, adding or removing tables, converting layout definitions between different layout systems, finding and editing name strings, and so on.

FontTools library empowers software programmers to programmatically access, edit, and analyze various aspects of font files. It supports several font formats, including TrueType (TTF), OpenType (OTF), AFM, to an extent Type 1, and some Mac-specific formats, making it a versatile choice for working with different font types. FontTools benefits from a vibrant and supportive community. The project is open source and actively maintained, with regular updates and bug fixes. Whether you are automating font manipulation, fine-tuning typefaces, or conducting detailed font analysis, FontTools provides the necessary tools and flexibility to accomplish your goals efficiently.

Previous Next

Getting Started with FontTools

FontTools requires Python 3.8 or later. The recommended way to install FontTools is using PyPI. Please use the following command a smooth installation.

Install FontTools via NuGet

 pip install fonttools 

You can also install it manually; download the latest release files directly from GitHub repository.

Font Editing & Manipulation via Python API

The open FontTools library is very easy to use and has included several important features that empowers software developers to manipulate fonts in various ways inside their Python applications. It allows them to extract information from font files, modify glyph outlines, manipulate metrics, and even create new fonts from scratch, automate repetitive font-related tasks and so on. Using FontTools library Software developers can also modify glyph contours, adjust metrics, change encoding, and add or remove tables. This flexibility allows designers to fine-tune fonts according to specific requirements or create customized versions for their projects.

How to Modifying Outline Paths via Python API?

path_orig = get_outline(font, "a")
startdraw(200,200)

paths = []
for i in path_orig:
    if i[0] == 'moveTo':
        path = draw.BezierPath()
        path.moveTo((i[1][0]))
    if i[0] == 'lineTo':
        path.lineTo((i[1][0]))
    if i[0] == 'qCurveTo':
        path.qCurveTo(*(i[1]))
    if i[0] == 'closePath':
        path.closePath()
        paths.append(path)

finalpath = paths[0].difference(paths[1])
finalpath.scale(0.3) #let's scale it down!
draw.fill(0.5)
draw.stroke(0)
draw.drawPath(finalpath)
show()

Glyph Manipulation via Python API

The open source FontTools library makes it easy for software developers to manipulate individual glyphs programmatically. It provides methods to access and modify glyph outlines, metrics, and attributes, giving users the flexibility to customize fonts according to their specific needs. Whether it's adjusting kerning pairs, changing glyph shapes, adding ligatures, or compare glyphs outlines, FontTools simplifies the process of glyph manipulation. The following example shows how to compare the outlines of two glyphs and display the results using Python code.

How to Compare the Outlines of Two Glyphs using Python API?

from fontTools.ttLib import TTFont
from fontTools.pens.recordingPen import RecordingPen

font = TTFont(filename)
cmap = font.getBestCmap()
gs = font.getGlyphSet()

g1 = gs[cmap[ord("A")]]
g2 = gs[cmap[ord("B")]]

p1 = RecordingPen()
p2 = RecordingPen()

g1.draw(p1)
g2.draw(p2)

print(p1.value == p2.value)

Font Generation and Conversion via Python API

The open source FontTools library offers functionalities to generate new font files or convert existing ones into different formats. This feature is particularly useful for designers and developers who need to create custom fonts or adapt fonts for specific platforms. With FontTools, users can generate font instances with different weights, styles, and variations, or convert fonts between TrueType and OpenType formats seamlessly. The following example shows how to convert CFF Format to CFF2 Format using Python commands.