| 1 |
from __future__ import print_function, absolute_import |
| 2 |
from copy import deepcopy |
| 3 |
from collections import OrderedDict |
| 4 |
from fontMath.mathFunctions import ( |
| 5 |
add, addPt, div, divPt, mul, mulPt, _roundNumber, sub, subPt) |
| 6 |
from fontMath.mathGuideline import ( |
| 7 |
_compressGuideline, _expandGuideline, _pairGuidelines, |
| 8 |
_processMathOneGuidelines, _processMathTwoGuidelines, _roundGuidelines) |
| 9 |
from fontTools.pens.pointPen import AbstractPointPen |
| 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 |
try: |
| 54 |
basestring, xrange |
| 55 |
range = xrange |
| 56 |
except NameError: |
| 57 |
basestring = str |
| 58 |
|
| 59 |
|
| 60 |
class MathGlyph(object): |
| 61 |
|
| 62 |
""" |
| 63 |
A very shallow glyph object for rapid math operations. |
| 64 |
|
| 65 |
Notes about glyph math: |
| 66 |
- absolute contour compatibility is required |
| 67 |
- absolute component, anchor, guideline and image compatibility is NOT required. |
| 68 |
in cases of incompatibility in this data, only compatible data is processed and |
| 69 |
returned. because of this, anchors and components may not be returned in the |
| 70 |
same order as the original. |
| 71 |
""" |
| 72 |
|
| 73 |
def __init__(self, glyph, scaleComponentTransform=True, strict=False): |
| 74 |
"""Initialize a new MathGlyph object. |
| 75 |
|
| 76 |
Args: |
| 77 |
glyph: Input defcon or defcon-like Glyph object to copy from. Set to None to |
| 78 |
to make an empty MathGlyph. |
| 79 |
scaleComponentTransform (bool): when performing multiplication or division, by |
| 80 |
default all elements of a component's affine transformation matrix are |
| 81 |
multiplied by the given scalar. If scaleComponentTransform is False, then |
| 82 |
only the component's xOffset and yOffset attributes are scaled, whereas the |
| 83 |
xScale, xyScale, yxScale and yScale attributes are kept unchanged. |
| 84 |
strict (bool): when set to False, offcurve points will be added to all |
| 85 |
straight segments to improve compatibility. Any offcurves that are |
| 86 |
still on-point will be filtered when extracted. When set to True, |
| 87 |
no offcurves will be added or filtered. |
| 88 |
""" |
| 89 |
self.scaleComponentTransform = scaleComponentTransform |
| 90 |
self.contours = [] |
| 91 |
self.components = [] |
| 92 |
self.strict = strict |
| 93 |
if glyph is None: |
| 94 |
self.anchors = [] |
| 95 |
self.guidelines = [] |
| 96 |
self.image = _expandImage(None) |
| 97 |
self.lib = {} |
| 98 |
self.name = None |
| 99 |
self.unicodes = None |
| 100 |
self.width = None |
| 101 |
self.height = None |
| 102 |
self.note = None |
| 103 |
else: |
| 104 |
p = MathGlyphPen(self, strict=self.strict) |
| 105 |
glyph.drawPoints(p) |
| 106 |
self.anchors = [dict(anchor) for anchor in glyph.anchors] |
| 107 |
self.guidelines = [_expandGuideline(guideline) for guideline in glyph.guidelines] |
| 108 |
self.image = _expandImage(glyph.image) |
| 109 |
self.lib = deepcopy(dict(glyph.lib)) |
| 110 |
self.name = glyph.name |
| 111 |
self.unicodes = list(glyph.unicodes) |
| 112 |
self.width = glyph.width |
| 113 |
self.height = glyph.height |
| 114 |
self.note = glyph.note |
| 115 |
|
| 116 |
def __eq__(self, other): |
| 117 |
try: |
| 118 |
return all(getattr(self, attr) == getattr(other, attr) |
| 119 |
for attr in ("name", "unicodes", "width", "height", |
| 120 |
"note", "lib", "contours", "components", |
| 121 |
"anchors", "guidelines", "image")) |
| 122 |
except AttributeError: |
| 123 |
return NotImplemented |
| 124 |
|
| 125 |
def __ne__(self, other): |
| 126 |
return not self == other |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
def copy(self): |
| 133 |
"""return a new MathGlyph containing all data in self""" |
| 134 |
return MathGlyph(self, scaleComponentTransform=self.scaleComponentTransform, strict=self.strict) |
| 135 |
|
| 136 |
def copyWithoutMathSubObjects(self): |
| 137 |
""" |
| 138 |
return a new MathGlyph containing all data except: |
| 139 |
contours |
| 140 |
components |
| 141 |
anchors |
| 142 |
guidelines |
| 143 |
|
| 144 |
this is used mainly for internal glyph math. |
| 145 |
""" |
| 146 |
n = MathGlyph(None, scaleComponentTransform=self.scaleComponentTransform, strict=self.strict) |
| 147 |
n.name = self.name |
| 148 |
if self.unicodes is not None: |
| 149 |
n.unicodes = list(self.unicodes) |
| 150 |
n.width = self.width |
| 151 |
n.height = self.height |
| 152 |
n.note = self.note |
| 153 |
n.lib = deepcopy(dict(self.lib)) |
| 154 |
return n |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
def __add__(self, otherGlyph): |
| 163 |
copiedGlyph = self.copyWithoutMathSubObjects() |
| 164 |
self._processMathOne(copiedGlyph, otherGlyph, addPt, add) |
| 165 |
return copiedGlyph |
| 166 |
|
| 167 |
def __sub__(self, otherGlyph): |
| 168 |
copiedGlyph = self.copyWithoutMathSubObjects() |
| 169 |
self._processMathOne(copiedGlyph, otherGlyph, subPt, sub) |
| 170 |
return copiedGlyph |
| 171 |
|
| 172 |
def _processMathOne(self, copiedGlyph, otherGlyph, ptFunc, func): |
| 173 |
|
| 174 |
copiedGlyph.width = func(self.width, otherGlyph.width) |
| 175 |
|
| 176 |
copiedGlyph.height = func(self.height, otherGlyph.height) |
| 177 |
|
| 178 |
copiedGlyph.contours = [] |
| 179 |
if self.contours: |
| 180 |
copiedGlyph.contours = _processMathOneContours(self.contours, otherGlyph.contours, ptFunc) |
| 181 |
|
| 182 |
copiedGlyph.components = [] |
| 183 |
if self.components: |
| 184 |
componentPairs = _pairComponents(self.components, otherGlyph.components) |
| 185 |
copiedGlyph.components = _processMathOneComponents(componentPairs, ptFunc) |
| 186 |
|
| 187 |
copiedGlyph.anchors = [] |
| 188 |
if self.anchors: |
| 189 |
anchorTree1 = _anchorTree(self.anchors) |
| 190 |
anchorTree2 = _anchorTree(otherGlyph.anchors) |
| 191 |
anchorPairs = _pairAnchors(anchorTree1, anchorTree2) |
| 192 |
copiedGlyph.anchors = _processMathOneAnchors(anchorPairs, ptFunc) |
| 193 |
|
| 194 |
copiedGlyph.guidelines = [] |
| 195 |
if self.guidelines: |
| 196 |
guidelinePairs = _pairGuidelines(self.guidelines, otherGlyph.guidelines) |
| 197 |
copiedGlyph.guidelines = _processMathOneGuidelines(guidelinePairs, ptFunc, func) |
| 198 |
|
| 199 |
copiedGlyph.image = _expandImage(None) |
| 200 |
imagePair = _pairImages(self.image, otherGlyph.image) |
| 201 |
if imagePair: |
| 202 |
copiedGlyph.image = _processMathOneImage(imagePair, ptFunc) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
def __mul__(self, factor): |
| 207 |
if not isinstance(factor, tuple): |
| 208 |
factor = (factor, factor) |
| 209 |
copiedGlyph = self.copyWithoutMathSubObjects() |
| 210 |
self._processMathTwo(copiedGlyph, factor, mulPt, mul) |
| 211 |
return copiedGlyph |
| 212 |
|
| 213 |
__rmul__ = __mul__ |
| 214 |
|
| 215 |
def __div__(self, factor): |
| 216 |
if not isinstance(factor, tuple): |
| 217 |
factor = (factor, factor) |
| 218 |
copiedGlyph = self.copyWithoutMathSubObjects() |
| 219 |
self._processMathTwo(copiedGlyph, factor, divPt, div) |
| 220 |
return copiedGlyph |
| 221 |
|
| 222 |
__truediv__ = __div__ |
| 223 |
|
| 224 |
__rdiv__ = __div__ |
| 225 |
|
| 226 |
__rtruediv__ = __rdiv__ |
| 227 |
|
| 228 |
def _processMathTwo(self, copiedGlyph, factor, ptFunc, func): |
| 229 |
|
| 230 |
copiedGlyph.width = func(self.width, factor[0]) |
| 231 |
|
| 232 |
copiedGlyph.height = func(self.height, factor[1]) |
| 233 |
|
| 234 |
copiedGlyph.contours = [] |
| 235 |
if self.contours: |
| 236 |
copiedGlyph.contours = _processMathTwoContours(self.contours, factor, ptFunc) |
| 237 |
|
| 238 |
copiedGlyph.components = [] |
| 239 |
if self.components: |
| 240 |
copiedGlyph.components = _processMathTwoComponents( |
| 241 |
self.components, factor, ptFunc, scaleComponentTransform=self.scaleComponentTransform |
| 242 |
) |
| 243 |
|
| 244 |
copiedGlyph.anchors = [] |
| 245 |
if self.anchors: |
| 246 |
copiedGlyph.anchors = _processMathTwoAnchors(self.anchors, factor, ptFunc) |
| 247 |
|
| 248 |
copiedGlyph.guidelines = [] |
| 249 |
if self.guidelines: |
| 250 |
copiedGlyph.guidelines = _processMathTwoGuidelines(self.guidelines, factor, func) |
| 251 |
|
| 252 |
if self.image: |
| 253 |
copiedGlyph.image = _processMathTwoImage(self.image, factor, ptFunc) |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
def round(self, digits=None): |
| 259 |
"""round the geometry.""" |
| 260 |
copiedGlyph = self.copyWithoutMathSubObjects() |
| 261 |
|
| 262 |
copiedGlyph.width = _roundNumber(self.width, digits) |
| 263 |
copiedGlyph.height = _roundNumber(self.height, digits) |
| 264 |
|
| 265 |
copiedGlyph.contours = [] |
| 266 |
if self.contours: |
| 267 |
copiedGlyph.contours = _roundContours(self.contours, digits) |
| 268 |
|
| 269 |
copiedGlyph.components = [] |
| 270 |
if self.components: |
| 271 |
copiedGlyph.components = _roundComponents(self.components, digits) |
| 272 |
|
| 273 |
copiedGlyph.guidelines = [] |
| 274 |
if self.guidelines: |
| 275 |
copiedGlyph.guidelines = _roundGuidelines(self.guidelines, digits) |
| 276 |
|
| 277 |
copiedGlyph.anchors = [] |
| 278 |
if self.anchors: |
| 279 |
copiedGlyph.anchors = _roundAnchors(self.anchors, digits) |
| 280 |
|
| 281 |
copiedGlyph.image = None |
| 282 |
if self.image: |
| 283 |
copiedGlyph.image = _roundImage(self.image, digits) |
| 284 |
return copiedGlyph |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
def getPointPen(self): |
| 292 |
"""get a point pen for drawing to this object""" |
| 293 |
return MathGlyphPen(self) |
| 294 |
|
| 295 |
def drawPoints(self, pointPen, filterRedundantPoints=False): |
| 296 |
"""draw self using pointPen""" |
| 297 |
if filterRedundantPoints: |
| 298 |
pointPen = FilterRedundantPointPen(pointPen) |
| 299 |
for contour in self.contours: |
| 300 |
pointPen.beginPath(identifier=contour["identifier"]) |
| 301 |
for segmentType, pt, smooth, name, identifier in contour["points"]: |
| 302 |
pointPen.addPoint(pt=pt, segmentType=segmentType, smooth=smooth, name=name, identifier=identifier) |
| 303 |
pointPen.endPath() |
| 304 |
for component in self.components: |
| 305 |
pointPen.addComponent(component["baseGlyph"], component["transformation"], identifier=component["identifier"]) |
| 306 |
|
| 307 |
def draw(self, pen, filterRedundantPoints=False): |
| 308 |
"""draw self using pen""" |
| 309 |
from fontTools.pens.pointPen import PointToSegmentPen |
| 310 |
pointPen = PointToSegmentPen(pen) |
| 311 |
self.drawPoints(pointPen, filterRedundantPoints=filterRedundantPoints) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
def extractGlyph(self, glyph, pointPen=None, onlyGeometry=False): |
| 318 |
""" |
| 319 |
"rehydrate" to a glyph. this requires |
| 320 |
a glyph as an argument. if a point pen other |
| 321 |
than the type of pen returned by glyph.getPointPen() |
| 322 |
is required for drawing, send this the needed point pen. |
| 323 |
""" |
| 324 |
if pointPen is None: |
| 325 |
pointPen = glyph.getPointPen() |
| 326 |
glyph.clearContours() |
| 327 |
glyph.clearComponents() |
| 328 |
glyph.clearAnchors() |
| 329 |
glyph.clearGuidelines() |
| 330 |
glyph.lib.clear() |
| 331 |
if self.strict: |
| 332 |
self.drawPoints(pointPen) |
| 333 |
else: |
| 334 |
cleanerPen = FilterRedundantPointPen(pointPen) |
| 335 |
self.drawPoints(cleanerPen) |
| 336 |
glyph.anchors = [dict(anchor) for anchor in self.anchors] |
| 337 |
glyph.guidelines = [_compressGuideline(guideline) for guideline in self.guidelines] |
| 338 |
glyph.image = _compressImage(self.image) |
| 339 |
glyph.lib = deepcopy(dict(self.lib)) |
| 340 |
glyph.width = self.width |
| 341 |
glyph.height = self.height |
| 342 |
glyph.note = self.note |
| 343 |
if not onlyGeometry: |
| 344 |
glyph.name = self.name |
| 345 |
glyph.unicodes = list(self.unicodes) |
| 346 |
return glyph |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
class MathGlyphPen(AbstractPointPen): |
| 354 |
|
| 355 |
""" |
| 356 |
Point pen for building MathGlyph data structures. |
| 357 |
""" |
| 358 |
|
| 359 |
def __init__(self, glyph=None, strict=False): |
| 360 |
self.strict = strict |
| 361 |
if glyph is None: |
| 362 |
self.contours = [] |
| 363 |
self.components = [] |
| 364 |
else: |
| 365 |
self.contours = glyph.contours |
| 366 |
self.components = glyph.components |
| 367 |
self._contourIdentifier = None |
| 368 |
self._points = [] |
| 369 |
|
| 370 |
def _flushContour(self): |
| 371 |
""" |
| 372 |
This normalizes the contour so that: |
| 373 |
- there are no line segments. in their place will be |
| 374 |
curve segments with the off curves positioned on top |
| 375 |
of the previous on curve and the new curve on curve. |
| 376 |
- the contour starts with an on curve |
| 377 |
""" |
| 378 |
self.contours.append( |
| 379 |
dict(identifier=self._contourIdentifier, points=[]) |
| 380 |
) |
| 381 |
contourPoints = self.contours[-1]["points"] |
| 382 |
points = self._points |
| 383 |
|
| 384 |
if not self.strict: |
| 385 |
haveOnCurve = False |
| 386 |
for point in points: |
| 387 |
if point[0] is not None: |
| 388 |
haveOnCurve = True |
| 389 |
break |
| 390 |
if haveOnCurve: |
| 391 |
while 1: |
| 392 |
if points[0][0] is None: |
| 393 |
point = points.pop(0) |
| 394 |
points.append(point) |
| 395 |
else: |
| 396 |
break |
| 397 |
|
| 398 |
holdingOffCurves = [] |
| 399 |
for index, point in enumerate(points): |
| 400 |
segmentType = point[0] |
| 401 |
if segmentType == "line" and not self.strict: |
| 402 |
pt, smooth, name, identifier = point[1:] |
| 403 |
prevPt = points[index - 1][1] |
| 404 |
if index == 0: |
| 405 |
holdingOffCurves.append((None, prevPt, False, None, None)) |
| 406 |
holdingOffCurves.append((None, pt, False, None, None)) |
| 407 |
else: |
| 408 |
contourPoints.append((None, prevPt, False, None, None)) |
| 409 |
contourPoints.append((None, pt, False, None, None)) |
| 410 |
contourPoints.append(("curve", pt, smooth, name, identifier)) |
| 411 |
else: |
| 412 |
contourPoints.append(point) |
| 413 |
contourPoints.extend(holdingOffCurves) |
| 414 |
|
| 415 |
def beginPath(self, identifier=None): |
| 416 |
self._contourIdentifier = identifier |
| 417 |
self._points = [] |
| 418 |
|
| 419 |
def addPoint(self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs): |
| 420 |
self._points.append((segmentType, pt, smooth, name, identifier)) |
| 421 |
|
| 422 |
def endPath(self): |
| 423 |
self._flushContour() |
| 424 |
|
| 425 |
def addComponent(self, baseGlyph, transformation, identifier=None, **kwargs): |
| 426 |
self.components.append(dict(baseGlyph=baseGlyph, transformation=transformation, identifier=identifier)) |
| 427 |
|
| 428 |
|
| 429 |
class FilterRedundantPointPen(AbstractPointPen): |
| 430 |
|
| 431 |
def __init__(self, anotherPointPen): |
| 432 |
self._pen = anotherPointPen |
| 433 |
self._points = [] |
| 434 |
|
| 435 |
def _flushContour(self): |
| 436 |
|
| 437 |
|
| 438 |
points = self._points |
| 439 |
for index, data in enumerate(points): |
| 440 |
if data["segmentType"] == "curve": |
| 441 |
prevOnCurve = points[index - 3] |
| 442 |
prevOffCurve1 = points[index - 2] |
| 443 |
prevOffCurve2 = points[index - 1] |
| 444 |
|
| 445 |
if prevOnCurve["segmentType"] is not None: |
| 446 |
if prevOnCurve["pt"] == prevOffCurve1["pt"] and prevOffCurve2["pt"] == data["pt"]: |
| 447 |
|
| 448 |
|
| 449 |
data["segmentType"] = "line" |
| 450 |
|
| 451 |
prevOffCurve1["removed"] = True |
| 452 |
prevOffCurve2["removed"] = True |
| 453 |
|
| 454 |
for data in points: |
| 455 |
if not data["removed"]: |
| 456 |
self._pen.addPoint( |
| 457 |
data["pt"], |
| 458 |
data["segmentType"], |
| 459 |
smooth=data["smooth"], |
| 460 |
name=data["name"], |
| 461 |
identifier=data["identifier"] |
| 462 |
) |
| 463 |
|
| 464 |
def beginPath(self, identifier=None, **kwargs): |
| 465 |
self._points = [] |
| 466 |
self._pen.beginPath(identifier=identifier) |
| 467 |
|
| 468 |
def addPoint(self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs): |
| 469 |
self._points.append( |
| 470 |
dict( |
| 471 |
pt=pt, |
| 472 |
segmentType=segmentType, |
| 473 |
smooth=smooth, |
| 474 |
name=name, |
| 475 |
identifier=identifier, |
| 476 |
removed=False |
| 477 |
) |
| 478 |
) |
| 479 |
|
| 480 |
def endPath(self): |
| 481 |
self._flushContour() |
| 482 |
self._pen.endPath() |
| 483 |
|
| 484 |
def addComponent(self, baseGlyph, transformation, identifier=None, **kwargs): |
| 485 |
self._pen.addComponent(baseGlyph, transformation, identifier) |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
def _processMathOneContours(contours1, contours2, func): |
| 495 |
result = [] |
| 496 |
for index, contour1 in enumerate(contours1): |
| 497 |
contourIdentifier = contour1["identifier"] |
| 498 |
points1 = contour1["points"] |
| 499 |
points2 = contours2[index]["points"] |
| 500 |
resultPoints = [] |
| 501 |
for index, point in enumerate(points1): |
| 502 |
segmentType, pt1, smooth, name, identifier = point |
| 503 |
pt2 = points2[index][1] |
| 504 |
pt = func(pt1, pt2) |
| 505 |
resultPoints.append((segmentType, pt, smooth, name, identifier)) |
| 506 |
result.append(dict(identifier=contourIdentifier, points=resultPoints)) |
| 507 |
return result |
| 508 |
|
| 509 |
def _processMathTwoContours(contours, factor, func): |
| 510 |
result = [] |
| 511 |
for contour in contours: |
| 512 |
contourIdentifier = contour["identifier"] |
| 513 |
points = contour["points"] |
| 514 |
resultPoints = [] |
| 515 |
for point in points: |
| 516 |
segmentType, pt, smooth, name, identifier = point |
| 517 |
pt = func(pt, factor) |
| 518 |
resultPoints.append((segmentType, pt, smooth, name, identifier)) |
| 519 |
result.append(dict(identifier=contourIdentifier, points=resultPoints)) |
| 520 |
return result |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
def _anchorTree(anchors): |
| 525 |
tree = OrderedDict() |
| 526 |
for anchor in anchors: |
| 527 |
x = anchor["x"] |
| 528 |
y = anchor["y"] |
| 529 |
name = anchor.get("name") |
| 530 |
identifier = anchor.get("identifier") |
| 531 |
color = anchor.get("color") |
| 532 |
if name not in tree: |
| 533 |
tree[name] = [] |
| 534 |
tree[name].append((identifier, x, y, color)) |
| 535 |
return tree |
| 536 |
|
| 537 |
def _pairAnchors(anchorDict1, anchorDict2): |
| 538 |
""" |
| 539 |
Anchors are paired using the following rules: |
| 540 |
|
| 541 |
|
| 542 |
Matching Identifiers |
| 543 |
-------------------- |
| 544 |
>>> anchors1 = { |
| 545 |
... "test" : [ |
| 546 |
... (None, 1, 2, None), |
| 547 |
... ("identifier 1", 3, 4, None) |
| 548 |
... ] |
| 549 |
... } |
| 550 |
>>> anchors2 = { |
| 551 |
... "test" : [ |
| 552 |
... ("identifier 1", 1, 2, None), |
| 553 |
... (None, 3, 4, None) |
| 554 |
... ] |
| 555 |
... } |
| 556 |
>>> expected = [ |
| 557 |
... ( |
| 558 |
... dict(name="test", identifier=None, x=1, y=2, color=None), |
| 559 |
... dict(name="test", identifier=None, x=3, y=4, color=None) |
| 560 |
... ), |
| 561 |
... ( |
| 562 |
... dict(name="test", identifier="identifier 1", x=3, y=4, color=None), |
| 563 |
... dict(name="test", identifier="identifier 1", x=1, y=2, color=None) |
| 564 |
... ) |
| 565 |
... ] |
| 566 |
>>> _pairAnchors(anchors1, anchors2) == expected |
| 567 |
True |
| 568 |
|
| 569 |
Mismatched Identifiers |
| 570 |
---------------------- |
| 571 |
>>> anchors1 = { |
| 572 |
... "test" : [ |
| 573 |
... ("identifier 1", 3, 4, None) |
| 574 |
... ] |
| 575 |
... } |
| 576 |
>>> anchors2 = { |
| 577 |
... "test" : [ |
| 578 |
... ("identifier 2", 1, 2, None), |
| 579 |
... ] |
| 580 |
... } |
| 581 |
>>> expected = [ |
| 582 |
... ( |
| 583 |
... dict(name="test", identifier="identifier 1", x=3, y=4, color=None), |
| 584 |
... dict(name="test", identifier="identifier 2", x=1, y=2, color=None) |
| 585 |
... ) |
| 586 |
... ] |
| 587 |
>>> _pairAnchors(anchors1, anchors2) == expected |
| 588 |
True |
| 589 |
""" |
| 590 |
pairs = [] |
| 591 |
for name, anchors1 in anchorDict1.items(): |
| 592 |
if name not in anchorDict2: |
| 593 |
continue |
| 594 |
anchors2 = anchorDict2[name] |
| 595 |
|
| 596 |
removeFromAnchors1 = [] |
| 597 |
for anchor1 in anchors1: |
| 598 |
match = None |
| 599 |
identifier = anchor1[0] |
| 600 |
for anchor2 in anchors2: |
| 601 |
if anchor2[0] == identifier: |
| 602 |
match = anchor2 |
| 603 |
break |
| 604 |
if match is not None: |
| 605 |
anchor2 = match |
| 606 |
anchors2.remove(anchor2) |
| 607 |
removeFromAnchors1.append(anchor1) |
| 608 |
a1 = dict(name=name, identifier=identifier) |
| 609 |
a1["x"], a1["y"], a1["color"] = anchor1[1:] |
| 610 |
a2 = dict(name=name, identifier=identifier) |
| 611 |
a2["x"], a2["y"], a2["color"] = anchor2[1:] |
| 612 |
pairs.append((a1, a2)) |
| 613 |
for anchor1 in removeFromAnchors1: |
| 614 |
anchors1.remove(anchor1) |
| 615 |
if not anchors1 or not anchors2: |
| 616 |
continue |
| 617 |
|
| 618 |
while 1: |
| 619 |
anchor1 = anchors1.pop(0) |
| 620 |
anchor2 = anchors2.pop(0) |
| 621 |
a1 = dict(name=name) |
| 622 |
a1["identifier"], a1["x"], a1["y"], a1["color"] = anchor1 |
| 623 |
a2 = dict(name=name, identifier=identifier) |
| 624 |
a2["identifier"], a2["x"], a2["y"], a2["color"] = anchor2 |
| 625 |
pairs.append((a1, a2)) |
| 626 |
if not anchors1: |
| 627 |
break |
| 628 |
if not anchors2: |
| 629 |
break |
| 630 |
return pairs |
| 631 |
|
| 632 |
def _processMathOneAnchors(anchorPairs, func): |
| 633 |
result = [] |
| 634 |
for anchor1, anchor2 in anchorPairs: |
| 635 |
anchor = dict(anchor1) |
| 636 |
pt1 = (anchor1["x"], anchor1["y"]) |
| 637 |
pt2 = (anchor2["x"], anchor2["y"]) |
| 638 |
anchor["x"], anchor["y"] = func(pt1, pt2) |
| 639 |
result.append(anchor) |
| 640 |
return result |
| 641 |
|
| 642 |
def _processMathTwoAnchors(anchors, factor, func): |
| 643 |
result = [] |
| 644 |
for anchor in anchors: |
| 645 |
anchor = dict(anchor) |
| 646 |
pt = (anchor["x"], anchor["y"]) |
| 647 |
anchor["x"], anchor["y"] = func(pt, factor) |
| 648 |
result.append(anchor) |
| 649 |
return result |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
def _pairComponents(components1, components2): |
| 654 |
components1 = list(components1) |
| 655 |
components2 = list(components2) |
| 656 |
pairs = [] |
| 657 |
|
| 658 |
removeFromComponents1 = [] |
| 659 |
for component1 in components1: |
| 660 |
baseGlyph = component1["baseGlyph"] |
| 661 |
identifier = component1["identifier"] |
| 662 |
match = None |
| 663 |
for component2 in components2: |
| 664 |
if component2["baseGlyph"] == baseGlyph and component2["identifier"] == identifier: |
| 665 |
match = component2 |
| 666 |
break |
| 667 |
if match is not None: |
| 668 |
component2 = match |
| 669 |
removeFromComponents1.append(component1) |
| 670 |
components2.remove(component2) |
| 671 |
pairs.append((component1, component2)) |
| 672 |
for component1 in removeFromComponents1: |
| 673 |
components1.remove(component1) |
| 674 |
|
| 675 |
for component1 in components1: |
| 676 |
baseGlyph = component1["baseGlyph"] |
| 677 |
for component2 in components2: |
| 678 |
if component2["baseGlyph"] == baseGlyph: |
| 679 |
components2.remove(component2) |
| 680 |
pairs.append((component1, component2)) |
| 681 |
break |
| 682 |
return pairs |
| 683 |
|
| 684 |
def _processMathOneComponents(componentPairs, func): |
| 685 |
result = [] |
| 686 |
for component1, component2 in componentPairs: |
| 687 |
component = dict(component1) |
| 688 |
component["transformation"] = _processMathOneTransformation(component1["transformation"], component2["transformation"], func) |
| 689 |
result.append(component) |
| 690 |
return result |
| 691 |
|
| 692 |
def _processMathTwoComponents(components, factor, func, scaleComponentTransform=True): |
| 693 |
result = [] |
| 694 |
for component in components: |
| 695 |
component = dict(component) |
| 696 |
component["transformation"] = _processMathTwoTransformation( |
| 697 |
component["transformation"], factor, func, doScale=scaleComponentTransform |
| 698 |
) |
| 699 |
result.append(component) |
| 700 |
return result |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
_imageTransformationKeys = "xScale xyScale yxScale yScale xOffset yOffset".split(" ") |
| 705 |
_defaultImageTransformation = (1, 0, 0, 1, 0, 0) |
| 706 |
_defaultImageTransformationDict = {} |
| 707 |
for key, value in zip(_imageTransformationKeys, _defaultImageTransformation): |
| 708 |
_defaultImageTransformationDict[key] = value |
| 709 |
|
| 710 |
def _expandImage(image): |
| 711 |
if image is None: |
| 712 |
fileName = None |
| 713 |
transformation = _defaultImageTransformation |
| 714 |
color = None |
| 715 |
else: |
| 716 |
if hasattr(image, "naked"): |
| 717 |
image = image.naked() |
| 718 |
fileName = image["fileName"] |
| 719 |
color = image.get("color") |
| 720 |
transformation = tuple([ |
| 721 |
image.get(key, _defaultImageTransformationDict[key]) |
| 722 |
for key in _imageTransformationKeys |
| 723 |
]) |
| 724 |
return dict(fileName=fileName, transformation=transformation, color=color) |
| 725 |
|
| 726 |
def _compressImage(image): |
| 727 |
fileName = image["fileName"] |
| 728 |
transformation = image["transformation"] |
| 729 |
color = image["color"] |
| 730 |
if fileName is None: |
| 731 |
return |
| 732 |
image = dict(fileName=fileName, color=color) |
| 733 |
for index, key in enumerate(_imageTransformationKeys): |
| 734 |
image[key] = transformation[index] |
| 735 |
return image |
| 736 |
|
| 737 |
def _pairImages(image1, image2): |
| 738 |
if image1["fileName"] != image2["fileName"]: |
| 739 |
return () |
| 740 |
return (image1, image2) |
| 741 |
|
| 742 |
def _processMathOneImage(imagePair, func): |
| 743 |
image1, image2 = imagePair |
| 744 |
fileName = image1["fileName"] |
| 745 |
color = image1["color"] |
| 746 |
transformation = _processMathOneTransformation(image1["transformation"], image2["transformation"], func) |
| 747 |
return dict(fileName=fileName, transformation=transformation, color=color) |
| 748 |
|
| 749 |
def _processMathTwoImage(image, factor, func): |
| 750 |
fileName = image["fileName"] |
| 751 |
color = image["color"] |
| 752 |
transformation = _processMathTwoTransformation(image["transformation"], factor, func) |
| 753 |
return dict(fileName=fileName, transformation=transformation, color=color) |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
def _processMathOneTransformation(transformation1, transformation2, func): |
| 759 |
xScale1, xyScale1, yxScale1, yScale1, xOffset1, yOffset1 = transformation1 |
| 760 |
xScale2, xyScale2, yxScale2, yScale2, xOffset2, yOffset2 = transformation2 |
| 761 |
xScale, yScale = func((xScale1, yScale1), (xScale2, yScale2)) |
| 762 |
xyScale, yxScale = func((xyScale1, yxScale1), (xyScale2, yxScale2)) |
| 763 |
xOffset, yOffset = func((xOffset1, yOffset1), (xOffset2, yOffset2)) |
| 764 |
return (xScale, xyScale, yxScale, yScale, xOffset, yOffset) |
| 765 |
|
| 766 |
def _processMathTwoTransformation(transformation, factor, func, doScale=True): |
| 767 |
xScale, xyScale, yxScale, yScale, xOffset, yOffset = transformation |
| 768 |
if doScale: |
| 769 |
xScale, yScale = func((xScale, yScale), factor) |
| 770 |
xyScale, yxScale = func((xyScale, yxScale), factor) |
| 771 |
xOffset, yOffset = func((xOffset, yOffset), factor) |
| 772 |
return (xScale, xyScale, yxScale, yScale, xOffset, yOffset) |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
def _roundContours(contours, digits=None): |
| 778 |
results = [] |
| 779 |
for contour in contours: |
| 780 |
contour = dict(contour) |
| 781 |
roundedPoints = [] |
| 782 |
for segmentType, pt, smooth, name, identifier in contour["points"]: |
| 783 |
roundedPt = (_roundNumber(pt[0],digits), _roundNumber(pt[1],digits)) |
| 784 |
roundedPoints.append((segmentType, roundedPt, smooth, name, identifier)) |
| 785 |
contour["points"] = roundedPoints |
| 786 |
results.append(contour) |
| 787 |
return results |
| 788 |
|
| 789 |
def _roundTransformation(transformation, digits=None): |
| 790 |
xScale, xyScale, yxScale, yScale, xOffset, yOffset = transformation |
| 791 |
return (xScale, xyScale, yxScale, yScale, _roundNumber(xOffset, digits), _roundNumber(yOffset, digits)) |
| 792 |
|
| 793 |
def _roundImage(image, digits=None): |
| 794 |
image = dict(image) |
| 795 |
fileName = image["fileName"] |
| 796 |
color = image["color"] |
| 797 |
transformation = _roundTransformation(image["transformation"], digits) |
| 798 |
return dict(fileName=fileName, transformation=transformation, color=color) |
| 799 |
|
| 800 |
def _roundComponents(components, digits=None): |
| 801 |
result = [] |
| 802 |
for component in components: |
| 803 |
component = dict(component) |
| 804 |
component["transformation"] = _roundTransformation(component["transformation"], digits) |
| 805 |
result.append(component) |
| 806 |
return result |
| 807 |
|
| 808 |
def _roundAnchors(anchors, digits=None): |
| 809 |
result = [] |
| 810 |
for anchor in anchors: |
| 811 |
anchor = dict(anchor) |
| 812 |
anchor["x"], anchor["y"] = _roundNumber(anchor["x"], digits), _roundNumber(anchor["y"], digits) |
| 813 |
result.append(anchor) |
| 814 |
return result |
| 815 |
|
| 816 |
|
| 817 |
if __name__ == "__main__": |
| 818 |
import sys |
| 819 |
import doctest |
| 820 |
sys.exit(doctest.testmod().failed) |