Fontforge can't get TTF names via Scripting

msandersen's picture

I'm trying to convert a collection of Windows Postscript files to OTF on OSX. For that, I decided to go with Fontforge because I can batch-process them with scripts in the terminal. I know little bash-scripting and no Python, but have been able to modify a script on the Fontforge site to do this: http://fontforge.sourceforge.net/scripting-tutorial.html
However, as it is, I need to modify a lot of the files by hand to get the Style to display as I like it, like "Bold Italic" instead of "BoldItalic" with no spaces. So, I've looked into the documentation and tried to use the GetTTFName() and SetTTFName() functions to do this. I can get it to set the Style (subfamily) just fine, but I get an empty string when trying to read the existing style. Opening the font in the GUI, it displays the Style under the TTF section, so it is there. What I am experimenting with:

style = GetTTFName(0x409,2)
Fontfamily=GetTTFName(0x409,1)
Print(Fontfamily)
Print(style)
if (style!="") #use 'case' ?
if (style=="BoldItalic")
style="Bold Italic"
elseif (style=="BoldCondensed")
style="Bold Condensed"
elseif (style=="BoldCondItalic")
style="Bold Italic Condensed")
elseif (style=="SemiBoldItalic")
style="SemiBold Italic"
elseif (style=="BlackItalic")
style="Black Italic"
elseif (style=="ExtraBoldItalic")
style="ExtraBold Italic"
elseif (style=="MediumItalic")
style="Medium Italic"
elseif (style=="LightItalic")
style="Light Italic"
elseif (style=="HeavyItalic")
style="Heavy Italic"
endif
SetTTFName(0x409,2,style)
endif
Generate($argv[i]:r + format)

I'm aware there would be cleaner ways of doing it, like using Case, if Bash supports it, or converting it to Python, but for now I'm keeping it simple. Ideally, the script would look for a capital letter in the Style string and add a space before it where it isn't the first letter.
In any case, the above doesn't work, as GetTTFName() always returns an empty string. The parameters for the Set and GetTTFName() function is referring to is
http://partners.adobe.com/public/developer/opentype/index_name.html#lang3
and
http://partners.adobe.com/public/developer/opentype/index_name.html#enc4

Does anyone here know enough Bash scripting, in particularly with Fontforge, to see the problem?

msandersen's picture

ok, I established that Fontforge uses its own form of scripting language, described here: http://fontforge.sourceforge.net/scripting.html
but that still doesn't explain why GetTTFName(0x409,2) returns an empty string, when it clearly has the TTF subfamily set when you open it up. Fontforge also does Python scripting. but I know virtually nothing of it, and it would be too difficult and timeconsuming to learn it just for this.
I guess there is nothing for it but to open up each newly-generated font and fixing it by hand.

Khaled Hosny's picture

Postscript fonts don't have a names table, even for TTF names FontForge seems to return empty strings for TTF names corresponding to PostScript names (i.e. what you see in the PS Names tab), instead you can use the builtin variables:

Open($1)
Print($fontname)
Print($fullname)
Print($familyname)

Style is usually what is after the hyphen in $fontname, you can then used SetTTFname() to set the new values as you like.

Michel Boyer's picture

You can get the English (US) "TTF Names" as seen by FontForge with the following bash script that calls fontforge on a python script (here FontForge is used as a Python interpreter):

---
#!/usr/bin/env fontforge -lang=py
# http://www.typophile.com/node/81351

import fontforge,sys
fnt=fontforge.open(sys.argv[1])
nametbl= fnt.sfnt_names
for N in nametbl:
  if (N[0] == 'English (US)'):
    print "%-20s %s" % (N[1]+":", N[2])
---

Michel

twardoch's picture

MS,

if you're on Mac OS X, you could just as easily use makeotf from the Adobe FDK for OpenType package. It's a console application, and you can specify the "name" table entries in the feature definition file or the project file.

Regards,
Adam

Syndicate content Syndicate content