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