After doing some transformations on a glyph in Python the resulting coordinates are fractional. Even though I set the grid spacing back to 1 afterwards and round all the coordinates in a script, the nodes are still coloured red in Glyphs as they are floating point numbers that differ from the integers after 10 decimal places or so:
# round all coordinates
for path in Layer.paths:
for node in path.nodes:
node.x = int(round(node.x))
node.y = int(round(node.y))
if (node.x != round(node.x)):
print ("fractional coordinates in ", node, node.x, round(node.x))
After processing (setting grid to zero, rotating by the slant angle and then rotating back by the same angle), it looks like this, i.e.:
# round all coordinates
for path in Layer.paths:
for node in path.nodes:
node.x = int(round(node.x))
node.y = int(round(node.y))
Glyphs.font.setGridMain_( 1 )
print("after")
for path in Layer.paths:
for node in path.nodes:
node.x = int(round(node.x))
node.y = int(round(node.y))
print (node, node.x, node.y)
results in:
after
<GSNode 0x6000224bedc0> 808 -24 OFFCURVE 807.9999999999999 -24.0
So what is the difference between the node before processing and afterwards? How are they stored internally? Are both floating-point numbers? If so, why is one equal to exactly 808.0 and the other isn’t? This seems to be what’s causing the plugin to paint the node red. Can I restore the previous state programmatically?
Just to make it clear, it doesn’t seem to make a difference if I round the nodes before or after setting the grid to 1; in fact, in this example, I do both. Even moving the path by hand by 1 unit and back doesn’t round the handles.