Best way to get layers for cursor position?

Hello, I am trying to find the best way to get the layers for my current cursor position in text mode. So far, I tried using Font.currentTab.layers[Font.currentTab.textCursor, but that yields erroneous results, as the cursor position is calculated in an odd way: If I have unencoded glyphs, the text cursor position is increased by 2 for each unencoded glyph.

For example, if I have an in my tab, putting the cursor after it yields 2 as my textCursor position, although it should be 1.

How can I solve this?

have you tried Font.currentTab.activeLayer()?

That’s great, thanks. How do I get the layers around that layer, though? For example, the layer just before the cursor? I used

l = Font.currentTab.layers[Font.currentTab.textCursor - 1]

which works for encoded glyphs, but not for unencoded glyphs.

Yeah, here too. Not sure why that is.

Try Glyphs.font.currentTab.layersCursor in the latest betas.


Very cool! Thank you.

There are two different levels to access the content of the edit view. One is the underlying text. That is a UTF-16 string. So most characters are one character, but some, and all unencoded (they use upper plane temp unicodes) use two bytes.
The other level is the list of resulting layers. That is after opentype features are applied (compared to the underlying text, that can reduce the number of item (with ligatures) or increase (with one to many subs).
In your case, you need to use the layers and layersCursor. Or tab.graphicView().selectedLayerRange()

1 Like