Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Last active January 10, 2023 18:03
Show Gist options
  • Save frankrolf/e75980e3895df26615cc8ed1e60f9c5b to your computer and use it in GitHub Desktop.
Save frankrolf/e75980e3895df26615cc8ed1e60f9c5b to your computer and use it in GitHub Desktop.
Avoid off-curve start points in UFO files
'''
Avoid offcurve start points
'''
from fontTools.ufoLib.pointPen import PointToSegmentPen
def find_offcurve_start(glyph):
'''
compare coordinates of first bPoint to coordinates of first point
return True (offcurve start) if they differ
'''
for contour in g.contours:
first_point = contour.points[0]
first_bPoint = contour.bPoints[0]
first_point_coords = (first_point.x, first_point.y)
if first_point_coords != first_bPoint.anchor:
return True
return False
def redraw_glyph(g):
contours = []
for contour in g.contours:
points = [p for p in contour.points]
while points[0].type == 'offcurve':
points.append(points.pop(0))
contours.append(points)
g.prepareUndo('redraw')
rg = RGlyph()
ppen = PointToSegmentPen(rg.getPen())
for contour in contours:
ppen.beginPath()
for point in contour:
if point.type == 'offcurve':
ptype = None
else:
ptype = point.type
ppen.addPoint((point.x, point.y), ptype)
ppen.endPath()
g.clearContours()
g.appendGlyph(rg)
g.performUndo()
g.changed()
a = AllFonts()
glyphs_with_offcurve_start = []
for f in a:
glyphs_with_contours = [g for g in f if len(g.contours)]
for g in glyphs_with_contours:
if find_offcurve_start(g):
glyphs_with_offcurve_start.append(g)
for g in glyphs_with_offcurve_start:
print(g.name)
print(g.contours[0].points[0].type, end=' ')
redraw_glyph(g)
print()
@colinmford
Copy link

Thanks, @frankrolf !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment