I am using Python fontTools in order to read data from a true type font file ("Roboto-Black.ttf"). I read online that truetype fonts use quadratic bezier curves for their outlines (https://jdhao.github.io/2018/11/27/font_shape_mathematics_bezier_curves/).
I wrote code to find the glyph for a given character, and return the coordinates of the points for that glpyh. However, I can't figure out how to extract the connectivity.
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.ttLib import TTFont
font = TTFont('Roboto-Black.ttf')
cmap = font.getBestCmap()
glyphSet = font.getGlyphSet()
chr = 'A'
pen = TTGlyphPen(glyphSet)
g = glyphSet[cmap[ord(chr)]]
g.draw(pen)
g=pen.glyph()
print(g.getCoordinates(glyphSet._glyphs))
Output:
(GlyphCoordinates([(937, 272),(456, 272),(372, 0),(-3, 0),(531, 1456),(861, 1456),(1399, 0),(1022, 0),(540, 543),(853, 543),(696, 1048)]), [7, 10], array('B', [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
According to the documentation at https://fonttools.readthedocs.io/en/latest/ttLib/tables/_g_l_y_f.html, the first entry is the coordinates of each point in the glyph, the second entry represents the index of last point of each contour, and the third entry is the flags. Looking at https://learn.microsoft.com/en-us/typography/opentype/spec/glyf#:~:text=below%20for%20details.-,Simple%20Glyph%20Flags,-Mask, 0
in the last entry is an off-curve point and 1
is an on-curve point. However, how can I combine this information to get a list of quadratic bezier curves?
There are many pens that can help you with that, see the documentation https://fonttools.readthedocs.io/en/latest/pens/index.html