1"""Pen calculating 0th, 1st, and 2nd moments of area of glyph shapes. 2This is low-level, autogenerated pen. Use statisticsPen instead.""" 3from __future__ import print_function, division, absolute_import 4from fontTools.misc.py23 import * 5from fontTools.pens.basePen import BasePen 6 7 8__all__ = ["MomentsPen"] 9 10 11class MomentsPen(BasePen): 12 13 def __init__(self, glyphset=None): 14 BasePen.__init__(self, glyphset) 15 16 self.area = 0 17 self.momentX = 0 18 self.momentY = 0 19 self.momentXX = 0 20 self.momentXY = 0 21 self.momentYY = 0 22 23 def _moveTo(self, p0): 24 self.__startPoint = p0 25 26 def _closePath(self): 27 p0 = self._getCurrentPoint() 28 if p0 != self.__startPoint: 29 self._lineTo(self.__startPoint) 30 31 def _endPath(self): 32 p0 = self._getCurrentPoint() 33 if p0 != self.__startPoint: 34 # Green theorem is not defined on open contours. 35 raise NotImplementedError 36 37 def _lineTo(self, p1): 38 x0,y0 = self._getCurrentPoint() 39 x1,y1 = p1 40 41 r0 = x1*y0 42 r1 = x1*y1 43 r2 = x1**2 44 r3 = x0**2 45 r4 = 2*y0 46 r5 = y0 - y1 47 r6 = r5*x0 48 r7 = y0**2 49 r8 = y1**2 50 r9 = x1**3 51 r10 = r4*y1 52 r11 = y0**3 53 r12 = y1**3 54 55 self.area += -r0/2 - r1/2 + x0*(y0 + y1)/2 56 self.momentX += -r2*y0/6 - r2*y1/3 + r3*(r4 + y1)/6 - r6*x1/6 57 self.momentY += -r0*y1/6 - r7*x1/6 - r8*x1/6 + x0*(r7 + r8 + y0*y1)/6 58 self.momentXX += -r2*r6/12 - r3*r5*x1/12 - r9*y0/12 - r9*y1/4 + x0**3*(3*y0 + y1)/12 59 self.momentXY += -r10*r2/24 - r2*r7/24 - r2*r8/8 + r3*(r10 + 3*r7 + r8)/24 - x0*x1*(r7 - r8)/12 60 self.momentYY += -r0*r8/12 - r1*r7/12 - r11*x1/12 - r12*x1/12 + x0*(r11 + r12 + r7*y1 + r8*y0)/12 61 62 def _qCurveToOne(self, p1, p2): 63 x0,y0 = self._getCurrentPoint() 64 x1,y1 = p1 65 x2,y2 = p2 66 67 r0 = 2*x1 68 r1 = r0*y2 69 r2 = 2*y1 70 r3 = r2*x2 71 r4 = 3*y2 72 r5 = r4*x2 73 r6 = 3*y0 74 r7 = x1**2 75 r8 = 2*y2 76 r9 = x2**2 77 r10 = 4*y1 78 r11 = 10*y2 79 r12 = r0*x2 80 r13 = x0**2 81 r14 = 10*y0 82 r15 = x2*y2 83 r16 = r0*y1 + r15 84 r17 = 4*x1 85 r18 = x2*y0 86 r19 = r10*r15 87 r20 = y1**2 88 r21 = 2*r20 89 r22 = y2**2 90 r23 = r22*x2 91 r24 = 5*r23 92 r25 = y0**2 93 r26 = y0*y2 94 r27 = 5*r25 95 r28 = 8*x1**3 96 r29 = x2**3 97 r30 = 30*y1 98 r31 = 6*y1 99 r32 = 10*r9*x1 100 r33 = 4*r7 101 r34 = 5*y2 102 r35 = 12*r7 103 r36 = r5 + 20*x1*y1 104 r37 = 30*x1 105 r38 = 12*x1 106 r39 = 20*r7 107 r40 = 8*r7*y1 108 r41 = r34*r9 109 r42 = 60*y1 110 r43 = 20*r20 111 r44 = 4*r20 112 r45 = 15*r22 113 r46 = r38*x2 114 r47 = y1*y2 115 r48 = 8*r20*x1 + r24 116 r49 = 6*x1 117 r50 = 8*y1**3 118 r51 = y2**3 119 r52 = y0**3 120 r53 = 10*y1 121 r54 = 12*y1 122 r55 = 12*r20 123 124 self.area += r1/6 - r3/6 - r5/6 + x0*(r2 + r6 + y2)/6 - y0*(r0 + x2)/6 125 self.momentX += -r10*r9/30 - r11*r9/30 - r12*(-r8 + y1)/30 + r13*(r10 + r14 + y2)/30 + r7*r8/30 + x0*(r1 + r16 - r17*y0 - r18)/30 - y0*(r12 + 2*r7 + r9)/30 126 self.momentY += r1*(r8 + y1)/30 - r19/30 - r21*x2/30 - r24/30 - r25*(r17 + x2)/30 + x0*(r10*y0 + r2*y2 + r21 + r22 + r26 + r27)/30 - y0*(r16 + r3)/30 127 self.momentXX += r13*(r11*x1 - 5*r18 + r3 + r36 - r37*y0)/420 + r28*y2/420 - r29*r30/420 - r29*y2/4 - r32*(r2 - r4)/420 - r33*x2*(r2 - r34)/420 + x0**3*(r31 + 21*y0 + y2)/84 - x0*(-r15*r38 + r18*r38 + r2*r9 - r35*y2 + r39*y0 - r40 - r41 + r6*r9)/420 - y0*(r28 + 5*r29 + r32 + r35*x2)/420 128 self.momentXY += r13*(r14*y2 + 3*r22 + 105*r25 + r42*y0 + r43 + 12*r47)/840 - r17*x2*(r44 - r45)/840 - r22*r9/8 - r25*(r39 + r46 + 3*r9)/840 + r33*y2*(r10 + r34)/840 - r42*r9*y2/840 - r43*r9/840 + x0*(-r10*r18 + r17*r26 + r19 + r22*r49 - r25*r37 - r27*x2 + r38*r47 + r48)/420 - y0*(r15*r17 + r31*r9 + r40 + r41 + r46*y1)/420 129 self.momentYY += r1*(r11*y1 + r44 + r45)/420 - r15*r43/420 - r23*r30/420 - r25*(r1 + r36 + r53*x2)/420 - r50*x2/420 - r51*x2/12 - r52*(r49 + x2)/84 + x0*(r22*r53 + r22*r6 + r25*r30 + r25*r34 + r26*r54 + r43*y0 + r50 + 5*r51 + 35*r52 + r55*y2)/420 - y0*(-r0*r22 + r15*r54 + r48 + r55*x2)/420 130 131 def _curveToOne(self, p1, p2, p3): 132 x0,y0 = self._getCurrentPoint() 133 x1,y1 = p1 134 x2,y2 = p2 135 x3,y3 = p3 136 137 r0 = 6*x2 138 r1 = r0*y3 139 r2 = 6*y2 140 r3 = 10*y3 141 r4 = r3*x3 142 r5 = 3*x1 143 r6 = 3*y1 144 r7 = 6*x1 145 r8 = 3*x2 146 r9 = 6*y1 147 r10 = 3*y2 148 r11 = x2**2 149 r12 = r11*y3 150 r13 = 45*r12 151 r14 = x3**2 152 r15 = r14*y2 153 r16 = r14*y3 154 r17 = x2*x3 155 r18 = 15*r17 156 r19 = 7*y3 157 r20 = x1**2 158 r21 = 9*r20 159 r22 = x0**2 160 r23 = 21*y1 161 r24 = 9*r11 162 r25 = 9*x2 163 r26 = x2*y3 164 r27 = 15*r26 165 r28 = -r25*y1 + r27 166 r29 = r25*y2 167 r30 = r9*x3 168 r31 = 45*x1 169 r32 = x1*x3 170 r33 = 45*r20 171 r34 = 5*r14 172 r35 = x2*y2 173 r36 = 18*r35 174 r37 = 5*x3 175 r38 = r37*y3 176 r39 = r31*y1 + r36 + r38 177 r40 = x1*y0 178 r41 = x1*y3 179 r42 = x2*y0 180 r43 = x3*y1 181 r44 = r10*x3 182 r45 = x3*y2*y3 183 r46 = y2**2 184 r47 = 45*r46 185 r48 = r47*x3 186 r49 = y3**2 187 r50 = r49*x3 188 r51 = y1**2 189 r52 = 9*r51 190 r53 = y0**2 191 r54 = 21*x1 192 r55 = x3*y2 193 r56 = 15*r55 194 r57 = 9*y2 195 r58 = y2*y3 196 r59 = 15*r58 197 r60 = 9*r46 198 r61 = 3*y3 199 r62 = 45*y1 200 r63 = r8*y3 201 r64 = y0*y1 202 r65 = y0*y2 203 r66 = 30*r65 204 r67 = 5*y3 205 r68 = y1*y3 206 r69 = 45*r51 207 r70 = 5*r49 208 r71 = x2**3 209 r72 = x3**3 210 r73 = 126*x3 211 r74 = x1**3 212 r75 = r14*x2 213 r76 = 63*r11 214 r77 = r76*x3 215 r78 = 15*r35 216 r79 = r19*x3 217 r80 = x1*y1 218 r81 = 63*r35 219 r82 = r38 + 378*r80 + r81 220 r83 = x1*y2 221 r84 = x2*y1 222 r85 = x3*y0 223 r86 = x2*x3*y1 224 r87 = x2*x3*y3 225 r88 = r11*y2 226 r89 = 27*r88 227 r90 = 42*y3 228 r91 = r14*r90 229 r92 = 90*x1*x2 230 r93 = 189*x2 231 r94 = 30*x1*x3 232 r95 = 14*r16 + 126*r20*y1 + 45*r88 + r94*y2 233 r96 = x1*x2 234 r97 = 252*r96 235 r98 = x1*x2*y2 236 r99 = 42*r32 237 r100 = x1*x3*y1 238 r101 = 30*r17 239 r102 = 18*r17 240 r103 = 378*r20 241 r104 = 189*y2 242 r105 = r20*y3 243 r106 = r11*y1 244 r107 = r14*y1 245 r108 = 378*r46 246 r109 = 252*y2 247 r110 = y1*y2 248 r111 = x2*x3*y2 249 r112 = y0*y3 250 r113 = 378*r51 251 r114 = 63*r46 252 r115 = 27*x2 253 r116 = r115*r46 + 42*r50 254 r117 = x2*y1*y3 255 r118 = x3*y1*y2 256 r119 = r49*x2 257 r120 = r51*x3 258 r121 = x3*y3 259 r122 = 14*x3 260 r123 = 30*r117 + r122*r49 + r47*x2 + 126*r51*x1 261 r124 = x1*y1*y3 262 r125 = x1*y2*y3 263 r126 = x2*y1*y2 264 r127 = 54*y3 265 r128 = 21*r55 266 r129 = 630*r53 267 r130 = r46*x1 268 r131 = r49*x1 269 r132 = 126*r53 270 r133 = y2**3 271 r134 = y3**3 272 r135 = 630*r49 273 r136 = y1**3 274 r137 = y0**3 275 r138 = r114*y3 + r23*r49 276 r139 = r49*y2 277 278 self.area += r1/20 - r2*x3/20 - r4/20 + r5*(y2 + y3)/20 - r6*(x2 + x3)/20 + x0*(r10 + r9 + 10*y0 + y3)/20 - y0*(r7 + r8 + x3)/20 279 self.momentX += r13/840 - r15/8 - r16/3 - r18*(r10 - r19)/840 + r21*(r10 + 2*y3)/840 + r22*(r2 + r23 + 56*y0 + y3)/168 + r5*(r28 + r29 - r30 + r4)/840 - r6*(10*r14 + r18 + r24)/840 + x0*(12*r26 + r31*y2 - r37*y0 + r39 - 105*r40 + 15*r41 - 30*r42 - 3*r43 + r44)/840 - y0*(18*r11 + r18 + r31*x2 + 12*r32 + r33 + r34)/840 280 self.momentY += r27*(r10 + r19)/840 - r45/8 - r48/840 + r5*(10*r49 + r57*y1 + r59 + r60 + r9*y3)/840 - r50/6 - r52*(r8 + 2*x3)/840 - r53*(r0 + r54 + x3)/168 - r6*(r29 + r4 + r56)/840 + x0*(18*r46 + 140*r53 + r59 + r62*y2 + 105*r64 + r66 + r67*y0 + 12*r68 + r69 + r70)/840 - y0*(r39 + 15*r43 + 12*r55 - r61*x1 + r62*x2 + r63)/840 281 self.momentXX += -r11*r73*(-r61 + y2)/9240 + r21*(r28 - r37*y1 + r44 + r78 + r79)/9240 + r22*(21*r26 - 630*r40 + 42*r41 - 126*r42 + r57*x3 + r82 + 210*r83 + 42*r84 - 14*r85)/9240 - r5*(r11*r62 + r14*r23 + 14*r15 - r76*y3 + 54*r86 - 84*r87 - r89 - r91)/9240 - r6*(27*r71 + 42*r72 + 70*r75 + r77)/9240 + 3*r71*y3/220 - 3*r72*y2/44 - r72*y3/4 + 3*r74*(r57 + r67)/3080 - r75*(378*y2 - 630*y3)/9240 + x0**3*(r57 + r62 + 165*y0 + y3)/660 + x0*(-18*r100 - r101*y0 - r101*y1 + r102*y2 - r103*y0 + r104*r20 + 63*r105 - 27*r106 - 9*r107 + r13 - r34*y0 - r76*y0 + 42*r87 + r92*y3 + r94*y3 + r95 - r97*y0 + 162*r98 - r99*y0)/9240 - y0*(135*r11*x1 + r14*r54 + r20*r93 + r33*x3 + 45*r71 + 14*r72 + 126*r74 + 42*r75 + r77 + r92*x3)/9240 282 self.momentXY += -r108*r14/18480 + r12*(r109 + 378*y3)/18480 - r14*r49/8 - 3*r14*r58/44 - r17*(252*r46 - 1260*r49)/18480 + r21*(18*r110 + r3*y1 + 15*r46 + 7*r49 + 18*r58)/18480 + r22*(252*r110 + 28*r112 + r113 + r114 + 2310*r53 + 30*r58 + 1260*r64 + 252*r65 + 42*r68 + r70)/18480 - r52*(r102 + 15*r11 + 7*r14)/18480 - r53*(r101 + r103 + r34 + r76 + r97 + r99)/18480 + r7*(-r115*r51 + r116 + 18*r117 - 18*r118 + 42*r119 - 15*r120 + 28*r45 + r81*y3)/18480 - r9*(63*r111 + 42*r15 + 28*r87 + r89 + r91)/18480 + x0*(r1*y0 + r104*r80 + r112*r54 + 21*r119 - 9*r120 - r122*r53 + r123 + 54*r124 + 60*r125 + 54*r126 + r127*r35 + r128*y3 - r129*x1 + 81*r130 + 15*r131 - r132*x2 - r2*r85 - r23*r85 + r30*y3 + 84*r40*y2 - 84*r42*y1 + r60*x3)/9240 - y0*(54*r100 - 9*r105 + 81*r106 + 15*r107 + 54*r111 + r121*r7 + 21*r15 + r24*y3 + 60*r86 + 21*r87 + r95 + 189*r96*y1 + 54*r98)/9240 283 self.momentYY += -r108*r121/9240 - r133*r73/9240 - r134*x3/12 - r135*r55/9240 - 3*r136*(r25 + r37)/3080 - r137*(r25 + r31 + x3)/660 + r26*(r135 + 126*r46 + 378*y2*y3)/9240 + r5*(r110*r127 + 27*r133 + 42*r134 + r138 + 70*r139 + r46*r62 + 27*r51*y2 + 15*r51*y3)/9240 - r52*(r56 + r63 + r78 + r79)/9240 - r53*(r128 + r25*y3 + 42*r43 + r82 + 42*r83 + 210*r84)/9240 - r6*(r114*x3 + r116 - 14*r119 + 84*r45)/9240 + x0*(r104*r51 + r109*r64 + 90*r110*y3 + r113*y0 + r114*y0 + r129*y1 + r132*y2 + 45*r133 + 14*r134 + 126*r136 + 770*r137 + r138 + 42*r139 + 135*r46*y1 + 14*r53*y3 + r64*r90 + r66*y3 + r69*y3 + r70*y0)/9240 - y0*(90*r118 + 63*r120 + r123 - 18*r124 - 30*r125 + 162*r126 - 27*r130 - 9*r131 + r36*y3 + 30*r43*y3 + 42*r45 + r48 + r51*r93)/9240 284 285if __name__ == '__main__': 286 from fontTools.misc.symfont import x, y, printGreenPen 287 printGreenPen('MomentsPen', [ 288 ('area', 1), 289 ('momentX', x), 290 ('momentY', y), 291 ('momentXX', x**2), 292 ('momentXY', x*y), 293 ('momentYY', y**2), 294 ]) 295