-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDuplicate Glyph with Component.py
More file actions
executable file
·78 lines (64 loc) · 2.22 KB
/
Duplicate Glyph with Component.py
File metadata and controls
executable file
·78 lines (64 loc) · 2.22 KB
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
#MenuTitle: Duplicate Glyph with Component
# -*- coding: utf-8 -*-
from __future__ import print_function, division, unicode_literals
__doc__ = """
Duplicates selected glyphs but as components, giving them 001 suffix or above depending on availability.
"""
from GlyphsApp import Glyphs, GSGlyph, GSComponent
f = Glyphs.font
# returns the base glyph name without ".00X" suffix.
# I'm doing it just in case the selected glyph already has such suffix.
def removeSuffix(glyphName):
try:
if glyphName[-4] == "." and glyphName[-3:].isdigit:
return glyphName[:-4]
else:
return glyphName
except: # glyph name too short, passes the test without removal
return glyphName
# returns the smallest available suffix number. Tries up to .100
def findSuffix(glyphName):
glyphName = removeSuffix(glyphName)
if glyphName is not None:
for i in range(1, 100):
suffix = ".%03d" % i
# if glyph name available, stop loop early
if f.glyphs[glyphName + suffix] is None:
break
return suffix
else:
return None
newGlyphs = []
# make set first before iteration to avoid running multiple times
# on the same glyph (that can happen if ran from Edit View)
for l in set(f.selectedLayers):
originGlyph = l.parent
# prepare new glyph name
newGlyphNameBase = removeSuffix(originGlyph.name)
newSuffix = findSuffix(newGlyphNameBase)
if newSuffix is not None:
newGlyphName = newGlyphNameBase + newSuffix
# add new glyph
newGlyph = GSGlyph(newGlyphName)
f.glyphs.append(newGlyph)
# newGlyph is currently empty.
# Place component to all layers in it.
for m in f.masters:
c = GSComponent(originGlyph.name)
c.alignment = True
newGlyph.layers[m.id].components.append(c)
# Setting width may be unncessary since component is auto-aligned
newGlyph.layers[m.id].width = originGlyph.layers[m.id].width
print("Added", newGlyphName)
newGlyphs.append(newGlyph)
# if the script was ran from Edit view, show the added glyphs
if f.currentTab is not None:
masterID = f.selectedFontMaster.id
theTab = f.currentTab
theTabLayers = list(theTab.layers)
insertPos = theTab.textCursor + theTab.textRange
for g in newGlyphs:
layer = g.layers[masterID]
theTabLayers.insert(insertPos, layer)
insertPos += 1
theTab.layers = theTabLayers