2
* Copyright (C) 2009 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "TransformState.h"
31
void TransformState::move(int x, int y, TransformAccumulation accumulate)
33
if (m_accumulatingTransform && m_accumulatedTransform) {
34
// If we're accumulating into an existing transform, apply the translation.
35
if (m_direction == ApplyTransformDirection)
36
m_accumulatedTransform->translateRight(x, y);
38
m_accumulatedTransform->translate(-x, -y); // We're unapplying, so negate
40
// Then flatten if necessary.
41
if (accumulate == FlattenTransform)
44
// Just move the point and, optionally, the quad.
45
m_lastPlanarPoint.move(x, y);
47
m_lastPlanarQuad.move(x, y);
49
m_accumulatingTransform = accumulate == AccumulateTransform;
52
void TransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate)
54
// If we have an accumulated transform from last time, multiply in this transform
55
if (m_accumulatedTransform) {
56
if (m_direction == ApplyTransformDirection)
57
m_accumulatedTransform->multiply(transformFromContainer);
59
m_accumulatedTransform->multLeft(transformFromContainer);
60
} else if (accumulate == AccumulateTransform) {
61
// Make one if we started to accumulate
62
m_accumulatedTransform.set(new TransformationMatrix(transformFromContainer));
65
if (accumulate == FlattenTransform) {
66
const TransformationMatrix* finalTransform = m_accumulatedTransform ? m_accumulatedTransform.get() : &transformFromContainer;
67
flattenWithTransform(*finalTransform);
69
m_accumulatingTransform = accumulate == AccumulateTransform;
72
void TransformState::flatten()
74
if (!m_accumulatedTransform) {
75
m_accumulatingTransform = false;
79
flattenWithTransform(*m_accumulatedTransform);
82
FloatPoint TransformState::mappedPoint() const
84
if (!m_accumulatedTransform)
85
return m_lastPlanarPoint;
87
if (m_direction == ApplyTransformDirection)
88
return m_accumulatedTransform->mapPoint(m_lastPlanarPoint);
90
return m_accumulatedTransform->inverse().projectPoint(m_lastPlanarPoint);
93
FloatQuad TransformState::mappedQuad() const
95
if (!m_accumulatedTransform)
96
return m_lastPlanarQuad;
98
if (m_direction == ApplyTransformDirection)
99
return m_accumulatedTransform->mapQuad(m_lastPlanarQuad);
101
return m_accumulatedTransform->inverse().projectQuad(m_lastPlanarQuad);
104
void TransformState::flattenWithTransform(const TransformationMatrix& t)
106
if (m_direction == ApplyTransformDirection) {
107
m_lastPlanarPoint = t.mapPoint(m_lastPlanarPoint);
109
m_lastPlanarQuad = t.mapQuad(m_lastPlanarQuad);
111
TransformationMatrix inverseTransform = t.inverse();
112
m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
114
m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad);
117
// We could throw away m_accumulatedTransform if we wanted to here, but that
118
// would cause thrash when traversing hierarachies with alternating
119
// preserve-3d and flat elements.
120
if (m_accumulatedTransform)
121
m_accumulatedTransform->makeIdentity();
122
m_accumulatingTransform = false;
125
// HitTestingTransformState methods
126
void HitTestingTransformState::translate(int x, int y, TransformAccumulation accumulate)
128
m_accumulatedTransform.translate(x, y);
129
if (accumulate == FlattenTransform)
130
flattenWithTransform(m_accumulatedTransform);
132
m_accumulatingTransform = accumulate == AccumulateTransform;
135
void HitTestingTransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate)
137
m_accumulatedTransform.multLeft(transformFromContainer);
138
if (accumulate == FlattenTransform)
139
flattenWithTransform(m_accumulatedTransform);
141
m_accumulatingTransform = accumulate == AccumulateTransform;
144
void HitTestingTransformState::flatten()
146
flattenWithTransform(m_accumulatedTransform);
149
void HitTestingTransformState::flattenWithTransform(const TransformationMatrix& t)
151
TransformationMatrix inverseTransform = t.inverse();
152
m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
153
m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad);
155
m_accumulatedTransform.makeIdentity();
156
m_accumulatingTransform = false;
159
FloatPoint HitTestingTransformState::mappedPoint() const
161
return m_accumulatedTransform.inverse().projectPoint(m_lastPlanarPoint);
164
FloatQuad HitTestingTransformState::mappedQuad() const
166
return m_accumulatedTransform.inverse().projectQuad(m_lastPlanarQuad);
169
} // namespace WebCore