New to Typophile? Accounts are free, and easy to set up.
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
26 Dec 2011 — 11:54pm
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
27 Dec 2011 — 5:03am
Then I get an error message: AttributeError: 'str' object has no attribute 'append'
George
27 Dec 2011 — 5:13am
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)27 Dec 2011 — 5:36am
right, my mistake :)