Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 5d92fed | 2014-06-20 14:52:37 +0100 | [diff] [blame] | 25 | #include "core/CSSPropertyNames.h" |
| 26 | #include "core/CSSValueKeywords.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 27 | #include "core/css/CSSValue.h" |
Torne (Richard Coles) | f79f16f | 2013-10-31 11:16:44 +0000 | [diff] [blame] | 28 | #include "platform/graphics/Color.h" |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 29 | #include "wtf/Forward.h" |
| 30 | #include "wtf/MathExtras.h" |
| 31 | #include "wtf/PassRefPtr.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 32 | |
| 33 | namespace WebCore { |
| 34 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 35 | class CSSBasicShape; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | class CSSCalcValue; |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 37 | class CSSToLengthConversionData; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 38 | class Counter; |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 39 | class ExceptionState; |
Torne (Richard Coles) | bfe3590 | 2013-10-22 16:41:51 +0100 | [diff] [blame] | 40 | class Length; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 41 | class LengthSize; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 42 | class Pair; |
| 43 | class Quad; |
| 44 | class RGBColor; |
| 45 | class Rect; |
| 46 | class RenderStyle; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 47 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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. |
| 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 | |
| 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 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 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. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 72 | class CSSPrimitiveValue : public CSSValue { |
| 73 | public: |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 74 | enum UnitType { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 | // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues. |
| 114 | CSS_PARSER_OPERATOR = 103, |
| 115 | CSS_PARSER_INTEGER = 104, |
| 116 | CSS_PARSER_HEXCOLOR = 105, |
| 117 | |
| 118 | // This is used internally for unknown identifiers |
| 119 | CSS_PARSER_IDENTIFIER = 106, |
| 120 | |
| 121 | // These are from CSS3 Values and Units, but that isn't a finished standard yet |
| 122 | CSS_TURN = 107, |
| 123 | CSS_REMS = 108, |
| 124 | CSS_CHS = 109, |
| 125 | |
| 126 | // This is used internally for counter names (as opposed to counter values) |
| 127 | CSS_COUNTER_NAME = 110, |
| 128 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 129 | // This is used by the CSS Shapes draft |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 130 | CSS_SHAPE = 111, |
| 131 | |
| 132 | // Used by border images. |
| 133 | CSS_QUAD = 112, |
| 134 | |
| 135 | CSS_CALC = 113, |
| 136 | CSS_CALC_PERCENTAGE_WITH_NUMBER = 114, |
| 137 | CSS_CALC_PERCENTAGE_WITH_LENGTH = 115, |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 138 | |
| 139 | CSS_PROPERTY_ID = 117, |
| 140 | CSS_VALUE_ID = 118 |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 143 | enum LengthUnitType { |
| 144 | UnitTypePixels = 0, |
| 145 | UnitTypePercentage, |
| 146 | UnitTypeFontSize, |
| 147 | UnitTypeFontXSize, |
| 148 | UnitTypeRootFontSize, |
| 149 | UnitTypeZeroCharacterWidth, |
| 150 | UnitTypeViewportWidth, |
| 151 | UnitTypeViewportHeight, |
| 152 | UnitTypeViewportMin, |
| 153 | UnitTypeViewportMax, |
| 154 | |
| 155 | // This value must come after the last length unit type to enable iteration over the length unit types. |
| 156 | LengthUnitTypeCount, |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | typedef Vector<double, CSSPrimitiveValue::LengthUnitTypeCount> CSSLengthArray; |
| 160 | void accumulateLengthArray(CSSLengthArray&, double multiplier = 1) const; |
| 161 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 162 | // This enum follows the BisonCSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 163 | enum UnitCategory { |
| 164 | UNumber, |
| 165 | UPercent, |
| 166 | ULength, |
| 167 | UAngle, |
| 168 | UTime, |
| 169 | UFrequency, |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 170 | UResolution, |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 171 | UOther |
| 172 | }; |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 173 | static UnitCategory unitCategory(UnitType); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 174 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 175 | static UnitType fromName(const String& unit); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 176 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 177 | bool isAngle() const |
| 178 | { |
| 179 | return m_primitiveUnitType == CSS_DEG |
| 180 | || m_primitiveUnitType == CSS_RAD |
| 181 | || m_primitiveUnitType == CSS_GRAD |
| 182 | || m_primitiveUnitType == CSS_TURN; |
| 183 | } |
| 184 | bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; } |
| 185 | bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; } |
| 186 | bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; } |
| 187 | bool isFontRelativeLength() const |
| 188 | { |
| 189 | return m_primitiveUnitType == CSS_EMS |
| 190 | || m_primitiveUnitType == CSS_EXS |
| 191 | || m_primitiveUnitType == CSS_REMS |
| 192 | || m_primitiveUnitType == CSS_CHS; |
| 193 | } |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 194 | bool isViewportPercentageLength() const { return isViewportPercentageLength(static_cast<UnitType>(m_primitiveUnitType)); } |
| 195 | static bool isViewportPercentageLength(UnitType type) { return type >= CSS_VW && type <= CSS_VMAX; } |
| 196 | static bool isLength(UnitType type) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 197 | { |
Ben Murdoch | 10f88d5 | 2014-04-24 10:50:33 +0100 | [diff] [blame] | 198 | return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS || isViewportPercentageLength(type); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 199 | } |
Ben Murdoch | 10f88d5 | 2014-04-24 10:50:33 +0100 | [diff] [blame] | 200 | bool isLength() const { return isLength(primitiveType()); } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 201 | bool isNumber() const { return primitiveType() == CSS_NUMBER; } |
| 202 | bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; } |
| 203 | bool isPx() const { return primitiveType() == CSS_PX; } |
| 204 | bool isRect() const { return m_primitiveUnitType == CSS_RECT; } |
| 205 | bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; } |
| 206 | bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; } |
| 207 | bool isString() const { return m_primitiveUnitType == CSS_STRING; } |
| 208 | bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; } |
| 209 | bool isURI() const { return m_primitiveUnitType == CSS_URI; } |
| 210 | bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; } |
| 211 | bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; } |
| 212 | bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; } |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 213 | static bool isDotsPerInch(UnitType type) { return type == CSS_DPI; } |
| 214 | static bool isDotsPerPixel(UnitType type) { return type == CSS_DPPX; } |
| 215 | static bool isDotsPerCentimeter(UnitType type) { return type == CSS_DPCM; } |
| 216 | static bool isResolution(UnitType type) { return type >= CSS_DPPX && type <= CSS_DPCM; } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 217 | bool isFlex() const { return primitiveType() == CSS_FR; } |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 218 | bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 219 | bool colorIsDerivedFromElement() const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 220 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 221 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSValueID valueID) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 222 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 223 | return adoptRefWillBeNoop(new CSSPrimitiveValue(valueID)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 224 | } |
| 225 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID) |
| 226 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 227 | return adoptRefWillBeNoop(new CSSPrimitiveValue(propertyID)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 228 | } |
| 229 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createParserOperator(int parserOperator) |
| 230 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 231 | return adoptRefWillBeNoop(new CSSPrimitiveValue(parserOperator, CSS_PARSER_OPERATOR)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 232 | } |
| 233 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) |
| 234 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 235 | return adoptRefWillBeNoop(new CSSPrimitiveValue(rgbValue, CSS_RGBCOLOR)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 236 | } |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 237 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(double value, UnitType type) |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 238 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 239 | return adoptRefWillBeNoop(new CSSPrimitiveValue(value, type)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 240 | } |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 241 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const String& value, UnitType type) |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 242 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 243 | return adoptRefWillBeNoop(new CSSPrimitiveValue(value, type)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 244 | } |
| 245 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const Length& value, float zoom) |
| 246 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 247 | return adoptRefWillBeNoop(new CSSPrimitiveValue(value, zoom)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 248 | } |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 249 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const LengthSize& value, const RenderStyle& style) |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 250 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 251 | return adoptRefWillBeNoop(new CSSPrimitiveValue(value, style)); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 252 | } |
| 253 | template<typename T> static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(T value) |
| 254 | { |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 255 | return adoptRefWillBeNoop(new CSSPrimitiveValue(value)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE. |
| 259 | // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em. |
| 260 | // When the quirky value is used, if you're in quirks mode, the margin will collapse away |
| 261 | // inside a table cell. |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 262 | static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitType type) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 263 | { |
| 264 | CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type); |
| 265 | quirkValue->m_isQuirkValue = true; |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 266 | return adoptRefWillBeNoop(quirkValue); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | ~CSSPrimitiveValue(); |
| 270 | |
| 271 | void cleanup(); |
| 272 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 273 | UnitType primitiveType() const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 274 | |
| 275 | double computeDegrees(); |
| 276 | |
| 277 | enum TimeUnit { Seconds, Milliseconds }; |
| 278 | template <typename T, TimeUnit timeUnit> T computeTime() |
| 279 | { |
| 280 | if (timeUnit == Seconds && m_primitiveUnitType == CSS_S) |
| 281 | return getValue<T>(); |
| 282 | if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS) |
| 283 | return getValue<T>() / 1000; |
| 284 | if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS) |
| 285 | return getValue<T>(); |
| 286 | if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S) |
| 287 | return getValue<T>() * 1000; |
| 288 | ASSERT_NOT_REACHED(); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 293 | * Computes a length in pixels out of the given CSSValue |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 294 | * |
| 295 | * The metrics have to be a bit different for screen and printer output. |
| 296 | * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi |
| 297 | * |
| 298 | * this is screen/printer dependent, so we probably need a config option for this, |
| 299 | * and some tool to calibrate. |
| 300 | */ |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 301 | template<typename T> T computeLength(const CSSToLengthConversionData&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 302 | |
| 303 | // Converts to a Length, mapping various unit types appropriately. |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 304 | template<int> Length convertToLength(const CSSToLengthConversionData&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 305 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 306 | double getDoubleValue(UnitType, ExceptionState&) const; |
| 307 | double getDoubleValue(UnitType) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 308 | double getDoubleValue() const; |
| 309 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 310 | // setFloatValue(..., ExceptionState&) and setStringValue() must use unsigned short instead of UnitType to match IDL bindings. |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 311 | void setFloatValue(unsigned short unitType, double floatValue, ExceptionState&); |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 312 | float getFloatValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<float>(static_cast<UnitType>(unitType), exceptionState); } |
| 313 | float getFloatValue(UnitType type) const { return getValue<float>(type); } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 314 | float getFloatValue() const { return getValue<float>(); } |
| 315 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 316 | int getIntValue(UnitType type, ExceptionState& exceptionState) const { return getValue<int>(type, exceptionState); } |
| 317 | int getIntValue(UnitType type) const { return getValue<int>(type); } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 318 | int getIntValue() const { return getValue<int>(); } |
| 319 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 320 | template<typename T> inline T getValue(UnitType type, ExceptionState& exceptionState) const { return clampTo<T>(getDoubleValue(type, exceptionState)); } |
| 321 | template<typename T> inline T getValue(UnitType type) const { return clampTo<T>(getDoubleValue(type)); } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 322 | template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); } |
| 323 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 324 | void setStringValue(unsigned short stringType, const String& stringValue, ExceptionState&); |
| 325 | String getStringValue(ExceptionState&) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 326 | String getStringValue() const; |
| 327 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 328 | Counter* getCounterValue(ExceptionState&) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 329 | Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; } |
| 330 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 331 | Rect* getRectValue(ExceptionState&) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 332 | Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; } |
| 333 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 334 | Quad* getQuadValue(ExceptionState&) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 335 | Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; } |
| 336 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 337 | PassRefPtrWillBeRawPtr<RGBColor> getRGBColorValue(ExceptionState&) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 338 | RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; } |
| 339 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 340 | Pair* getPairValue(ExceptionState&) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 341 | Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; } |
| 342 | |
| 343 | CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; } |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 344 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 345 | CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; } |
| 346 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 347 | CSSPropertyID getPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; } |
| 348 | CSSValueID getValueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 349 | |
| 350 | template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h |
| 351 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 352 | static const char* unitTypeToString(UnitType); |
Torne (Richard Coles) | bfe3590 | 2013-10-22 16:41:51 +0100 | [diff] [blame] | 353 | String customCSSText(CSSTextFormattingFlags = QuoteCSSStringIfNeeded) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 354 | |
| 355 | bool isQuirkValue() { return m_isQuirkValue; } |
| 356 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 357 | PassRefPtrWillBeRawPtr<CSSPrimitiveValue> cloneForCSSOM() const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 358 | void setCSSOMSafe() { m_isCSSOMSafe = true; } |
| 359 | |
| 360 | bool equals(const CSSPrimitiveValue&) const; |
| 361 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 362 | void traceAfterDispatch(Visitor*); |
| 363 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 364 | static UnitType canonicalUnitTypeForCategory(UnitCategory); |
| 365 | static double conversionToCanonicalUnitsScaleFactor(UnitType); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 366 | |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 367 | // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false. |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 368 | static bool unitTypeToLengthUnitType(UnitType, LengthUnitType&); |
| 369 | static UnitType lengthUnitTypeToUnitType(LengthUnitType); |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 370 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 371 | private: |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 372 | CSSPrimitiveValue(CSSValueID); |
| 373 | CSSPrimitiveValue(CSSPropertyID); |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 374 | // int vs. unsigned is too subtle to distinguish types, so require a UnitType. |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 375 | CSSPrimitiveValue(int parserOperator, UnitType); |
| 376 | CSSPrimitiveValue(unsigned color, UnitType); // RGB value |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 377 | CSSPrimitiveValue(const Length&, float zoom); |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 378 | CSSPrimitiveValue(const LengthSize&, const RenderStyle&); |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 379 | CSSPrimitiveValue(const String&, UnitType); |
| 380 | CSSPrimitiveValue(double, UnitType); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 381 | |
| 382 | template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h |
| 383 | template<typename T> CSSPrimitiveValue(T* val) |
| 384 | : CSSValue(PrimitiveClass) |
| 385 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 386 | init(PassRefPtrWillBeRawPtr<T>(val)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 389 | template<typename T> CSSPrimitiveValue(PassRefPtrWillBeRawPtr<T> val) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 390 | : CSSValue(PrimitiveClass) |
| 391 | { |
| 392 | init(val); |
| 393 | } |
| 394 | |
| 395 | static void create(int); // compile-time guard |
| 396 | static void create(unsigned); // compile-time guard |
| 397 | template<typename T> operator T*(); // compile-time guard |
| 398 | |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 399 | void init(const Length&); |
Bo Liu | f91f5fa | 2014-05-01 10:37:55 -0700 | [diff] [blame] | 400 | void init(const LengthSize&, const RenderStyle&); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 401 | void init(PassRefPtrWillBeRawPtr<Counter>); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 402 | void init(PassRefPtrWillBeRawPtr<Rect>); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 403 | void init(PassRefPtrWillBeRawPtr<Pair>); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 404 | void init(PassRefPtrWillBeRawPtr<Quad>); |
| 405 | void init(PassRefPtrWillBeRawPtr<CSSBasicShape>); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 406 | void init(PassRefPtrWillBeRawPtr<CSSCalcValue>); |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 407 | bool getDoubleValueInternal(UnitType targetUnitType, double* result) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 408 | |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 409 | double computeLengthDouble(const CSSToLengthConversionData&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 410 | |
| 411 | union { |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 412 | CSSPropertyID propertyID; |
| 413 | CSSValueID valueID; |
| 414 | int parserOperator; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 415 | double num; |
| 416 | StringImpl* string; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 417 | unsigned rgbcolor; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 418 | // FIXME: oilpan: Should be members, but no support for members in unions. Just trace the raw ptr for now. |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 419 | CSSBasicShape* shape; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 420 | CSSCalcValue* calc; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 421 | Counter* counter; |
| 422 | Pair* pair; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 423 | Rect* rect; |
| 424 | Quad* quad; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 425 | } m_value; |
| 426 | }; |
| 427 | |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 428 | typedef CSSPrimitiveValue::CSSLengthArray CSSLengthArray; |
| 429 | |
Torne (Richard Coles) | bfe3590 | 2013-10-22 16:41:51 +0100 | [diff] [blame] | 430 | DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue()); |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 431 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 432 | } // namespace WebCore |
| 433 | |
| 434 | #endif // CSSPrimitiveValue_h |