Delete OT Classes with Python

Primary tabs

5 posts / 0 new
Last post
Malcolm Wooden's picture
Offline
Joined: 30 Mar 2004 - 4:57am
Delete OT Classes with Python
0

Hi

Can anyone tell if it's possible to delete OT Classes using Python. I have tried something like:

for k in range(len(f.classes)):
s = f.classes[k][0]
if s=="_":
del f.classes[k]

to delete every class that starts with an underscore, but this approach doesn't work. Is there another way to delete classes or am I doing something stupid?

tia
M

Karsten Luecke's picture
Offline
Joined: 6 Aug 2005 - 8:41am
0

Try this:

f=fl.font
fIdx=fl.ifont
classes = f.classes
for k in range(len(classes)-1,-1,-1):
   if len(classes[k].strip()) and classes[k][0] == "_":
      del classes[k]
f.classes = classes
fl.UpdateFont(fIdx)

Tim Ahrens's picture
Offline
Joined: 28 Sep 2004 - 9:15am
0

Yeah, very strange behaviour of the API. The elements of the list are read-only but the enclosing structure can be overwritten. I ended up with the same workaround.

Malcolm Wooden's picture
Offline
Joined: 30 Mar 2004 - 4:57am
0

Thank you Karsten. I must admit I don't understand 100% why that works but thanks you for your solution.

M

Adam Twardoch's picture
Offline
Joined: 3 Dec 2002 - 7:36pm
0

Two things:

1. When you delete an element from a list, the number of items in the list obviously is reduced by one. But the initial range(len()) loop is just a static list so if you remove any elements, you'll get an "Index out of range". This is why Karsten is iterating through the loop in the descending order of elements. This is not FontLab-specific, it would happen with every Python list for which you'd remove elements that way.

2. Many of FontLab's pseudolist objects only implement partial behavior of list objects. But you can assign such pseudolist object to a real Python list, then perform some list modifications, and then reassign the list back to the FontLab pseudolist. I don't consider this "normal behavior" but, well, the FontLab Python API was developed quite a few years ago and some of the objects behave a bit strangely.