1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxMatrix.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- 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 +#include "gfxMatrix.h" 1.10 +#include "cairo.h" 1.11 +#include "mozilla/gfx/Tools.h" 1.12 + 1.13 +#define CAIRO_MATRIX(x) reinterpret_cast<cairo_matrix_t*>((x)) 1.14 +#define CONST_CAIRO_MATRIX(x) reinterpret_cast<const cairo_matrix_t*>((x)) 1.15 + 1.16 +const gfxMatrix& 1.17 +gfxMatrix::Reset() 1.18 +{ 1.19 + cairo_matrix_init_identity(CAIRO_MATRIX(this)); 1.20 + return *this; 1.21 +} 1.22 + 1.23 +const gfxMatrix& 1.24 +gfxMatrix::Invert() 1.25 +{ 1.26 + cairo_matrix_invert(CAIRO_MATRIX(this)); 1.27 + return *this; 1.28 +} 1.29 + 1.30 +const gfxMatrix& 1.31 +gfxMatrix::Scale(gfxFloat x, gfxFloat y) 1.32 +{ 1.33 + cairo_matrix_scale(CAIRO_MATRIX(this), x, y); 1.34 + return *this; 1.35 +} 1.36 + 1.37 +const gfxMatrix& 1.38 +gfxMatrix::Translate(const gfxPoint& pt) 1.39 +{ 1.40 + cairo_matrix_translate(CAIRO_MATRIX(this), pt.x, pt.y); 1.41 + return *this; 1.42 +} 1.43 + 1.44 +const gfxMatrix& 1.45 +gfxMatrix::Rotate(gfxFloat radians) 1.46 +{ 1.47 + cairo_matrix_rotate(CAIRO_MATRIX(this), radians); 1.48 + return *this; 1.49 +} 1.50 + 1.51 +const gfxMatrix& 1.52 +gfxMatrix::Multiply(const gfxMatrix& m) 1.53 +{ 1.54 + cairo_matrix_multiply(CAIRO_MATRIX(this), CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m)); 1.55 + return *this; 1.56 +} 1.57 + 1.58 +const gfxMatrix& 1.59 +gfxMatrix::PreMultiply(const gfxMatrix& m) 1.60 +{ 1.61 + cairo_matrix_multiply(CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m), CAIRO_MATRIX(this)); 1.62 + return *this; 1.63 +} 1.64 + 1.65 +gfxPoint 1.66 +gfxMatrix::Transform(const gfxPoint& point) const 1.67 +{ 1.68 + gfxPoint ret = point; 1.69 + cairo_matrix_transform_point(CONST_CAIRO_MATRIX(this), &ret.x, &ret.y); 1.70 + return ret; 1.71 +} 1.72 + 1.73 +gfxSize 1.74 +gfxMatrix::Transform(const gfxSize& size) const 1.75 +{ 1.76 + gfxSize ret = size; 1.77 + cairo_matrix_transform_distance(CONST_CAIRO_MATRIX(this), &ret.width, &ret.height); 1.78 + return ret; 1.79 +} 1.80 + 1.81 +gfxRect 1.82 +gfxMatrix::Transform(const gfxRect& rect) const 1.83 +{ 1.84 + return gfxRect(Transform(rect.TopLeft()), Transform(rect.Size())); 1.85 +} 1.86 + 1.87 +gfxRect 1.88 +gfxMatrix::TransformBounds(const gfxRect& rect) const 1.89 +{ 1.90 + /* Code taken from cairo-matrix.c, _cairo_matrix_transform_bounding_box isn't public */ 1.91 + int i; 1.92 + double quad_x[4], quad_y[4]; 1.93 + double min_x, max_x; 1.94 + double min_y, max_y; 1.95 + 1.96 + quad_x[0] = rect.X(); 1.97 + quad_y[0] = rect.Y(); 1.98 + cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[0], &quad_y[0]); 1.99 + 1.100 + quad_x[1] = rect.XMost(); 1.101 + quad_y[1] = rect.Y(); 1.102 + cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[1], &quad_y[1]); 1.103 + 1.104 + quad_x[2] = rect.X(); 1.105 + quad_y[2] = rect.YMost(); 1.106 + cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[2], &quad_y[2]); 1.107 + 1.108 + quad_x[3] = rect.XMost(); 1.109 + quad_y[3] = rect.YMost(); 1.110 + cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[3], &quad_y[3]); 1.111 + 1.112 + min_x = max_x = quad_x[0]; 1.113 + min_y = max_y = quad_y[0]; 1.114 + 1.115 + for (i = 1; i < 4; i++) { 1.116 + if (quad_x[i] < min_x) 1.117 + min_x = quad_x[i]; 1.118 + if (quad_x[i] > max_x) 1.119 + max_x = quad_x[i]; 1.120 + 1.121 + if (quad_y[i] < min_y) 1.122 + min_y = quad_y[i]; 1.123 + if (quad_y[i] > max_y) 1.124 + max_y = quad_y[i]; 1.125 + } 1.126 + 1.127 + return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y); 1.128 +} 1.129 + 1.130 + 1.131 +static void NudgeToInteger(double *aVal) 1.132 +{ 1.133 + float f = float(*aVal); 1.134 + mozilla::gfx::NudgeToInteger(&f); 1.135 + *aVal = f; 1.136 +} 1.137 + 1.138 +void 1.139 +gfxMatrix::NudgeToIntegers(void) 1.140 +{ 1.141 + NudgeToInteger(&xx); 1.142 + NudgeToInteger(&xy); 1.143 + NudgeToInteger(&yx); 1.144 + NudgeToInteger(&yy); 1.145 + NudgeToInteger(&x0); 1.146 + NudgeToInteger(&y0); 1.147 +}