/platform/external/webkit/WebCore/css/CSSPrimitiveValue.h

https://github.com/aharish/totoro-gb-opensource-update2 · C Header · 218 lines · 141 code · 42 blank · 35 comment · 8 complexity · 7524238f041aad4cf0d5c0bf3bba277d MD5 · raw file

  1. /*
  2. * (C) 1999-2003 Lars Knoll (knoll@kde.org)
  3. * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
  4. * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this library; see the file COPYING.LIB. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef CSSPrimitiveValue_h
  22. #define CSSPrimitiveValue_h
  23. #include "CSSValue.h"
  24. #include "Color.h"
  25. #include <wtf/PassRefPtr.h>
  26. namespace WebCore {
  27. class Counter;
  28. class DashboardRegion;
  29. class Pair;
  30. class RGBColor;
  31. class Rect;
  32. class RenderStyle;
  33. class StringImpl;
  34. struct Length;
  35. class CSSPrimitiveValue : public CSSValue {
  36. public:
  37. enum UnitTypes {
  38. CSS_UNKNOWN = 0,
  39. CSS_NUMBER = 1,
  40. CSS_PERCENTAGE = 2,
  41. CSS_EMS = 3,
  42. CSS_EXS = 4,
  43. CSS_PX = 5,
  44. CSS_CM = 6,
  45. CSS_MM = 7,
  46. CSS_IN = 8,
  47. CSS_PT = 9,
  48. CSS_PC = 10,
  49. CSS_DEG = 11,
  50. CSS_RAD = 12,
  51. CSS_GRAD = 13,
  52. CSS_MS = 14,
  53. CSS_S = 15,
  54. CSS_HZ = 16,
  55. CSS_KHZ = 17,
  56. CSS_DIMENSION = 18,
  57. CSS_STRING = 19,
  58. CSS_URI = 20,
  59. CSS_IDENT = 21,
  60. CSS_ATTR = 22,
  61. CSS_COUNTER = 23,
  62. CSS_RECT = 24,
  63. CSS_RGBCOLOR = 25,
  64. CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
  65. CSS_DASHBOARD_REGION = 101, // FIXME: Dashboard region should not be a primitive value.
  66. CSS_UNICODE_RANGE = 102,
  67. // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
  68. CSS_PARSER_OPERATOR = 103,
  69. CSS_PARSER_INTEGER = 104,
  70. CSS_PARSER_VARIABLE_FUNCTION_SYNTAX = 105,
  71. CSS_PARSER_HEXCOLOR = 106,
  72. // This is used internally for unknown identifiers
  73. CSS_PARSER_IDENTIFIER = 107,
  74. // These are from CSS3 Values and Units, but that isn't a finished standard yet
  75. CSS_TURN = 108,
  76. CSS_REMS = 109
  77. };
  78. static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
  79. type == CSSPrimitiveValue::CSS_REMS; }
  80. static PassRefPtr<CSSPrimitiveValue> createIdentifier(int ident);
  81. static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue);
  82. static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type);
  83. static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type);
  84. template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
  85. {
  86. return adoptRef(new CSSPrimitiveValue(value));
  87. }
  88. virtual ~CSSPrimitiveValue();
  89. void cleanup();
  90. unsigned short primitiveType() const { return m_type; }
  91. bool isVariable() const { return m_type == CSS_PARSER_VARIABLE_FUNCTION_SYNTAX; }
  92. /*
  93. * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
  94. * the fontinfo in case val is defined in em or ex.
  95. *
  96. * The metrics have to be a bit different for screen and printer output.
  97. * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
  98. *
  99. * this is screen/printer dependent, so we probably need a config option for this,
  100. * and some tool to calibrate.
  101. */
  102. int computeLengthInt(RenderStyle* currStyle, RenderStyle* rootStyle);
  103. int computeLengthInt(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier);
  104. int computeLengthIntForLength(RenderStyle* currStyle, RenderStyle* rootStyle);
  105. int computeLengthIntForLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier);
  106. short computeLengthShort(RenderStyle* currStyle, RenderStyle* rootStyle);
  107. short computeLengthShort(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier);
  108. float computeLengthFloat(RenderStyle* currStyle, RenderStyle* rootStyle, bool computingFontSize = false);
  109. float computeLengthFloat(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize = false);
  110. double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
  111. // use with care!!!
  112. void setPrimitiveType(unsigned short type) { m_type = type; }
  113. double getDoubleValue(unsigned short unitType, ExceptionCode&);
  114. double getDoubleValue(unsigned short unitType);
  115. double getDoubleValue() const { return m_value.num; }
  116. void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
  117. float getFloatValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<float>(getDoubleValue(unitType, ec)); }
  118. float getFloatValue(unsigned short unitType) { return static_cast<float>(getDoubleValue(unitType)); }
  119. float getFloatValue() const { return static_cast<float>(m_value.num); }
  120. int getIntValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<int>(getDoubleValue(unitType, ec)); }
  121. int getIntValue(unsigned short unitType) { return static_cast<int>(getDoubleValue(unitType)); }
  122. int getIntValue() const { return static_cast<int>(m_value.num); }
  123. void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
  124. String getStringValue(ExceptionCode&) const;
  125. String getStringValue() const;
  126. Counter* getCounterValue(ExceptionCode&) const;
  127. Counter* getCounterValue() const { return m_type != CSS_COUNTER ? 0 : m_value.counter; }
  128. Rect* getRectValue(ExceptionCode&) const;
  129. Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; }
  130. PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
  131. RGBA32 getRGBA32Value() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
  132. Pair* getPairValue(ExceptionCode&) const;
  133. Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; }
  134. DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
  135. int getIdent();
  136. template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
  137. virtual bool parseString(const String&, bool = false);
  138. virtual String cssText() const;
  139. virtual bool isQuirkValue() { return false; }
  140. virtual CSSParserValue parserValue() const;
  141. virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
  142. protected:
  143. // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases.
  144. CSSPrimitiveValue(int ident);
  145. CSSPrimitiveValue(double, UnitTypes);
  146. CSSPrimitiveValue(const String&, UnitTypes);
  147. private:
  148. CSSPrimitiveValue();
  149. CSSPrimitiveValue(unsigned color); // RGB value
  150. CSSPrimitiveValue(const Length&);
  151. template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
  152. template<typename T> CSSPrimitiveValue(T* val) { init(PassRefPtr<T>(val)); }
  153. template<typename T> CSSPrimitiveValue(PassRefPtr<T> val) { init(val); }
  154. static void create(int); // compile-time guard
  155. static void create(unsigned); // compile-time guard
  156. template<typename T> operator T*(); // compile-time guard
  157. void init(PassRefPtr<Counter>);
  158. void init(PassRefPtr<Rect>);
  159. void init(PassRefPtr<Pair>);
  160. void init(PassRefPtr<DashboardRegion>); // FIXME: Dashboard region should not be a primitive value.
  161. virtual bool isPrimitiveValue() const { return true; }
  162. virtual unsigned short cssValueType() const;
  163. int m_type;
  164. union {
  165. int ident;
  166. double num;
  167. StringImpl* string;
  168. Counter* counter;
  169. Rect* rect;
  170. unsigned rgbcolor;
  171. Pair* pair;
  172. DashboardRegion* region;
  173. } m_value;
  174. };
  175. } // namespace WebCore
  176. #endif // CSSPrimitiveValue_h