Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 1999-2003 Lars Knoll (knoll (at) kde.org)
      3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #ifndef Rect_h
     22 #define Rect_h
     23 
     24 #include "core/css/CSSPrimitiveValue.h"
     25 #include "wtf/RefPtr.h"
     26 #include "wtf/text/StringBuilder.h"
     27 
     28 namespace WebCore {
     29 
     30 class RectBase : public RefCountedWillBeGarbageCollected<RectBase> {
     31     DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(RectBase);
     32 public:
     33     CSSPrimitiveValue* top() const { return m_top.get(); }
     34     CSSPrimitiveValue* right() const { return m_right.get(); }
     35     CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
     36     CSSPrimitiveValue* left() const { return m_left.get(); }
     37 
     38     void setTop(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> top) { m_top = top; }
     39     void setRight(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> right) { m_right = right; }
     40     void setBottom(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
     41     void setLeft(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> left) { m_left = left; }
     42 
     43     bool equals(const RectBase& other) const
     44     {
     45         return compareCSSValuePtr(m_top, other.m_top)
     46             && compareCSSValuePtr(m_right, other.m_right)
     47             && compareCSSValuePtr(m_left, other.m_left)
     48             && compareCSSValuePtr(m_bottom, other.m_bottom);
     49     }
     50 
     51     void trace(Visitor*);
     52 
     53 protected:
     54     RectBase() { }
     55     RectBase(const RectBase& cloneFrom)
     56         : m_top(cloneFrom.m_top ? cloneFrom.m_top->cloneForCSSOM() : nullptr)
     57         , m_right(cloneFrom.m_right ? cloneFrom.m_right->cloneForCSSOM() : nullptr)
     58         , m_bottom(cloneFrom.m_bottom ? cloneFrom.m_bottom->cloneForCSSOM() : nullptr)
     59         , m_left(cloneFrom.m_left ? cloneFrom.m_left->cloneForCSSOM() : nullptr)
     60     {
     61     }
     62 
     63 private:
     64     RefPtrWillBeMember<CSSPrimitiveValue> m_top;
     65     RefPtrWillBeMember<CSSPrimitiveValue> m_right;
     66     RefPtrWillBeMember<CSSPrimitiveValue> m_bottom;
     67     RefPtrWillBeMember<CSSPrimitiveValue> m_left;
     68 };
     69 
     70 class Rect : public RectBase {
     71 public:
     72     static PassRefPtrWillBeRawPtr<Rect> create() { return adoptRefWillBeNoop(new Rect); }
     73 
     74     PassRefPtrWillBeRawPtr<Rect> cloneForCSSOM() const { return adoptRefWillBeNoop(new Rect(*this)); }
     75 
     76     String cssText() const
     77     {
     78         return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
     79     }
     80 
     81 private:
     82     Rect() { }
     83     Rect(const Rect& cloneFrom) : RectBase(cloneFrom) { }
     84     static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
     85     {
     86         return "rect(" + top + ' ' + right + ' ' + bottom + ' ' + left + ')';
     87     }
     88 
     89     // NOTE: If adding fields to this class please make the RectBase trace
     90     // method virtual and add a trace method in this subclass tracing the new
     91     // fields.
     92 };
     93 
     94 class Quad : public RectBase {
     95 public:
     96     static PassRefPtrWillBeRawPtr<Quad> create() { return adoptRefWillBeNoop(new Quad); }
     97 
     98     PassRefPtrWillBeRawPtr<Quad> cloneForCSSOM() const { return adoptRefWillBeNoop(new Quad(*this)); }
     99 
    100     String cssText() const
    101     {
    102         return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
    103     }
    104 
    105 private:
    106     Quad() { }
    107     Quad(const Quad& cloneFrom) : RectBase(cloneFrom) { }
    108     static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
    109     {
    110         StringBuilder result;
    111         // reserve space for the four strings, plus three space separator characters.
    112         result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
    113         result.append(top);
    114         if (right != top || bottom != top || left != top) {
    115             result.append(' ');
    116             result.append(right);
    117             if (bottom != top || right != left) {
    118                 result.append(' ');
    119                 result.append(bottom);
    120                 if (left != right) {
    121                     result.append(' ');
    122                     result.append(left);
    123                 }
    124             }
    125         }
    126         return result.toString();
    127     }
    128 
    129     // NOTE: If adding fields to this class please make the RectBase trace
    130     // method virtual and add a trace method in this subclass tracing the new
    131     // fields.
    132 };
    133 
    134 } // namespace WebCore
    135 
    136 #endif // Rect_h
    137