Accessing measurement tool data in script

When you’re in text mode and you click the measurement tool somewhere within a glyph, a horizontal line appears with values representing the left and right distances from the glyph.

Is there a way to access those distance values through scripting? For example, you specify a height that you want to query, and it returns a list with those values?

I’m working on auto-kerning, and that data would be invaluable…

Here is a snippet that gives you all intersections between too points. Usually you need to look a the second and the second to last value.

Layer = Glyphs.font.selectedLayers[0]
print Layer
P1 = NSMakePoint(-1000, 537)
P2 = NSMakePoint(3000, 537)

Measure = NSClassFromString(“GlyphsToolMeasurement”).alloc().init()
print Measure.calculateIntersectionsForLayer_startPoint_endPoint_(Layer, P1, P2)

That’s extremely useful - thanks!

One issue: the result of the calculateIntersectionsForLayer_startPoint_endPoint_() function is a list of NSConcreteValue types instead of NSPoint types.

I’m not familiar with “Python/Cocoa” transition code… how do I access the NSConcreteValue’s x and y values?

points = Measure.calculateIntersectionsForLayer_startPoint_endPoint_(Layer, P1, P2)

print points[0].pointValue().x

Perfect - that solved it.

If this is helpful to anyone:

def sideMeasurementsAtHeight(layer, measurementHeight):

…Measure = NSClassFromString(“GlyphsToolMeasurement”).alloc().init()

#this padding value is added to either side of the horizontal measuring line to ensure measurement is wider than layer.
…padding = 10.0

#capture horizontal intersections from beyond leftmost part of layer to beyond rightmost part of layer.
…intersections = Measure.calculateIntersectionsForLayer_startPoint_endPoint_(layer, NSMakePoint(layer.LSB - padding, measurementHeight), NSMakePoint((layer.width - layer.RSB) + padding, measurementHeight))

#if intersections contains more than two points, layer was intersected.
…if len(intersections) > 2:
…return (intersections[1].pointValue().x, layer.width - intersections[-2].pointValue().x)
…else:
…return None

I would just use a far left and far right point for the measurement. There is no performance problem or anything.

Good to know. So this (as an extreme example) is acceptable without performance issues?:

Measure.calculateIntersectionsForLayer_startPoint_endPoint_(someLayer, NSMakePoint(-10000, 50), NSMakePoint(10000, 50))

And just out of curiosity… with the .alloc().init() calls here (and in other examples on this forum)… should there be a corresponding release call? How is memory managed when Python meets Objective-C? Is there a leak in the function I wrote?