-
Notifications
You must be signed in to change notification settings - Fork 4
/
anchorBackupAndClone.py
executable file
·120 lines (95 loc) · 3.56 KB
/
anchorBackupAndClone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#MenuTitle: Anchor Backup and Clone
# -*- coding: utf-8 -*-
__doc__="""
(UI) Backup anchors and copy layer to layer.
"""
from GlyphsApp import *
from vanilla import *
from Foundation import NSPoint
font = Glyphs.font
class AnchorTeleporter(object):
def __init__(self):
self.createLayerList()
infoText = ""
self.w = Window((240, 180), "Anchor Backup and Clone")
self.w.button = Button((10, 10, -10, 20), "Backup Anchors",
callback=self.buttonCallback)
self.w.line = HorizontalLine((10, 40, -10, 1))
self.w.textBox = TextBox((10, 50, -10, 17), "Clone Anchors from")
self.w.popUpButton = PopUpButton((10, 75, -50, 20),
self.createLayerList())
self.w.reloadButton = Button((-40, 75, -10, 20), u"↺",
callback=self.reloadButtonCallback)
self.w.restoreButton = Button((10, 105, -10, 20), "Clone Anchors", callback=self.cloneCallback)
self.w.line2 = HorizontalLine((10, 135, -10, 1))
self.w.box = Box((10, 145, -10, 24))
self.w.box.infoBox = TextBox((0, 0, -0, -0), infoText, alignment='center', sizeStyle='small')
self.w.open()
def buttonCallback(self, sender):
selectedLayers = Glyphs.font.selectedLayers
self.w.box.infoBox.set("Anchors Backuped")
def createLayer (thisLayer):
glyph = thisLayer.parent
newLayer = GSLayer()
#print font.masters[masterId]
thisMaster = font.masters[thisLayer.associatedMasterId]
newLayer.associatedMasterId = thisMaster.id
font.glyphs[glyph.name].layers.append(newLayer)
id = newLayer.layerId
backupLayer = font.glyphs[thisLayer.parent.name].layers[id]
relatedMaster = font.masters[backupLayer.associatedMasterId].name
newLayer.name = 'AnchorBackup ' + str(relatedMaster)
return backupLayer
#Copy anchors to backup layer
def copyToBackup(backupLayer, anchorName, x, y):
newAnchor = GSAnchor.alloc().init()
newAnchor.name = anchorName
backupLayer.addAnchor_( newAnchor )
newPosition = NSPoint( x, y)
newAnchor.setPosition_( newPosition )
for thisLayer in selectedLayers:
backupLayer = createLayer (thisLayer)
allAnchors = thisLayer.anchors
for thisAnchor in allAnchors:
anchorName = thisAnchor.name
x = thisAnchor.position.x
y = thisAnchor.position.y
copyToBackup (backupLayer, anchorName, x, y)
# Reload List
layerList = self.createLayerList()
self.w.popUpButton.setItems( layerList )
def createLayerList(self):
selectedLayers = Glyphs.font.selectedLayers
for thisLayer in selectedLayers:
layerList = []
glyph = thisLayer.parent
for layer in glyph.layers:
layerList.append(layer.name)
return layerList
def reloadButtonCallback( self, sender ):
layerList = self.createLayerList()
self.w.popUpButton.setItems( layerList )
self.w.box.infoBox.set("Anchor list reloaded")
def cloneCallback(self, sender):
selectedLayers = Glyphs.font.selectedLayers
self.w.box.infoBox.set("Anchors cloned!")
def copyToFront( thisLayer, anchorName, x, y):
newAnchor = GSAnchor.alloc().init()
newAnchor.name = anchorName
thisLayer.addAnchor_( newAnchor )
newPosition = NSPoint( x, y)
newAnchor.setPosition_( newPosition )
for thisLayer in selectedLayers:
glyph = thisLayer.parent
layerIndex = self.w.popUpButton.get()
popchoose = self.w.popUpButton.getItems()
for i in font.glyphs[glyph.name].layers:
if i.name == popchoose [layerIndex]:
backupLayer = i
allAnchors = backupLayer.anchors
for thisAnchor in allAnchors:
anchorName = thisAnchor.name
x = thisAnchor.position.x
y = thisAnchor.position.y
copyToFront (thisLayer, anchorName, x, y)
AnchorTeleporter()