forked from Tosche/Glyphs-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDuplicate Glyph with Component.py
66 lines (57 loc) · 1.92 KB
/
Duplicate Glyph with Component.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
#MenuTitle: Duplicate Glyph with Component
# -*- coding: utf-8 -*-
__doc__="""
Duplicates selected glyphs but as components, giving them 001 suffix or above depending on availability.
"""
import GlyphsApp
Font = Glyphs.font
FirstMasterID = Font.masters[0].id
def removeDuplicate(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
selectedLayers = removeDuplicate(Font.selectedLayers)
def removeSuffix(glyphName):
try:
if glyphName[-4] == "." and glyphName[-3:].isdigit:
return glyphName[:-4]
except:
return glyphName
def findSuffix( glyphName ):
glyphName = removeSuffix(glyphName)
nameIsFree = False
suffixNumber = 0
while nameIsFree is False:
suffixNumber += 1
suffix = ".%03d" % suffixNumber
if Font.glyphs[ glyphName+suffix ] == None:
nameIsFree = True
if suffixNumber == 100:
break
return suffix
def process( sourceLayer ):
sourceGlyphName = sourceLayer.parent.name
sourceGlyphName = removeSuffix(sourceGlyphName)
targetSuffix = findSuffix( sourceGlyphName )
# append suffix, create glyph:
targetGlyphName = sourceGlyphName + targetSuffix
targetGlyph = GSGlyph( targetGlyphName )
Font.glyphs.append( targetGlyph )
# place component to all layers in the new glyph:
sourceComponent = GSComponent( sourceGlyphName )
for thisMaster in Font.masters:
targetGlyph.layers[thisMaster.id].components.append(sourceComponent)
targetGlyph.layers[thisMaster.id].width = Font.glyphs[sourceGlyphName].layers[thisMaster.id].width
print "Added", targetGlyphName
return Font.glyphs[targetGlyphName].layers[Font.selectedFontMaster.id]
targetLayers =[]
for thisLayer in selectedLayers:
targetLayers.append( process(thisLayer) )
if Font.currentTab != None:
theTab = Font.currentTab
theTabLayers = theTab.layers
insertPos = theTab.textCursor + theTab.textRange
for layer in targetLayers:
theTabLayers.insert(insertPos, layer)
insertPos += 1
theTab.layers = theTabLayers