Skip to content

Instantly share code, notes, and snippets.

@mekkablue
Created February 14, 2024 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mekkablue/01bd2c7b9da97ebf82b36b1571d5d926 to your computer and use it in GitHub Desktop.
Save mekkablue/01bd2c7b9da97ebf82b36b1571d5d926 to your computer and use it in GitHub Desktop.
Quick script: Fix ()[]{}
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.")
@mekkablue
Copy link
Author

Glyphs 3.x script for the Macro window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment