2 * Copyright (C) 2011 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"
29 #include <wtf/PassOwnPtr.h>
33 TransformState& TransformState::operator=(const TransformState& other)
35 m_mapPoint = other.m_mapPoint;
36 m_mapQuad = other.m_mapQuad;
38 m_lastPlanarPoint = other.m_lastPlanarPoint;
40 m_lastPlanarQuad = other.m_lastPlanarQuad;
41 m_accumulatingTransform = other.m_accumulatingTransform;
42 m_direction = other.m_direction;
44 m_accumulatedTransform.clear();
46 if (other.m_accumulatedTransform)
47 m_accumulatedTransform = adoptPtr(new TransformationMatrix(*other.m_accumulatedTransform));
52 void TransformState::move(int x, int y, TransformAccumulation accumulate)
54 if (m_accumulatingTransform && m_accumulatedTransform) {
55 // If we're accumulating into an existing transform, apply the translation.
56 if (m_direction == ApplyTransformDirection)
57 m_accumulatedTransform->translateRight(x, y);
59 m_accumulatedTransform->translate(x, y);
61 // Then flatten if necessary.
62 if (accumulate == FlattenTransform)
65 // Just move the point and, optionally, the quad.
66 if (m_direction == UnapplyInverseTransformDirection) {
71 m_lastPlanarPoint.move(x, y);
73 m_lastPlanarQuad.move(x, y);
75 m_accumulatingTransform = accumulate == AccumulateTransform;
78 // FIXME: We transform AffineTransform to TransformationMatrix. This is rather inefficient.
79 void TransformState::applyTransform(const AffineTransform& transformFromContainer, TransformAccumulation accumulate)
81 applyTransform(transformFromContainer.toTransformationMatrix(), accumulate);
84 void TransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate)
86 // If we have an accumulated transform from last time, multiply in this transform
87 if (m_accumulatedTransform) {
88 if (m_direction == ApplyTransformDirection)
89 m_accumulatedTransform = adoptPtr(new TransformationMatrix(transformFromContainer * *m_accumulatedTransform));
91 m_accumulatedTransform->multiply(transformFromContainer);
92 } else if (accumulate == AccumulateTransform) {
93 // Make one if we started to accumulate
94 m_accumulatedTransform = adoptPtr(new TransformationMatrix(transformFromContainer));
97 if (accumulate == FlattenTransform) {
98 const TransformationMatrix* finalTransform = m_accumulatedTransform ? m_accumulatedTransform.get() : &transformFromContainer;
99 flattenWithTransform(*finalTransform);
101 m_accumulatingTransform = accumulate == AccumulateTransform;
104 void TransformState::flatten()
106 if (!m_accumulatedTransform) {
107 m_accumulatingTransform = false;
111 flattenWithTransform(*m_accumulatedTransform);
114 FloatPoint TransformState::mappedPoint() const
116 if (!m_accumulatedTransform)
117 return m_lastPlanarPoint;
119 if (m_direction == ApplyTransformDirection)
120 return m_accumulatedTransform->mapPoint(m_lastPlanarPoint);
122 return m_accumulatedTransform->inverse().projectPoint(m_lastPlanarPoint);
125 FloatQuad TransformState::mappedQuad() const
127 if (!m_accumulatedTransform)
128 return m_lastPlanarQuad;
130 if (m_direction == ApplyTransformDirection)
131 return m_accumulatedTransform->mapQuad(m_lastPlanarQuad);
133 return m_accumulatedTransform->inverse().projectQuad(m_lastPlanarQuad);
136 void TransformState::flattenWithTransform(const TransformationMatrix& t)
138 if (m_direction == ApplyTransformDirection) {
140 m_lastPlanarPoint = t.mapPoint(m_lastPlanarPoint);
142 m_lastPlanarQuad = t.mapQuad(m_lastPlanarQuad);
144 TransformationMatrix inverseTransform = t.inverse();
146 m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
148 m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad);
151 // We could throw away m_accumulatedTransform if we wanted to here, but that
152 // would cause thrash when traversing hierarchies with alternating
153 // preserve-3d and flat elements.
154 if (m_accumulatedTransform)
155 m_accumulatedTransform->makeIdentity();
156 m_accumulatingTransform = false;
159 } // namespace WebCore