summaryrefslogtreecommitdiffstats
path: root/ui/gfx/transform.cc
blob: 03e2873d27c5ca1e795c3d4c5049854bf87c0c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/transform.h"

#include <cmath>

#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/skia_util.h"

namespace ui {

Transform::Transform() {
  matrix_.reset();
}

Transform::~Transform() {}

void Transform::SetRotate(float degree) {
  matrix_.setRotate(SkFloatToScalar(degree));
}

void Transform::SetScaleX(float x) {
  matrix_.setScaleX(SkFloatToScalar(x));
}

void Transform::SetScaleY(float y) {
  matrix_.setScaleY(SkFloatToScalar(y));
}

void Transform::SetScale(float x, float y) {
  matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y));
}

void Transform::SetTranslateX(float x) {
  matrix_.setTranslateX(SkFloatToScalar(x));
}

void Transform::SetTranslateY(float y) {
  matrix_.setTranslateY(SkFloatToScalar(y));
}

void Transform::SetTranslate(float x, float y) {
  matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y));
}

void Transform::ConcatRotate(float degree) {
  matrix_.postRotate(SkFloatToScalar(degree));
}

void Transform::ConcatScale(float x, float y) {
  matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y));
}

void Transform::ConcatTranslate(float x, float y) {
  matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y));
}

bool Transform::PreconcatTransform(const Transform& transform) {
  return matrix_.setConcat(matrix_, transform.matrix_);
}

bool Transform::ConcatTransform(const Transform& transform) {
  return matrix_.setConcat(transform.matrix_, matrix_);
}

bool Transform::HasChange() const {
  return !matrix_.isIdentity();
}

bool Transform::TransformPoint(gfx::Point* point) {
  SkPoint skp;
  matrix_.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp);
  point->SetPoint(static_cast<int>(std::floor(skp.fX)),
                  static_cast<int>(std::floor(skp.fY)));
  return true;
}

bool Transform::TransformPointReverse(gfx::Point* point) {
  SkMatrix inverse;
  // TODO(sad): Try to avoid trying to invert the matrix.
  if (matrix_.invert(&inverse)) {
    SkPoint skp;
    inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp);
    point->SetPoint(static_cast<int>(std::floor(skp.fX)),
                    static_cast<int>(std::floor(skp.fY)));
    return true;
  }
  return false;
}

bool Transform::TransformRect(gfx::Rect* rect) {
  SkRect src = gfx::RectToSkRect(*rect);
  if (!matrix_.mapRect(&src))
    return false;
  *rect = gfx::SkRectToRect(src);
  return true;
}

bool Transform::TransformRectReverse(gfx::Rect* rect) {
  SkMatrix inverse;
  if (!matrix_.invert(&inverse))
    return false;

  SkRect src = gfx::RectToSkRect(*rect);
  if (!inverse.mapRect(&src))
    return false;
  *rect = gfx::SkRectToRect(src);
  return true;
}

}  // namespace ui