Home | History | Annotate | Download | only in geometry
      1 /*
      2  * Copyright (C) 2011 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 #ifndef TransformState_h
     27 #define TransformState_h
     28 
     29 #include "platform/geometry/FloatPoint.h"
     30 #include "platform/geometry/FloatQuad.h"
     31 #include "platform/geometry/IntSize.h"
     32 #include "platform/geometry/LayoutSize.h"
     33 #include "platform/transforms/AffineTransform.h"
     34 #include "platform/transforms/TransformationMatrix.h"
     35 #include "wtf/OwnPtr.h"
     36 
     37 namespace WebCore {
     38 
     39 class PLATFORM_EXPORT TransformState {
     40 public:
     41     enum TransformDirection { ApplyTransformDirection, UnapplyInverseTransformDirection };
     42     enum TransformAccumulation { FlattenTransform, AccumulateTransform };
     43 
     44     TransformState(TransformDirection mappingDirection, const FloatPoint& p, const FloatQuad& quad)
     45         : m_lastPlanarPoint(p)
     46         , m_lastPlanarQuad(quad)
     47         , m_accumulatingTransform(false)
     48         , m_mapPoint(true)
     49         , m_mapQuad(true)
     50         , m_direction(mappingDirection)
     51     {
     52     }
     53 
     54     TransformState(TransformDirection mappingDirection, const FloatPoint& p)
     55         : m_lastPlanarPoint(p)
     56         , m_accumulatingTransform(false)
     57         , m_mapPoint(true)
     58         , m_mapQuad(false)
     59         , m_direction(mappingDirection)
     60     {
     61     }
     62 
     63     TransformState(TransformDirection mappingDirection, const FloatQuad& quad)
     64         : m_lastPlanarQuad(quad)
     65         , m_accumulatingTransform(false)
     66         , m_mapPoint(false)
     67         , m_mapQuad(true)
     68         , m_direction(mappingDirection)
     69     {
     70     }
     71 
     72     TransformState(const TransformState& other) { *this = other; }
     73 
     74     TransformState& operator=(const TransformState&);
     75 
     76     void setQuad(const FloatQuad& quad)
     77     {
     78         // FIXME: this assumes that the quad being added is in the coordinate system of the current state.
     79         // This breaks if we're simultaneously mapping a point. https://bugs.webkit.org/show_bug.cgi?id=106680
     80         ASSERT(!m_mapPoint);
     81         m_accumulatedOffset = LayoutSize();
     82         m_lastPlanarQuad = quad;
     83     }
     84 
     85     void move(LayoutUnit x, LayoutUnit y, TransformAccumulation accumulate = FlattenTransform)
     86     {
     87         move(LayoutSize(x, y), accumulate);
     88     }
     89 
     90     void move(const LayoutSize&, TransformAccumulation = FlattenTransform);
     91     void applyTransform(const AffineTransform& transformFromContainer, TransformAccumulation = FlattenTransform, bool* wasClamped = 0);
     92     void applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation = FlattenTransform, bool* wasClamped = 0);
     93     void flatten(bool* wasClamped = 0);
     94 
     95     // Return the coords of the point or quad in the last flattened layer
     96     FloatPoint lastPlanarPoint() const { return m_lastPlanarPoint; }
     97     FloatQuad lastPlanarQuad() const { return m_lastPlanarQuad; }
     98 
     99     // Return the point or quad mapped through the current transform
    100     FloatPoint mappedPoint(bool* wasClamped = 0) const;
    101     FloatQuad mappedQuad(bool* wasClamped = 0) const;
    102 
    103 private:
    104     void translateTransform(const LayoutSize&);
    105     void translateMappedCoordinates(const LayoutSize&);
    106     void flattenWithTransform(const TransformationMatrix&, bool* wasClamped);
    107     void applyAccumulatedOffset();
    108 
    109     FloatPoint m_lastPlanarPoint;
    110     FloatQuad m_lastPlanarQuad;
    111 
    112     // We only allocate the transform if we need to
    113     OwnPtr<TransformationMatrix> m_accumulatedTransform;
    114     LayoutSize m_accumulatedOffset;
    115     bool m_accumulatingTransform;
    116     bool m_mapPoint, m_mapQuad;
    117     TransformDirection m_direction;
    118 };
    119 
    120 } // namespace WebCore
    121 
    122 #endif // TransformState_h
    123