Skip to content

Instantly share code, notes, and snippets.

@mathieureguer
Created August 23, 2020 22:43
Show Gist options
  • Save mathieureguer/6a89f75d6deaeba0d7c5a349e0c97b34 to your computer and use it in GitHub Desktop.
Save mathieureguer/6a89f75d6deaeba0d7c5a349e0c97b34 to your computer and use it in GitHub Desktop.
# menuTitle : auto align points
# shortCut : command+shift+E
"""
Move 2 points to align them.
Will guess between x and y alignment based on shortest move required.
Should align according to italic angle as well.
"""
do_italic_angle = True
#----------
# import
#----------
from math import tan, atan, degrees, radians
#----------
# funcions
#----------
def find_angle(pt1, pt2):
"""return the angle of a segment (against the vertical axis)"""
dist_x = pt2.x - pt1.x
dist_y = pt2.y - pt1.y
if dist_y == 0:
return 90
else:
a = atan(dist_x / dist_y)
return degrees(a)
def alignWithAngle(pt1, pt2, italic_angle):
# calculate ideal pt2 position
a = radians(italic_angle)
dist_y = pt2.y - pt1.y
dist_x = pt2.x - pt1.x
dist_x_ideal = tan(a) * dist_y
pt2.move((dist_x_ideal - dist_x, 0))
#----------
# let's go
#----------
g = CurrentGlyph()
pts = g.selectedPoints
# set up target angles
angles = [0, 90, -90]
try:
it_angle = abs(g.font.info.italicAngle)
except:
it_angle = None
if do_italic_angle == True and it_angle != None:
angles.append(it_angle)
if len(pts) == 2:
pt1, pt2 = pts
a = find_angle(pt1, pt2)
if a in angles:
pass
else:
closest_match = min(angles, key=lambda x: abs(x - a))
g.prepareUndo("Auto Align")
if closest_match == 0:
pt2.moveBy((pt1.x - pt2.x, 0))
elif closest_match == 90 or closest_match == -90:
pt2.moveBy((0, pt1.y - pt2.y))
elif closest_match == it_angle:
alignWithAngle(pt1, pt2, it_angle)
g.changed()
g.performUndo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment