File: svg_path.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 (172 lines) | stat: -rw-r--r-- 6,414 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann
 * <zimmermann@kde.org>
 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
 * Copyright (C) Research In Motion Limited 2010. 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_path.h"

#include <memory>
#include <utility>

#include "third_party/blink/renderer/core/svg/animation/smil_animation_effect_parameters.h"
#include "third_party/blink/renderer/core/svg/svg_path_blender.h"
#include "third_party/blink/renderer/core/svg/svg_path_byte_stream.h"
#include "third_party/blink/renderer/core/svg/svg_path_byte_stream_builder.h"
#include "third_party/blink/renderer/core/svg/svg_path_byte_stream_source.h"
#include "third_party/blink/renderer/core/svg/svg_path_utilities.h"
#include "third_party/blink/renderer/platform/geometry/path.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

using cssvalue::CSSPathValue;

namespace {

SVGPathByteStream BlendPathByteStreams(const SVGPathByteStream& from_stream,
                                       const SVGPathByteStream& to_stream,
                                       float progress) {
  SVGPathByteStreamBuilder builder;
  SVGPathByteStreamSource from_source(from_stream);
  SVGPathByteStreamSource to_source(to_stream);
  SVGPathBlender blender(&from_source, &to_source, &builder);
  blender.BlendAnimatedPath(progress);
  return builder.CopyByteStream();
}

SVGPathByteStream AddPathByteStreams(const SVGPathByteStream& from_stream,
                                     const SVGPathByteStream& by_stream,
                                     unsigned repeat_count = 1) {
  SVGPathByteStreamBuilder builder;
  SVGPathByteStreamSource from_source(from_stream);
  SVGPathByteStreamSource by_source(by_stream);
  SVGPathBlender blender(&from_source, &by_source, &builder);
  blender.AddAnimatedPath(repeat_count);
  return builder.CopyByteStream();
}

SVGPathByteStream ConditionallyAddPathByteStreams(
    SVGPathByteStream from_stream,
    const SVGPathByteStream& by_stream,
    unsigned repeat_count = 1) {
  if (from_stream.IsEmpty() || by_stream.IsEmpty()) {
    return from_stream;
  }
  return AddPathByteStreams(from_stream, by_stream, repeat_count);
}

}  // namespace

SVGPath::SVGPath() : path_value_(CSSPathValue::EmptyPathValue()) {}

SVGPath::SVGPath(const CSSPathValue& path_value) : path_value_(path_value) {}

SVGPath::~SVGPath() = default;

String SVGPath::ValueAsString() const {
  return BuildStringFromByteStream(ByteStream(), kNoTransformation);
}

SVGPath* SVGPath::Clone() const {
  return MakeGarbageCollected<SVGPath>(*path_value_);
}

SVGParsingError SVGPath::SetValueAsString(const String& string) {
  SVGPathByteStreamBuilder builder;
  SVGParsingError parse_status = BuildByteStreamFromString(string, builder);
  path_value_ = MakeGarbageCollected<CSSPathValue>(builder.CopyByteStream());
  return parse_status;
}

void SVGPath::Add(const SVGPropertyBase* other, const SVGElement*) {
  const auto& other_path_byte_stream = To<SVGPath>(other)->ByteStream();
  if (ByteStream().size() != other_path_byte_stream.size() ||
      ByteStream().IsEmpty() || other_path_byte_stream.IsEmpty())
    return;

  path_value_ = MakeGarbageCollected<CSSPathValue>(
      AddPathByteStreams(ByteStream(), other_path_byte_stream));
}

void SVGPath::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*) {
  const auto& to = To<SVGPath>(*to_value);
  const SVGPathByteStream& to_stream = to.ByteStream();

  // If no 'to' value is given, nothing to animate.
  if (!to_stream.size())
    return;

  const auto& from = To<SVGPath>(*from_value);
  const SVGPathByteStream& from_stream = from.ByteStream();

  // If the 'from' value is given and it's length doesn't match the 'to' value
  // list length, fallback to a discrete animation.
  if (from_stream.size() != to_stream.size() && from_stream.size()) {
    // If this is a 'to' animation, the "from" value will be the same
    // object as this object, so this will be a no-op but shouldn't
    // clobber the object.
    path_value_ = percentage < 0.5 ? from.PathValue() : to.PathValue();
    return;
  }

  // If this is a 'to' animation, the "from" value will be the same
  // object as this object, so make sure to update the state of this
  // object as the last thing to avoid clobbering the result. As long
  // as all intermediate results are computed into |new_stream| that
  // should be unproblematic.
  SVGPathByteStream new_stream =
      BlendPathByteStreams(from_stream, to_stream, percentage);

  // Handle accumulate='sum'.
  if (repeat_count && parameters.is_cumulative) {
    new_stream = ConditionallyAddPathByteStreams(
        std::move(new_stream),
        To<SVGPath>(to_at_end_of_duration_value)->ByteStream(), repeat_count);
  }

  // Handle additive='sum'.
  if (parameters.is_additive) {
    new_stream =
        ConditionallyAddPathByteStreams(std::move(new_stream), ByteStream());
  }

  path_value_ = MakeGarbageCollected<CSSPathValue>(std::move(new_stream));
}

float SVGPath::CalculateDistance(const SVGPropertyBase* to,
                                 const SVGElement*) const {
  // FIXME: Support paced animations.
  return -1;
}

void SVGPath::Trace(Visitor* visitor) const {
  visitor->Trace(path_value_);
  SVGPropertyBase::Trace(visitor);
}

}  // namespace blink