• 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 "core/CSSPropertyNames.h"
26 #include "core/CSSValueKeywords.h"
27 #include "core/css/CSSValue.h"
28 #include "platform/graphics/Color.h"
29 #include "wtf/Forward.h"
30 #include "wtf/MathExtras.h"
31 #include "wtf/PassRefPtr.h"
32 
33 namespace blink {
34 
35 class CSSBasicShape;
36 class CSSCalcValue;
37 class CSSToLengthConversionData;
38 class Counter;
39 class ExceptionState;
40 class Length;
41 class LengthSize;
42 class Pair;
43 class Quad;
44 class RGBColor;
45 class Rect;
46 class RenderStyle;
47 
48 // Dimension calculations are imprecise, often resulting in values of e.g.
49 // 44.99998. We need to go ahead and round if we're really close to the next
50 // integer value.
roundForImpreciseConversion(double value)51 template<typename T> inline T roundForImpreciseConversion(double value)
52 {
53     value += (value < 0) ? -0.01 : +0.01;
54     return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
55 }
56 
roundForImpreciseConversion(double value)57 template<> inline float roundForImpreciseConversion(double value)
58 {
59     double ceiledValue = ceil(value);
60     double proximityToNextInt = ceiledValue - value;
61     if (proximityToNextInt <= 0.01 && value > 0)
62         return static_cast<float>(ceiledValue);
63     if (proximityToNextInt >= 0.99 && value < 0)
64         return static_cast<float>(floor(value));
65     return static_cast<float>(value);
66 }
67 
68 // CSSPrimitiveValues are immutable. This class has manual ref-counting
69 // of unioned types and does not have the code necessary
70 // to handle any kind of mutations. All DOM-exposed "setters" just throw
71 // exceptions.
72 class CSSPrimitiveValue : public CSSValue {
73 public:
74     enum UnitType {
75         CSS_UNKNOWN = 0,
76         CSS_NUMBER = 1,
77         CSS_PERCENTAGE = 2,
78         CSS_EMS = 3,
79         CSS_EXS = 4,
80         CSS_PX = 5,
81         CSS_CM = 6,
82         CSS_MM = 7,
83         CSS_IN = 8,
84         CSS_PT = 9,
85         CSS_PC = 10,
86         CSS_DEG = 11,
87         CSS_RAD = 12,
88         CSS_GRAD = 13,
89         CSS_MS = 14,
90         CSS_S = 15,
91         CSS_HZ = 16,
92         CSS_KHZ = 17,
93         CSS_DIMENSION = 18,
94         CSS_STRING = 19,
95         CSS_URI = 20,
96         CSS_IDENT = 21,
97         CSS_ATTR = 22,
98         CSS_COUNTER = 23,
99         CSS_RECT = 24,
100         CSS_RGBCOLOR = 25,
101         // From CSS Values and Units. Viewport-percentage Lengths (vw/vh/vmin/vmax).
102         CSS_VW = 26,
103         CSS_VH = 27,
104         CSS_VMIN = 28,
105         CSS_VMAX = 29,
106         CSS_DPPX = 30,
107         CSS_DPI = 31,
108         CSS_DPCM = 32,
109         CSS_FR = 33,
110         CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
111         CSS_UNICODE_RANGE = 102,
112 
113         // FIXME: This is only used in CSSParserValue, so it's probably better as part of the enum there
114         CSS_PARSER_HEXCOLOR = 105,
115 
116         // These are from CSS3 Values and Units, but that isn't a finished standard yet
117         CSS_TURN = 107,
118         CSS_REMS = 108,
119         CSS_CHS = 109,
120 
121         // This is used internally for counter names (as opposed to counter values)
122         CSS_COUNTER_NAME = 110,
123 
124         // This is used by the CSS Shapes draft
125         CSS_SHAPE = 111,
126 
127         // Used by border images.
128         CSS_QUAD = 112,
129 
130         CSS_CALC = 113,
131         CSS_CALC_PERCENTAGE_WITH_NUMBER = 114,
132         CSS_CALC_PERCENTAGE_WITH_LENGTH = 115,
133 
134         CSS_PROPERTY_ID = 117,
135         CSS_VALUE_ID = 118
136     };
137 
138     enum LengthUnitType {
139         UnitTypePixels = 0,
140         UnitTypePercentage,
141         UnitTypeFontSize,
142         UnitTypeFontXSize,
143         UnitTypeRootFontSize,
144         UnitTypeZeroCharacterWidth,
145         UnitTypeViewportWidth,
146         UnitTypeViewportHeight,
147         UnitTypeViewportMin,
148         UnitTypeViewportMax,
149 
150         // This value must come after the last length unit type to enable iteration over the length unit types.
151         LengthUnitTypeCount,
152     };
153 
154     typedef Vector<double, CSSPrimitiveValue::LengthUnitTypeCount> CSSLengthArray;
155     void accumulateLengthArray(CSSLengthArray&, double multiplier = 1) const;
156 
157     // This enum follows the BisonCSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
158     enum UnitCategory {
159         UNumber,
160         UPercent,
161         ULength,
162         UAngle,
163         UTime,
164         UFrequency,
165         UResolution,
166         UOther
167     };
168     static UnitCategory unitCategory(UnitType);
169 
170     static UnitType fromName(const String& unit);
171 
isAngle()172     bool isAngle() const
173     {
174         return m_primitiveUnitType == CSS_DEG
175                || m_primitiveUnitType == CSS_RAD
176                || m_primitiveUnitType == CSS_GRAD
177                || m_primitiveUnitType == CSS_TURN;
178     }
isAttr()179     bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
isCounter()180     bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
isFontIndependentLength()181     bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
isFontRelativeLength()182     bool isFontRelativeLength() const
183     {
184         return m_primitiveUnitType == CSS_EMS
185             || m_primitiveUnitType == CSS_EXS
186             || m_primitiveUnitType == CSS_REMS
187             || m_primitiveUnitType == CSS_CHS;
188     }
isViewportPercentageLength()189     bool isViewportPercentageLength() const { return isViewportPercentageLength(static_cast<UnitType>(m_primitiveUnitType)); }
isViewportPercentageLength(UnitType type)190     static bool isViewportPercentageLength(UnitType type) { return type >= CSS_VW && type <= CSS_VMAX; }
isLength(UnitType type)191     static bool isLength(UnitType type)
192     {
193         return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS || isViewportPercentageLength(type);
194     }
isLength()195     bool isLength() const { return isLength(primitiveType()); }
isNumber()196     bool isNumber() const { return primitiveType() == CSS_NUMBER; }
isPercentage()197     bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
isPx()198     bool isPx() const { return primitiveType() == CSS_PX; }
isRect()199     bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
isRGBColor()200     bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
isShape()201     bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
isString()202     bool isString() const { return m_primitiveUnitType == CSS_STRING; }
isTime()203     bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
isURI()204     bool isURI() const { return m_primitiveUnitType == CSS_URI; }
isCalculated()205     bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
isCalculatedPercentageWithNumber()206     bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
isCalculatedPercentageWithLength()207     bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
isDotsPerInch(UnitType type)208     static bool isDotsPerInch(UnitType type) { return type == CSS_DPI; }
isDotsPerPixel(UnitType type)209     static bool isDotsPerPixel(UnitType type) { return type == CSS_DPPX; }
isDotsPerCentimeter(UnitType type)210     static bool isDotsPerCentimeter(UnitType type) { return type == CSS_DPCM; }
isResolution(UnitType type)211     static bool isResolution(UnitType type) { return type >= CSS_DPPX && type <= CSS_DPCM; }
isFlex()212     bool isFlex() const { return primitiveType() == CSS_FR; }
isValueID()213     bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; }
214     bool colorIsDerivedFromElement() const;
215 
createIdentifier(CSSValueID valueID)216     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSValueID valueID)
217     {
218         return adoptRefWillBeNoop(new CSSPrimitiveValue(valueID));
219     }
createIdentifier(CSSPropertyID propertyID)220     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID)
221     {
222         return adoptRefWillBeNoop(new CSSPrimitiveValue(propertyID));
223     }
createColor(unsigned rgbValue)224     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createColor(unsigned rgbValue)
225     {
226         return adoptRefWillBeNoop(new CSSPrimitiveValue(rgbValue, CSS_RGBCOLOR));
227     }
create(double value,UnitType type)228     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(double value, UnitType type)
229     {
230         return adoptRefWillBeNoop(new CSSPrimitiveValue(value, type));
231     }
create(const String & value,UnitType type)232     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const String& value, UnitType type)
233     {
234         return adoptRefWillBeNoop(new CSSPrimitiveValue(value, type));
235     }
create(const Length & value,float zoom)236     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const Length& value, float zoom)
237     {
238         return adoptRefWillBeNoop(new CSSPrimitiveValue(value, zoom));
239     }
create(const LengthSize & value,const RenderStyle & style)240     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const LengthSize& value, const RenderStyle& style)
241     {
242         return adoptRefWillBeNoop(new CSSPrimitiveValue(value, style));
243     }
create(T value)244     template<typename T> static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(T value)
245     {
246         return adoptRefWillBeNoop(new CSSPrimitiveValue(value));
247     }
248 
249     // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
250     // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
251     // When the quirky value is used, if you're in quirks mode, the margin will collapse away
252     // inside a table cell.
createAllowingMarginQuirk(double value,UnitType type)253     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitType type)
254     {
255         CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
256         quirkValue->m_isQuirkValue = true;
257         return adoptRefWillBeNoop(quirkValue);
258     }
259 
260     ~CSSPrimitiveValue();
261 
262     void cleanup();
263 
264     UnitType primitiveType() const;
265 
266     double computeDegrees();
267     double computeSeconds();
268 
269     /*
270      * Computes a length in pixels out of the given CSSValue
271      *
272      * The metrics have to be a bit different for screen and printer output.
273      * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
274      *
275      * this is screen/printer dependent, so we probably need a config option for this,
276      * and some tool to calibrate.
277      */
278     template<typename T> T computeLength(const CSSToLengthConversionData&);
279 
280     // Converts to a Length, mapping various unit types appropriately.
281     template<int> Length convertToLength(const CSSToLengthConversionData&);
282 
283     double getDoubleValue(UnitType, ExceptionState&) const;
284     double getDoubleValue(UnitType) const;
285     double getDoubleValue() const;
286 
287     // setFloatValue(..., ExceptionState&) and setStringValue() must use unsigned short instead of UnitType to match IDL bindings.
288     void setFloatValue(unsigned short unitType, double floatValue, ExceptionState&);
getFloatValue(unsigned short unitType,ExceptionState & exceptionState)289     float getFloatValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<float>(static_cast<UnitType>(unitType), exceptionState); }
getFloatValue(UnitType type)290     float getFloatValue(UnitType type) const { return getValue<float>(type); }
getFloatValue()291     float getFloatValue() const { return getValue<float>(); }
292 
getIntValue(UnitType type,ExceptionState & exceptionState)293     int getIntValue(UnitType type, ExceptionState& exceptionState) const { return getValue<int>(type, exceptionState); }
getIntValue(UnitType type)294     int getIntValue(UnitType type) const { return getValue<int>(type); }
getIntValue()295     int getIntValue() const { return getValue<int>(); }
296 
getValue(UnitType type,ExceptionState & exceptionState)297     template<typename T> inline T getValue(UnitType type, ExceptionState& exceptionState) const { return clampTo<T>(getDoubleValue(type, exceptionState)); }
getValue(UnitType type)298     template<typename T> inline T getValue(UnitType type) const { return clampTo<T>(getDoubleValue(type)); }
getValue()299     template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
300 
301     void setStringValue(unsigned short stringType, const String& stringValue, ExceptionState&);
302     String getStringValue(ExceptionState&) const;
303     String getStringValue() const;
304 
305     Counter* getCounterValue(ExceptionState&) const;
getCounterValue()306     Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
307 
308     Rect* getRectValue(ExceptionState&) const;
getRectValue()309     Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
310 
311     Quad* getQuadValue(ExceptionState&) const;
getQuadValue()312     Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
313 
314     PassRefPtrWillBeRawPtr<RGBColor> getRGBColorValue(ExceptionState&) const;
getRGBA32Value()315     RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
316 
317     Pair* getPairValue(ExceptionState&) const;
getPairValue()318     Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
319 
getShapeValue()320     CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
321 
cssCalcValue()322     CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
323 
getPropertyID()324     CSSPropertyID getPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
getValueID()325     CSSValueID getValueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
326 
327     template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
328 
329     static const char* unitTypeToString(UnitType);
330     String customCSSText(CSSTextFormattingFlags = QuoteCSSStringIfNeeded) const;
331 
isQuirkValue()332     bool isQuirkValue() { return m_isQuirkValue; }
333 
334     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> cloneForCSSOM() const;
setCSSOMSafe()335     void setCSSOMSafe() { m_isCSSOMSafe = true; }
336 
337     bool equals(const CSSPrimitiveValue&) const;
338 
339     void traceAfterDispatch(Visitor*);
340 
341     static UnitType canonicalUnitTypeForCategory(UnitCategory);
342     static double conversionToCanonicalUnitsScaleFactor(UnitType);
343 
344     // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false.
345     static bool unitTypeToLengthUnitType(UnitType, LengthUnitType&);
346     static UnitType lengthUnitTypeToUnitType(LengthUnitType);
347 
348 private:
349     CSSPrimitiveValue(CSSValueID);
350     CSSPrimitiveValue(CSSPropertyID);
351     // int vs. unsigned is too subtle to distinguish types, so require a UnitType.
352     CSSPrimitiveValue(int parserOperator, UnitType);
353     CSSPrimitiveValue(unsigned color, UnitType); // RGB value
354     CSSPrimitiveValue(const Length&, float zoom);
355     CSSPrimitiveValue(const LengthSize&, const RenderStyle&);
356     CSSPrimitiveValue(const String&, UnitType);
357     CSSPrimitiveValue(double, UnitType);
358 
359     template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
CSSPrimitiveValue(T * val)360     template<typename T> CSSPrimitiveValue(T* val)
361         : CSSValue(PrimitiveClass)
362     {
363         init(PassRefPtrWillBeRawPtr<T>(val));
364     }
365 
CSSPrimitiveValue(PassRefPtrWillBeRawPtr<T> val)366     template<typename T> CSSPrimitiveValue(PassRefPtrWillBeRawPtr<T> val)
367         : CSSValue(PrimitiveClass)
368     {
369         init(val);
370     }
371 
372     static void create(int); // compile-time guard
373     static void create(unsigned); // compile-time guard
374     template<typename T> operator T*(); // compile-time guard
375 
376     void init(const Length&);
377     void init(const LengthSize&, const RenderStyle&);
378     void init(PassRefPtrWillBeRawPtr<Counter>);
379     void init(PassRefPtrWillBeRawPtr<Rect>);
380     void init(PassRefPtrWillBeRawPtr<Pair>);
381     void init(PassRefPtrWillBeRawPtr<Quad>);
382     void init(PassRefPtrWillBeRawPtr<CSSBasicShape>);
383     void init(PassRefPtrWillBeRawPtr<CSSCalcValue>);
384     bool getDoubleValueInternal(UnitType targetUnitType, double* result) const;
385 
386     double computeLengthDouble(const CSSToLengthConversionData&);
387 
388     union {
389         CSSPropertyID propertyID;
390         CSSValueID valueID;
391         int parserOperator;
392         double num;
393         StringImpl* string;
394         unsigned rgbcolor;
395         // FIXME: oilpan: Should be members, but no support for members in unions. Just trace the raw ptr for now.
396         CSSBasicShape* shape;
397         CSSCalcValue* calc;
398         Counter* counter;
399         Pair* pair;
400         Rect* rect;
401         Quad* quad;
402     } m_value;
403 };
404 
405 typedef CSSPrimitiveValue::CSSLengthArray CSSLengthArray;
406 
407 DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue());
408 
409 } // namespace blink
410 
411 #endif // CSSPrimitiveValue_h
412