Created
April 16, 2019 10:45
-
-
Save frankrolf/ec5a50aaad6d7b3b471498e39ebe3b4d to your computer and use it in GitHub Desktop.
Robofont script to reset a composite glyph according to the anchor found in the base glyph.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def largest_component(g): | |
if not g.components: | |
return | |
else: | |
boxList = [] | |
for c in g.components: | |
boxList.append( | |
( | |
math.hypot(c.box[2] - c.box[0], c.box[3] - c.box[1]), | |
g.components.index(c) | |
) | |
) | |
return max(boxList)[1] | |
f = CurrentFont() | |
g = CurrentGlyph() | |
def reset_accent_to_anchor(g): | |
main_component_index = largest_component(g) | |
if main_component_index is not None: | |
g.prepareUndo('recompose according to anchor') | |
main_component = g.components[main_component_index] | |
base_g_name = main_component.baseGlyph | |
base_glyph = f[base_g_name] | |
accent_components = [ | |
c for c in g.components if not c.baseGlyph == base_g_name] | |
template_anchor_dict = { | |
anchor.name: anchor.position for anchor in base_glyph.anchors} | |
for accent_component in accent_components: | |
accent_glyph = f[accent_component.baseGlyph] | |
accent_anchor_dict = { | |
anchor.name: anchor.position for anchor in accent_glyph.anchors} | |
for a_name, a_position in list(accent_anchor_dict.items()): | |
target_position = template_anchor_dict.get(a_name[1:]) | |
if target_position: | |
current_offset = accent_component.offset | |
current_anchor_position = ( | |
a_position[0] + current_offset[0], | |
a_position[1] + current_offset[1], | |
) | |
offset = ( | |
target_position[0] - current_anchor_position[0], | |
target_position[1] - current_anchor_position[1] | |
) | |
accent_component.moveBy(offset) | |
g.performUndo() | |
reset_accent_to_anchor(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment