Typophile

My first Robofab script

kosal

I've been working at this for hours..ugh! Sure, if there's a better way to do this, please inform. Regardless, I want to really build this script from scratch to learn.

What I want to do:
Shift points at a certain y value to a different y value.

Logical Process:
Cycle through each glyph in my font. In each glyph, cycle through contours. Within the contours cycle through points. If the point has a y value or 395, or 265, change accordingly.

--------------------------------------------------
from robofab.world import CurrentGlyph
from robofab.world import CurrentFont

font=CurrentFont()

for x in font:
print font[x.name][0]
contour= font[x.name][0]
for p in contour.points:
if p.y==395:
p.y += 100
if p.y==265:
p.y -= 100
font.update()
--------------------------------------------------

What am I misunderstanding here? When I target the contour by doing CurrentGlyph()[0], it works. So why not font[x.name][0]? Oddly enough font[x.name] is returning the value expected, but CurrentGlyph() returns the glyph that apparently belongs to "unnamed_font". And for some reason, the unnamed_font one works. Am I totally off in my approach?

#!@$%!!
just when I mustered up the will to post it, I figured it out:


from robofab.world import CurrentGlyph
from robofab.world import CurrentFont

font=CurrentFont()

for x in font:
for m in x:
print m
for p in m.points:
if p.y==395:
p.y += 100
if p.y==265:
p.y -= 100

font.update()

The only question now is, would this be the best way you would solve this problem?

I suggest using more descriptive variable names. Why x for the glyph? I bet if you look at this code again in a year's time you will find this very confusing – especially since x would also be used for the abscissa of a point.

I agree with Tim: using more descriptive variable names makes your code more readable, both to you and other people.

One small optimisation could be "from robofab.world import CurrentFont, CurrentGlyph" instead of using two seperate lines.

-Paul van der Laan
www.type-invaders.com

Thanks for the tips. I started using descriptive variable names, but i ended up confusing myself by not remembering which word was my own variable versus which were reserved words. Hope to get the hang of it soon...

Regarding the naming of variables some one letter variables are used very often in RoboFab scripts:
f for a font object, i.e. f = CurrentFont()
g for a glyph object, i.e. g = f['Adieresis']
p for a point object
and when dealing with multiple objects of the same type variables can be numbered, i.e. f1, f2, etc.

To distinguish variable names from reserved keywords you can always opt to let them begin with the same suffix. For instance 'my':


from robofab.world import CurrentFont, CurrentGlyph

myFont = CurrentFont()

for myGlyph in myFont:
> for myContour in myGlyph:
> > for myPoint in myContour.points:
> > > if myPoint.y == 395:
> > > > myPoint.y += 100
> > > if myPoint.y == 265:
> > > > myPoint.y -= 100

myFont.update()

-Paul van der Laan
www.type-invaders.com

Captured from web.archive.org on 3 Feb 2010. view this capture