CFF and fonttools

Hi! I wonder if anyone here could please help to understand how to modify the CFF table with fonttools?

I use it to make demo fonts out of otfs, which includes adding a “demo” suffix into the names. The Name table is easy, but apparently the CFF table also has fontName which needs to match the PostScript name. I can’t wrap my head around how to modify that one, it doesn’t have a built-in function like setName. Tried this:

font = TTFont(path)
font['CFF '].cff.fontNames[0] = 'newName-Light' 
font.save(new_path) # This saves a new file but the name change is ignored

# This breaks the file so that even Font Table Viewer can’t open it:
with open(new_path, 'wb') as file_handle:
	font['CFF '].cff.compile(file_handle, font, isCFF2=None) 

Side question: the specs says that “CFF2 is a successor to and refinement of the 'CFF ’ table” and it’s almost 10 years old now — why does Glyphs outputs CFF rather than CFF2?

Does this snippet help you?

from fontTools.ttLib import TTFont

font = TTFont("input.otf")
cff = font["CFF "].cff
top_dict = cff.topDictIndex[0]
top_dict.FontName = "NewPSName"
font.save("output.otf")

It does, thank you! However, apparently I was a little wrong and confused:

Font Table Viewer only shows CFF FullName, which is not the same as that FontName, which fontbakery complains about. What I wrote does correct the fontbakery check:

font['CFF '].cff.fontNames[0] = NewPSName 
font.save(new_path)

At this point Font Table Viewer still shows the old FullName. Fontbakery says nothing about it, but it shouldn’t hurt to correct it anyway with

font['CFF '].cff. topDictIndex[0].FullName = NewFullName
font.save(new_path)