File: svg_rect.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 5,299 bytes parent folder | download | duplicates (3)
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
/*
 * 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/animation/smil_animation_effect_parameters.h"
#include "third_party/blink/renderer/core/svg/svg_parser_utilities.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.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::Clone() const {
  return MakeGarbageCollected<SVGRect>(x_, y_, width_, height_);
}

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

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

SVGParsingError SVGRect::SetValueAsString(const String& string) {
  // In case the string is invalid, the rect will be treated as invalid.
  is_valid_ = false;
  // Also clear the existing values.
  Set(0, 0, 0, 0);

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

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

  return WTF::VisitCharacters(string, [&](auto chars) {
    const auto* start = chars.data();
    return Parse(start, start + chars.size());
  });
}

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(const SVGPropertyBase* other, const SVGElement*) {
  auto* other_rect = To<SVGRect>(other);
  Add(other_rect->x_, other_rect->y_, other_rect->width_, other_rect->height_);
}

void SVGRect::Set(float x, float y, float width, float height) {
  x_ = x;
  y_ = y;
  width_ = width;
  height_ = height;
}

void SVGRect::Add(float x, float y, float width, float height) {
  x_ += x;
  y_ += y;
  width_ += width;
  height_ += height;
}

void SVGRect::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*) {
  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 x = ComputeAnimatedNumber(parameters, percentage, repeat_count,
                                  from_rect->X(), to_rect->X(),
                                  to_at_end_of_duration_rect->X());
  float y = ComputeAnimatedNumber(parameters, percentage, repeat_count,
                                  from_rect->Y(), to_rect->Y(),
                                  to_at_end_of_duration_rect->Y());
  float width = ComputeAnimatedNumber(parameters, percentage, repeat_count,
                                      from_rect->Width(), to_rect->Width(),
                                      to_at_end_of_duration_rect->Width());
  float height = ComputeAnimatedNumber(parameters, percentage, repeat_count,
                                       from_rect->Height(), to_rect->Height(),
                                       to_at_end_of_duration_rect->Height());
  if (parameters.is_additive)
    Add(x, y, width, height);
  else
    Set(x, y, width, height);
}

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

}  // namespace blink