Python script to find glyph vertical centers

Primary tabs

5 posts / 0 new
Last post
George Everet Thompson's picture
Joined: 8 Oct 2004 - 11:00am
Python script to find glyph vertical centers
0

I managed to write a script that would give the vertical center of a single glyph. Now I have been trying to expand it to give the centers of a list of glyphs with no luck. The script below only gives the center of the last glyph in the list. So what am I doing wrong here?

# Find All Centers

import os
from robofab.world import CurrentFont
from robofab.interface.all.dialogs import GetFolder

f = CurrentFont()

tString = "zero minus plus equal"
glyphList = tString.split(" ")

def collectData(f):
path = GetFolder()
if path:
collectedData = []

for i in glyphList:
g = fl.font[fl.font.FindGlyph(i)]
rect = g.GetBoundingRect()
gcenter = str(rect.height / 2)
collectedData.append("g.name: "+g.name+", "+"gcenter: "+gcenter)
collectedData.sort()
collectedData = "\n".join(collectedData)
theFileName = os.path.basename(f.path)
theFileName = theFileName.split(".")[0] + ".txt"
thePath = os.path.join(path,theFileName)
theFile = open(thePath,"w")
theFile.write(collectedData)
theFile.close()
print "Done with %s!" % f.info.fontName

if len(f) != 0:
collectData(f)

George

Grzegorz's picture
Offline
Joined: 8 Dec 2011 - 2:10pm
0

Hi,
your script gives the center of only last glyph because the loop for glyphs in glyphList is incomplete.
Try:

...
for i in glyphList:
g = fl.font[fl.font.FindGlyph(i)]
rect = g.GetBoundingRect()
gcenter = str(rect.height / 2)
collectedData.append("g.name: "+g.name+", "+"gcenter: "+gcenter)
collectedData.sort()
collectedData = "\n".join(collectedData)
theFileName = os.path.basename(f.path)
theFileName = theFileName.split(".")[0] + ".txt"
...

regards,
gluk

George Everet Thompson's picture
Joined: 8 Oct 2004 - 11:00am
0

Then I get an error message: AttributeError: 'str' object has no attribute 'append'

George

George Everet Thompson's picture
Joined: 8 Oct 2004 - 11:00am
0

Finally got it! Thanks for your suggestion, it was an error in the indents.

for i in glyphList:
g = fl.font[fl.font.FindGlyph(i)]
rect = g.GetBoundingRect()
gcenter = str(rect.height / 2)
collectedData.append("g.name: "+g.name+", "+"gcenter: "+gcenter)
collectedData.sort()
collectedData = "\n".join(collectedData)

Grzegorz's picture
Offline
Joined: 8 Dec 2011 - 2:10pm
0

right, my mistake :)