| OLD | NEW |
| 1 /* | 1 /* |
| 2 * | 2 * |
| 3 * Redistribution and use in source and binary forms, with or without | 3 * Redistribution and use in source and binary forms, with or without |
| 4 * modification, are permitted provided that the following conditions | 4 * modification, are permitted provided that the following conditions |
| 5 * are met: | 5 * are met: |
| 6 * 1. Redistributions of source code must retain the above copyright | 6 * 1. Redistributions of source code must retain the above copyright |
| 7 * notice, this list of conditions and the following disclaimer. | 7 * notice, this list of conditions and the following disclaimer. |
| 8 * 2. Redistributions in binary form must reproduce the above copyright | 8 * 2. Redistributions in binary form must reproduce the above copyright |
| 9 * notice, this list of conditions and the following disclaimer in the | 9 * notice, this list of conditions and the following disclaimer in the |
| 10 * documentation and/or other materials provided with the distribution. | 10 * documentation and/or other materials provided with the distribution. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "config.h" | 25 #include "config.h" |
| 26 #include "TransformationMatrix.h" | 26 #include "TransformationMatrix.h" |
| 27 | 27 |
| 28 #include "IntRect.h" | 28 #include "IntRect.h" |
| 29 #include "FloatRect.h" | 29 #include "FloatRect.h" |
| 30 | 30 |
| 31 #include <cairo.h> | 31 #include <cairo.h> |
| 32 | 32 |
| 33 namespace WebCore { | 33 namespace WebCore { |
| 34 | 34 |
| 35 static const double deg2rad = 0.017453292519943295769; // pi/180 | |
| 36 | |
| 37 TransformationMatrix::TransformationMatrix() | |
| 38 { | |
| 39 cairo_matrix_init_identity(&m_transform); | |
| 40 } | |
| 41 | |
| 42 TransformationMatrix::TransformationMatrix(double a, double b, double c, double
d, double tx, double ty) | |
| 43 { | |
| 44 cairo_matrix_init(&m_transform, a, b, c, d, tx, ty); | |
| 45 } | |
| 46 | |
| 47 TransformationMatrix::TransformationMatrix(const PlatformTransformationMatrix& m
atrix) | |
| 48 { | |
| 49 m_transform = matrix; | |
| 50 } | |
| 51 | |
| 52 void TransformationMatrix::setMatrix(double a, double b, double c, double d, dou
ble tx, double ty) | |
| 53 { | |
| 54 cairo_matrix_init(&m_transform, a, b, c, d, tx, ty); | |
| 55 } | |
| 56 | |
| 57 void TransformationMatrix::map(double x, double y, double* x2, double* y2) const | |
| 58 { | |
| 59 *x2 = x; | |
| 60 *y2 = y; | |
| 61 cairo_matrix_transform_point(&m_transform, x2, y2); | |
| 62 } | |
| 63 | |
| 64 IntRect TransformationMatrix::mapRect(const IntRect &rect) const | |
| 65 { | |
| 66 FloatRect floatRect(rect); | |
| 67 FloatRect enclosingFloatRect = this->mapRect(floatRect); | |
| 68 | |
| 69 return enclosingIntRect(enclosingFloatRect); | |
| 70 } | |
| 71 | |
| 72 FloatRect TransformationMatrix::mapRect(const FloatRect &rect) const | |
| 73 { | |
| 74 double rectMinX = rect.x(); | |
| 75 double rectMaxX = rect.x() + rect.width(); | |
| 76 double rectMinY = rect.y(); | |
| 77 double rectMaxY = rect.y() + rect.height(); | |
| 78 | |
| 79 double px = rectMinX; | |
| 80 double py = rectMinY; | |
| 81 cairo_matrix_transform_point(&m_transform, &px, &py); | |
| 82 | |
| 83 double enclosingRectMinX = px; | |
| 84 double enclosingRectMinY = py; | |
| 85 double enclosingRectMaxX = px; | |
| 86 double enclosingRectMaxY = py; | |
| 87 | |
| 88 px = rectMaxX; | |
| 89 py = rectMinY; | |
| 90 cairo_matrix_transform_point(&m_transform, &px, &py); | |
| 91 if (px < enclosingRectMinX) | |
| 92 enclosingRectMinX = px; | |
| 93 else if (px > enclosingRectMaxX) | |
| 94 enclosingRectMaxX = px; | |
| 95 if (py < enclosingRectMinY) | |
| 96 enclosingRectMinY = py; | |
| 97 else if (py > enclosingRectMaxY) | |
| 98 enclosingRectMaxY = py; | |
| 99 | |
| 100 px = rectMaxX; | |
| 101 py = rectMaxY; | |
| 102 cairo_matrix_transform_point(&m_transform, &px, &py); | |
| 103 if (px < enclosingRectMinX) | |
| 104 enclosingRectMinX = px; | |
| 105 else if (px > enclosingRectMaxX) | |
| 106 enclosingRectMaxX = px; | |
| 107 if (py < enclosingRectMinY) | |
| 108 enclosingRectMinY = py; | |
| 109 else if (py > enclosingRectMaxY) | |
| 110 enclosingRectMaxY = py; | |
| 111 | |
| 112 px = rectMinX; | |
| 113 py = rectMaxY; | |
| 114 cairo_matrix_transform_point(&m_transform, &px, &py); | |
| 115 if (px < enclosingRectMinX) | |
| 116 enclosingRectMinX = px; | |
| 117 else if (px > enclosingRectMaxX) | |
| 118 enclosingRectMaxX = px; | |
| 119 if (py < enclosingRectMinY) | |
| 120 enclosingRectMinY = py; | |
| 121 else if (py > enclosingRectMaxY) | |
| 122 enclosingRectMaxY = py; | |
| 123 | |
| 124 | |
| 125 double enclosingRectWidth = enclosingRectMaxX - enclosingRectMinX; | |
| 126 double enclosingRectHeight = enclosingRectMaxY - enclosingRectMinY; | |
| 127 | |
| 128 return FloatRect(enclosingRectMinX, enclosingRectMinY, enclosingRectWidth, e
nclosingRectHeight); | |
| 129 } | |
| 130 | |
| 131 bool TransformationMatrix::isIdentity() const | |
| 132 { | |
| 133 return ((m_transform.xx == 1) && (m_transform.yy == 1) | |
| 134 && (m_transform.xy == 0) && (m_transform.yx == 0) | |
| 135 && (m_transform.x0 == 0) && (m_transform.y0 == 0)); | |
| 136 } | |
| 137 | |
| 138 double TransformationMatrix::a() const | |
| 139 { | |
| 140 return m_transform.xx; | |
| 141 } | |
| 142 | |
| 143 void TransformationMatrix::setA(double a) | |
| 144 { | |
| 145 m_transform.xx = a; | |
| 146 } | |
| 147 | |
| 148 double TransformationMatrix::b() const | |
| 149 { | |
| 150 return m_transform.yx; | |
| 151 } | |
| 152 | |
| 153 void TransformationMatrix::setB(double b) | |
| 154 { | |
| 155 m_transform.yx = b; | |
| 156 } | |
| 157 | |
| 158 double TransformationMatrix::c() const | |
| 159 { | |
| 160 return m_transform.xy; | |
| 161 } | |
| 162 | |
| 163 void TransformationMatrix::setC(double c) | |
| 164 { | |
| 165 m_transform.xy = c; | |
| 166 } | |
| 167 | |
| 168 double TransformationMatrix::d() const | |
| 169 { | |
| 170 return m_transform.yy; | |
| 171 } | |
| 172 | |
| 173 void TransformationMatrix::setD(double d) | |
| 174 { | |
| 175 m_transform.yy = d; | |
| 176 } | |
| 177 | |
| 178 double TransformationMatrix::e() const | |
| 179 { | |
| 180 return m_transform.x0; | |
| 181 } | |
| 182 | |
| 183 void TransformationMatrix::setE(double e) | |
| 184 { | |
| 185 m_transform.x0 = e; | |
| 186 } | |
| 187 | |
| 188 double TransformationMatrix::f() const | |
| 189 { | |
| 190 return m_transform.y0; | |
| 191 } | |
| 192 | |
| 193 void TransformationMatrix::setF(double f) | |
| 194 { | |
| 195 m_transform.y0 = f; | |
| 196 } | |
| 197 | |
| 198 void TransformationMatrix::reset() | |
| 199 { | |
| 200 cairo_matrix_init_identity(&m_transform); | |
| 201 } | |
| 202 | |
| 203 TransformationMatrix &TransformationMatrix::scale(double sx, double sy) | |
| 204 { | |
| 205 cairo_matrix_scale(&m_transform, sx, sy); | |
| 206 return *this; | |
| 207 } | |
| 208 | |
| 209 TransformationMatrix &TransformationMatrix::rotate(double d) | |
| 210 { | |
| 211 cairo_matrix_rotate(&m_transform, d * deg2rad); | |
| 212 return *this; | |
| 213 } | |
| 214 | |
| 215 TransformationMatrix &TransformationMatrix::translate(double tx, double ty) | |
| 216 { | |
| 217 cairo_matrix_translate(&m_transform, tx, ty); | |
| 218 return *this; | |
| 219 } | |
| 220 | |
| 221 TransformationMatrix &TransformationMatrix::shear(double sx, double sy) | |
| 222 { | |
| 223 cairo_matrix_t shear; | |
| 224 cairo_matrix_init(&shear, 1, sy, sx, 1, 0, 0); | |
| 225 | |
| 226 cairo_matrix_t result; | |
| 227 cairo_matrix_multiply(&result, &shear, &m_transform); | |
| 228 m_transform = result; | |
| 229 | |
| 230 return *this; | |
| 231 } | |
| 232 | |
| 233 double TransformationMatrix::det() const | |
| 234 { | |
| 235 return m_transform.xx * m_transform.yy - m_transform.xy * m_transform.yx; | |
| 236 } | |
| 237 | |
| 238 TransformationMatrix TransformationMatrix::inverse() const | |
| 239 { | |
| 240 if (!isInvertible()) return TransformationMatrix(); | |
| 241 | |
| 242 cairo_matrix_t result = m_transform; | |
| 243 cairo_matrix_invert(&result); | |
| 244 return TransformationMatrix(result); | |
| 245 } | |
| 246 | |
| 247 TransformationMatrix::operator cairo_matrix_t() const | 35 TransformationMatrix::operator cairo_matrix_t() const |
| 248 { | 36 { |
| 249 return m_transform; | 37 cairo_matrix_t m; |
| 250 } | |
| 251 | 38 |
| 252 bool TransformationMatrix::operator== (const TransformationMatrix &m2) const | 39 cairo_matrix_init (&m, |
| 253 { | 40 a(), |
| 254 return ((m_transform.xx == m2.m_transform.xx) | 41 b(), |
| 255 && (m_transform.yy == m2.m_transform.yy) | 42 c(), |
| 256 && (m_transform.xy == m2.m_transform.xy) | 43 d(), |
| 257 && (m_transform.yx == m2.m_transform.yx) | 44 e(), |
| 258 && (m_transform.x0 == m2.m_transform.x0) | 45 f()); |
| 259 && (m_transform.y0 == m2.m_transform.y0)); | 46 return m; |
| 260 | |
| 261 } | |
| 262 | |
| 263 TransformationMatrix &TransformationMatrix::operator*= (const TransformationMatr
ix &m2) | |
| 264 { | |
| 265 cairo_matrix_t result; | |
| 266 cairo_matrix_multiply(&result, &m_transform, &m2.m_transform); | |
| 267 m_transform = result; | |
| 268 | |
| 269 return *this; | |
| 270 } | |
| 271 | |
| 272 TransformationMatrix TransformationMatrix::operator* (const TransformationMatrix
&m2) | |
| 273 { | |
| 274 cairo_matrix_t result; | |
| 275 cairo_matrix_multiply(&result, &m_transform, &m2.m_transform); | |
| 276 return result; | |
| 277 } | 47 } |
| 278 | 48 |
| 279 } | 49 } |
| 280 | 50 |
| 281 // vim: ts=4 sw=4 et | 51 // vim: ts=4 sw=4 et |
| OLD | NEW |