summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/svg/svg_rect.cc
blob: a1ca2ab12d341b5aad40e42a0745e266385e9e5c (plain)
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
/*
 * 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_rect.h"

#include "third_party/blink/renderer/core/svg/svg_animate_element.h"
#include "third_party/blink/renderer/core/svg/svg_parser_utilities.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/wtf/text/character_visitor.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

SVGRect::SVGRect() : is_valid_(true) {}

SVGRect::SVGRect(const FloatRect& rect) : is_valid_(true), value_(rect) {}

SVGRect* SVGRect::Clone() const {
  return MakeGarbageCollected<SVGRect>(value_);
}

template <typename CharType>
SVGParsingError SVGRect::Parse(const CharType*& ptr, const CharType* end) {
  const CharType* start = ptr;
  float x = 0;
  float y = 0;
  float width = 0;
  float height = 0;
  if (!ParseNumber(ptr, end, x) || !ParseNumber(ptr, end, y) ||
      !ParseNumber(ptr, end, width) ||
      !ParseNumber(ptr, end, height, kDisallowWhitespace))
    return SVGParsingError(SVGParseStatus::kExpectedNumber, ptr - start);

  if (SkipOptionalSVGSpaces(ptr, end)) {
    // Nothing should come after the last, fourth number.
    return SVGParsingError(SVGParseStatus::kTrailingGarbage, ptr - start);
  }

  value_ = FloatRect(x, y, width, height);
  is_valid_ = true;
  return SVGParseStatus::kNoError;
}

SVGParsingError SVGRect::SetValueAsString(const String& string) {
  SetInvalid();

  if (string.IsNull())
    return SVGParseStatus::kNoError;

  if (string.IsEmpty())
    return SVGParsingError(SVGParseStatus::kExpectedNumber, 0);

  return WTF::VisitCharacters(string, [&](const auto* chars, unsigned length) {
    return Parse(chars, chars + length);
  });
}

String SVGRect::ValueAsString() const {
  StringBuilder builder;
  builder.AppendNumber(X());
  builder.Append(' ');
  builder.AppendNumber(Y());
  builder.Append(' ');
  builder.AppendNumber(Width());
  builder.Append(' ');
  builder.AppendNumber(Height());
  return builder.ToString();
}

void SVGRect::Add(SVGPropertyBase* other, SVGElement*) {
  value_ += To<SVGRect>(other)->Value();
}

void SVGRect::CalculateAnimatedValue(
    const SVGAnimateElement& animation_element,
    float percentage,
    unsigned repeat_count,
    SVGPropertyBase* from_value,
    SVGPropertyBase* to_value,
    SVGPropertyBase* to_at_end_of_duration_value,
    SVGElement*) {
  auto* from_rect = To<SVGRect>(from_value);
  auto* to_rect = To<SVGRect>(to_value);
  auto* to_at_end_of_duration_rect = To<SVGRect>(to_at_end_of_duration_value);

  float animated_x = X();
  float animated_y = Y();
  float animated_width = Width();
  float animated_height = Height();
  animation_element.AnimateAdditiveNumber(
      percentage, repeat_count, from_rect->X(), to_rect->X(),
      to_at_end_of_duration_rect->X(), animated_x);
  animation_element.AnimateAdditiveNumber(
      percentage, repeat_count, from_rect->Y(), to_rect->Y(),
      to_at_end_of_duration_rect->Y(), animated_y);
  animation_element.AnimateAdditiveNumber(
      percentage, repeat_count, from_rect->Width(), to_rect->Width(),
      to_at_end_of_duration_rect->Width(), animated_width);
  animation_element.AnimateAdditiveNumber(
      percentage, repeat_count, from_rect->Height(), to_rect->Height(),
      to_at_end_of_duration_rect->Height(), animated_height);

  value_ = FloatRect(animated_x, animated_y, animated_width, animated_height);
}

float SVGRect::CalculateDistance(SVGPropertyBase* to,
                                 SVGElement* context_element) {
  // FIXME: Distance calculation is not possible for SVGRect right now. We need
  // the distance for every single value.
  return -1;
}

void SVGRect::SetInvalid() {
  value_ = FloatRect(0.0f, 0.0f, 0.0f, 0.0f);
  is_valid_ = false;
}

}  // namespace blink