Last active
November 11, 2026 16:56
-
-
Save ryanbugden/f455e369bbcfbee854c9554cf6f86504 to your computer and use it in GitHub Desktop.
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
| # menuTitle: Fill Missing Anchors (All Fonts) | |
| # author: Ryan Bugden | |
| """ | |
| Fill missing anchors using anchors in the same glyph across other open fonts. | |
| Calculates x from the average proportional position across glyph widths, | |
| or the average absolute x position for zero-width glyphs. | |
| Accounts for italic angle and slant offset. | |
| Calculates y from the most common nearest font dimension, | |
| plus the average distance from that dimension among anchors nearest to it. | |
| """ | |
| import math | |
| from statistics import mode | |
| from fontTools.misc.fixedTools import otRound | |
| from mojo.UI import getDefault | |
| # Want to show changes use mark colors? | |
| MARK = True | |
| # Set your special mark color | |
| MARK_COLOR = (0.99, 0.50, 0.03, 0.75) | |
| # ====================================================== | |
| def my_round(value): | |
| """Rounds a number according to your RF preferences.""" | |
| pref = getDefault("glyphViewRoundValues", defaultValue=1) | |
| if pref == 0: | |
| return value | |
| return pref * otRound(value / pref) | |
| def get_all_glyph_names(fonts): | |
| names = set() | |
| for font in fonts: | |
| for glyph_name in font.glyphOrder: | |
| names.add(glyph_name) | |
| return sorted(names) | |
| def pos_x(anchor): | |
| """ | |
| Given an anchor object, return its x position in the | |
| form of a percentage along the glyph width (non-zero width), | |
| or absolute coordinates (zero width). | |
| """ | |
| def get_normalized_x(anchor): | |
| """ | |
| Return an x-position of a given anchor, | |
| normalizing italic angle to 0. | |
| """ | |
| font = anchor.font | |
| italic = font.info.italicAngle or 0 | |
| # Get italic slant offset | |
| offset = font.lib.get("com.typemytype.robofont.italicSlantOffset", 0) | |
| # Remove the italic compensation from the absolute coordinates | |
| return anchor.x - math.tan(math.radians(-italic)) * anchor.y - offset | |
| glyph = anchor.glyph | |
| x = get_normalized_x(anchor) | |
| # Return absolute coordinates if the glyph is non-spacing | |
| if glyph.width == 0: | |
| return "absolute", x | |
| # Return ratio x position if the glyph has a non-zero width | |
| else: | |
| return "ratio", x / glyph.width | |
| def pos_y(anchor): | |
| """ | |
| Given an anchor object, return its y position in the | |
| form of (nearest font dimension, distance from it). | |
| """ | |
| glyph = anchor.glyph | |
| font = glyph.font | |
| dimensions = ("descender", 0, "xHeight", "capHeight", "ascender") | |
| nearest_dimension = None | |
| nearest_distance = float("inf") | |
| for dimension in dimensions: | |
| y_value = 0 if dimension == 0 else getattr(font.info, dimension) | |
| if y_value is None: | |
| continue | |
| distance = anchor.y - y_value | |
| if abs(distance) < abs(nearest_distance): | |
| nearest_dimension = dimension | |
| nearest_distance = distance | |
| return nearest_dimension, nearest_distance | |
| def calc_pos_color(glyph, anchor_data): | |
| """ | |
| Calculate an average position and most common color for a future anchor, given | |
| anchor data across other fonts, and the glyph you’d like the anchor added to. | |
| """ | |
| # Calculate x value | |
| des_x_type = "absolute" if glyph.width == 0 else "ratio" | |
| x_poses = [ | |
| x_pos for ((x_type, x_pos), (_, _)), _ in anchor_data if x_type == des_x_type | |
| ] | |
| if not x_poses: | |
| return None | |
| x_pos = sum(x_poses) / len(x_poses) | |
| # Absolute | |
| if des_x_type == "absolute": | |
| x = x_pos | |
| # Ratio | |
| else: | |
| x = x_pos * glyph.width | |
| # Calculate y value | |
| font = glyph.font | |
| common_dim = mode([dim for ((_, _), (dim, _)), _ in anchor_data]) | |
| y_dists = [ | |
| y_dist for ((_, _), (dim, y_dist)), _ in anchor_data if dim == common_dim | |
| ] | |
| y_dist = sum(y_dists) / len(y_dists) | |
| # Baseline | |
| if common_dim == 0: | |
| y = y_dist | |
| # Other dimension | |
| else: | |
| y = getattr(font.info, common_dim) + y_dist | |
| # Get the most common color | |
| color = mode([color for ((_, _), (_, _)), color in anchor_data]) | |
| return (x, y), color | |
| def fill_missing_anchors(fonts): | |
| """ | |
| Fill missing anchors using anchors in the same glyph across other open fonts. | |
| Calculates x from the average proportional position across glyph widths, | |
| or the average absolute x position for zero-width glyphs. | |
| Calculates y from the most common nearest font dimension, | |
| plus the average distance from that dimension among anchors nearest to it. | |
| """ | |
| def get_italicized_pos(font, position): | |
| x, y = position | |
| italic = font.info.italicAngle or 0 | |
| # Get italic slant offset | |
| offset = font.lib.get("com.typemytype.robofont.italicSlantOffset", 0) | |
| # Add the italic compensation to the absolute coordinates | |
| italic_compensation = math.tan(math.radians(-italic)) * y + offset | |
| x = x + italic_compensation | |
| return x, y | |
| for glyph_name in get_all_glyph_names(fonts): | |
| # Scan all fonts and build anchor data for this glyph | |
| anchor_data = {} | |
| for font in fonts: | |
| if glyph_name in font: | |
| for anchor in font[glyph_name].anchors: | |
| anchor_data.setdefault(anchor.name, []).append( | |
| ((pos_x(anchor), pos_y(anchor)), anchor.color) | |
| ) | |
| if not anchor_data: | |
| continue | |
| # Scan all fonts for glyphs with missing anchors, and append them, | |
| # using an average of the others as reference. | |
| for font in fonts: | |
| if glyph_name in font: | |
| glyph = font[glyph_name] | |
| # Reset mark color | |
| if MARK and glyph.markColor == MARK_COLOR: | |
| glyph.markColor = None | |
| for anchor_name, this_anchor_data in anchor_data.items(): | |
| if anchor_name not in [a.name for a in glyph.anchors]: | |
| pos_color = calc_pos_color(glyph, this_anchor_data) | |
| if pos_color is None: | |
| print( | |
| "⚠️ Couldn’t fill missing anchor. No source anchors with matching glyph width type.", | |
| font.info.familyName, | |
| font.info.styleName, | |
| glyph_name, | |
| anchor_name, | |
| ) | |
| continue | |
| position, color = pos_color | |
| # Add italic angle compensation | |
| position = get_italicized_pos(font, position) | |
| # Round the coordinates | |
| position = (my_round(position[0]), my_round(position[1])) | |
| print( | |
| "⚓︎ Filling missing anchor:", | |
| font.info.familyName, | |
| font.info.styleName, | |
| glyph_name, | |
| anchor_name, | |
| ) | |
| glyph.appendAnchor( | |
| name=anchor_name, | |
| position=position, | |
| color=color, | |
| ) | |
| # Set mark color to show something has changed | |
| if MARK: | |
| glyph.markColor = MARK_COLOR | |
| a = AllFonts() | |
| fill_missing_anchors(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment