Changing characters on both sides of cursor

Hello everyone.

I’m currently working on an adaptation of Andy Climer’s kerntroller (some of you might have heard about it) for Glyphs. The point is that I am now breaking the challenge into small parts and I am currently trying to access the cursor via a script and try to change the glyph before the cursor (just an example) for the next glyph inside the font.

Here the code that I now have and that prints where is the cursor and which characters are on the left and on the right.

# Font

font = Glyphs.font

    for tab in font.tabs:

    # Text in the tab
    tabText = tab.text

    #print(tabText[1])
    
    # Cursor
    cursor = tab.textCursor

    # Print statement
    print ("""Cursor at index %s. Letter '%s' is on the left and '%s' on the right."""

% (cursor, tabText[cursor-1], tabText[cursor]))

# Clear macro window
Glyphs.clearLog()

Anyone knows where can I get the information about this or how to do it?

Thank you so much!

Does this help:


font = Glyphs.font

tab = font.currentTab
# Text in the tab
tabText = tab.text
# Cursor
cursor = tab.textCursor

print ("Cursor at index %s. Letter '%s' is on the left and '%s' on the right." % (cursor, tabText[cursor-1], tabText[cursor]))

# replace glyphs next to the cursor
tabText = tabText[:cursor - 1] + "ab" + tabText[cursor + 1:]
# set the string
tab.text = tabText

Hi @GeorgSeifert, maybe I didn’t explain myself properly. What I want is accessing the Glyph order of the font so that the script would change the letters before and after the cursor (for example) for the next glyph in glyph order.

An example:
If I would have the word nova and the cursor between o and a, once run the script the word would be something like nówa. So o changed to the next glyph in the font (ó) and v too (w). Is there a way to access the glyph as an index value inside font so that I could add or subtract to change a glyph in a text.

Am I explaining in a clear way?

Thank you so much again!

I see. For the glyph on the right of the cursor you can use the “Show Next/Previous Glyph” from the view menu.

But to answer your question.
That is a bit more complicated. Specially if you have opentype features applied. But we ignore that for now.

What we need to do is to get the character for the glyph (from the text), get the corresponding glyph (with font.glyphs[tabText[cursor - 1]] get its index …


font = Glyphs.font

tab = font.currentTab
# Text in the tab
tabText = tab.text
# Cursor
cursor = tab.textCursor

print ("Cursor at index %s. Letter '%s' is on the left and '%s' on the right." % (cursor, tabText[cursor-1], tabText[cursor]))

def nextGlyphForChar(font, char):
	#prefChar = tabText[cursor - 1]
	glyph = font.glyphs[char]
	print glyph
	index = font.glyphs.index(glyph)
	nextGlyph = font.glyphs[index + 1]
	nextChar = unichr(font.characterForGlyph_(nextGlyph))
	print nextGlyph.name, nextChar
	return nextChar
	
prefChar = tabText[cursor - 1]
nextChar = tabText[cursor]
replaceString = nextGlyphForChar(font, prefChar) + nextGlyphForChar(font, nextChar)
# replace glyphs next to the cursor
tabText = tabText[:cursor - 1] + replaceString + tabText[cursor + 1:]
# set the string
tab.text = tabText
1 Like

Thank you so much again, Georg! That’s exactly what I wanted to do.

I think I can solve myself the problem with opentype features. I have been running it a bit and pushing to show these kind of problems and I will move on and solve this.

Again, thank you so much, Georg! I will post the resultant code when it is done or on its way.