Back to index

lightning-sunbird  0.9+nobinonly
gfxMatrix.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Oracle Corporation code.
00016  *
00017  * The Initial Developer of the Original Code is Oracle Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Stuart Parmenter <pavlov@pavlov.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef GFX_MATRIX_H
00039 #define GFX_MATRIX_H
00040 
00041 #include <cairo.h>
00042 
00043 #include "gfxPoint.h"
00044 #include "gfxTypes.h"
00045 
00046 // XX - I don't think this class should use gfxFloat at all,
00047 // but should use 'double' and be called gfxDoubleMatrix;
00048 // we can then typedef that to gfxMatrix where we typedef
00049 // double to be gfxFloat.
00050 
00051 class gfxMatrix {
00052 protected:
00053     cairo_matrix_t mat;
00054 
00055 public:
00056     gfxMatrix() { Reset(); }
00057     gfxMatrix(const gfxMatrix& m) : mat(m.mat) {}
00058     gfxMatrix(gfxFloat a, gfxFloat b, gfxFloat c, gfxFloat d, gfxFloat tx, gfxFloat ty) {
00059         mat.xx = a; mat.yx = b; mat.xy = c; mat.yy = d; mat.x0 = tx; mat.y0 = ty;
00060     }
00061 
00062     gfxMatrix(const cairo_matrix_t& m) {
00063         mat = m;
00064     }
00065 
00066     gfxMatrix& operator=(const cairo_matrix_t& m) {
00067         mat = m;
00068         return *this;
00069     }
00070 
00071     const gfxMatrix& operator *= (const gfxMatrix& m) {
00072         return Multiply(m);
00073     }
00074 
00075     gfxMatrix operator * (const gfxMatrix& m) {
00076         return gfxMatrix(*this).Multiply(m);
00077     }
00078 
00079     // conversion to other types
00080     cairo_matrix_t ToCairoMatrix() const {
00081         return mat;
00082     }
00083 
00084     void ToValues(gfxFloat *xx, gfxFloat *yx,
00085                   gfxFloat *xy, gfxFloat *yy,
00086                   gfxFloat *x0, gfxFloat *y0) const
00087     {
00088         *xx = mat.xx;
00089         *yx = mat.yx;
00090         *xy = mat.xy;
00091         *yy = mat.yy;
00092         *x0 = mat.x0;
00093         *y0 = mat.y0;
00094     }
00095 
00096     // matrix operations
00097     const gfxMatrix& Reset() {
00098         cairo_matrix_init_identity(&mat);
00099         return *this;
00100     }
00101 
00102     const gfxMatrix& Invert() {
00103         cairo_matrix_invert(&mat);
00104         return *this;
00105     }
00106 
00107     const gfxMatrix& Scale(gfxFloat x, gfxFloat y) {
00108         cairo_matrix_scale(&mat, x, y);
00109         return *this;
00110     }
00111 
00112     const gfxMatrix& Translate(const gfxPoint& pt) {
00113         cairo_matrix_translate(&mat, pt.x, pt.y);
00114         return *this;
00115     }
00116 
00117     const gfxMatrix& Rotate(gfxFloat radians) {
00118         gfxFloat s = sin(radians);
00119         gfxFloat c = cos(radians);
00120         gfxMatrix t( c, s,
00121                     -s, c,
00122                      0, 0);
00123         return *this = t.Multiply(*this);
00124     }
00125 
00126     const gfxMatrix& Multiply(const gfxMatrix& m) {
00127         cairo_matrix_multiply(&mat, &mat, &m.mat);
00128         return *this;
00129     }
00130 
00131     void TransformDistance(gfxFloat *dx, gfxFloat *dy) const {
00132         cairo_matrix_transform_distance(&mat, dx, dy);
00133     }
00134 
00135     void TransformPoint(gfxFloat *x, gfxFloat *y) const {
00136         cairo_matrix_transform_point(&mat, x, y);
00137     }
00138 
00139     gfxSize GetScaling() const {
00140         return gfxSize(mat.xx, mat.yy);
00141     }
00142 
00143     gfxPoint GetTranslation() const {
00144         return gfxPoint(mat.x0, mat.y0);
00145     }
00146 
00147     bool HasShear() const {
00148         return ((mat.xy != 0.0) || (mat.yx != 0.0));
00149     }
00150 };
00151 
00152 #endif /* GFX_MATRIX_H */