Question about segments
def getSegment_line(self):
contour, unrequested = self.objectGenerator("contour")
unrequested.append(contour)
contour.appendPoint((0, 0), "move")
contour.appendPoint((101, 202), "line")
segment = contour.segments[1]
return segment
this contour has two points and it is expected that there are two segments. I understand a segment to be "the thing between two points". I would expect one segment (from (0, 0) to (101, 202))
def test_set_move(self):
segment = self.getSegment_line()
segment.type = "move"
self.assertEqual(
segment.type,
"move"
)
So what is a "move" segment, then?
So what is a "move" segment, then?
I think these only appear on open contours these days. This decision was made 17 years ago, so I'm not 100% sure if this is 100% accurate, but this is what I remember about making that decision:
- FontLab had
moveeven for closed contours and I couldn't abstract it away. - I didn't want to start a list of segment objects with a point object.
So, there's this little anachronism.
In Glyphs, segments contain all points that belong to that price of contour. So a line segment has 2 coordinates and a curve segment four. That makes the segments very useful compared to the points list (for certain operations). If the segment misses it starting point, why not use the points directly.
They are just different models. If this was 17 years ago, we could consider changing it but a lot of stuff depends on the existing behavior. (No joke, I literally am writing some code right now that uses the existing segment API. 😄)
(a side question: What is the internal storage. It seems to be points. So every time you access the contours.segments, it recalculates the segment list? isn’t that incredibly wastful? And what is the advantage? What do those segments offer?
for idx, point in enumerate(points):
if point.type == "line":
# do stuff
elif point.type == "curve":
off1 = points[idx - 2]
off2 = points[idx - 1]
# do stuff
for segment in self.segments:
if segment.type == "line":
# do stuff
elif segment.type == "curve":
off1 = segment.points[0]
off2 = segment.points[1]
point = segment.points[2]
# do stuff
)