Skip to content

Instantly share code, notes, and snippets.

@mekkablue
Created July 27, 2019 16:59
Show Gist options
  • Save mekkablue/94999b983c78c8c11bf7ea89f4ae0b33 to your computer and use it in GitHub Desktop.
Save mekkablue/94999b983c78c8c11bf7ea89f4ae0b33 to your computer and use it in GitHub Desktop.
Align to Metrics scripts
#MenuTitle: Bump down
# -*- coding: utf-8 -*-
__doc__="""
Align selected components, paths (and parts of paths) to the next metric line or guideline below.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
Master = Font.selectedFontMaster
allMetrics = [ Master.ascender, Master.capHeight, Master.xHeight, 0.0, Master.descender ] + [ g.y for g in Master.guideLines if g.angle == 0.0 ]
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
if selection:
try:
lowestPathY = min( n.y for n in selection if type(n) == GSNode )
except:
# No path selected
lowestPathY = None
try:
lowestCompY = min( c.bounds.origin.y for c in selection if type(c) == GSComponent )
except:
# No component selected
lowestCompY = None
lowestY = min( y for y in [lowestCompY, lowestPathY] if y != None )
try:
nextMetricLineBelow = max( ( m for m in allMetrics if m < lowestY ) )
except:
nextMetricLineBelow = min( allMetrics )
Font.disableUpdateInterface()
for thisThing in selection:
thisType = type(thisThing)
if thisType == GSNode or thisType == GSComponent:
thisThing.y -= ( lowestY - nextMetricLineBelow )
Font.enableUpdateInterface()
except Exception, e:
if selection == ():
print "Cannot bump down: nothing selected in frontmost layer."
else:
print "Error. Cannot bump selection:", selection
print e
#MenuTitle: Bump left
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) to the next sidebearing or glyph center to the left.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
Master = Font.selectedFontMaster
selectedLayer = Font.selectedLayers[0]
allMetrics = [ 0.0, selectedLayer.width, selectedLayer.width//2 ]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
leftMostX = min( ( n.x for n in selection ) )
try:
nextMetricLineToTheLeft = max( ( m for m in allMetrics if m < leftMostX ) )
except:
nextMetricLineToTheLeft = min( allMetrics )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.x -= ( leftMostX - nextMetricLineToTheLeft )
Font.enableUpdateInterface()
except Exception, e:
if selection == ():
print "Cannot bump left: nothing selected in frontmost layer."
else:
print "Error. Cannot bump selection:", selection
print e
#MenuTitle: Bump right
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) to the next sidebearing or glyph center to the right.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
Master = Font.selectedFontMaster
allMetrics = [ 0.0, selectedLayer.width, selectedLayer.width//2 ]
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
rightMostX = max( ( n.x for n in selection ) )
try:
nextMetricLineToTheRight = min( ( m for m in allMetrics if m > rightMostX ) )
except:
nextMetricLineToTheRight = max( allMetrics )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.x += ( nextMetricLineToTheRight - rightMostX )
Font.enableUpdateInterface()
except Exception, e:
if selection == ():
print "Cannot bump right: nothing selected in frontmost layer."
else:
print "Error. Cannot bump selection:", selection
print e
#MenuTitle: Bump up
# -*- coding: utf-8 -*-
__doc__="""
Align selected components, paths (and parts of paths) to the next metric line or guideline above.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
Master = Font.selectedFontMaster
allMetrics = [ Master.ascender, Master.capHeight, Master.xHeight, 0.0, Master.descender ] + [ g.y for g in Master.guideLines if g.angle == 0.0 ]
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
if selection:
try:
highestPathY = max( n.y for n in selection if type(n) == GSNode )
except:
# No path selected
highestPathY = None
try:
highestCompY = max( (c.bounds.origin.y + c.bounds.size.height) for c in selection if type(c) == GSComponent )
except:
# No component selected
highestCompY = None
highestY = max( y for y in [highestCompY, highestPathY] if y != None )
try:
nextMetricLineAbove = min( ( m for m in allMetrics if m > highestY ) )
except:
nextMetricLineAbove = max( allMetrics )
Font.disableUpdateInterface()
for thisThing in selection:
thisType = type(thisThing)
if thisType == GSNode or thisType == GSComponent:
thisThing.y += ( nextMetricLineAbove - highestY )
Font.enableUpdateInterface()
except Exception, e:
print "Error. Cannot bump selection:"
print selection
print e
#MenuTitle: Align Selection to Ascender
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the Ascender.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
myAscender = Font.selectedFontMaster.ascender
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
highestY = max( ( n.y for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.y += ( myAscender - highestY )
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to Baseline
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the Baseline.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
lowestY = min( ( n.y for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.y -= lowestY
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to Cap Height
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the Cap Height.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
myCapHeight = Font.selectedFontMaster.capHeight
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
highestY = max( ( n.y for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.y += ( myCapHeight - highestY )
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to Horizontal Center
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the Horizontal Center.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
selectedLayer = Font.selectedLayers[0]
layerCenter = selectedLayer.width // 2
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
selectionXList = [ n.x for n in selection ]
leftMostX = min( selectionXList )
rightMostX = max( selectionXList )
selectionCenter = ( leftMostX + rightMostX ) // 2
centerOffset = float( layerCenter - selectionCenter )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.x += centerOffset
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to Descender
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the Descender.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
myDescender = Font.selectedFontMaster.descender
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
lowestY = min( ( n.y for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.y -= ( lowestY - myDescender )
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to LSB
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the LSB.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
leftMostX = min( ( n.x for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.x -= leftMostX
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to RSB
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the RSB.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
selectedLayer = Font.selectedLayers[0]
layerWidth = selectedLayer.width
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
rightMostX = max( ( n.x for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.x += ( layerWidth - rightMostX )
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
#MenuTitle: Align Selection to x-Height
# -*- coding: utf-8 -*-
__doc__="""
Align selected paths (and parts of paths) in the frontmost layer to the x-Height.
"""
Font = Glyphs.font
Doc = Glyphs.currentDocument
myXHeight = Font.selectedFontMaster.xHeight
selectedLayer = Font.selectedLayers[0]
try:
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
highestY = max( ( n.y for n in selection ) )
Font.disableUpdateInterface()
for thisNode in selection:
thisNode.y += ( myXHeight - highestY )
Font.enableUpdateInterface()
except Exception, e:
print "Error: Nothing selected in frontmost layer?"
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment