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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
from __future__ import print_function
import math
from fontTools.misc.transform import Transform
"""
This is a more sophisticated approach to performing math on transformation matrices.
Traditionally glyphMath applies the math straight to the elements in the matrix.
By decomposing the matrix into offset, scale and rotation factors, the interpoations
are much more natural. Or more intuitive.
This could help in complex glyphs in which the rotation of a component plays am important role.
This MathTransform object itself has its own interpolation method. But in order to be able
to participate in (for instance) superpolator math, it is necessary to keep the
offset, scale and rotation decomposed for more than one math operation.
So, MathTransform decomposes the matrix, ShallowTransform carries it through the math,
then MathTransform is used again to compose the new matrix. If you don't need to math with
the transformation object itself, the MathTransform object is fine.
MathTransform by Frederik Berlaen
Transformation decomposition algorithm from
http://dojotoolkit.org/reference-guide/1.9/dojox/gfx.html#decompose-js
http://dojotoolkit.org/license
"""
def matrixToMathTransform(matrix):
""" Take a 6-tuple and return a ShallowTransform object."""
if isinstance(matrix, ShallowTransform):
return matrix
off, scl, rot = MathTransform(matrix).decompose()
return ShallowTransform(off, scl, rot)
def mathTransformToMatrix(mathTransform):
""" Take a ShallowTransform object and return a 6-tuple. """
m = MathTransform().compose(mathTransform.offset, mathTransform.scale, mathTransform.rotation)
return tuple(m)
class ShallowTransform(object):
""" A shallow math container for offset, scale and rotation. """
def __init__(self, offset, scale, rotation):
self.offset = offset
self.scale = scale
self.rotation = rotation
def __repr__(self):
return "<ShallowTransform offset(%3.3f,%3.3f) scale(%3.3f,%3.3f) rotation(%3.3f,%3.3f)>"%(self.offset[0], self.offset[1], self.scale[0], self.scale[1], self.rotation[0], self.rotation[1])
def __add__(self, other):
newOffset = self.offset[0]+other.offset[0],self.offset[1]+other.offset[1]
newScale = self.scale[0]+other.scale[0],self.scale[1]+other.scale[1]
newRotation = self.rotation[0]+other.rotation[0],self.rotation[1]+other.rotation[1]
return self.__class__(newOffset, newScale, newRotation)
def __sub__(self, other):
newOffset = self.offset[0]-other.offset[0],self.offset[1]-other.offset[1]
newScale = self.scale[0]-other.scale[0],self.scale[1]-other.scale[1]
newRotation = self.rotation[0]-other.rotation[0],self.rotation[1]-other.rotation[1]
return self.__class__(newOffset, newScale, newRotation)
def __mul__(self, factor):
if isinstance(factor, (int, float)):
fx = fy = float(factor)
else:
fx, fy = float(factor[0]), float(factor[1])
newOffset = self.offset[0]*fx,self.offset[1]*fy
newScale = self.scale[0]*fx,self.scale[1]*fy
newRotation = self.rotation[0]*fx,self.rotation[1]*fy
return self.__class__(newOffset, newScale, newRotation)
__rmul__ = __mul__
def __truediv__(self, factor):
""" XXX why not __div__ ?"""
if isinstance(factor, (int, float)):
fx = fy = float(factor)
else:
fx, fy = float(factor)
if fx==0 or fy==0:
raise ZeroDivisionError((fx, fy))
newOffset = self.offset[0]/fx,self.offset[1]/fy
newScale = self.scale[0]/fx,self.scale[1]/fy
newRotation = self.rotation[0]/fx,self.rotation[1]/fy
return self.__class__(newOffset, newScale, newRotation)
def asTuple(self):
m = MathTransform().compose(self.offset, self.scale, self.rotation)
return tuple(m)
class MathTransform(object):
""" A Transform object that can compose and decompose the matrix into offset, scale and rotation."""
transformClass = Transform
def __init__(self, *matrixes):
matrix = self.transformClass()
if matrixes:
if isinstance(matrixes[0], (int, float)):
matrixes = [matrixes]
for m in matrixes:
matrix = matrix.transform(m)
self.matrix = matrix
def _get_matrix(self):
return (self.xx, self.xy, self.yx, self.yy, self.dx, self.dy)
def _set_matrix(self, matrix):
self.xx, self.xy, self.yx, self.yy, self.dx, self.dy = matrix
matrix = property(_get_matrix, _set_matrix)
def __repr__(self):
return "< %.8f %.8f %.8f %.8f %.8f %.8f >" % (self.xx, self.xy, self.yx, self.yy, self.dx, self.dy)
def __len__(self):
return 6
def __getitem__(self, index):
return self.matrix[index]
def __getslice__(self, i, j):
return self.matrix[i:j]
def __eq__(self, other):
return str(self) == str(other)
## transformations
def translate(self, x=0, y=0):
return self.__class__(self.transformClass(*self.matrix).translate(x, y))
def scale(self, x=1, y=None):
return self.__class__(self.transformClass(*self.matrix).scale(x, y))
def rotate(self, angle):
return self.__class__(self.transformClass(*self.matrix).rotate(angle))
def rotateDegrees(self, angle):
return self.rotate(math.radians(angle))
def skew(self, x=0, y=0):
return self.__class__(self.transformClass(*self.matrix).skew(x, y))
def skewDegrees(self, x=0, y=0):
return self.skew(math.radians(x), math.radians(y))
def transform(self, other):
return self.__class__(self.transformClass(*self.matrix).transform(other))
def reverseTransform(self, other):
return self.__class__(self.transformClass(*self.matrix).reverseTransform(other))
def inverse(self):
return self.__class__(self.transformClass(*self.matrix).inverse())
def copy(self):
return self.__class__(self.matrix)
## tools
def scaleSign(self):
if self.xx * self.yy < 0 or self.xy * self.yx > 0:
return -1
return 1
def eq(self, a, b):
return abs(a - b) <= 1e-6 * (abs(a) + abs(b))
def calcFromValues(self, r1, m1, r2, m2):
m1 = abs(m1)
m2 = abs(m2)
return (m1 * r1 + m2 * r2) / (m1 + m2)
def transpose(self):
return self.__class__(self.xx, self.yx, self.xy, self.yy, 0, 0)
def decompose(self):
self.translateX = self.dx
self.translateY = self.dy
self.scaleX = 1
self.scaleY = 1
self.angle1 = 0
self.angle2 = 0
if self.eq(self.xy, 0) and self.eq(self.yx, 0):
self.scaleX = self.xx
self.scaleY = self.yy
elif self.eq(self.xx * self.yx, -self.xy * self.yy):
self._decomposeScaleRotate()
elif self.eq(self.xx * self.xy, -self.yx * self.yy):
self._decomposeRotateScale()
else:
transpose = self.transpose()
(vx1, vy1), (vx2, vy2) = self._eigenvalueDecomposition(self.matrix, transpose.matrix)
u = self.__class__(vx1, vx2, vy1, vy2, 0, 0)
(vx1, vy1), (vx2, vy2) = self._eigenvalueDecomposition(transpose.matrix, self.matrix)
vt = self.__class__(vx1, vy1, vx2, vy2, 0, 0)
s = self.__class__(self.__class__().reverseTransform(u), self, self.__class__().reverseTransform(vt))
vt._decomposeScaleRotate()
self.angle1 = -vt.angle2
u._decomposeRotateScale()
self.angle2 = -u.angle1
self.scaleX = s.xx * vt.scaleX * u.scaleX
self.scaleY = s.yy * vt.scaleY * u.scaleY
return (self.translateX, self.translateY), (self.scaleX, self.scaleY), (self.angle1, self.angle2)
def _decomposeScaleRotate(self):
sign = self.scaleSign()
a = (math.atan2(self.yx, self.yy) + math.atan2(-sign * self.xy, sign * self.xx)) * .5
c = math.cos(a)
s = math.sin(a)
if c == 0: ## ????
c = 0.0000000000000000000000000000000001
if s == 0:
s = 0.0000000000000000000000000000000001
self.angle2 = -a
self.scaleX = self.calcFromValues(self.xx / float(c), c, -self.xy / float(s), s)
self.scaleY = self.calcFromValues(self.yy / float(c), c, self.yx / float(s), s)
def _decomposeRotateScale(self):
sign = self.scaleSign()
a = (math.atan2(sign * self.yx, sign * self.xx) + math.atan2(-self.xy, self.yy)) * .5
c = math.cos(a)
s = math.sin(a)
if c == 0:
c = 0.0000000000000000000000000000000001
if s == 0:
s = 0.0000000000000000000000000000000001
self.angle1 = -a
self.scaleX = self.calcFromValues(self.xx / float(c), c, self.yx / float(s), s)
self.scaleY = self.calcFromValues(self.yy / float(c), c, -self.xy / float(s), s)
def _eigenvalueDecomposition(self, *matrixes):
m = self.__class__(*matrixes)
b = -m.xx - m.yy
c = m.xx * m.yy - m.xy * m.yx
d = math.sqrt(abs(b * b - 4 * c))
if b < 0:
d *= -1
l1 = -(b + d) * .5
l2 = c / float(l1)
vx1 = vy2 = None
if l1 - m.xx != 0:
vx1 = m.xy / (l1 - m.xx)
vy1 = 1
elif m.xy != 0:
vx1 = 1
vy1 = (l1 - m.xx) / m.xy
elif m.yx != 0:
vx1 = (l1 - m.yy) / m.yx
vy1 = 1
elif l1 - m.yy != 0:
vx1 = 1
vy1 = m.yx / (l1 - m.yy)
vx2 = vy2 = None
if l2 - m.xx != 0:
vx2 = m.xy / (l2 - m.xx)
vy2 = 1
elif m.xy != 0:
vx2 = 1
vy2 = (l2 - m.xx) / m.xy
elif m.yx != 0:
vx2 = (l2 - m.yy) / m.yx
vy2 = 1
elif l2 - m.yy != 0:
vx2 = 1
vy2 = m.yx / (l2 - m.yy)
if self.eq(l1, l2):
vx1 = 1
vy1 = 0
vx2 = 0
vy2 = 1
d1 = math.sqrt(vx1 * vx1 + vy1 * vy1)
d2 = math.sqrt(vx2 * vx2 + vy2 * vy2)
vx1 /= d1
vy1 /= d1
vx2 /= d2
vy2 /= d2
return (vx1, vy1), (vx2, vy2)
def compose(self, translate, scale, angle):
translateX, translateY = translate
scaleX, scaleY = scale
angle1, angle2 = angle
matrix = self.transformClass()
matrix = matrix.translate(translateX, translateY)
matrix = matrix.rotate(angle2)
matrix = matrix.scale(scaleX, scaleY)
matrix = matrix.rotate(angle1)
return self.__class__(matrix)
def _interpolate(self, v1, v2, value):
return v1 * (1 - value) + v2 * value
def interpolate(self, other, value):
if isinstance(value, (int, float)):
x = y = value
else:
x, y = value
self.decompose()
other.decompose()
translateX = self._interpolate(self.translateX, other.translateX, x)
translateY = self._interpolate(self.translateY, other.translateY, y)
scaleX = self._interpolate(self.scaleX, other.scaleX, x)
scaleY = self._interpolate(self.scaleY, other.scaleY, y)
angle1 = self._interpolate(self.angle1, other.angle1, x)
angle2 = self._interpolate(self.angle2, other.angle2, y)
return self.compose((translateX, translateY), (scaleX, scaleY), (angle1, angle2))
class FontMathWarning(Exception): pass
def _interpolateValue(data1, data2, value):
return data1 * (1 - value) + data2 * value
def _linearInterpolationTransformMatrix(matrix1, matrix2, value):
""" Linear, 'oldstyle' interpolation of the transform matrix."""
return tuple(_interpolateValue(matrix1[i], matrix2[i], value) for i in range(len(matrix1)))
def _polarDecomposeInterpolationTransformation(matrix1, matrix2, value):
""" Interpolate using the MathTransform method. """
m1 = MathTransform(matrix1)
m2 = MathTransform(matrix2)
return tuple(m1.interpolate(m2, value))
def _mathPolarDecomposeInterpolationTransformation(matrix1, matrix2, value):
""" Interpolation with ShallowTransfor, wrapped by decompose / compose actions."""
off, scl, rot = MathTransform(matrix1).decompose()
m1 = ShallowTransform(off, scl, rot)
off, scl, rot = MathTransform(matrix2).decompose()
m2 = ShallowTransform(off, scl, rot)
m3 = m1 + value * (m2-m1)
m3 = MathTransform().compose(m3.offset, m3.scale, m3.rotation)
return tuple(m3)
if __name__ == "__main__":
from random import random
import sys
import doctest
sys.exit(doctest.testmod().failed)
|