| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012, Google Inc. All rights reserved. | 2 * Copyright (c) 2012, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef LayoutUnit_h | 31 #ifndef LayoutUnit_h |
| 32 #define LayoutUnit_h | 32 #define LayoutUnit_h |
| 33 | 33 |
| 34 #include "wtf/Allocator.h" | 34 #include "wtf/Allocator.h" |
| 35 #include "wtf/Assertions.h" | 35 #include "wtf/Assertions.h" |
| 36 #include "wtf/MathExtras.h" | 36 #include "wtf/MathExtras.h" |
| 37 #include "wtf/SaturatedArithmetic.h" | 37 #include "wtf/SaturatedArithmetic.h" |
| 38 #include <algorithm> |
| 38 #include <limits.h> | 39 #include <limits.h> |
| 39 #include <limits> | 40 #include <limits> |
| 40 #include <stdlib.h> | 41 #include <stdlib.h> |
| 41 | 42 |
| 42 namespace blink { | 43 namespace blink { |
| 43 | 44 |
| 44 #if !ERROR_DISABLED | 45 #if !ERROR_DISABLED |
| 45 | 46 |
| 46 #define REPORT_OVERFLOW(doesOverflow) ((void)0) | 47 #define REPORT_OVERFLOW(doesOverflow) ((void)0) |
| 47 | 48 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 58 static const int kLayoutUnitFractionalBits = 6; | 59 static const int kLayoutUnitFractionalBits = 6; |
| 59 static const int kFixedPointDenominator = 1 << kLayoutUnitFractionalBits; | 60 static const int kFixedPointDenominator = 1 << kLayoutUnitFractionalBits; |
| 60 | 61 |
| 61 const int intMaxForLayoutUnit = INT_MAX / kFixedPointDenominator; | 62 const int intMaxForLayoutUnit = INT_MAX / kFixedPointDenominator; |
| 62 const int intMinForLayoutUnit = INT_MIN / kFixedPointDenominator; | 63 const int intMinForLayoutUnit = INT_MIN / kFixedPointDenominator; |
| 63 | 64 |
| 64 class LayoutUnit { | 65 class LayoutUnit { |
| 65 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | 66 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
| 66 public: | 67 public: |
| 67 LayoutUnit() : m_value(0) { } | 68 LayoutUnit() : m_value(0) { } |
| 69 // TODO(leviw): All of the below constructors should be explicit. crbug.com/
581254 |
| 68 LayoutUnit(int value) { setValue(value); } | 70 LayoutUnit(int value) { setValue(value); } |
| 69 LayoutUnit(unsigned short value) { setValue(value); } | 71 LayoutUnit(unsigned short value) { setValue(value); } |
| 70 LayoutUnit(unsigned value) { setValue(value); } | 72 LayoutUnit(unsigned value) { setValue(value); } |
| 71 LayoutUnit(unsigned long value) { m_value = clampTo<int>(value * kFixedPoint
Denominator); } | 73 LayoutUnit(unsigned long value) { m_value = clampTo<int>(value * kFixedPoint
Denominator); } |
| 72 LayoutUnit(unsigned long long value) { m_value = clampTo<int>(value * kFixed
PointDenominator); } | 74 LayoutUnit(unsigned long long value) { m_value = clampTo<int>(value * kFixed
PointDenominator); } |
| 73 LayoutUnit(float value) { m_value = clampTo<int>(value * kFixedPointDenomina
tor); } | 75 LayoutUnit(float value) { m_value = clampTo<int>(value * kFixedPointDenomina
tor); } |
| 74 LayoutUnit(double value) { m_value = clampTo<int>(value * kFixedPointDenomin
ator); } | 76 LayoutUnit(double value) { m_value = clampTo<int>(value * kFixedPointDenomin
ator); } |
| 75 | 77 |
| 76 static LayoutUnit fromFloatCeil(float value) | 78 static LayoutUnit fromFloatCeil(float value) |
| 77 { | 79 { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 151 } |
| 150 | 152 |
| 151 int floor() const | 153 int floor() const |
| 152 { | 154 { |
| 153 if (UNLIKELY(m_value <= INT_MIN + kFixedPointDenominator - 1)) | 155 if (UNLIKELY(m_value <= INT_MIN + kFixedPointDenominator - 1)) |
| 154 return intMinForLayoutUnit; | 156 return intMinForLayoutUnit; |
| 155 | 157 |
| 156 return m_value >> kLayoutUnitFractionalBits; | 158 return m_value >> kLayoutUnitFractionalBits; |
| 157 } | 159 } |
| 158 | 160 |
| 161 LayoutUnit clampToZero() const |
| 162 { |
| 163 return std::max(*this, LayoutUnit()); |
| 164 } |
| 165 |
| 159 LayoutUnit fraction() const | 166 LayoutUnit fraction() const |
| 160 { | 167 { |
| 161 // Add the fraction to the size (as opposed to the full location) to avo
id overflows. | 168 // Add the fraction to the size (as opposed to the full location) to avo
id overflows. |
| 162 // Compute fraction using the mod operator to preserve the sign of the v
alue as it may affect rounding. | 169 // Compute fraction using the mod operator to preserve the sign of the v
alue as it may affect rounding. |
| 163 LayoutUnit fraction; | 170 LayoutUnit fraction; |
| 164 fraction.setRawValue(rawValue() % kFixedPointDenominator); | 171 fraction.setRawValue(rawValue() % kFixedPointDenominator); |
| 165 return fraction; | 172 return fraction; |
| 166 } | 173 } |
| 167 | 174 |
| 168 bool mightBeSaturated() const | 175 bool mightBeSaturated() const |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 } | 678 } |
| 672 | 679 |
| 673 inline LayoutUnit& operator+=(LayoutUnit& a, const LayoutUnit& b) | 680 inline LayoutUnit& operator+=(LayoutUnit& a, const LayoutUnit& b) |
| 674 { | 681 { |
| 675 a.setRawValue(saturatedAddition(a.rawValue(), b.rawValue())); | 682 a.setRawValue(saturatedAddition(a.rawValue(), b.rawValue())); |
| 676 return a; | 683 return a; |
| 677 } | 684 } |
| 678 | 685 |
| 679 inline LayoutUnit& operator+=(LayoutUnit& a, int b) | 686 inline LayoutUnit& operator+=(LayoutUnit& a, int b) |
| 680 { | 687 { |
| 681 a = a + b; | 688 a = a + LayoutUnit(b); |
| 682 return a; | 689 return a; |
| 683 } | 690 } |
| 684 | 691 |
| 685 inline LayoutUnit& operator+=(LayoutUnit& a, float b) | 692 inline LayoutUnit& operator+=(LayoutUnit& a, float b) |
| 686 { | 693 { |
| 687 a = a + b; | 694 a = LayoutUnit(a + b); |
| 688 return a; | 695 return a; |
| 689 } | 696 } |
| 690 | 697 |
| 691 inline float& operator+=(float& a, const LayoutUnit& b) | 698 inline float& operator+=(float& a, const LayoutUnit& b) |
| 692 { | 699 { |
| 693 a = a + b; | 700 a = a + b; |
| 694 return a; | 701 return a; |
| 695 } | 702 } |
| 696 | 703 |
| 697 inline LayoutUnit& operator-=(LayoutUnit& a, int b) | 704 inline LayoutUnit& operator-=(LayoutUnit& a, int b) |
| 698 { | 705 { |
| 699 a = a - b; | 706 a = a - LayoutUnit(b); |
| 700 return a; | 707 return a; |
| 701 } | 708 } |
| 702 | 709 |
| 703 inline LayoutUnit& operator-=(LayoutUnit& a, const LayoutUnit& b) | 710 inline LayoutUnit& operator-=(LayoutUnit& a, const LayoutUnit& b) |
| 704 { | 711 { |
| 705 a.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue())); | 712 a.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue())); |
| 706 return a; | 713 return a; |
| 707 } | 714 } |
| 708 | 715 |
| 709 inline LayoutUnit& operator-=(LayoutUnit& a, float b) | 716 inline LayoutUnit& operator-=(LayoutUnit& a, float b) |
| 710 { | 717 { |
| 711 a = a - b; | 718 a = LayoutUnit(a - b); |
| 712 return a; | 719 return a; |
| 713 } | 720 } |
| 714 | 721 |
| 715 inline float& operator-=(float& a, const LayoutUnit& b) | 722 inline float& operator-=(float& a, const LayoutUnit& b) |
| 716 { | 723 { |
| 717 a = a - b; | 724 a = a - b; |
| 718 return a; | 725 return a; |
| 719 } | 726 } |
| 720 | 727 |
| 721 inline LayoutUnit& operator*=(LayoutUnit& a, const LayoutUnit& b) | 728 inline LayoutUnit& operator*=(LayoutUnit& a, const LayoutUnit& b) |
| 722 { | 729 { |
| 723 a = a * b; | 730 a = a * b; |
| 724 return a; | 731 return a; |
| 725 } | 732 } |
| 726 // operator*=(LayoutUnit& a, int b) is supported by the operator above plus Layo
utUnit(int). | |
| 727 | |
| 728 inline LayoutUnit& operator*=(LayoutUnit& a, float b) | |
| 729 { | |
| 730 a = a * b; | |
| 731 return a; | |
| 732 } | |
| 733 | 733 |
| 734 inline LayoutUnit& operator*=(LayoutUnit& a, float b) |
| 735 { |
| 736 a = LayoutUnit(a * b); |
| 737 return a; |
| 738 } |
| 739 |
| 734 inline float& operator*=(float& a, const LayoutUnit& b) | 740 inline float& operator*=(float& a, const LayoutUnit& b) |
| 735 { | 741 { |
| 736 a = a * b; | 742 a = a * b; |
| 737 return a; | 743 return a; |
| 738 } | 744 } |
| 739 | 745 |
| 740 inline LayoutUnit& operator/=(LayoutUnit& a, const LayoutUnit& b) | 746 inline LayoutUnit& operator/=(LayoutUnit& a, const LayoutUnit& b) |
| 741 { | 747 { |
| 742 a = a / b; | 748 a = a / b; |
| 743 return a; | 749 return a; |
| 744 } | 750 } |
| 745 // operator/=(LayoutUnit& a, int b) is supported by the operator above plus Layo
utUnit(int). | |
| 746 | |
| 747 inline LayoutUnit& operator/=(LayoutUnit& a, float b) | |
| 748 { | |
| 749 a = a / b; | |
| 750 return a; | |
| 751 } | |
| 752 | 751 |
| 752 inline LayoutUnit& operator/=(LayoutUnit& a, float b) |
| 753 { |
| 754 a = LayoutUnit(a / b); |
| 755 return a; |
| 756 } |
| 757 |
| 753 inline float& operator/=(float& a, const LayoutUnit& b) | 758 inline float& operator/=(float& a, const LayoutUnit& b) |
| 754 { | 759 { |
| 755 a = a / b; | 760 a = a / b; |
| 756 return a; | 761 return a; |
| 757 } | 762 } |
| 758 | 763 |
| 759 inline int snapSizeToPixel(LayoutUnit size, LayoutUnit location) | 764 inline int snapSizeToPixel(LayoutUnit size, LayoutUnit location) |
| 760 { | 765 { |
| 761 LayoutUnit fraction = location.fraction(); | 766 LayoutUnit fraction = location.fraction(); |
| 762 return (fraction + size).round() - fraction.round(); | 767 return (fraction + size).round() - fraction.round(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 792 if (value >= max) | 797 if (value >= max) |
| 793 return max; | 798 return max; |
| 794 if (value <= min) | 799 if (value <= min) |
| 795 return min; | 800 return min; |
| 796 return value; | 801 return value; |
| 797 } | 802 } |
| 798 | 803 |
| 799 } // namespace blink | 804 } // namespace blink |
| 800 | 805 |
| 801 #endif // LayoutUnit_h | 806 #endif // LayoutUnit_h |
| OLD | NEW |