Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / LayoutUnit.h
blob95e33a16dc1cd9bab3d9f2c16663103960e89de2
1 /*
2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef LayoutUnit_h
32 #define LayoutUnit_h
34 #include "wtf/Assertions.h"
35 #include "wtf/MathExtras.h"
36 #include "wtf/SaturatedArithmetic.h"
37 #include <limits.h>
38 #include <limits>
39 #include <stdlib.h>
41 namespace blink {
43 #if !ERROR_DISABLED
45 #define REPORT_OVERFLOW(doesOverflow) ((void)0)
47 #else
49 #define REPORT_OVERFLOW(doesOverflow) do \
50 if (!(doesOverflow)) { \
51 WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, "!(%s)", #doesOverflow); \
52 } \
53 while (0)
55 #endif
57 static const int kLayoutUnitFractionalBits = 6;
58 static const int kFixedPointDenominator = 1 << kLayoutUnitFractionalBits;
60 const int intMaxForLayoutUnit = INT_MAX / kFixedPointDenominator;
61 const int intMinForLayoutUnit = INT_MIN / kFixedPointDenominator;
63 class LayoutUnit {
64 public:
65 LayoutUnit() : m_value(0) { }
66 LayoutUnit(int value) { setValue(value); }
67 LayoutUnit(unsigned short value) { setValue(value); }
68 LayoutUnit(unsigned value) { setValue(value); }
69 LayoutUnit(unsigned long value) { m_value = clampTo<int>(value * kFixedPointDenominator); }
70 LayoutUnit(unsigned long long value) { m_value = clampTo<int>(value * kFixedPointDenominator); }
71 LayoutUnit(float value) { m_value = clampTo<int>(value * kFixedPointDenominator); }
72 LayoutUnit(double value) { m_value = clampTo<int>(value * kFixedPointDenominator); }
74 static LayoutUnit fromFloatCeil(float value)
76 LayoutUnit v;
77 v.m_value = clampTo<int>(ceilf(value * kFixedPointDenominator));
78 return v;
81 static LayoutUnit fromFloatFloor(float value)
83 LayoutUnit v;
84 v.m_value = clampTo<int>(floorf(value * kFixedPointDenominator));
85 return v;
88 static LayoutUnit fromFloatRound(float value)
90 if (value >= 0)
91 return clamp(value + epsilon() / 2.0f);
92 return clamp(value - epsilon() / 2.0f);
95 int toInt() const { return m_value / kFixedPointDenominator; }
96 float toFloat() const { return static_cast<float>(m_value) / kFixedPointDenominator; }
97 double toDouble() const { return static_cast<double>(m_value) / kFixedPointDenominator; }
98 float ceilToFloat() const
100 float floatValue = toFloat();
101 if (static_cast<int>(floatValue * kFixedPointDenominator) == m_value)
102 return floatValue;
103 if (floatValue > 0)
104 return nextafterf(floatValue, std::numeric_limits<float>::max());
105 return nextafterf(floatValue, std::numeric_limits<float>::min());
107 unsigned toUnsigned() const { REPORT_OVERFLOW(m_value >= 0); return toInt(); }
109 operator int() const { return toInt(); }
110 operator unsigned() const { return toUnsigned(); }
111 operator double() const { return toDouble(); }
112 operator bool() const { return m_value; }
114 LayoutUnit operator++(int)
116 m_value += kFixedPointDenominator;
117 return *this;
120 inline int rawValue() const { return m_value; }
121 inline void setRawValue(int value) { m_value = value; }
122 void setRawValue(long long value)
124 REPORT_OVERFLOW(value > std::numeric_limits<int>::min() && value < std::numeric_limits<int>::max());
125 m_value = static_cast<int>(value);
128 LayoutUnit abs() const
130 LayoutUnit returnValue;
131 returnValue.setRawValue(::abs(m_value));
132 return returnValue;
134 int ceil() const
136 if (UNLIKELY(m_value >= INT_MAX - kFixedPointDenominator + 1))
137 return intMaxForLayoutUnit;
139 if (m_value >= 0)
140 return (m_value + kFixedPointDenominator - 1) / kFixedPointDenominator;
141 return toInt();
143 ALWAYS_INLINE int round() const
145 return saturatedAddition(rawValue(), kFixedPointDenominator / 2) >> kLayoutUnitFractionalBits;
148 int floor() const
150 if (UNLIKELY(m_value <= INT_MIN + kFixedPointDenominator - 1))
151 return intMinForLayoutUnit;
153 return m_value >> kLayoutUnitFractionalBits;
156 LayoutUnit fraction() const
158 // Add the fraction to the size (as opposed to the full location) to avoid overflows.
159 // Compute fraction using the mod operator to preserve the sign of the value as it may affect rounding.
160 LayoutUnit fraction;
161 fraction.setRawValue(rawValue() % kFixedPointDenominator);
162 return fraction;
165 bool mightBeSaturated() const
167 return rawValue() == std::numeric_limits<int>::max()
168 || rawValue() == std::numeric_limits<int>::min();
171 static float epsilon() { return 1.0f / kFixedPointDenominator; }
173 static const LayoutUnit max()
175 LayoutUnit m;
176 m.m_value = std::numeric_limits<int>::max();
177 return m;
179 static const LayoutUnit min()
181 LayoutUnit m;
182 m.m_value = std::numeric_limits<int>::min();
183 return m;
186 // Versions of max/min that are slightly smaller/larger than max/min() to allow for roinding without overflowing.
187 static const LayoutUnit nearlyMax()
189 LayoutUnit m;
190 m.m_value = std::numeric_limits<int>::max() - kFixedPointDenominator / 2;
191 return m;
193 static const LayoutUnit nearlyMin()
195 LayoutUnit m;
196 m.m_value = std::numeric_limits<int>::min() + kFixedPointDenominator / 2;
197 return m;
200 static LayoutUnit clamp(double value)
202 return clampTo<LayoutUnit>(value, LayoutUnit::min(), LayoutUnit::max());
205 private:
206 static bool isInBounds(int value)
208 return ::abs(value) <= std::numeric_limits<int>::max() / kFixedPointDenominator;
210 static bool isInBounds(unsigned value)
212 return value <= static_cast<unsigned>(std::numeric_limits<int>::max()) / kFixedPointDenominator;
214 static bool isInBounds(double value)
216 return ::fabs(value) <= std::numeric_limits<int>::max() / kFixedPointDenominator;
219 ALWAYS_INLINE void setValue(int value)
221 m_value = saturatedSet(value, kLayoutUnitFractionalBits);
224 inline void setValue(unsigned value)
226 m_value = saturatedSet(value, kLayoutUnitFractionalBits);
229 int m_value;
232 inline bool operator<=(const LayoutUnit& a, const LayoutUnit& b)
234 return a.rawValue() <= b.rawValue();
237 inline bool operator<=(const LayoutUnit& a, float b)
239 return a.toFloat() <= b;
242 inline bool operator<=(const LayoutUnit& a, int b)
244 return a <= LayoutUnit(b);
247 inline bool operator<=(const float a, const LayoutUnit& b)
249 return a <= b.toFloat();
252 inline bool operator<=(const int a, const LayoutUnit& b)
254 return LayoutUnit(a) <= b;
257 inline bool operator>=(const LayoutUnit& a, const LayoutUnit& b)
259 return a.rawValue() >= b.rawValue();
262 inline bool operator>=(const LayoutUnit& a, int b)
264 return a >= LayoutUnit(b);
267 inline bool operator>=(const float a, const LayoutUnit& b)
269 return a >= b.toFloat();
272 inline bool operator>=(const LayoutUnit& a, float b)
274 return a.toFloat() >= b;
277 inline bool operator>=(const int a, const LayoutUnit& b)
279 return LayoutUnit(a) >= b;
282 inline bool operator<(const LayoutUnit& a, const LayoutUnit& b)
284 return a.rawValue() < b.rawValue();
287 inline bool operator<(const LayoutUnit& a, int b)
289 return a < LayoutUnit(b);
292 inline bool operator<(const LayoutUnit& a, float b)
294 return a.toFloat() < b;
297 inline bool operator<(const LayoutUnit& a, double b)
299 return a.toDouble() < b;
302 inline bool operator<(const int a, const LayoutUnit& b)
304 return LayoutUnit(a) < b;
307 inline bool operator<(const float a, const LayoutUnit& b)
309 return a < b.toFloat();
312 inline bool operator>(const LayoutUnit& a, const LayoutUnit& b)
314 return a.rawValue() > b.rawValue();
317 inline bool operator>(const LayoutUnit& a, double b)
319 return a.toDouble() > b;
322 inline bool operator>(const LayoutUnit& a, float b)
324 return a.toFloat() > b;
327 inline bool operator>(const LayoutUnit& a, int b)
329 return a > LayoutUnit(b);
332 inline bool operator>(const int a, const LayoutUnit& b)
334 return LayoutUnit(a) > b;
337 inline bool operator>(const float a, const LayoutUnit& b)
339 return a > b.toFloat();
342 inline bool operator>(const double a, const LayoutUnit& b)
344 return a > b.toDouble();
347 inline bool operator!=(const LayoutUnit& a, const LayoutUnit& b)
349 return a.rawValue() != b.rawValue();
352 inline bool operator!=(const LayoutUnit& a, float b)
354 return a != LayoutUnit(b);
357 inline bool operator!=(const int a, const LayoutUnit& b)
359 return LayoutUnit(a) != b;
362 inline bool operator!=(const LayoutUnit& a, int b)
364 return a != LayoutUnit(b);
367 inline bool operator==(const LayoutUnit& a, const LayoutUnit& b)
369 return a.rawValue() == b.rawValue();
372 inline bool operator==(const LayoutUnit& a, int b)
374 return a == LayoutUnit(b);
377 inline bool operator==(const int a, const LayoutUnit& b)
379 return LayoutUnit(a) == b;
382 inline bool operator==(const LayoutUnit& a, float b)
384 return a.toFloat() == b;
387 inline bool operator==(const float a, const LayoutUnit& b)
389 return a == b.toFloat();
392 // For multiplication that's prone to overflow, this bounds it to LayoutUnit::max() and ::min()
393 inline LayoutUnit boundedMultiply(const LayoutUnit& a, const LayoutUnit& b)
395 int64_t result = static_cast<int64_t>(a.rawValue()) * static_cast<int64_t>(b.rawValue()) / kFixedPointDenominator;
396 int32_t high = static_cast<int32_t>(result >> 32);
397 int32_t low = static_cast<int32_t>(result);
398 uint32_t saturated = (static_cast<uint32_t>(a.rawValue() ^ b.rawValue()) >> 31) + std::numeric_limits<int>::max();
399 // If the higher 32 bits does not match the lower 32 with sign extension the operation overflowed.
400 if (high != low >> 31)
401 result = saturated;
403 LayoutUnit returnVal;
404 returnVal.setRawValue(static_cast<int>(result));
405 return returnVal;
408 inline LayoutUnit operator*(const LayoutUnit& a, const LayoutUnit& b)
410 return boundedMultiply(a, b);
413 inline double operator*(const LayoutUnit& a, double b)
415 return a.toDouble() * b;
418 inline float operator*(const LayoutUnit& a, float b)
420 return a.toFloat() * b;
423 inline LayoutUnit operator*(const LayoutUnit& a, int b)
425 return a * LayoutUnit(b);
428 inline LayoutUnit operator*(const LayoutUnit& a, unsigned short b)
430 return a * LayoutUnit(b);
433 inline LayoutUnit operator*(const LayoutUnit& a, unsigned b)
435 return a * LayoutUnit(b);
438 inline LayoutUnit operator*(const LayoutUnit& a, unsigned long b)
440 return a * LayoutUnit(b);
443 inline LayoutUnit operator*(const LayoutUnit& a, unsigned long long b)
445 return a * LayoutUnit(b);
448 inline LayoutUnit operator*(unsigned short a, const LayoutUnit& b)
450 return LayoutUnit(a) * b;
453 inline LayoutUnit operator*(unsigned a, const LayoutUnit& b)
455 return LayoutUnit(a) * b;
458 inline LayoutUnit operator*(unsigned long a, const LayoutUnit& b)
460 return LayoutUnit(a) * b;
463 inline LayoutUnit operator*(unsigned long long a, const LayoutUnit& b)
465 return LayoutUnit(a) * b;
468 inline LayoutUnit operator*(const int a, const LayoutUnit& b)
470 return LayoutUnit(a) * b;
473 inline float operator*(const float a, const LayoutUnit& b)
475 return a * b.toFloat();
478 inline double operator*(const double a, const LayoutUnit& b)
480 return a * b.toDouble();
483 inline LayoutUnit operator/(const LayoutUnit& a, const LayoutUnit& b)
485 LayoutUnit returnVal;
486 long long rawVal = static_cast<long long>(kFixedPointDenominator) * a.rawValue() / b.rawValue();
487 returnVal.setRawValue(clampTo<int>(rawVal));
488 return returnVal;
491 inline float operator/(const LayoutUnit& a, float b)
493 return a.toFloat() / b;
496 inline double operator/(const LayoutUnit& a, double b)
498 return a.toDouble() / b;
501 inline LayoutUnit operator/(const LayoutUnit& a, int b)
503 return a / LayoutUnit(b);
506 inline LayoutUnit operator/(const LayoutUnit& a, unsigned short b)
508 return a / LayoutUnit(b);
511 inline LayoutUnit operator/(const LayoutUnit& a, unsigned b)
513 return a / LayoutUnit(b);
516 inline LayoutUnit operator/(const LayoutUnit& a, unsigned long b)
518 return a / LayoutUnit(b);
521 inline LayoutUnit operator/(const LayoutUnit& a, unsigned long long b)
523 return a / LayoutUnit(b);
526 inline float operator/(const float a, const LayoutUnit& b)
528 return a / b.toFloat();
531 inline double operator/(const double a, const LayoutUnit& b)
533 return a / b.toDouble();
536 inline LayoutUnit operator/(const int a, const LayoutUnit& b)
538 return LayoutUnit(a) / b;
541 inline LayoutUnit operator/(unsigned short a, const LayoutUnit& b)
543 return LayoutUnit(a) / b;
546 inline LayoutUnit operator/(unsigned a, const LayoutUnit& b)
548 return LayoutUnit(a) / b;
551 inline LayoutUnit operator/(unsigned long a, const LayoutUnit& b)
553 return LayoutUnit(a) / b;
556 inline LayoutUnit operator/(unsigned long long a, const LayoutUnit& b)
558 return LayoutUnit(a) / b;
561 ALWAYS_INLINE LayoutUnit operator+(const LayoutUnit& a, const LayoutUnit& b)
563 LayoutUnit returnVal;
564 returnVal.setRawValue(saturatedAddition(a.rawValue(), b.rawValue()));
565 return returnVal;
568 inline LayoutUnit operator+(const LayoutUnit& a, int b)
570 return a + LayoutUnit(b);
573 inline float operator+(const LayoutUnit& a, float b)
575 return a.toFloat() + b;
578 inline double operator+(const LayoutUnit& a, double b)
580 return a.toDouble() + b;
583 inline LayoutUnit operator+(const int a, const LayoutUnit& b)
585 return LayoutUnit(a) + b;
588 inline float operator+(const float a, const LayoutUnit& b)
590 return a + b.toFloat();
593 inline double operator+(const double a, const LayoutUnit& b)
595 return a + b.toDouble();
598 ALWAYS_INLINE LayoutUnit operator-(const LayoutUnit& a, const LayoutUnit& b)
600 LayoutUnit returnVal;
601 returnVal.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue()));
602 return returnVal;
605 inline LayoutUnit operator-(const LayoutUnit& a, int b)
607 return a - LayoutUnit(b);
610 inline LayoutUnit operator-(const LayoutUnit& a, unsigned b)
612 return a - LayoutUnit(b);
615 inline float operator-(const LayoutUnit& a, float b)
617 return a.toFloat() - b;
620 inline double operator-(const LayoutUnit& a, double b)
622 return a.toDouble() - b;
625 inline LayoutUnit operator-(const int a, const LayoutUnit& b)
627 return LayoutUnit(a) - b;
630 inline float operator-(const float a, const LayoutUnit& b)
632 return a - b.toFloat();
635 inline LayoutUnit operator-(const LayoutUnit& a)
637 LayoutUnit returnVal;
638 returnVal.setRawValue(-a.rawValue());
639 return returnVal;
642 // For returning the remainder after a division with integer results.
643 inline LayoutUnit intMod(const LayoutUnit& a, const LayoutUnit& b)
645 // This calculates the modulo so that: a = static_cast<int>(a / b) * b + intMod(a, b).
646 LayoutUnit returnVal;
647 returnVal.setRawValue(a.rawValue() % b.rawValue());
648 return returnVal;
651 inline LayoutUnit operator%(const LayoutUnit& a, const LayoutUnit& b)
653 // This calculates the modulo so that: a = (a / b) * b + a % b.
654 LayoutUnit returnVal;
655 long long rawVal = (static_cast<long long>(kFixedPointDenominator) * a.rawValue()) % b.rawValue();
656 returnVal.setRawValue(rawVal / kFixedPointDenominator);
657 return returnVal;
660 inline LayoutUnit operator%(const LayoutUnit& a, int b)
662 return a % LayoutUnit(b);
665 inline LayoutUnit operator%(int a, const LayoutUnit& b)
667 return LayoutUnit(a) % b;
670 inline LayoutUnit& operator+=(LayoutUnit& a, const LayoutUnit& b)
672 a.setRawValue(saturatedAddition(a.rawValue(), b.rawValue()));
673 return a;
676 inline LayoutUnit& operator+=(LayoutUnit& a, int b)
678 a = a + b;
679 return a;
682 inline LayoutUnit& operator+=(LayoutUnit& a, float b)
684 a = a + b;
685 return a;
688 inline float& operator+=(float& a, const LayoutUnit& b)
690 a = a + b;
691 return a;
694 inline LayoutUnit& operator-=(LayoutUnit& a, int b)
696 a = a - b;
697 return a;
700 inline LayoutUnit& operator-=(LayoutUnit& a, const LayoutUnit& b)
702 a.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue()));
703 return a;
706 inline LayoutUnit& operator-=(LayoutUnit& a, float b)
708 a = a - b;
709 return a;
712 inline float& operator-=(float& a, const LayoutUnit& b)
714 a = a - b;
715 return a;
718 inline LayoutUnit& operator*=(LayoutUnit& a, const LayoutUnit& b)
720 a = a * b;
721 return a;
723 // operator*=(LayoutUnit& a, int b) is supported by the operator above plus LayoutUnit(int).
725 inline LayoutUnit& operator*=(LayoutUnit& a, float b)
727 a = a * b;
728 return a;
731 inline float& operator*=(float& a, const LayoutUnit& b)
733 a = a * b;
734 return a;
737 inline LayoutUnit& operator/=(LayoutUnit& a, const LayoutUnit& b)
739 a = a / b;
740 return a;
742 // operator/=(LayoutUnit& a, int b) is supported by the operator above plus LayoutUnit(int).
744 inline LayoutUnit& operator/=(LayoutUnit& a, float b)
746 a = a / b;
747 return a;
750 inline float& operator/=(float& a, const LayoutUnit& b)
752 a = a / b;
753 return a;
756 inline int snapSizeToPixel(LayoutUnit size, LayoutUnit location)
758 LayoutUnit fraction = location.fraction();
759 return (fraction + size).round() - fraction.round();
762 inline int roundToInt(LayoutUnit value)
764 return value.round();
767 inline int floorToInt(LayoutUnit value)
769 return value.floor();
772 inline LayoutUnit absoluteValue(const LayoutUnit& value)
774 return value.abs();
777 inline LayoutUnit layoutMod(const LayoutUnit& numerator, const LayoutUnit& denominator)
779 return numerator % denominator;
782 inline bool isIntegerValue(const LayoutUnit value)
784 return value.toInt() == value;
787 inline LayoutUnit clampToLayoutUnit(LayoutUnit value, LayoutUnit min, LayoutUnit max)
789 if (value >= max)
790 return max;
791 if (value <= min)
792 return min;
793 return value;
796 } // namespace blink
798 #endif // LayoutUnit_h