| 1 |
from __future__ import print_function |
| 2 |
import math |
| 3 |
from fontTools.misc.transform import Transform |
| 4 |
|
| 5 |
""" |
| 6 |
|
| 7 |
This is a more sophisticated approach to performing math on transformation matrices. |
| 8 |
Traditionally glyphMath applies the math straight to the elements in the matrix. |
| 9 |
By decomposing the matrix into offset, scale and rotation factors, the interpoations |
| 10 |
are much more natural. Or more intuitive. |
| 11 |
|
| 12 |
This could help in complex glyphs in which the rotation of a component plays am important role. |
| 13 |
|
| 14 |
This MathTransform object itself has its own interpolation method. But in order to be able |
| 15 |
to participate in (for instance) superpolator math, it is necessary to keep the |
| 16 |
offset, scale and rotation decomposed for more than one math operation. |
| 17 |
So, MathTransform decomposes the matrix, ShallowTransform carries it through the math, |
| 18 |
then MathTransform is used again to compose the new matrix. If you don't need to math with |
| 19 |
the transformation object itself, the MathTransform object is fine. |
| 20 |
|
| 21 |
MathTransform by Frederik Berlaen |
| 22 |
|
| 23 |
Transformation decomposition algorithm from |
| 24 |
http://dojotoolkit.org/reference-guide/1.9/dojox/gfx.html#decompose-js |
| 25 |
http://dojotoolkit.org/license |
| 26 |
|
| 27 |
|
| 28 |
""" |
| 29 |
|
| 30 |
def matrixToMathTransform(matrix): |
| 31 |
""" Take a 6-tuple and return a ShallowTransform object.""" |
| 32 |
if isinstance(matrix, ShallowTransform): |
| 33 |
return matrix |
| 34 |
off, scl, rot = MathTransform(matrix).decompose() |
| 35 |
return ShallowTransform(off, scl, rot) |
| 36 |
|
| 37 |
def mathTransformToMatrix(mathTransform): |
| 38 |
""" Take a ShallowTransform object and return a 6-tuple. """ |
| 39 |
m = MathTransform().compose(mathTransform.offset, mathTransform.scale, mathTransform.rotation) |
| 40 |
return tuple(m) |
| 41 |
|
| 42 |
class ShallowTransform(object): |
| 43 |
""" A shallow math container for offset, scale and rotation. """ |
| 44 |
def __init__(self, offset, scale, rotation): |
| 45 |
self.offset = offset |
| 46 |
self.scale = scale |
| 47 |
self.rotation = rotation |
| 48 |
|
| 49 |
def __repr__(self): |
| 50 |
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]) |
| 51 |
|
| 52 |
def __add__(self, other): |
| 53 |
newOffset = self.offset[0]+other.offset[0],self.offset[1]+other.offset[1] |
| 54 |
newScale = self.scale[0]+other.scale[0],self.scale[1]+other.scale[1] |
| 55 |
newRotation = self.rotation[0]+other.rotation[0],self.rotation[1]+other.rotation[1] |
| 56 |
return self.__class__(newOffset, newScale, newRotation) |
| 57 |
|
| 58 |
def __sub__(self, other): |
| 59 |
newOffset = self.offset[0]-other.offset[0],self.offset[1]-other.offset[1] |
| 60 |
newScale = self.scale[0]-other.scale[0],self.scale[1]-other.scale[1] |
| 61 |
|
| 62 |
newRotation = self.rotation[0]-other.rotation[0],self.rotation[1]-other.rotation[1] |
| 63 |
return self.__class__(newOffset, newScale, newRotation) |
| 64 |
|
| 65 |
def __mul__(self, factor): |
| 66 |
if isinstance(factor, (int, float)): |
| 67 |
fx = fy = float(factor) |
| 68 |
else: |
| 69 |
fx, fy = float(factor[0]), float(factor[1]) |
| 70 |
newOffset = self.offset[0]*fx,self.offset[1]*fy |
| 71 |
newScale = self.scale[0]*fx,self.scale[1]*fy |
| 72 |
newRotation = self.rotation[0]*fx,self.rotation[1]*fy |
| 73 |
return self.__class__(newOffset, newScale, newRotation) |
| 74 |
|
| 75 |
__rmul__ = __mul__ |
| 76 |
|
| 77 |
def __truediv__(self, factor): |
| 78 |
""" XXX why not __div__ ?""" |
| 79 |
if isinstance(factor, (int, float)): |
| 80 |
fx = fy = float(factor) |
| 81 |
else: |
| 82 |
fx, fy = float(factor) |
| 83 |
if fx==0 or fy==0: |
| 84 |
raise ZeroDivisionError((fx, fy)) |
| 85 |
newOffset = self.offset[0]/fx,self.offset[1]/fy |
| 86 |
newScale = self.scale[0]/fx,self.scale[1]/fy |
| 87 |
newRotation = self.rotation[0]/fx,self.rotation[1]/fy |
| 88 |
return self.__class__(newOffset, newScale, newRotation) |
| 89 |
|
| 90 |
def asTuple(self): |
| 91 |
m = MathTransform().compose(self.offset, self.scale, self.rotation) |
| 92 |
return tuple(m) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
class MathTransform(object): |
| 97 |
""" A Transform object that can compose and decompose the matrix into offset, scale and rotation.""" |
| 98 |
transformClass = Transform |
| 99 |
|
| 100 |
def __init__(self, *matrixes): |
| 101 |
matrix = self.transformClass() |
| 102 |
if matrixes: |
| 103 |
if isinstance(matrixes[0], (int, float)): |
| 104 |
matrixes = [matrixes] |
| 105 |
for m in matrixes: |
| 106 |
matrix = matrix.transform(m) |
| 107 |
self.matrix = matrix |
| 108 |
|
| 109 |
def _get_matrix(self): |
| 110 |
return (self.xx, self.xy, self.yx, self.yy, self.dx, self.dy) |
| 111 |
|
| 112 |
def _set_matrix(self, matrix): |
| 113 |
self.xx, self.xy, self.yx, self.yy, self.dx, self.dy = matrix |
| 114 |
|
| 115 |
matrix = property(_get_matrix, _set_matrix) |
| 116 |
|
| 117 |
def __repr__(self): |
| 118 |
return "< %.8f %.8f %.8f %.8f %.8f %.8f >" % (self.xx, self.xy, self.yx, self.yy, self.dx, self.dy) |
| 119 |
|
| 120 |
def __len__(self): |
| 121 |
return 6 |
| 122 |
|
| 123 |
def __getitem__(self, index): |
| 124 |
return self.matrix[index] |
| 125 |
|
| 126 |
def __getslice__(self, i, j): |
| 127 |
return self.matrix[i:j] |
| 128 |
|
| 129 |
def __eq__(self, other): |
| 130 |
return str(self) == str(other) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
def translate(self, x=0, y=0): |
| 135 |
return self.__class__(self.transformClass(*self.matrix).translate(x, y)) |
| 136 |
|
| 137 |
def scale(self, x=1, y=None): |
| 138 |
return self.__class__(self.transformClass(*self.matrix).scale(x, y)) |
| 139 |
|
| 140 |
def rotate(self, angle): |
| 141 |
return self.__class__(self.transformClass(*self.matrix).rotate(angle)) |
| 142 |
|
| 143 |
def rotateDegrees(self, angle): |
| 144 |
return self.rotate(math.radians(angle)) |
| 145 |
|
| 146 |
def skew(self, x=0, y=0): |
| 147 |
return self.__class__(self.transformClass(*self.matrix).skew(x, y)) |
| 148 |
|
| 149 |
def skewDegrees(self, x=0, y=0): |
| 150 |
return self.skew(math.radians(x), math.radians(y)) |
| 151 |
|
| 152 |
def transform(self, other): |
| 153 |
return self.__class__(self.transformClass(*self.matrix).transform(other)) |
| 154 |
|
| 155 |
def reverseTransform(self, other): |
| 156 |
return self.__class__(self.transformClass(*self.matrix).reverseTransform(other)) |
| 157 |
|
| 158 |
def inverse(self): |
| 159 |
return self.__class__(self.transformClass(*self.matrix).inverse()) |
| 160 |
|
| 161 |
def copy(self): |
| 162 |
return self.__class__(self.matrix) |
| 163 |
|
| 164 |
|
| 165 |
def scaleSign(self): |
| 166 |
if self.xx * self.yy < 0 or self.xy * self.yx > 0: |
| 167 |
return -1 |
| 168 |
return 1 |
| 169 |
|
| 170 |
def eq(self, a, b): |
| 171 |
return abs(a - b) <= 1e-6 * (abs(a) + abs(b)) |
| 172 |
|
| 173 |
def calcFromValues(self, r1, m1, r2, m2): |
| 174 |
m1 = abs(m1) |
| 175 |
m2 = abs(m2) |
| 176 |
return (m1 * r1 + m2 * r2) / (m1 + m2) |
| 177 |
|
| 178 |
def transpose(self): |
| 179 |
return self.__class__(self.xx, self.yx, self.xy, self.yy, 0, 0) |
| 180 |
|
| 181 |
def decompose(self): |
| 182 |
self.translateX = self.dx |
| 183 |
self.translateY = self.dy |
| 184 |
self.scaleX = 1 |
| 185 |
self.scaleY = 1 |
| 186 |
self.angle1 = 0 |
| 187 |
self.angle2 = 0 |
| 188 |
|
| 189 |
if self.eq(self.xy, 0) and self.eq(self.yx, 0): |
| 190 |
self.scaleX = self.xx |
| 191 |
self.scaleY = self.yy |
| 192 |
|
| 193 |
elif self.eq(self.xx * self.yx, -self.xy * self.yy): |
| 194 |
self._decomposeScaleRotate() |
| 195 |
|
| 196 |
elif self.eq(self.xx * self.xy, -self.yx * self.yy): |
| 197 |
self._decomposeRotateScale() |
| 198 |
|
| 199 |
else: |
| 200 |
transpose = self.transpose() |
| 201 |
(vx1, vy1), (vx2, vy2) = self._eigenvalueDecomposition(self.matrix, transpose.matrix) |
| 202 |
u = self.__class__(vx1, vx2, vy1, vy2, 0, 0) |
| 203 |
|
| 204 |
(vx1, vy1), (vx2, vy2) = self._eigenvalueDecomposition(transpose.matrix, self.matrix) |
| 205 |
vt = self.__class__(vx1, vy1, vx2, vy2, 0, 0) |
| 206 |
|
| 207 |
s = self.__class__(self.__class__().reverseTransform(u), self, self.__class__().reverseTransform(vt)) |
| 208 |
|
| 209 |
vt._decomposeScaleRotate() |
| 210 |
self.angle1 = -vt.angle2 |
| 211 |
|
| 212 |
u._decomposeRotateScale() |
| 213 |
self.angle2 = -u.angle1 |
| 214 |
|
| 215 |
self.scaleX = s.xx * vt.scaleX * u.scaleX |
| 216 |
self.scaleY = s.yy * vt.scaleY * u.scaleY |
| 217 |
|
| 218 |
return (self.translateX, self.translateY), (self.scaleX, self.scaleY), (self.angle1, self.angle2) |
| 219 |
|
| 220 |
def _decomposeScaleRotate(self): |
| 221 |
sign = self.scaleSign() |
| 222 |
a = (math.atan2(self.yx, self.yy) + math.atan2(-sign * self.xy, sign * self.xx)) * .5 |
| 223 |
c = math.cos(a) |
| 224 |
s = math.sin(a) |
| 225 |
if c == 0: |
| 226 |
c = 0.0000000000000000000000000000000001 |
| 227 |
if s == 0: |
| 228 |
s = 0.0000000000000000000000000000000001 |
| 229 |
self.angle2 = -a |
| 230 |
self.scaleX = self.calcFromValues(self.xx / float(c), c, -self.xy / float(s), s) |
| 231 |
self.scaleY = self.calcFromValues(self.yy / float(c), c, self.yx / float(s), s) |
| 232 |
|
| 233 |
def _decomposeRotateScale(self): |
| 234 |
sign = self.scaleSign() |
| 235 |
a = (math.atan2(sign * self.yx, sign * self.xx) + math.atan2(-self.xy, self.yy)) * .5 |
| 236 |
c = math.cos(a) |
| 237 |
s = math.sin(a) |
| 238 |
if c == 0: |
| 239 |
c = 0.0000000000000000000000000000000001 |
| 240 |
if s == 0: |
| 241 |
s = 0.0000000000000000000000000000000001 |
| 242 |
self.angle1 = -a |
| 243 |
self.scaleX = self.calcFromValues(self.xx / float(c), c, self.yx / float(s), s) |
| 244 |
self.scaleY = self.calcFromValues(self.yy / float(c), c, -self.xy / float(s), s) |
| 245 |
|
| 246 |
def _eigenvalueDecomposition(self, *matrixes): |
| 247 |
m = self.__class__(*matrixes) |
| 248 |
b = -m.xx - m.yy |
| 249 |
c = m.xx * m.yy - m.xy * m.yx |
| 250 |
d = math.sqrt(abs(b * b - 4 * c)) |
| 251 |
if b < 0: |
| 252 |
d *= -1 |
| 253 |
l1 = -(b + d) * .5 |
| 254 |
l2 = c / float(l1) |
| 255 |
|
| 256 |
vx1 = vy2 = None |
| 257 |
if l1 - m.xx != 0: |
| 258 |
vx1 = m.xy / (l1 - m.xx) |
| 259 |
vy1 = 1 |
| 260 |
elif m.xy != 0: |
| 261 |
vx1 = 1 |
| 262 |
vy1 = (l1 - m.xx) / m.xy |
| 263 |
elif m.yx != 0: |
| 264 |
vx1 = (l1 - m.yy) / m.yx |
| 265 |
vy1 = 1 |
| 266 |
elif l1 - m.yy != 0: |
| 267 |
vx1 = 1 |
| 268 |
vy1 = m.yx / (l1 - m.yy) |
| 269 |
|
| 270 |
vx2 = vy2 = None |
| 271 |
if l2 - m.xx != 0: |
| 272 |
vx2 = m.xy / (l2 - m.xx) |
| 273 |
vy2 = 1 |
| 274 |
elif m.xy != 0: |
| 275 |
vx2 = 1 |
| 276 |
vy2 = (l2 - m.xx) / m.xy |
| 277 |
elif m.yx != 0: |
| 278 |
vx2 = (l2 - m.yy) / m.yx |
| 279 |
vy2 = 1 |
| 280 |
elif l2 - m.yy != 0: |
| 281 |
vx2 = 1 |
| 282 |
vy2 = m.yx / (l2 - m.yy) |
| 283 |
|
| 284 |
|
| 285 |
if self.eq(l1, l2): |
| 286 |
vx1 = 1 |
| 287 |
vy1 = 0 |
| 288 |
vx2 = 0 |
| 289 |
vy2 = 1 |
| 290 |
|
| 291 |
d1 = math.sqrt(vx1 * vx1 + vy1 * vy1) |
| 292 |
d2 = math.sqrt(vx2 * vx2 + vy2 * vy2) |
| 293 |
|
| 294 |
vx1 /= d1 |
| 295 |
vy1 /= d1 |
| 296 |
vx2 /= d2 |
| 297 |
vy2 /= d2 |
| 298 |
|
| 299 |
return (vx1, vy1), (vx2, vy2) |
| 300 |
|
| 301 |
def compose(self, translate, scale, angle): |
| 302 |
translateX, translateY = translate |
| 303 |
scaleX, scaleY = scale |
| 304 |
angle1, angle2 = angle |
| 305 |
matrix = self.transformClass() |
| 306 |
matrix = matrix.translate(translateX, translateY) |
| 307 |
matrix = matrix.rotate(angle2) |
| 308 |
matrix = matrix.scale(scaleX, scaleY) |
| 309 |
matrix = matrix.rotate(angle1) |
| 310 |
return self.__class__(matrix) |
| 311 |
|
| 312 |
def _interpolate(self, v1, v2, value): |
| 313 |
return v1 * (1 - value) + v2 * value |
| 314 |
|
| 315 |
def interpolate(self, other, value): |
| 316 |
if isinstance(value, (int, float)): |
| 317 |
x = y = value |
| 318 |
else: |
| 319 |
x, y = value |
| 320 |
|
| 321 |
self.decompose() |
| 322 |
other.decompose() |
| 323 |
|
| 324 |
translateX = self._interpolate(self.translateX, other.translateX, x) |
| 325 |
translateY = self._interpolate(self.translateY, other.translateY, y) |
| 326 |
scaleX = self._interpolate(self.scaleX, other.scaleX, x) |
| 327 |
scaleY = self._interpolate(self.scaleY, other.scaleY, y) |
| 328 |
angle1 = self._interpolate(self.angle1, other.angle1, x) |
| 329 |
angle2 = self._interpolate(self.angle2, other.angle2, y) |
| 330 |
return self.compose((translateX, translateY), (scaleX, scaleY), (angle1, angle2)) |
| 331 |
|
| 332 |
|
| 333 |
class FontMathWarning(Exception): pass |
| 334 |
|
| 335 |
def _interpolateValue(data1, data2, value): |
| 336 |
return data1 * (1 - value) + data2 * value |
| 337 |
|
| 338 |
def _linearInterpolationTransformMatrix(matrix1, matrix2, value): |
| 339 |
""" Linear, 'oldstyle' interpolation of the transform matrix.""" |
| 340 |
return tuple(_interpolateValue(matrix1[i], matrix2[i], value) for i in range(len(matrix1))) |
| 341 |
|
| 342 |
def _polarDecomposeInterpolationTransformation(matrix1, matrix2, value): |
| 343 |
""" Interpolate using the MathTransform method. """ |
| 344 |
m1 = MathTransform(matrix1) |
| 345 |
m2 = MathTransform(matrix2) |
| 346 |
return tuple(m1.interpolate(m2, value)) |
| 347 |
|
| 348 |
def _mathPolarDecomposeInterpolationTransformation(matrix1, matrix2, value): |
| 349 |
""" Interpolation with ShallowTransfor, wrapped by decompose / compose actions.""" |
| 350 |
off, scl, rot = MathTransform(matrix1).decompose() |
| 351 |
m1 = ShallowTransform(off, scl, rot) |
| 352 |
off, scl, rot = MathTransform(matrix2).decompose() |
| 353 |
m2 = ShallowTransform(off, scl, rot) |
| 354 |
m3 = m1 + value * (m2-m1) |
| 355 |
m3 = MathTransform().compose(m3.offset, m3.scale, m3.rotation) |
| 356 |
return tuple(m3) |
| 357 |
|
| 358 |
|
| 359 |
if __name__ == "__main__": |
| 360 |
from random import random |
| 361 |
import sys |
| 362 |
import doctest |
| 363 |
sys.exit(doctest.testmod().failed) |