UNSOLVED Decorating the RF objects!
bahman last edited by gferreira
Hi, I wanted to add a method to the glyph object every time on startup. Like decorating the Glyph class for example I could do this:
g = CurrentGlyph() g.myCustomMethod() >>> "Hello!"
Is this possible?
Thanks ( ͡° ͜ʖ ͡°)
yes, this is possible and super useful. here’s an example:
# my custom method def drawRect(self, x, y, w, h): pen = self.getPen() pen.moveTo((x, y)) pen.lineTo((x + w, y)) pen.lineTo((x + w, y + h)) pen.lineTo((x, y + h)) pen.closePath() # do this once at startup from mojo.roboFont import RGlyph RGlyph.rect = drawRect # then use the custom method anywhere g = CurrentGlyph() g.rect(0, 100, 200, 300) g.rect(50, 400, 200, 300)
bahman last edited by
Ow so simple! I thought if I do that it wouldn't affect all the objects. Thank you!
If you add a method to the class all instances of the class will have that new method!
the beauty of python!
bahman last edited by bahman
@frederik Yeah that is very powerful. I have another related question. let's say I want my custom method to be executed every time an object changes. I don't want to add a defcon notification but rather override a method and use the
superon an internal method of glyph object, then execute my own method. I looked at defcon but it seemed that it's only possible by overriding
_set_dirty, but not sure if that's the right way. What do you think?