Skip to content

Commit eae4635

Browse files
committed
Sanity-check: skip glyphs with no area
They cause the python-poly2tri call to segfault, so skip them if they exist.
1 parent 0322b2b commit eae4635

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

extractpoints.py

100755100644
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929
from generalfuncs import (
3030
pairwise, by_threes, flatten,
31-
vectorlength, are_points_equal, are_lines_equal,
31+
vectorlength, are_points_equal, are_lines_equal, is_sane_contour,
3232
averagepoint_as_ffpoint, averagepoint_as_tuple, averagepoint_as_tuplevector,
3333
comp, iterfilter_stopatvectors, itermap_stopatvectors,
3434
AttrDict, closer, closerish, further, angle, similar_direction, shallow_angle,
@@ -572,6 +572,10 @@ def extract_dots(glyph, show_glyph=True):
572572
# Extract vectors with the real stroke width now
573573
approx_outlines = []
574574
for contour in layer:
575+
if not is_sane_contour(contour):
576+
print("Skipping invalid contour:")
577+
print(", ".join(str((p.x, p.y)) for p in contour))
578+
continue
575579
points = extrapolate_midpoints(list(contour))
576580
approx_vectors = extract_vectors(points)
577581
linestring = vectorpairs_to_linestring(approx_vectors)
@@ -657,7 +661,10 @@ def make_triangles(polygon_data, holes = None):
657661
new_polyline = convert_polyline_to_polytri_version(polygon_data['line'])
658662
if are_points_equal(new_polyline[-1], new_polyline[0]):
659663
del new_polyline[-1]
664+
#print("About to call p2t with polyline:")
665+
#print(", ".join(str((p.x, p.y)) for p in new_polyline))
660666
cdt = p2t.CDT(new_polyline)
667+
#print("Just called p2t")
661668
for hole_data in holes:
662669
hole = hole_data['line']
663670
if hasattr(hole, 'coords'):

generalfuncs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ def vectorlength(point1, point2):
3333
length = squaredlength**0.5
3434
return length
3535

36+
def is_sane_contour(contour):
37+
"""Takes a contour in Fontforge format, and makes sure that it's sane.
38+
One way contours can be insane is if all their points have the same x
39+
coordinate, or the same y coordinate. This produces a glyph with no area,
40+
which causes the poly2tri operation to segfault."""
41+
x1, y1 = contour[0].x, contour[0].y
42+
if (all(p.x == x1 for p in contour)):
43+
return False
44+
if (all(p.y == y1 for p in contour)):
45+
return False
46+
return True
47+
3648
def ux(p):
3749
"""Extract the x value of a point in any format"""
3850
try:

0 commit comments

Comments
 (0)