|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsTransform2D_h___ |
|
7 #define nsTransform2D_h___ |
|
8 |
|
9 #include "gfxCore.h" |
|
10 #include "nsCoord.h" |
|
11 |
|
12 class NS_GFX nsTransform2D |
|
13 { |
|
14 private: |
|
15 /** |
|
16 * This represents the following matrix (note that the order of row/column |
|
17 * indices is opposite to usual notation) |
|
18 * |
|
19 * / m00 0 m20 \ |
|
20 * M = | 0 m11 m21 | |
|
21 * \ 0 0 1 / |
|
22 * |
|
23 * Transformation of a coordinate (x, y) is obtained by setting |
|
24 * v = (x, y, 1)^T and evaluating M . v |
|
25 **/ |
|
26 |
|
27 float m00, m11, m20, m21; |
|
28 |
|
29 public: |
|
30 nsTransform2D(void) { m20 = m21 = 0.0f; m00 = m11 = 1.0f; } |
|
31 nsTransform2D(nsTransform2D *aTransform2D) { |
|
32 m00 = aTransform2D->m00; |
|
33 m11 = aTransform2D->m11; |
|
34 m20 = aTransform2D->m20; |
|
35 m21 = aTransform2D->m21; |
|
36 } |
|
37 |
|
38 ~nsTransform2D(void) { } |
|
39 |
|
40 /** |
|
41 * set this transform to a translation |
|
42 * |
|
43 * @param tx, x translation |
|
44 * @param ty, y translation |
|
45 **/ |
|
46 |
|
47 void SetToTranslate(float tx, float ty) { m00 = m11 = 1.0f; m20 = tx; m21 = ty; } |
|
48 |
|
49 /** |
|
50 * get the translation portion of this transform |
|
51 * |
|
52 * @param pt, Point to return translation values in |
|
53 **/ |
|
54 |
|
55 void GetTranslationCoord(nscoord *ptX, nscoord *ptY) const { *ptX = NSToCoordRound(m20); *ptY = NSToCoordRound(m21); } |
|
56 |
|
57 /** |
|
58 * apply matrix to vector |
|
59 * |
|
60 * @param pt Point to transform |
|
61 **/ |
|
62 |
|
63 void TransformCoord(nscoord *ptX, nscoord *ptY) const; |
|
64 |
|
65 /** |
|
66 * apply matrix to rect |
|
67 * |
|
68 * @param rect Rect to transform |
|
69 **/ |
|
70 |
|
71 void TransformCoord(nscoord *aX, nscoord *aY, nscoord *aWidth, nscoord *aHeight) const; |
|
72 |
|
73 /** |
|
74 * add a scale to a Transform via x, y pair |
|
75 * |
|
76 * @param ptX x value to add as x scale |
|
77 * @param ptY y value to add as y scale |
|
78 **/ |
|
79 |
|
80 void AddScale(float ptX, float ptY) { m00 *= ptX; m11 *= ptY; } |
|
81 |
|
82 /** |
|
83 * Set the scale (overriding any previous calls to AddScale, but leaving |
|
84 * any existing translation). |
|
85 * |
|
86 * @param ptX x value to add as x scale |
|
87 * @param ptY y value to add as y scale |
|
88 **/ |
|
89 |
|
90 void SetScale(float ptX, float ptY) { m00 = ptX; m11 = ptY; } |
|
91 }; |
|
92 |
|
93 #endif |