gfx/src/nsTransform2D.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/src/nsTransform2D.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsTransform2D_h___
    1.10 +#define nsTransform2D_h___
    1.11 +
    1.12 +#include "gfxCore.h"
    1.13 +#include "nsCoord.h"
    1.14 +
    1.15 +class NS_GFX nsTransform2D
    1.16 +{
    1.17 +private:
    1.18 + /**
    1.19 +  * This represents the following matrix (note that the order of row/column
    1.20 +  * indices is opposite to usual notation)
    1.21 +  *
    1.22 +  *      / m00   0   m20  \
    1.23 +  * M =  |  0   m11  m21  |
    1.24 +  *      \  0    0    1   /
    1.25 +  *
    1.26 +  * Transformation of a coordinate (x, y) is obtained by setting
    1.27 +  * v = (x, y, 1)^T and evaluating  M . v
    1.28 +  **/
    1.29 +
    1.30 +  float     m00, m11, m20, m21;
    1.31 +
    1.32 +public:
    1.33 +  nsTransform2D(void)                         { m20 = m21 = 0.0f; m00 = m11 = 1.0f; }
    1.34 +  nsTransform2D(nsTransform2D *aTransform2D)  {
    1.35 +    m00 = aTransform2D->m00;
    1.36 +    m11 = aTransform2D->m11;
    1.37 +    m20 = aTransform2D->m20;
    1.38 +    m21 = aTransform2D->m21;
    1.39 +  }
    1.40 +
    1.41 +  ~nsTransform2D(void)                        { }
    1.42 +
    1.43 + /**
    1.44 +  * set this transform to a translation
    1.45 +  *
    1.46 +  * @param      tx, x translation
    1.47 +  * @param      ty, y translation
    1.48 +  **/
    1.49 +
    1.50 +  void SetToTranslate(float tx, float ty)    { m00 = m11 = 1.0f; m20 = tx; m21 = ty; }
    1.51 +  
    1.52 + /**
    1.53 +  * get the translation portion of this transform
    1.54 +  *
    1.55 +  * @param      pt, Point to return translation values in
    1.56 +  **/
    1.57 +
    1.58 +  void GetTranslationCoord(nscoord *ptX, nscoord *ptY) const { *ptX = NSToCoordRound(m20); *ptY = NSToCoordRound(m21); }
    1.59 +
    1.60 + /**
    1.61 +  * apply matrix to vector
    1.62 +  *
    1.63 +  * @param    pt Point to transform
    1.64 +  **/
    1.65 +
    1.66 +  void TransformCoord(nscoord *ptX, nscoord *ptY) const;
    1.67 +
    1.68 + /**
    1.69 +  * apply matrix to rect
    1.70 +  *
    1.71 +  * @param    rect Rect to transform
    1.72 +  **/
    1.73 +
    1.74 +  void TransformCoord(nscoord *aX, nscoord *aY, nscoord *aWidth, nscoord *aHeight) const;
    1.75 +
    1.76 + /**
    1.77 +  * add a scale to a Transform via x, y pair
    1.78 +  *
    1.79 +  * @param    ptX x value to add as x scale
    1.80 +  * @param    ptY y value to add as y scale
    1.81 +  **/
    1.82 +
    1.83 +  void AddScale(float ptX, float ptY) { m00 *= ptX; m11 *= ptY; }
    1.84 +
    1.85 + /**
    1.86 +  * Set the scale (overriding any previous calls to AddScale, but leaving
    1.87 +  * any existing translation).
    1.88 +  *
    1.89 +  * @param    ptX x value to add as x scale
    1.90 +  * @param    ptY y value to add as y scale
    1.91 +  **/
    1.92 +
    1.93 +  void SetScale(float ptX, float ptY) { m00 = ptX; m11 = ptY; }
    1.94 +};
    1.95 +
    1.96 +#endif

mercurial