fontforge / designwithfontforge.com

A book about how to design new typefaces with FontForge
designwithfontforge.com
935 stars 167 forks source link

Add example python script for glyph names #16

Open davelab6 opened 11 years ago

davelab6 commented 11 years ago

This python script was made by me an Andrew Miller [A.J.Miller@bcs.org.uk] and could be a nice example of python scripting; it renames all glyphs to the "AGL without afii" namelist:

import fontforge
font = fontforge.open("Font.sfd")
for glyph in font:
  if font[glyph].unicode != -1:
    font[glyph].glyphname = fontforge.nameFromUnicode (font[glyph].unicode, "AGL without afii")
Welshmilla commented 11 years ago

I've expanded the above into a generic cleanup script that loads a font and does some basic maintenance.

Andrew Miller

cleanup.py:

#! /usr/bin/env python
import fontforge
import sys

fontfile = sys.argv[1]

try:
    font = fontforge.open (fontfile)
except EnvironmentError:
    sys.exit (1)

font.selection.all ()
font.removeOverlap ()
font.simplify ()
font.correctDirection ()
font.round ()

for glyph in font:
    if font[glyph].unicode != -1:
        font[glyph].glyphname = fontforge.nameFromUnicode (font[glyph].unicode, "AGL without afii")

font.save (fontfile)
davelab6 commented 11 years ago

Nice! :) Would be great to have some print() methods in there (or logging() etc) to tell the user what glyphs change in each step...

mrmartin commented 6 years ago

Convenient script. Just watch out for removing overlaps before correcting the direction. This causes the removal of inpainted regions before they get a chance to be fixed.

Do this instead:

font.selection.all ()
font.correctDirection ()
font.removeOverlap ()
font.simplify ()
font.round ()