-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockLayerMetrics.py
59 lines (50 loc) · 2.48 KB
/
LockLayerMetrics.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
from mojo.events import addObserver, removeObserver
class LockLayerMetrics():
"""
Lock Layer Metrics
-- Andy Clymer, github.com/andyclymer
Add as a RoboFont Startup Script to permanently apply all "foreground" layer
width and left side bearing changes to all other layers.
"""
def __init__(self):
self.glyph = None
self.prevLeftMargins = []
addObserver(self, "currentGlyphChanged", "currentGlyphChanged")
def currentGlyphChanged(self, info):
if self.glyph:
# If there was already a glyph, remove its observers
self.glyph.removeObserver(self, "Glyph.WidthChanged")
self.glyph.removeObserver(self, "Glyph.Changed")
self.glyph = info["glyph"]
if self.glyph:
# If there's a new glyph,
# only subscribe to notifications on the foregronud layer
currentLayer = self.glyph.layerName
if currentLayer == "foreground":
self.glyph.addObserver(self, "widthChanged", "Glyph.WidthChanged")
self.glyph.addObserver(self, "glyphChanged", "Glyph.Changed")
self.glyphChanged(None)
else: self.glyph = None
def glyphChanged(self, info):
# Every time the glyph drawing changes, hold aside the LSB, but only keep the last two values.
# This is beacause when a width does finally change, glyphChanged() will be called first,
# so we really need to know the second to last LSB.
if self.glyph:
self.prevLeftMargins.insert(0, self.glyph.leftMargin)
if len(self.prevLeftMargins) == 3:
self.prevLeftMargins = self.prevLeftMargins[:2]
else: self.prevLeftMargins.clear()
def widthChanged(self, info):
# Every time the width changes, apply the difference of the last LSB in the list,
# which would be the previous LSB measurement and not the new measurement, and update the width
if self.glyph:
leftDiff = 0
if len(self.prevLeftMargins):
leftDiff = self.glyph.leftMargin - self.prevLeftMargins[-1]
currentLayer = self.glyph.layerName
for layerName in self.glyph.font.layerOrder:
if not layerName == currentLayer:
layerGlyph = self.glyph.getLayer(layerName)
layerGlyph.leftMargin += leftDiff
layerGlyph.width = self.glyph.width
LockLayerMetrics()