Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: third_party/WebKit/Source/core/css/cssom/CSSUnitValue.cpp

Issue 2958233002: [CSS Typed OM] Implement CSSUnit::to for fixed length types (Closed)
Patch Set: Use new constants Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/css/cssom/CSSUnitValue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/css/cssom/CSSUnitValue.h" 5 #include "core/css/cssom/CSSUnitValue.h"
6 6
7 #include "bindings/core/v8/ExceptionState.h" 7 #include "bindings/core/v8/ExceptionState.h"
8 #include "core/css/CSSHelper.h"
8 #include "platform/wtf/MathExtras.h" 9 #include "platform/wtf/MathExtras.h"
9 10
10 namespace blink { 11 namespace blink {
11 12
12 CSSUnitValue* CSSUnitValue::Create(double value, 13 CSSUnitValue* CSSUnitValue::Create(double value,
13 const String& unit_name, 14 const String& unit_name,
14 ExceptionState& exception_state) { 15 ExceptionState& exception_state) {
15 CSSPrimitiveValue::UnitType unit = UnitFromName(unit_name); 16 CSSPrimitiveValue::UnitType unit = UnitFromName(unit_name);
16 if (!IsValidUnit(unit)) { 17 if (!IsValidUnit(unit)) {
17 exception_state.ThrowTypeError("Invalid unit: " + unit_name); 18 exception_state.ThrowTypeError("Invalid unit: " + unit_name);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 78 }
78 79
79 const CSSValue* CSSUnitValue::ToCSSValue() const { 80 const CSSValue* CSSUnitValue::ToCSSValue() const {
80 return CSSPrimitiveValue::Create(value_, unit_); 81 return CSSPrimitiveValue::Create(value_, unit_);
81 } 82 }
82 83
83 CSSUnitValue* CSSUnitValue::to(CSSPrimitiveValue::UnitType unit) const { 84 CSSUnitValue* CSSUnitValue::to(CSSPrimitiveValue::UnitType unit) const {
84 if (unit_ == unit) 85 if (unit_ == unit)
85 return Create(value_, unit_); 86 return Create(value_, unit_);
86 87
87 // TODO(meade): Implement other types. 88 // TODO(meade): Implement other types: time, frequency and resolution.
89 if (CSSPrimitiveValue::IsLength(unit_) && CSSPrimitiveValue::IsLength(unit)) {
90 // Only fixed lengths can be converted.
91 if (CSSPrimitiveValue::IsRelativeUnit(unit_) ||
92 CSSPrimitiveValue::IsRelativeUnit(unit))
93 return nullptr;
94 return Create(ConvertFixedLength(unit), unit);
95 }
88 if (CSSPrimitiveValue::IsAngle(unit_) && CSSPrimitiveValue::IsAngle(unit)) 96 if (CSSPrimitiveValue::IsAngle(unit_) && CSSPrimitiveValue::IsAngle(unit))
89 return Create(ConvertAngle(unit), unit); 97 return Create(ConvertAngle(unit), unit);
90 98
91 return nullptr; 99 return nullptr;
92 } 100 }
93 101
102 double CSSUnitValue::ConvertFixedLength(
103 CSSPrimitiveValue::UnitType unit) const {
104 switch (unit_) {
105 case CSSPrimitiveValue::UnitType::kPixels:
106 switch (unit) {
107 case CSSPrimitiveValue::UnitType::kCentimeters:
108 return value_ / kCssPixelsPerCentimeter;
109 case CSSPrimitiveValue::UnitType::kMillimeters:
110 return value_ / kCssPixelsPerMillimeter;
111 case CSSPrimitiveValue::UnitType::kInches:
112 return value_ / kCssPixelsPerInch;
113 case CSSPrimitiveValue::UnitType::kPoints:
114 return value_ / kCssPixelsPerPoint;
115 case CSSPrimitiveValue::UnitType::kPicas:
116 return value_ / kCssPixelsPerPica;
117 default:
118 NOTREACHED();
119 return 0;
120 }
121 case CSSPrimitiveValue::UnitType::kCentimeters:
122 switch (unit) {
123 case CSSPrimitiveValue::UnitType::kPixels:
124 return value_ * kCssPixelsPerCentimeter;
125 case CSSPrimitiveValue::UnitType::kMillimeters:
126 return value_ * kMillimetersPerCentimeter;
127 case CSSPrimitiveValue::UnitType::kInches:
128 return value_ / kCentimetersPerInch;
129 case CSSPrimitiveValue::UnitType::kPoints:
130 return value_ * (kPointsPerInch / kCentimetersPerInch);
131 case CSSPrimitiveValue::UnitType::kPicas:
132 return value_ * (kPicasPerInch / kCentimetersPerInch);
133 default:
134 NOTREACHED();
135 return 0;
136 }
137 case CSSPrimitiveValue::UnitType::kMillimeters:
138 switch (unit) {
139 case CSSPrimitiveValue::UnitType::kPixels:
140 return value_ * kCssPixelsPerMillimeter;
141 case CSSPrimitiveValue::UnitType::kCentimeters:
142 return value_ / kMillimetersPerCentimeter;
143 case CSSPrimitiveValue::UnitType::kInches:
144 return value_ / (kMillimetersPerCentimeter * kCentimetersPerInch);
145 case CSSPrimitiveValue::UnitType::kPoints:
146 return value_ * (kPointsPerInch / kMillimetersPerInch);
147 case CSSPrimitiveValue::UnitType::kPicas:
148 return value_ * (kPicasPerInch / kMillimetersPerInch);
149 default:
150 NOTREACHED();
151 return 0;
152 }
153 case CSSPrimitiveValue::UnitType::kInches:
154 switch (unit) {
155 case CSSPrimitiveValue::UnitType::kPixels:
156 return value_ * kCssPixelsPerInch;
157 case CSSPrimitiveValue::UnitType::kMillimeters:
158 return value_ * kCentimetersPerInch * kMillimetersPerCentimeter;
159 case CSSPrimitiveValue::UnitType::kCentimeters:
160 return value_ * kCentimetersPerInch;
161 case CSSPrimitiveValue::UnitType::kPoints:
162 return value_ * kPointsPerInch;
163 case CSSPrimitiveValue::UnitType::kPicas:
164 return value_ * kPicasPerInch;
165 default:
166 NOTREACHED();
167 return 0;
168 }
169 case CSSPrimitiveValue::UnitType::kPoints:
170 switch (unit) {
171 case CSSPrimitiveValue::UnitType::kPixels:
172 return value_ * kCssPixelsPerPoint;
173 case CSSPrimitiveValue::UnitType::kMillimeters:
174 return value_ * (kMillimetersPerInch / kPointsPerInch);
175 case CSSPrimitiveValue::UnitType::kCentimeters:
176 return value_ * (kCentimetersPerInch / kPointsPerInch);
177 case CSSPrimitiveValue::UnitType::kInches:
178 return value_ / kPointsPerInch;
179 case CSSPrimitiveValue::UnitType::kPicas:
180 return value_ * (kPicasPerInch / kPointsPerInch);
181 default:
182 NOTREACHED();
183 return 0;
184 }
185 case CSSPrimitiveValue::UnitType::kPicas:
186 switch (unit) {
187 case CSSPrimitiveValue::UnitType::kPixels:
188 return value_ * kCssPixelsPerPica;
189 case CSSPrimitiveValue::UnitType::kMillimeters:
190 return value_ * (kMillimetersPerInch / kPicasPerInch);
191 case CSSPrimitiveValue::UnitType::kCentimeters:
192 return value_ * (kCentimetersPerInch / kPicasPerInch);
193 case CSSPrimitiveValue::UnitType::kInches:
194 return value_ / kPicasPerInch;
195 case CSSPrimitiveValue::UnitType::kPoints:
196 return value_ * (kPointsPerInch / kPicasPerInch);
197 default:
198 NOTREACHED();
199 return 0;
200 }
201 default:
202 NOTREACHED();
203 return 0;
204 }
205 }
206
94 double CSSUnitValue::ConvertAngle(CSSPrimitiveValue::UnitType unit) const { 207 double CSSUnitValue::ConvertAngle(CSSPrimitiveValue::UnitType unit) const {
95 switch (unit_) { 208 switch (unit_) {
96 case CSSPrimitiveValue::UnitType::kDegrees: 209 case CSSPrimitiveValue::UnitType::kDegrees:
97 switch (unit) { 210 switch (unit) {
98 case CSSPrimitiveValue::UnitType::kRadians: 211 case CSSPrimitiveValue::UnitType::kRadians:
99 return deg2rad(value_); 212 return deg2rad(value_);
100 case CSSPrimitiveValue::UnitType::kGradians: 213 case CSSPrimitiveValue::UnitType::kGradians:
101 return deg2grad(value_); 214 return deg2grad(value_);
102 case CSSPrimitiveValue::UnitType::kTurns: 215 case CSSPrimitiveValue::UnitType::kTurns:
103 return deg2turn(value_); 216 return deg2turn(value_);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 NOTREACHED(); 254 NOTREACHED();
142 return 0; 255 return 0;
143 } 256 }
144 default: 257 default:
145 NOTREACHED(); 258 NOTREACHED();
146 return 0; 259 return 0;
147 } 260 }
148 } 261 }
149 262
150 } // namespace blink 263 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/css/cssom/CSSUnitValue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698