Skip to content

Instantly share code, notes, and snippets.

@benkiel
Created July 15, 2013 12:45
Show Gist options
  • Save benkiel/5999663 to your computer and use it in GitHub Desktop.
Save benkiel/5999663 to your computer and use it in GitHub Desktop.
Set side bearings based on a base glyph
from mojo.tools import IntersectGlyphWithLine
font = CurrentFont()
glyphs = { 'e':['eacute', 'egrave', 'ediresis'],
'i':['iacute', 'igrave']
}
for base, accented in glyphs.items():
if base in font.keys():
base = font[base]
for g in accented:
if g in font.keys():
g = font[g]
if base.box[2] - base.box[0] < g.box[2] - g.box[0]:
y = (base.box[3] - base.box[1])/2
bl = [x[0] for x in IntersectGlyphWithLine(base, ((-10000, y), (10000, y)), canHaveComponent=True, addSideBearings=True)]
gl = [x[0] for x in IntersectGlyphWithLine(g, ((-10000, y), (10000, y)), canHaveComponent=True, addSideBearings=True)]
bl.sort()
gl.sort()
bLeft = abs(bl[:2][0] - bl[:2][1])
bRight = abs(bl[-2:][0] - bl[-2:][1])
gLeft = abs(gl[:2][0] - gl[:2][1])
gRight = abs(gl[-2:][0] - gl[-2:][1])
if bLeft != gLeft:
d = gLeft - bLeft
g.leftMargin = g.leftMargin - d
if bRight != gRight:
d = gRight - bRight
g.rightMargin = g.rightMargin - d
else:
g.leftMargin = base.leftMargin
g.rightMargin = base.rightMargin
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment