Rect.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 1999-2003 Lars Knoll (knoll@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. #ifndef Rect_h
  21. #define Rect_h
  22. #include "CSSPrimitiveValue.h"
  23. #include <wtf/RefPtr.h>
  24. #include <wtf/text/StringBuilder.h>
  25. namespace WebCore {
  26. class RectBase {
  27. public:
  28. CSSPrimitiveValue* top() const { return m_top.get(); }
  29. CSSPrimitiveValue* right() const { return m_right.get(); }
  30. CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
  31. CSSPrimitiveValue* left() const { return m_left.get(); }
  32. void setTop(PassRefPtr<CSSPrimitiveValue> top) { m_top = top; }
  33. void setRight(PassRefPtr<CSSPrimitiveValue> right) { m_right = right; }
  34. void setBottom(PassRefPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
  35. void setLeft(PassRefPtr<CSSPrimitiveValue> left) { m_left = left; }
  36. bool equals(const RectBase& other) const
  37. {
  38. return compareCSSValuePtr(m_top, other.m_top)
  39. && compareCSSValuePtr(m_right, other.m_right)
  40. && compareCSSValuePtr(m_left, other.m_left)
  41. && compareCSSValuePtr(m_bottom, other.m_bottom);
  42. }
  43. #if ENABLE(CSS_VARIABLES)
  44. bool hasVariableReference() const
  45. {
  46. return m_top->hasVariableReference()
  47. || m_right->hasVariableReference()
  48. || m_bottom->hasVariableReference()
  49. || m_left->hasVariableReference();
  50. }
  51. #endif
  52. protected:
  53. RectBase() { }
  54. RectBase(const RectBase& cloneFrom)
  55. : m_top(cloneFrom.m_top ? cloneFrom.m_top->cloneForCSSOM() : 0)
  56. , m_right(cloneFrom.m_right ? cloneFrom.m_right->cloneForCSSOM() : 0)
  57. , m_bottom(cloneFrom.m_bottom ? cloneFrom.m_bottom->cloneForCSSOM() : 0)
  58. , m_left(cloneFrom.m_left ? cloneFrom.m_left->cloneForCSSOM() : 0)
  59. {
  60. }
  61. ~RectBase() { }
  62. private:
  63. RefPtr<CSSPrimitiveValue> m_top;
  64. RefPtr<CSSPrimitiveValue> m_right;
  65. RefPtr<CSSPrimitiveValue> m_bottom;
  66. RefPtr<CSSPrimitiveValue> m_left;
  67. };
  68. class Rect : public RectBase, public RefCounted<Rect> {
  69. public:
  70. static PassRefPtr<Rect> create() { return adoptRef(new Rect); }
  71. PassRefPtr<Rect> cloneForCSSOM() const { return adoptRef(new Rect(*this)); }
  72. String cssText() const
  73. {
  74. return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
  75. }
  76. #if ENABLE(CSS_VARIABLES)
  77. String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
  78. {
  79. return generateCSSString(top()->customSerializeResolvingVariables(variables),
  80. right()->customSerializeResolvingVariables(variables),
  81. bottom()->customSerializeResolvingVariables(variables),
  82. left()->customSerializeResolvingVariables(variables));
  83. }
  84. #endif
  85. private:
  86. Rect() { }
  87. Rect(const Rect& cloneFrom) : RectBase(cloneFrom), RefCounted<Rect>() { }
  88. static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
  89. {
  90. return "rect(" + top + ' ' + right + ' ' + bottom + ' ' + left + ')';
  91. }
  92. };
  93. class Quad : public RectBase, public RefCounted<Quad> {
  94. public:
  95. static PassRefPtr<Quad> create() { return adoptRef(new Quad); }
  96. PassRefPtr<Quad> cloneForCSSOM() const { return adoptRef(new Quad(*this)); }
  97. String cssText() const
  98. {
  99. return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
  100. }
  101. #if ENABLE(CSS_VARIABLES)
  102. String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
  103. {
  104. return generateCSSString(top()->customSerializeResolvingVariables(variables),
  105. right()->customSerializeResolvingVariables(variables),
  106. bottom()->customSerializeResolvingVariables(variables),
  107. left()->customSerializeResolvingVariables(variables));
  108. }
  109. #endif
  110. private:
  111. Quad() { }
  112. Quad(const Quad& cloneFrom) : RectBase(cloneFrom), RefCounted<Quad>() { }
  113. static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
  114. {
  115. StringBuilder result;
  116. // reserve space for the four strings, plus three space separator characters.
  117. result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
  118. result.append(top);
  119. if (right != top || bottom != top || left != top) {
  120. result.append(' ');
  121. result.append(right);
  122. if (bottom != top || right != left) {
  123. result.append(' ');
  124. result.append(bottom);
  125. if (left != right) {
  126. result.append(' ');
  127. result.append(left);
  128. }
  129. }
  130. }
  131. return result.toString();
  132. }
  133. };
  134. } // namespace WebCore
  135. #endif // Rect_h