File: svg_length.cc

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (372 lines) | stat: -rw-r--r-- 14,132 bytes parent folder | download | duplicates (2)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * 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/svg/animation/smil_animation_effect_parameters.h"
#include "third_party/blink/renderer/core/svg_names.h"
#include "third_party/blink/renderer/platform/heap/heap.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.
const struct {
  int8_t value;
  uint8_t unit;
} g_initial_lengths_table[] = {
    {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) ==
                  base::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, base::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());
}

SVGPropertyBase* SVGLength::CloneForAnimation(const String& value) const {
  auto* length = MakeGarbageCollected<SVGLength>(UnitMode());
  length->SetValueAsString(value);
  return length;
}

bool SVGLength::operator==(const SVGLength& other) const {
  return unit_mode_ == other.unit_mode_ && value_ == other.value_;
}

float SVGLength::Value(const SVGLengthContext& context) const {
  if (IsCalculated())
    return context.ResolveValue(AsCSSPrimitiveValue(), UnitMode());

  return context.ConvertValueToUserUnits(value_->GetFloatValue(), UnitMode(),
                                         NumericLiteralType());
}

void SVGLength::SetValueAsNumber(float value) {
  value_ = CSSNumericLiteralValue::Create(
      value, CSSPrimitiveValue::UnitType::kUserUnits);
}

void SVGLength::SetValue(float value, const SVGLengthContext& context) {
  // |value| is in user units.
  if (IsCalculated()) {
    value_ = CSSNumericLiteralValue::Create(
        value, CSSPrimitiveValue::UnitType::kUserUnits);
    return;
  }
  value_ = CSSNumericLiteralValue::Create(
      context.ConvertValueFromUserUnits(value, UnitMode(),
                                        NumericLiteralType()),
      NumericLiteralType());
}

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(CalculationCategory category) {
  switch (category) {
    case kCalcLength:
    case kCalcNumber:
    case kCalcPercent:
    case kCalcPercentLength:
      return true;
    default:
      return false;
  }
}

void SVGLength::SetUnitType(CSSPrimitiveValue::UnitType type) {
  DCHECK(IsSupportedCSSUnitType(type));
  value_ = CSSNumericLiteralValue::Create(value_->GetFloatValue(), type);
}

float SVGLength::ValueAsPercentage() const {
  // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy
  // but could eventually be changed.
  if (value_->IsPercentage()) {
    // Note: This division is a source of floating point inaccuracy.
    return value_->GetFloatValue() / 100;
  }

  return value_->GetFloatValue();
}

float SVGLength::ScaleByPercentage(float input) const {
  float result = input * value_->GetFloatValue();
  if (value_->IsPercentage()) {
    // Delaying division by 100 as long as possible since it introduces floating
    // point errors.
    result = result / 100;
  }
  return result;
}

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);
}

SVGLengthMode SVGLength::LengthModeForAnimatedLengthAttribute(
    const QualifiedName& attr_name) {
  typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMap;
  DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, length_mode_map, ());

  if (length_mode_map.IsEmpty()) {
    length_mode_map.Set(svg_names::kXAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kYAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kCxAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kCyAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kDxAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kDyAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kFrAttr, SVGLengthMode::kOther);
    length_mode_map.Set(svg_names::kFxAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kFyAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kRAttr, SVGLengthMode::kOther);
    length_mode_map.Set(svg_names::kRxAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kRyAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kWidthAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kHeightAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kX1Attr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kX2Attr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kY1Attr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kY2Attr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kRefXAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kRefYAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kMarkerWidthAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kMarkerHeightAttr, SVGLengthMode::kHeight);
    length_mode_map.Set(svg_names::kTextLengthAttr, SVGLengthMode::kWidth);
    length_mode_map.Set(svg_names::kStartOffsetAttr, SVGLengthMode::kWidth);
  }

  if (length_mode_map.Contains(attr_name))
    return length_mode_map.at(attr_name);

  return SVGLengthMode::kOther;
}

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);
  SetValue(Value(length_context) + To<SVGLength>(other)->Value(length_context),
           length_context);
}

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.
  CSSPrimitiveValue::UnitType result_unit =
      CSSPrimitiveValue::UnitType::kUserUnits;
  if (percentage < 0.5) {
    if (!from_length->IsCalculated()) {
      result_unit = from_length->NumericLiteralType();
    }
  } else {
    if (!to_length->IsCalculated()) {
      result_unit = to_length->NumericLiteralType();
    }
  }

  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 {
  if (!value_->IsNumericLiteralValue())
    return false;
  return value_->GetDoubleValue() < 0;
}

}  // namespace blink