/usr/lib/python3/dist-packages/fontMath/mathKerning.py is in python3-fontmath 0.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | from __future__ import division, absolute_import
from copy import deepcopy
from fontMath.mathFunctions import add, sub, mul, div
from fontTools.misc.py23 import round2
"""
An object that serves kerning data from a
class kerning dictionary.
It scans a group dictionary and stores
a mapping of glyph to group relationships.
this map is then used to lookup kerning values.
"""
side1Prefix = "public.kern1."
side2Prefix = "public.kern2."
class MathKerning(object):
def __init__(self, kerning={}, groups={}):
self.update(kerning)
self.updateGroups(groups)
# -------
# Loading
# -------
def update(self, kerning):
self._kerning = dict(kerning)
def updateGroups(self, groups):
self._groups = {}
self._side1GroupMap = {}
self._side2GroupMap = {}
self._side1Groups = {}
self._side2Groups = {}
for groupName, glyphList in groups.items():
if groupName.startswith(side1Prefix):
self._groups[groupName] = list(glyphList)
self._side1Groups[groupName] = glyphList
for glyphName in glyphList:
self._side1GroupMap[glyphName] = groupName
elif groupName.startswith(side2Prefix):
self._groups[groupName] = list(glyphList)
self._side2Groups[groupName] = glyphList
for glyphName in glyphList:
self._side2GroupMap[glyphName] = groupName
def addTo(self, value):
for k, v in self._kerning.items():
self._kerning[k] = v + value
# -------------
# dict Behavior
# -------------
def keys(self):
return self._kerning.keys()
def values(self):
return self._kerning.values()
def items(self):
return self._kerning.items()
def groups(self):
return deepcopy(self._groups)
def __contains__(self, pair):
return pair in self._kerning
def __getitem__(self, pair):
if pair in self._kerning:
return self._kerning[pair]
side1, side2 = pair
if side1.startswith(side1Prefix):
side1Group = side1
side1 = None
else:
side1Group = self._side1GroupMap.get(side1)
if side2.startswith(side2Prefix):
side2Group = side2
side2 = None
else:
side2Group = self._side2GroupMap.get(side2)
if (side1Group, side2) in self._kerning:
return self._kerning[side1Group, side2]
elif (side1, side2Group) in self._kerning:
return self._kerning[side1, side2Group]
elif (side1Group, side2Group) in self._kerning:
return self._kerning[side1Group, side2Group]
else:
return 0
def get(self, pair):
v = self[pair]
return v
# ---------
# Pair Type
# ---------
def guessPairType(self, pair):
side1, side2 = pair
if side1.startswith(side1Prefix):
side1Group = side1
else:
side1Group = self._side1GroupMap.get(side1)
if side2.startswith(side2Prefix):
side2Group = side2
else:
side2Group = self._side2GroupMap.get(side2)
side1Type = side2Type = "glyph"
if pair in self:
if side1 == side1Group:
side1Type = "group"
elif side1Group is not None:
side1Type = "exception"
if side2 == side2Group:
side2Type = "group"
elif side2Group is not None:
side2Type = "exception"
elif (side1Group, side2) in self:
side1Type = "group"
if side2Group is not None:
if side2 != side2Group:
side2Type = "exception"
elif (side1, side2Group) in self:
side2Type = "group"
if side1Group is not None:
if side1 != side1Group:
side1Type = "exception"
else:
if side1Group is not None:
side1Type = "group"
if side2Group is not None:
side2Type = "group"
return side1Type, side2Type
# ----
# Copy
# ----
def copy(self):
k = MathKerning(self._kerning)
k._side1Groups = deepcopy(self._side1Groups)
k._side2Groups = deepcopy(self._side2Groups)
k._side1GroupMap = deepcopy(self._side1GroupMap)
k._side2GroupMap = deepcopy(self._side2GroupMap)
return k
# ----
# Math
# ----
# math with other kerning
def __add__(self, other):
k = self._processMathOne(other, add)
k.cleanup()
return k
def __sub__(self, other):
k = self._processMathOne(other, sub)
k.cleanup()
return k
def _processMathOne(self, other, funct):
comboPairs = set(self._kerning.keys()) | set(other._kerning.keys())
kerning = dict.fromkeys(comboPairs, None)
for k in comboPairs:
v1 = self.get(k)
v2 = other.get(k)
v = funct(v1, v2)
kerning[k] = v
g1 = self.groups()
g2 = other.groups()
if g1 == g2 or not g1 or not g2:
groups = g1 or g2
else:
comboGroups = set(g1.keys()) | set(g2.keys())
groups = dict.fromkeys(comboGroups, None)
for groupName in comboGroups:
s1 = set(g1.get(groupName, []))
s2 = set(g2.get(groupName, []))
groups[groupName] = sorted(list(s1 | s2))
ks = MathKerning(kerning, groups)
return ks
# math with factor
def __mul__(self, factor):
if isinstance(factor, tuple):
factor = factor[0]
k = self._processMathTwo(factor, mul)
k.cleanup()
return k
def __rmul__(self, factor):
if isinstance(factor, tuple):
factor = factor[0]
k = self._processMathTwo(factor, mul)
k.cleanup()
return k
def __div__(self, factor):
if isinstance(factor, tuple):
factor = factor[0]
k = self._processMathTwo(factor, div)
k.cleanup()
return k
__truediv__ = __div__
def _processMathTwo(self, factor, funct):
kerning = deepcopy(self._kerning)
for k, v in self._kerning.items():
v = funct(v, factor)
kerning[k] = v
ks = MathKerning(kerning)
ks._side1Groups = deepcopy(self._side1Groups)
ks._side2Groups = deepcopy(self._side2Groups)
ks._side1GroupMap = deepcopy(self._side1GroupMap)
ks._side2GroupMap = deepcopy(self._side2GroupMap)
return ks
# ---------
# More math
# ---------
def round(self, multiple=1):
multiple = float(multiple)
for k, v in self._kerning.items():
self._kerning[k] = int(round2(int(round2(v / multiple)) * multiple))
# -------
# Cleanup
# -------
def cleanup(self):
for (side1, side2), v in list(self._kerning.items()):
if int(v) == v:
v = int(v)
self._kerning[side1, side2] = v
if v == 0:
side1Type, side2Type = self.guessPairType((side1, side2))
if side1Type != "exception" and side2Type != "exception":
del self._kerning[side1, side2]
# ----------
# Extraction
# ----------
def extractKerning(self, font):
font.kerning.clear()
font.kerning.update(self._kerning)
font.groups.update(self.groups())
# -------
# Sorting
# -------
def _isLessish(self, first, second):
if len(first) < len(second):
return True
elif len(first) > len(second):
return False
first_keys = sorted(first)
second_keys = sorted(second)
for i, key in enumerate(first_keys):
if first_keys[i] < second_keys[i]:
return True
elif first_keys[i] > second_keys[i]:
return False
elif first[key] < second[key]:
return True
return None
def __lt__(self, other):
if other is None:
return False
lessish = self._isLessish(self._kerning, other._kerning)
if lessish is not None:
return lessish
lessish = self._isLessish(self._side1Groups, other._side1Groups)
if lessish is not None:
return lessish
lessish = self._isLessish(self._side2Groups, other._side2Groups)
if lessish is not None:
return lessish
return False
def __eq__(self, other):
if self._kerning != other._kerning:
return False
if self._side1Groups != other._side1Groups:
return False
if self._side2Groups != other._side2Groups:
return False
return True
if __name__ == "__main__":
import sys
import doctest
sys.exit(doctest.testmod().failed)
|