1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/css/cssom/CSSCalcLength.h"
#include "bindings/core/v8/ExceptionState.h"
#include "core/css/CSSCalculationValue.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/css/cssom/CSSCalcDictionary.h"
#include "core/css/cssom/CSSSimpleLength.h"
#include "wtf/Vector.h"
namespace blink {
namespace {
static CSSPrimitiveValue::UnitType unitFromIndex(int index) {
DCHECK(index < CSSLengthValue::kNumSupportedUnits);
int lowestValue = static_cast<int>(CSSPrimitiveValue::UnitType::Percentage);
return static_cast<CSSPrimitiveValue::UnitType>(index + lowestValue);
}
int indexForUnit(CSSPrimitiveValue::UnitType unit) {
return (static_cast<int>(unit) -
static_cast<int>(CSSPrimitiveValue::UnitType::Percentage));
}
} // namespace
CSSCalcLength::CSSCalcLength(const CSSCalcLength& other)
: m_unitData(other.m_unitData) {}
CSSCalcLength::CSSCalcLength(const CSSSimpleLength& other) {
m_unitData.set(other.lengthUnit(), other.value());
}
CSSCalcLength* CSSCalcLength::create(const CSSLengthValue* length) {
if (length->type() == SimpleLengthType) {
const CSSSimpleLength* simpleLength = toCSSSimpleLength(length);
return new CSSCalcLength(*simpleLength);
}
return new CSSCalcLength(*toCSSCalcLength(length));
}
CSSCalcLength* CSSCalcLength::create(const CSSCalcDictionary& dictionary,
ExceptionState& exceptionState) {
int numSet = 0;
UnitData result;
#define SET_FROM_DICT_VALUE(name, camelName, primitiveName) \
if (dictionary.has##camelName()) { \
result.set(CSSPrimitiveValue::UnitType::primitiveName, dictionary.name()); \
numSet++; \
}
SET_FROM_DICT_VALUE(px, Px, Pixels)
SET_FROM_DICT_VALUE(percent, Percent, Percentage)
SET_FROM_DICT_VALUE(em, Em, Ems)
SET_FROM_DICT_VALUE(ex, Ex, Exs)
SET_FROM_DICT_VALUE(ch, Ch, Chs)
SET_FROM_DICT_VALUE(rem, Rem, Rems)
SET_FROM_DICT_VALUE(vw, Vw, ViewportWidth)
SET_FROM_DICT_VALUE(vh, Vh, ViewportHeight)
SET_FROM_DICT_VALUE(vmin, Vmin, ViewportMin)
SET_FROM_DICT_VALUE(vmax, Vmax, ViewportMax)
SET_FROM_DICT_VALUE(cm, Cm, Centimeters)
SET_FROM_DICT_VALUE(mm, Mm, Millimeters)
SET_FROM_DICT_VALUE(in, In, Inches)
SET_FROM_DICT_VALUE(pc, Pc, Picas)
SET_FROM_DICT_VALUE(pt, Pt, Points)
#undef SET_FROM_DICT_VALUE
if (numSet == 0) {
exceptionState.throwTypeError(
"Must specify at least one value in CSSCalcDictionary for creating a "
"CSSCalcLength.");
return nullptr;
}
return new CSSCalcLength(result);
}
CSSCalcLength* CSSCalcLength::fromCSSValue(const CSSPrimitiveValue& value) {
std::unique_ptr<UnitData> unitData =
UnitData::fromExpressionNode(value.cssCalcValue()->expressionNode());
if (unitData)
return new CSSCalcLength(*unitData);
return nullptr;
}
bool CSSCalcLength::containsPercent() const {
return m_unitData.has(CSSPrimitiveValue::UnitType::Percentage);
}
CSSLengthValue* CSSCalcLength::addInternal(const CSSLengthValue* other) {
UnitData result = m_unitData;
if (other->type() == SimpleLengthType) {
const CSSSimpleLength* simpleLength = toCSSSimpleLength(other);
result.set(
simpleLength->lengthUnit(),
m_unitData.get(simpleLength->lengthUnit()) + simpleLength->value());
} else {
result.add(toCSSCalcLength(other)->m_unitData);
}
return new CSSCalcLength(result);
}
CSSLengthValue* CSSCalcLength::subtractInternal(const CSSLengthValue* other) {
UnitData result = m_unitData;
if (other->type() == SimpleLengthType) {
const CSSSimpleLength* simpleLength = toCSSSimpleLength(other);
result.set(
simpleLength->lengthUnit(),
m_unitData.get(simpleLength->lengthUnit()) - simpleLength->value());
} else {
result.subtract(toCSSCalcLength(other)->m_unitData);
}
return new CSSCalcLength(result);
}
CSSLengthValue* CSSCalcLength::multiplyInternal(double x) {
UnitData result = m_unitData;
result.multiply(x);
return new CSSCalcLength(result);
}
CSSLengthValue* CSSCalcLength::divideInternal(double x) {
UnitData result = m_unitData;
result.divide(x);
return new CSSCalcLength(result);
}
CSSValue* CSSCalcLength::toCSSValue() const {
CSSCalcExpressionNode* node = m_unitData.toCSSCalcExpressionNode();
if (node)
return CSSPrimitiveValue::create(CSSCalcValue::create(node));
return nullptr;
}
std::unique_ptr<CSSCalcLength::UnitData>
CSSCalcLength::UnitData::fromExpressionNode(
const CSSCalcExpressionNode* expressionNode) {
CalculationCategory category = expressionNode->category();
DCHECK(category == CalculationCategory::CalcLength ||
category == CalculationCategory::CalcPercentLength ||
category == CalculationCategory::CalcPercent);
if (expressionNode->getType() ==
CSSCalcExpressionNode::Type::CssCalcPrimitiveValue) {
CSSPrimitiveValue::UnitType unit = expressionNode->typeWithCalcResolved();
DCHECK(CSSLengthValue::isSupportedLengthUnit(unit));
std::unique_ptr<UnitData> result(new UnitData());
result->set(unit, expressionNode->doubleValue());
return result;
}
const CSSCalcExpressionNode* left = expressionNode->leftExpressionNode();
const CSSCalcExpressionNode* right = expressionNode->rightExpressionNode();
CalcOperator op = expressionNode->operatorType();
if (op == CalcMultiply) {
std::unique_ptr<UnitData> unitData = nullptr;
double argument = 0;
// One side should be a number.
if (left->category() == CalculationCategory::CalcNumber) {
unitData = UnitData::fromExpressionNode(right);
argument = left->doubleValue();
} else if (right->category() == CalculationCategory::CalcNumber) {
unitData = UnitData::fromExpressionNode(left);
argument = right->doubleValue();
} else {
NOTREACHED();
return nullptr;
}
DCHECK(unitData);
unitData->multiply(argument);
return unitData;
}
if (op == CalcDivide) {
// Divisor must always be on the RHS.
DCHECK_EQ(right->category(), CalculationCategory::CalcNumber);
std::unique_ptr<UnitData> unitData = UnitData::fromExpressionNode(left);
DCHECK(unitData);
unitData->divide(right->doubleValue());
return unitData;
}
// Add and subtract.
std::unique_ptr<UnitData> leftUnitData = UnitData::fromExpressionNode(left);
std::unique_ptr<UnitData> rightUnitData = UnitData::fromExpressionNode(right);
DCHECK(leftUnitData);
DCHECK(rightUnitData);
if (op == CalcAdd)
leftUnitData->add(*rightUnitData);
else if (op == CalcSubtract)
leftUnitData->subtract(*rightUnitData);
else
NOTREACHED();
return leftUnitData;
}
CSSCalcExpressionNode* CSSCalcLength::UnitData::toCSSCalcExpressionNode()
const {
CSSCalcExpressionNode* node = nullptr;
for (unsigned i = 0; i < CSSLengthValue::kNumSupportedUnits; ++i) {
if (!hasAtIndex(i))
continue;
double value = getAtIndex(i);
if (node) {
node = CSSCalcValue::createExpressionNode(
node, CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(
std::abs(value), unitFromIndex(i))),
value >= 0 ? CalcAdd : CalcSubtract);
} else {
node = CSSCalcValue::createExpressionNode(
CSSPrimitiveValue::create(value, unitFromIndex(i)));
}
}
return node;
}
bool CSSCalcLength::UnitData::hasAtIndex(int i) const {
DCHECK_GE(i, 0);
DCHECK_LT(i, CSSLengthValue::kNumSupportedUnits);
return m_hasValueForUnit[i];
}
bool CSSCalcLength::UnitData::has(CSSPrimitiveValue::UnitType unit) const {
return hasAtIndex(indexForUnit(unit));
}
void CSSCalcLength::UnitData::setAtIndex(int i, double value) {
DCHECK_GE(i, 0);
DCHECK_LT(i, CSSLengthValue::kNumSupportedUnits);
m_hasValueForUnit.set(i);
m_values[i] = value;
}
void CSSCalcLength::UnitData::set(CSSPrimitiveValue::UnitType unit,
double value) {
setAtIndex(indexForUnit(unit), value);
}
double CSSCalcLength::UnitData::getAtIndex(int i) const {
DCHECK_GE(i, 0);
DCHECK_LT(i, CSSLengthValue::kNumSupportedUnits);
return m_values[i];
}
double CSSCalcLength::UnitData::get(CSSPrimitiveValue::UnitType unit) const {
return getAtIndex(indexForUnit(unit));
}
void CSSCalcLength::UnitData::add(const CSSCalcLength::UnitData& right) {
for (int i = 0; i < CSSLengthValue::kNumSupportedUnits; ++i) {
if (right.hasAtIndex(i)) {
setAtIndex(i, getAtIndex(i) + right.getAtIndex(i));
}
}
}
void CSSCalcLength::UnitData::subtract(const CSSCalcLength::UnitData& right) {
for (int i = 0; i < CSSLengthValue::kNumSupportedUnits; ++i) {
if (right.hasAtIndex(i)) {
setAtIndex(i, getAtIndex(i) - right.getAtIndex(i));
}
}
}
void CSSCalcLength::UnitData::multiply(double x) {
for (int i = 0; i < CSSLengthValue::kNumSupportedUnits; ++i) {
if (hasAtIndex(i)) {
setAtIndex(i, getAtIndex(i) * x);
}
}
}
void CSSCalcLength::UnitData::divide(double x) {
DCHECK_NE(x, 0);
for (int i = 0; i < CSSLengthValue::kNumSupportedUnits; ++i) {
if (hasAtIndex(i)) {
setAtIndex(i, getAtIndex(i) / x);
}
}
}
} // namespace blink
|