• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #ifndef CSSPrimitiveValue_h
23 #define CSSPrimitiveValue_h
24 
25 #include "CSSValue.h"
26 #include <wtf/PassRefPtr.h>
27 
28 namespace WebCore {
29 
30 class Counter;
31 class DashboardRegion;
32 class Pair;
33 class Rect;
34 class RenderStyle;
35 class StringImpl;
36 
37 struct Length;
38 
39 class CSSPrimitiveValue : public CSSValue {
40 public:
41     enum UnitTypes {
42         CSS_UNKNOWN = 0,
43         CSS_NUMBER = 1,
44         CSS_PERCENTAGE = 2,
45         CSS_EMS = 3,
46         CSS_EXS = 4,
47         CSS_PX = 5,
48         CSS_CM = 6,
49         CSS_MM = 7,
50         CSS_IN = 8,
51         CSS_PT = 9,
52         CSS_PC = 10,
53         CSS_DEG = 11,
54         CSS_RAD = 12,
55         CSS_GRAD = 13,
56         CSS_MS = 14,
57         CSS_S = 15,
58         CSS_HZ = 16,
59         CSS_KHZ = 17,
60         CSS_DIMENSION = 18,
61         CSS_STRING = 19,
62         CSS_URI = 20,
63         CSS_IDENT = 21,
64         CSS_ATTR = 22,
65         CSS_COUNTER = 23,
66         CSS_RECT = 24,
67         CSS_RGBCOLOR = 25,
68         CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
69         CSS_DASHBOARD_REGION = 101, // FIXME: Dashboard region should not be a primitive value.
70         CSS_UNICODE_RANGE = 102,
71 
72         // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
73         CSS_PARSER_OPERATOR = 103,
74         CSS_PARSER_INTEGER = 104,
75         CSS_PARSER_VARIABLE_FUNCTION_SYNTAX = 105,
76         CSS_PARSER_HEXCOLOR = 106,
77 
78         // This is used internally for unknown identifiers
79         CSS_PARSER_IDENTIFIER = 107,
80 
81         // This unit is in CSS 3, but that isn't a finished standard yet
82         CSS_TURN = 108
83     };
84 
85     static PassRefPtr<CSSPrimitiveValue> createIdentifier(int ident);
86     static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue);
87     static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type);
88     static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type);
89 
create(T value)90     template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
91     {
92         return adoptRef(new CSSPrimitiveValue(value));
93     }
94 
95     virtual ~CSSPrimitiveValue();
96 
97     void cleanup();
98 
primitiveType()99     unsigned short primitiveType() const { return m_type; }
100 
isVariable()101     bool isVariable() const { return m_type == CSS_PARSER_VARIABLE_FUNCTION_SYNTAX; }
102 
103     /*
104      * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
105      * the fontinfo in case val is defined in em or ex.
106      *
107      * The metrics have to be a bit different for screen and printer output.
108      * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
109      *
110      * this is screen/printer dependent, so we probably need a config option for this,
111      * and some tool to calibrate.
112      */
113     int computeLengthInt(RenderStyle*);
114     int computeLengthInt(RenderStyle*, double multiplier);
115     int computeLengthIntForLength(RenderStyle*);
116     int computeLengthIntForLength(RenderStyle*, double multiplier);
117     short computeLengthShort(RenderStyle*);
118     short computeLengthShort(RenderStyle*, double multiplier);
119     float computeLengthFloat(RenderStyle*, bool computingFontSize = false);
120     float computeLengthFloat(RenderStyle*, double multiplier, bool computingFontSize = false);
121     double computeLengthDouble(RenderStyle*, double multiplier = 1.0, bool computingFontSize = false);
122 
123     // use with care!!!
setPrimitiveType(unsigned short type)124     void setPrimitiveType(unsigned short type) { m_type = type; }
125 
126     double getDoubleValue(unsigned short unitType, ExceptionCode&);
127     double getDoubleValue(unsigned short unitType);
getDoubleValue()128     double getDoubleValue() const { return m_value.num; }
129 
130     void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
getFloatValue(unsigned short unitType,ExceptionCode & ec)131     float getFloatValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<float>(getDoubleValue(unitType, ec)); }
getFloatValue(unsigned short unitType)132     float getFloatValue(unsigned short unitType) { return static_cast<float>(getDoubleValue(unitType)); }
getFloatValue()133     float getFloatValue() const { return static_cast<float>(m_value.num); }
134 
getIntValue(unsigned short unitType,ExceptionCode & ec)135     int getIntValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<int>(getDoubleValue(unitType, ec)); }
getIntValue(unsigned short unitType)136     int getIntValue(unsigned short unitType) { return static_cast<int>(getDoubleValue(unitType)); }
getIntValue()137     int getIntValue() const { return static_cast<int>(m_value.num); }
138 
139     void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
140     String getStringValue(ExceptionCode&) const;
141     String getStringValue() const;
142 
143     Counter* getCounterValue(ExceptionCode&) const;
getCounterValue()144     Counter* getCounterValue() const { return m_type != CSS_COUNTER ? 0 : m_value.counter; }
145 
146     Rect* getRectValue(ExceptionCode&) const;
getRectValue()147     Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; }
148 
149     unsigned getRGBColorValue(ExceptionCode&) const;
getRGBColorValue()150     unsigned getRGBColorValue() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
151 
152     Pair* getPairValue(ExceptionCode&) const;
getPairValue()153     Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; }
154 
getDashboardRegionValue()155     DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
156 
157     int getIdent();
158     template<typename T> operator T() const; // Defined in CSSPrimitiveValueMappings.h
159 
160     virtual bool parseString(const String&, bool = false);
161     virtual String cssText() const;
162 
isQuirkValue()163     virtual bool isQuirkValue() { return false; }
164 
165     virtual CSSParserValue parserValue() const;
166 
167     virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
168 
169 protected:
170     // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases.
171     CSSPrimitiveValue(int ident);
172     CSSPrimitiveValue(double, UnitTypes);
173     CSSPrimitiveValue(const String&, UnitTypes);
174 
175 private:
176     CSSPrimitiveValue();
177     CSSPrimitiveValue(unsigned color); // RGB value
178     CSSPrimitiveValue(const Length&);
179 
180     template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
CSSPrimitiveValue(T * val)181     template<typename T> CSSPrimitiveValue(T* val) { init(PassRefPtr<T>(val)); }
CSSPrimitiveValue(PassRefPtr<T> val)182     template<typename T> CSSPrimitiveValue(PassRefPtr<T> val) { init(val); }
183 
184     static void create(int); // compile-time guard
185     static void create(unsigned); // compile-time guard
186     template<typename T> operator T*(); // compile-time guard
187 
188     void init(PassRefPtr<Counter>);
189     void init(PassRefPtr<Rect>);
190     void init(PassRefPtr<Pair>);
191     void init(PassRefPtr<DashboardRegion>); // FIXME: Dashboard region should not be a primitive value.
192 
isPrimitiveValue()193     virtual bool isPrimitiveValue() const { return true; }
194 
195     virtual unsigned short cssValueType() const;
196 
197     int m_type;
198     union {
199         int ident;
200         double num;
201         StringImpl* string;
202         Counter* counter;
203         Rect* rect;
204         unsigned rgbcolor;
205         Pair* pair;
206         DashboardRegion* region;
207     } m_value;
208 };
209 
210 } // namespace WebCore
211 
212 #endif // CSSPrimitiveValue_h
213