`BaseGlyph.__len__` The number of contours in the glyph.
Why do the contours get a special treatment? len(glyph) looks strange to me. What’s wrong with len(glyph.contours).
The special treatment is a holdover from RoboFog/RoboFab. There isn't anything wrong with len(glyph.contours), it is nicely explicit. However, keeping len(glyph) to the number of contours meant less breaking of prior scripts. What would you propose len(glyph) to be, the len of anchors + components + contours?
I don’t see why the glyph should have a length at all. You could keep it for now but log a deprecation warning? (wasn’t fontparts not meat to clean up legacy stuff?)
A fair point. FontParts is to clear up legacy things (90% FontLab code and spaghetti code) and also to make the API more clear. Will think about.
Found another issue like this. The contour has a __len__ and it returns the length of the segments. (at least the tests suggest this as the documentation is empty for BaseContour.__len__().
Why not the number of the points?
looks strange to me.
Me too and I wrote it. 😄
I really wanted to remove/deprecate/change this and other things like it, but I tried to balance my quest for purity against annoying the heck out of everyone with changes. If I felt like a bit of API was likely in use by lots of existing code (ie len(glyph)) and there wasn't a functional reason to remove/deprecate/change the API, I didn't touch the API. Rather, I decided to document the preferred way (ie len(glyph.contours)) while leaving the old API in place either without documentation or with documentation that says "Don't use this. Do it this way."
This applies not just to len but to iteration and other stuff. I much prefer:
for glyph in layer.glyphs:
for contour in glyph.contours:
if len(contour.segments):
over
for glyph in layer:
for contour in glyph:
if len(contour):
Why not the number of the points?
The same reason. This would have been a massively breaking change. The iteration is segments because that's what RoboFab had. RoboFab had segments because that's what RoboFog had. Explicit contour.segments and contour.points is recommended now.
But changing the API will not need much work to adapt the script. Just add a .contours here and there. And the actual API can stay around for a while with a big ugly Deprecation warning (including the instruction how to fix it). And remove it altogether form the documentation. So all new scripts will be nice, all old scripts still work but will be fix sooner then later.
But changing the API will not need much work to adapt the script.
No, it's not a huge change. Rather, it was that I was worried about generating a huge number of changes when converting lots and lots of scripts to fontParts from RoboFab.