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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
* Copyright (C) 2007 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/core/svg/svg_length.h"
#include "third_party/blink/renderer/core/css/css_math_function_value.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/css/css_value.h"
#include "third_party/blink/renderer/core/css/parser/css_parser.h"
#include "third_party/blink/renderer/core/execution_context/security_context.h"
#include "third_party/blink/renderer/core/svg/animation/smil_animation_effect_parameters.h"
#include "third_party/blink/renderer/core/svg/svg_length_context.h"
#include "third_party/blink/renderer/core/svg_names.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
namespace {
#define CAST_UNIT(unit) \
(static_cast<uint8_t>(CSSPrimitiveValue::UnitType::unit))
// Table of initial values for SVGLength properties. Indexed by the
// SVGLength::Initial enumeration, hence these two need to be kept
// synchronized.
struct InitialLengthData {
int8_t value;
uint8_t unit;
};
constexpr auto g_initial_lengths_table = std::to_array<InitialLengthData>({
{0, CAST_UNIT(kUserUnits)},
{-10, CAST_UNIT(kPercentage)},
{0, CAST_UNIT(kPercentage)},
{50, CAST_UNIT(kPercentage)},
{100, CAST_UNIT(kPercentage)},
{120, CAST_UNIT(kPercentage)},
{3, CAST_UNIT(kUserUnits)},
});
static_assert(static_cast<size_t>(SVGLength::Initial::kNumValues) ==
std::size(g_initial_lengths_table),
"the enumeration is synchronized with the value table");
static_assert(static_cast<size_t>(SVGLength::Initial::kNumValues) <=
1u << SVGLength::kInitialValueBits,
"the enumeration is synchronized with the value table");
#undef CAST_UNIT
const CSSPrimitiveValue& CreateInitialCSSValue(
SVGLength::Initial initial_value) {
size_t initial_value_index = static_cast<size_t>(initial_value);
DCHECK_LT(initial_value_index, std::size(g_initial_lengths_table));
const auto& entry = g_initial_lengths_table[initial_value_index];
return *CSSNumericLiteralValue::Create(
entry.value, static_cast<CSSPrimitiveValue::UnitType>(entry.unit));
}
} // namespace
SVGLength::SVGLength(SVGLengthMode mode)
: SVGLength(*CSSNumericLiteralValue::Create(
0,
CSSPrimitiveValue::UnitType::kUserUnits),
mode) {}
SVGLength::SVGLength(Initial initial, SVGLengthMode mode)
: SVGLength(CreateInitialCSSValue(initial), mode) {}
SVGLength::SVGLength(const CSSPrimitiveValue& value, SVGLengthMode mode)
: value_(value), unit_mode_(static_cast<unsigned>(mode)) {
DCHECK_EQ(UnitMode(), mode);
}
void SVGLength::Trace(Visitor* visitor) const {
visitor->Trace(value_);
SVGListablePropertyBase::Trace(visitor);
}
SVGLength* SVGLength::Clone() const {
return MakeGarbageCollected<SVGLength>(*value_, UnitMode());
}
bool SVGLength::operator==(const SVGLength& other) const {
return unit_mode_ == other.unit_mode_ && value_ == other.value_;
}
Length SVGLength::ConvertToLength(
const SVGLengthConversionData& conversion_data) const {
return value_->ConvertToLength(conversion_data);
}
float SVGLength::Value(const SVGLengthConversionData& conversion_data,
float dimension) const {
return FloatValueForLength(value_->ConvertToLength(conversion_data),
dimension);
}
float SVGLength::Value(const SVGLengthContext& context) const {
if (const auto* math_function = DynamicTo<CSSMathFunctionValue>(*value_)) {
return context.ResolveValue(*math_function, UnitMode());
}
return context.ConvertValueToUserUnits(
To<CSSNumericLiteralValue>(*value_).DoubleValue(), UnitMode(),
NumericLiteralType());
}
void SVGLength::SetValueAsNumber(float value) {
value_ = CSSNumericLiteralValue::Create(
value, CSSPrimitiveValue::UnitType::kUserUnits);
}
void SVGLength::SetValueInSpecifiedUnits(float value) {
DCHECK(!IsCalculated());
value_ = CSSNumericLiteralValue::Create(value, NumericLiteralType());
}
bool SVGLength::IsRelative() const {
if (IsPercentage())
return true;
// TODO(crbug.com/979895): This is the result of a refactoring, which might
// have revealed an existing bug with relative units in math functions.
return !IsCalculated() &&
CSSPrimitiveValue::IsRelativeUnit(NumericLiteralType());
}
static bool IsSupportedCSSUnitType(CSSPrimitiveValue::UnitType type) {
return (CSSPrimitiveValue::IsLength(type) ||
type == CSSPrimitiveValue::UnitType::kNumber ||
type == CSSPrimitiveValue::UnitType::kPercentage) &&
type != CSSPrimitiveValue::UnitType::kQuirkyEms;
}
static bool IsSupportedCalculationCategory(CalculationResultCategory category) {
switch (category) {
case kCalcLength:
case kCalcNumber:
case kCalcPercent:
case kCalcLengthFunction:
return true;
default:
return false;
}
}
namespace {
const CSSParserContext* GetSVGAttributeParserContext() {
// NOTE(ikilpatrick): We will always parse SVG lengths in the insecure
// context mode. If a function/unit/etc will require a secure context check
// in the future, plumbing will need to be added.
DEFINE_STATIC_LOCAL(
const Persistent<CSSParserContext>, svg_parser_context,
(MakeGarbageCollected<CSSParserContext>(
kSVGAttributeMode, SecureContextMode::kInsecureContext)));
return svg_parser_context;
}
} // namespace
SVGParsingError SVGLength::SetValueAsString(const String& string) {
// TODO(fs): Preferably we wouldn't need to special-case the null
// string (which we'll get for example for removeAttribute.)
// Hopefully work on crbug.com/225807 can help here.
if (string.IsNull()) {
value_ = CSSNumericLiteralValue::Create(
0, CSSPrimitiveValue::UnitType::kUserUnits);
return SVGParseStatus::kNoError;
}
const CSSValue* parsed = CSSParser::ParseSingleValue(
CSSPropertyID::kX, string, GetSVGAttributeParserContext());
const auto* new_value = DynamicTo<CSSPrimitiveValue>(parsed);
if (!new_value)
return SVGParseStatus::kExpectedLength;
if (const auto* math_value = DynamicTo<CSSMathFunctionValue>(new_value)) {
if (!IsSupportedCalculationCategory(math_value->Category()))
return SVGParseStatus::kExpectedLength;
} else {
const auto* numeric_literal_value = To<CSSNumericLiteralValue>(new_value);
if (!IsSupportedCSSUnitType(numeric_literal_value->GetType()))
return SVGParseStatus::kExpectedLength;
}
value_ = new_value;
return SVGParseStatus::kNoError;
}
String SVGLength::ValueAsString() const {
return value_->CustomCSSText();
}
void SVGLength::NewValueSpecifiedUnits(CSSPrimitiveValue::UnitType type,
float value) {
value_ = CSSNumericLiteralValue::Create(value, type);
}
void SVGLength::ConvertToSpecifiedUnits(CSSPrimitiveValue::UnitType type,
const SVGLengthContext& context) {
DCHECK(IsSupportedCSSUnitType(type));
float value_in_user_units = Value(context);
value_ = CSSNumericLiteralValue::Create(
context.ConvertValueFromUserUnits(value_in_user_units, UnitMode(), type),
type);
}
bool SVGLength::NegativeValuesForbiddenForAnimatedLengthAttribute(
const QualifiedName& attr_name) {
DEFINE_STATIC_LOCAL(
HashSet<QualifiedName>, no_negative_values_set,
({
svg_names::kFrAttr, svg_names::kRAttr, svg_names::kRxAttr,
svg_names::kRyAttr, svg_names::kWidthAttr, svg_names::kHeightAttr,
svg_names::kMarkerWidthAttr, svg_names::kMarkerHeightAttr,
svg_names::kTextLengthAttr,
}));
return no_negative_values_set.Contains(attr_name);
}
void SVGLength::Add(const SVGPropertyBase* other,
const SVGElement* context_element) {
SVGLengthContext length_context(context_element);
const float sum =
Value(length_context) + To<SVGLength>(other)->Value(length_context);
if (IsCalculated()) {
SetValueAsNumber(sum);
return;
}
SetValueInSpecifiedUnits(length_context.ConvertValueFromUserUnits(
sum, UnitMode(), NumericLiteralType()));
}
void SVGLength::CalculateAnimatedValue(
const SMILAnimationEffectParameters& parameters,
float percentage,
unsigned repeat_count,
const SVGPropertyBase* from_value,
const SVGPropertyBase* to_value,
const SVGPropertyBase* to_at_end_of_duration_value,
const SVGElement* context_element) {
auto* from_length = To<SVGLength>(from_value);
auto* to_length = To<SVGLength>(to_value);
auto* to_at_end_of_duration_length =
To<SVGLength>(to_at_end_of_duration_value);
SVGLengthContext length_context(context_element);
float result = ComputeAnimatedNumber(
parameters, percentage, repeat_count, from_length->Value(length_context),
to_length->Value(length_context),
to_at_end_of_duration_length->Value(length_context));
// TODO(shanmuga.m): Construct a calc() expression if the units fall in
// different categories.
const SVGLength* unit_determining_length =
(percentage < 0.5) ? from_length : to_length;
CSSPrimitiveValue::UnitType result_unit =
!unit_determining_length->IsCalculated()
? unit_determining_length->NumericLiteralType()
: CSSPrimitiveValue::UnitType::kUserUnits;
if (parameters.is_additive)
result += Value(length_context);
value_ = CSSNumericLiteralValue::Create(
length_context.ConvertValueFromUserUnits(result, UnitMode(), result_unit),
result_unit);
}
float SVGLength::CalculateDistance(const SVGPropertyBase* to_value,
const SVGElement* context_element) const {
SVGLengthContext length_context(context_element);
auto* to_length = To<SVGLength>(to_value);
return fabsf(to_length->Value(length_context) - Value(length_context));
}
void SVGLength::SetInitial(unsigned initial_value) {
value_ = CreateInitialCSSValue(static_cast<Initial>(initial_value));
}
bool SVGLength::IsNegativeNumericLiteral() const {
std::optional<double> value = value_->GetValueIfKnown();
return value && *value < 0.0;
}
} // namespace blink
|