Created
February 14, 2024 18:32
-
-
Save mekkablue/01bd2c7b9da97ebf82b36b1571d5d926 to your computer and use it in GitHub Desktop.
Quick script: Fix ()[]{}
This file contains 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
MacroTab.title = "Fix ()[]{}" | |
Glyphs.clearLog() | |
Glyphs.showMacroWindow() | |
font = Glyphs.font | |
print(font.familyName) | |
collectedLayers = [] | |
threshold = 1.0 | |
for m in font.masters: | |
mid = m.id | |
for bracket in ("paren", "bracket", "brace"): | |
leftGlyph = font.glyphs[bracket+"left"] | |
rightGlyph = font.glyphs[bracket+"right"] | |
if leftGlyph is None or rightGlyph is None: | |
print(f"❌ master ‘{m.name}’: {bracket}left and {bracket}right do not exist.") | |
continue | |
leftLayer = leftGlyph.layers[mid] | |
rightLayer = rightGlyph.layers[mid] | |
layerToFix = None | |
if leftLayer.components and leftLayer.components[0].componentLayer == rightLayer: | |
layerToFix = leftLayer | |
factor = -1 | |
elif rightLayer.components and rightLayer.components[0].componentLayer == leftLayer: | |
layerToFix = rightLayer | |
factor = 1 | |
if layerToFix: | |
componentToFix = layerToFix.components[0] | |
componentToFix.alignment = 3 | |
leftOrigin = leftLayer.bounds.origin.y | |
rightOrigin = rightLayer.bounds.origin.y | |
leftTop = leftOrigin + leftLayer.bounds.size.height | |
rightTop = rightOrigin + rightLayer.bounds.size.height | |
originDiff = leftOrigin-rightOrigin | |
if abs(originDiff)>=threshold: | |
if layerToFix is None: | |
print(f"⚠️ {m.name}: could NOT fix {bracket}left and {bracket}right, {originDiff:.1f} off.") | |
collectedLayers.extend([leftLayer, rightLayer]) | |
else: | |
componentToFix.y += factor * originDiff | |
print(f"👍🏻 {m.name}: fixed {bracket}left and {bracket}right") | |
if not collectedLayers: | |
Message( | |
"All pairs of ()[]{} on the same height. Details in Macro Window", | |
title='🥳 Congrats', | |
OKButton="Yippie", | |
) | |
else: | |
font.newTab(collectedLayers) | |
print(f"🚨 Could not fix {int(len(collectedLayers)/2)} pairs. Opened in a new tab.") | |
print("✅ Done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Glyphs 3.x script for the Macro window.