Skip to content

Instantly share code, notes, and snippets.

@brew
Created January 11, 2010 18:00
Show Gist options
  • Save brew/274439 to your computer and use it in GitHub Desktop.
Save brew/274439 to your computer and use it in GitHub Desktop.
def altCompileGlyph(font, glyphName, baseName, accentNames, adjustWidth=False, printErrors=True):
"""Compile components into a new glyph using components and anchorpoints.
font: the font
glyphName: the name of the glyph where it all needs to go
baseName: the name of the base glyph
accentNames: a list of accentName, anchorName tuples, [('acute', 'top'), etc]
"""
anchors = {}
errors = {}
baseGlyph = font[baseName]
for anchor in baseGlyph.getAnchors():
anchors[anchor.name] = anchor.position
destGlyph = font.newGlyph(glyphName, clear=True)
destGlyph.appendComponent(baseName)
destGlyph.width = baseGlyph.width
for accentName, anchorName in accentNames:
try:
accent = font[accentName]
except IndexError:
errors["glyph '%s' is missing in font %s"%(accentName, font.info.fullName)] = 1
continue
shift = None
for accentAnchor in accent.getAnchors():
if '_'+anchorName == accentAnchor.name:
shift = anchors[anchorName][0] - accentAnchor.position[0], anchors[anchorName][1] - accentAnchor.position[1]
destGlyph.appendComponent(accentName, offset=shift)
break
if shift is not None:
for accentAnchor in accent.getAnchors():
if accentAnchor.name in anchors:
anchors[accentAnchor.name] = shift[0]+accentAnchor.position[0], shift[1]+accentAnchor.position[1]
if printErrors:
for px in errors.keys():
print px
return destGlyph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment