Skip to content

Instantly share code, notes, and snippets.

@mekkablue
Created May 2, 2014 07:06
Show Gist options
  • Save mekkablue/1fcafb0ee9f0484ccbcf to your computer and use it in GitHub Desktop.
Save mekkablue/1fcafb0ee9f0484ccbcf to your computer and use it in GitHub Desktop.
Adds hints for the current node selection in Glyphs.app
#MenuTitle: Add Hints
# -*- coding: utf-8 -*-
"""Add Hints for the Selected Nodes. Tries to guess whether it should be H or V. If exactly one node inside a zone is selected, it will add a Ghost Hint."""
import GlyphsApp
Font = Glyphs.font
FontMaster = Font.selectedFontMaster
thisLayer = Font.selectedLayers[0]
thisSelection = [ n for n in thisLayer.selection() if n.className() == "GSNode" ]
numberOfSelectedNodes = len( thisSelection )
def hintTypeForYInTopZone( yValue ):
for thisZone in FontMaster.alignmentZones:
zonePosition = thisZone.position
zoneSize = thisZone.size
if yValue == sorted([zonePosition, zonePosition + zoneSize, yValue])[1]:
if zoneSize > 0:
return -1
elif zoneSize < 0:
return 1
return False
if numberOfSelectedNodes == 1:
# Ghost Hint
thisNode = thisSelection[0]
thisNodePosition = thisNode.y
hintType = hintTypeForYInTopZone( thisNodePosition )
if hintType is not False:
newHint = GSHint()
newHint.originNode = thisNode
newHint.type = hintType
newHint.horizontal = True
thisLayer.addHint_( newHint )
elif numberOfSelectedNodes % 2 == 0:
# Determine horizontal/vertical hints:
xCoordinates = sorted( [n.x for n in thisSelection] )
yCoordinates = sorted( [n.y for n in thisSelection] )
xDiff = xCoordinates[-1] - xCoordinates[0]
yDiff = yCoordinates[-1] - yCoordinates[0]
isHorizontal = yDiff > xDiff
if isHorizontal:
sortedListOfNodes = sorted( thisSelection, key=lambda n: n.y )
else:
sortedListOfNodes = sorted( thisSelection, key=lambda n: n.x )
# Add Hints:
for i in range( numberOfSelectedNodes // 2 ):
firstIndex = ( i + 1 ) * 2 - 2
secondIndex = ( i + 1 ) * 2 - 1
firstNode = sortedListOfNodes[ firstIndex ]
secondNode = sortedListOfNodes[ secondIndex ]
newHint = GSHint()
newHint.originNode = firstNode
newHint.targetNode = secondNode
newHint.type = 0
newHint.horizontal = isHorizontal
thisLayer.addHint_( newHint )
else:
print "Error: Either 1 node, or an even number of nodes must be selected."
@mekkablue
Copy link
Author

Oops, I misspelled the function, but it still works. :-)
Should be hintTypeForY() instead of hintTypeForYInTopZone().

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