1 /*
2  * Copyright (C) 2009 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #include "config.h"
27 #include "TransformState.h"
28 
29 #include <wtf/PassOwnPtr.h>
30 
31 namespace WebCore {
32 
move(int x,int y,TransformAccumulation accumulate)33 void TransformState::move(int x, int y, TransformAccumulation accumulate)
34 {
35     if (m_accumulatingTransform && m_accumulatedTransform) {
36         // If we're accumulating into an existing transform, apply the translation.
37         if (m_direction == ApplyTransformDirection)
38             m_accumulatedTransform->translateRight(x, y);
39         else
40             m_accumulatedTransform->translate(-x, -y);  // We're unapplying, so negate
41 
42         // Then flatten if necessary.
43         if (accumulate == FlattenTransform)
44             flatten();
45     } else {
46         // Just move the point and, optionally, the quad.
47         m_lastPlanarPoint.move(x, y);
48         if (m_mapQuad)
49             m_lastPlanarQuad.move(x, y);
50     }
51     m_accumulatingTransform = accumulate == AccumulateTransform;
52 }
53 
54 // FIXME: We transform AffineTransform to TransformationMatrix. This is rather inefficient.
applyTransform(const AffineTransform & transformFromContainer,TransformAccumulation accumulate)55 void TransformState::applyTransform(const AffineTransform& transformFromContainer, TransformAccumulation accumulate)
56 {
57     applyTransform(transformFromContainer.toTransformationMatrix(), accumulate);
58 }
59 
applyTransform(const TransformationMatrix & transformFromContainer,TransformAccumulation accumulate)60 void TransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate)
61 {
62     // If we have an accumulated transform from last time, multiply in this transform
63     if (m_accumulatedTransform) {
64         if (m_direction == ApplyTransformDirection)
65             m_accumulatedTransform = adoptPtr(new TransformationMatrix(transformFromContainer * *m_accumulatedTransform));
66         else
67             m_accumulatedTransform->multiply(transformFromContainer);
68     } else if (accumulate == AccumulateTransform) {
69         // Make one if we started to accumulate
70         m_accumulatedTransform = adoptPtr(new TransformationMatrix(transformFromContainer));
71     }
72 
73     if (accumulate == FlattenTransform) {
74         const TransformationMatrix* finalTransform = m_accumulatedTransform ? m_accumulatedTransform.get() : &transformFromContainer;
75         flattenWithTransform(*finalTransform);
76     }
77     m_accumulatingTransform = accumulate == AccumulateTransform;
78 }
79 
flatten()80 void TransformState::flatten()
81 {
82     if (!m_accumulatedTransform) {
83         m_accumulatingTransform = false;
84         return;
85     }
86 
87     flattenWithTransform(*m_accumulatedTransform);
88 }
89 
mappedPoint() const90 FloatPoint TransformState::mappedPoint() const
91 {
92     if (!m_accumulatedTransform)
93         return m_lastPlanarPoint;
94 
95     if (m_direction == ApplyTransformDirection)
96         return m_accumulatedTransform->mapPoint(m_lastPlanarPoint);
97 
98     return m_accumulatedTransform->inverse().projectPoint(m_lastPlanarPoint);
99 }
100 
mappedQuad() const101 FloatQuad TransformState::mappedQuad() const
102 {
103     if (!m_accumulatedTransform)
104         return m_lastPlanarQuad;
105 
106     if (m_direction == ApplyTransformDirection)
107         return m_accumulatedTransform->mapQuad(m_lastPlanarQuad);
108 
109     return m_accumulatedTransform->inverse().projectQuad(m_lastPlanarQuad);
110 }
111 
flattenWithTransform(const TransformationMatrix & t)112 void TransformState::flattenWithTransform(const TransformationMatrix& t)
113 {
114     if (m_direction == ApplyTransformDirection) {
115         m_lastPlanarPoint = t.mapPoint(m_lastPlanarPoint);
116         if (m_mapQuad)
117             m_lastPlanarQuad = t.mapQuad(m_lastPlanarQuad);
118     } else {
119         TransformationMatrix inverseTransform = t.inverse();
120         m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
121         if (m_mapQuad)
122             m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad);
123     }
124 
125     // We could throw away m_accumulatedTransform if we wanted to here, but that
126     // would cause thrash when traversing hierarchies with alternating
127     // preserve-3d and flat elements.
128     if (m_accumulatedTransform)
129         m_accumulatedTransform->makeIdentity();
130     m_accumulatingTransform = false;
131 }
132 
133 // HitTestingTransformState methods
translate(int x,int y,TransformAccumulation accumulate)134 void HitTestingTransformState::translate(int x, int y, TransformAccumulation accumulate)
135 {
136     m_accumulatedTransform.translate(x, y);
137     if (accumulate == FlattenTransform)
138         flattenWithTransform(m_accumulatedTransform);
139 
140     m_accumulatingTransform = accumulate == AccumulateTransform;
141 }
142 
applyTransform(const TransformationMatrix & transformFromContainer,TransformAccumulation accumulate)143 void HitTestingTransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate)
144 {
145     m_accumulatedTransform.multiply(transformFromContainer);
146     if (accumulate == FlattenTransform)
147         flattenWithTransform(m_accumulatedTransform);
148 
149     m_accumulatingTransform = accumulate == AccumulateTransform;
150 }
151 
flatten()152 void HitTestingTransformState::flatten()
153 {
154     flattenWithTransform(m_accumulatedTransform);
155 }
156 
flattenWithTransform(const TransformationMatrix & t)157 void HitTestingTransformState::flattenWithTransform(const TransformationMatrix& t)
158 {
159     TransformationMatrix inverseTransform = t.inverse();
160     m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
161     m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad);
162 
163     m_accumulatedTransform.makeIdentity();
164     m_accumulatingTransform = false;
165 }
166 
mappedPoint() const167 FloatPoint HitTestingTransformState::mappedPoint() const
168 {
169     return m_accumulatedTransform.inverse().projectPoint(m_lastPlanarPoint);
170 }
171 
mappedQuad() const172 FloatQuad HitTestingTransformState::mappedQuad() const
173 {
174     return m_accumulatedTransform.inverse().projectQuad(m_lastPlanarQuad);
175 }
176 
177 } // namespace WebCore
178