Python / Fontlab Script isdigit() Problem

Ken Krugh's picture

I've reached the exaspiration point so I'm hoping someone can weigh in on this for me.

I have a python script that dumps out all the character names and unicodes but I'm getting an error using the isdigit() command.

I couldn't get the indents I have in the code to show here so I've used backslashes for each indent.

This smaller script works fine, in the line "unistr = str(f.glyphs[1].unicode)" if I change the "1" to a "0" I get the correct result:

#FLM: Testing Macro
f = fl.font
gcount = len(f)
unistr = str(f.glyphs[1].unicode)
isnumeric=unistr.isdigit()
if isnumeric:
-print "It is!"
else:
-print "It isn't!"

However, doing nearly the same darn thing I'm getting an invalid syntax error. I'm at a loss.

Many thanks,
Ken


#FLM: Writes marked glyph unicode numbers and names to a file
f = fl.font
fname=f.font_name
finame=str("C:\Program Files\FontLab\Studio5\Macros\UnicodeAndNamesMarked_") + fname + str(".txt")

TheFile = open(finame, "w")
TheFile.write(f.family_name + " marked glyphs" + chr(10))
if f != None:
-gcount = len(f)
-for i in range(gcount):
--if f.glyphs[i].mark:
---unistr = str(f.glyphs[i].unicode)
---isnumeric=unistr.isdigit()
---if isnumeric:
----TheFile.write(hex(unistr) + chr(9) + f.glyphs[i].name + chr(10))
---else:
----TheFile.write(unistr + chr(9) + f.glyphs[i].name + chr(10))
else:
-print "There are no fonts opened"
TheFile.close()
print "Filename: " + finame

Ken Krugh's picture

Oy, got it. It had something to do with the indents in the code. I must have had a space somehwere instead of a tab. Once I retyped them all everything worked fine.

eigi's picture

Hello,
I think this prints what you want:

if len(fl) > 0:
    for glyph in fl.font.glyphs:
        if glyph.mark:
            if glyph.unicode is None:
                print 'None\t%s' % glyph.name
            else:
                print '0x%04X\t%s' % (glyph.unicode, glyph.name)


Redirecting the output to a file is up to you ;-)
Best
Eigi

Bahman Eslami's picture

Eigi:

all the code does for me is the list of glyphs and "none" unicode for all of them, no matter they have unicode or not.

Ken:
Could I have the code, I just need it for generating unicode for many glyphs, and your script will make it a cakewalk, thanks.

eigi's picture

Hello Bahman,

Hm - this confuses me!? This is what I get if I mark standard figures (with unicode) and old Style figures (without unicode)

0x0030	zero
0x0031	one
0x0032	two
0x0033	three
0x0034	four
0x0035	five
0x0037	seven
0x0036	six
0x0038	eight
0x0039	nine
None	zero.onum
None	one.onum
None	two.onum
None	three.onum
None	four.onum
None	five.onum
None	seven.onum
None	six.onum
None	eight.onum
None	nine.onum

Are you running FontLab on Mac or Win?

eigi's picture

Stil confused. Just checked FontLab 5.0.4 Win and 5.0.2 Mac - same output!?

Bahman Eslami's picture

Hi Eigi
I got it, it wasn't the code, the problem was that I had too many glyphs (about 800) and output window is not enough for it, so it just leaves out the glyphs which had unicode. I made the ken code to work but there is a problem, hex function is not working in it. without hex function it works but it's useless for me. what should I do?
Thanks,
Code:

#FLM: Writes marked glyph unicode numbers and names to a file
f = fl.font
fname=f.font_name
finame=str("C:\Program Files\FontLab\Studio5\Macros\UnicodeAndNamesMarked_") + fname + str(".txt")

TheFile = open(finame, "w")
TheFile.write(f.family_name + " marked glyphs" + chr(10))
if f != None:
 gcount = len(f)
 for i in range(gcount):
  if f.glyphs[i].mark:
   unistr = str(f.glyphs[i].unicode)
   isnumeric = unistr.isdigit()
   if isnumeric:
    TheFile.write(hex(unistr) + chr(9) + f.glyphs[i].name + chr(10))
   else:
    TheFile.write(unistr + chr(9) + f.glyphs[i].name + chr(10))
else:
 print "There are no fonts opened"
TheFile.close()
print "Filename: " + finame
eigi's picture

Hello Bahman,

The hex() function takes an integer as parameter not a string. See http://docs.python.org/library/functions.html#hex
I prefer the string formatting operations to get a hex representation of an integer (as shown above). See http://docs.python.org/library/stdtypes.html#string-formatting-operations

Best
Eigi

Bahman Eslami's picture

Thanks :)

Syndicate content Syndicate content