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
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/animation/PathInterpolationFunctions.h"
#include "core/animation/InterpolatedSVGPathSource.h"
#include "core/animation/InterpolationEnvironment.h"
#include "core/animation/SVGPathSegInterpolationFunctions.h"
#include "core/css/CSSPathValue.h"
#include "core/svg/SVGPath.h"
#include "core/svg/SVGPathByteStreamBuilder.h"
#include "core/svg/SVGPathByteStreamSource.h"
#include "core/svg/SVGPathParser.h"
#include "wtf/PtrUtil.h"
#include <memory>
namespace blink {
class SVGPathNonInterpolableValue : public NonInterpolableValue {
public:
virtual ~SVGPathNonInterpolableValue() {}
static PassRefPtr<SVGPathNonInterpolableValue> create(
Vector<SVGPathSegType>& pathSegTypes) {
return adoptRef(new SVGPathNonInterpolableValue(pathSegTypes));
}
const Vector<SVGPathSegType>& pathSegTypes() const { return m_pathSegTypes; }
DECLARE_NON_INTERPOLABLE_VALUE_TYPE();
private:
SVGPathNonInterpolableValue(Vector<SVGPathSegType>& pathSegTypes) {
m_pathSegTypes.swap(pathSegTypes);
}
Vector<SVGPathSegType> m_pathSegTypes;
};
DEFINE_NON_INTERPOLABLE_VALUE_TYPE(SVGPathNonInterpolableValue);
DEFINE_NON_INTERPOLABLE_VALUE_TYPE_CASTS(SVGPathNonInterpolableValue);
enum PathComponentIndex : unsigned {
PathArgsIndex,
PathNeutralIndex,
PathComponentIndexCount,
};
InterpolationValue PathInterpolationFunctions::convertValue(
const SVGPathByteStream& byteStream) {
SVGPathByteStreamSource pathSource(byteStream);
size_t length = 0;
PathCoordinates currentCoordinates;
Vector<std::unique_ptr<InterpolableValue>> interpolablePathSegs;
Vector<SVGPathSegType> pathSegTypes;
while (pathSource.hasMoreData()) {
const PathSegmentData segment = pathSource.parseSegment();
interpolablePathSegs.push_back(
SVGPathSegInterpolationFunctions::consumePathSeg(segment,
currentCoordinates));
pathSegTypes.push_back(segment.command);
length++;
}
std::unique_ptr<InterpolableList> pathArgs = InterpolableList::create(length);
for (size_t i = 0; i < interpolablePathSegs.size(); i++)
pathArgs->set(i, std::move(interpolablePathSegs[i]));
std::unique_ptr<InterpolableList> result =
InterpolableList::create(PathComponentIndexCount);
result->set(PathArgsIndex, std::move(pathArgs));
result->set(PathNeutralIndex, InterpolableNumber::create(0));
return InterpolationValue(std::move(result),
SVGPathNonInterpolableValue::create(pathSegTypes));
}
InterpolationValue PathInterpolationFunctions::convertValue(
const StylePath* stylePath) {
if (stylePath)
return convertValue(stylePath->byteStream());
std::unique_ptr<SVGPathByteStream> emptyPath = SVGPathByteStream::create();
return convertValue(*emptyPath);
}
class UnderlyingPathSegTypesChecker
: public InterpolationType::ConversionChecker {
public:
~UnderlyingPathSegTypesChecker() final {}
static std::unique_ptr<UnderlyingPathSegTypesChecker> create(
const InterpolationValue& underlying) {
return WTF::wrapUnique(
new UnderlyingPathSegTypesChecker(getPathSegTypes(underlying)));
}
private:
UnderlyingPathSegTypesChecker(const Vector<SVGPathSegType>& pathSegTypes)
: m_pathSegTypes(pathSegTypes) {}
static const Vector<SVGPathSegType>& getPathSegTypes(
const InterpolationValue& underlying) {
return toSVGPathNonInterpolableValue(*underlying.nonInterpolableValue)
.pathSegTypes();
}
bool isValid(const InterpolationEnvironment&,
const InterpolationValue& underlying) const final {
return m_pathSegTypes == getPathSegTypes(underlying);
}
Vector<SVGPathSegType> m_pathSegTypes;
};
InterpolationValue PathInterpolationFunctions::maybeConvertNeutral(
const InterpolationValue& underlying,
InterpolationType::ConversionCheckers& conversionCheckers) {
conversionCheckers.push_back(
UnderlyingPathSegTypesChecker::create(underlying));
std::unique_ptr<InterpolableList> result =
InterpolableList::create(PathComponentIndexCount);
result->set(PathArgsIndex, toInterpolableList(*underlying.interpolableValue)
.get(PathArgsIndex)
->cloneAndZero());
result->set(PathNeutralIndex, InterpolableNumber::create(1));
return InterpolationValue(std::move(result),
underlying.nonInterpolableValue.get());
}
static bool pathSegTypesMatch(const Vector<SVGPathSegType>& a,
const Vector<SVGPathSegType>& b) {
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); i++) {
if (toAbsolutePathSegType(a[i]) != toAbsolutePathSegType(b[i]))
return false;
}
return true;
}
PairwiseInterpolationValue PathInterpolationFunctions::maybeMergeSingles(
InterpolationValue&& start,
InterpolationValue&& end) {
const Vector<SVGPathSegType>& startTypes =
toSVGPathNonInterpolableValue(*start.nonInterpolableValue).pathSegTypes();
const Vector<SVGPathSegType>& endTypes =
toSVGPathNonInterpolableValue(*end.nonInterpolableValue).pathSegTypes();
if (!pathSegTypesMatch(startTypes, endTypes))
return nullptr;
return PairwiseInterpolationValue(std::move(start.interpolableValue),
std::move(end.interpolableValue),
std::move(end.nonInterpolableValue));
}
void PathInterpolationFunctions::composite(
UnderlyingValueOwner& underlyingValueOwner,
double underlyingFraction,
const InterpolationType& type,
const InterpolationValue& value) {
const InterpolableList& list = toInterpolableList(*value.interpolableValue);
double neutralComponent =
toInterpolableNumber(list.get(PathNeutralIndex))->value();
if (neutralComponent == 0) {
underlyingValueOwner.set(type, value);
return;
}
DCHECK(pathSegTypesMatch(
toSVGPathNonInterpolableValue(
*underlyingValueOwner.value().nonInterpolableValue)
.pathSegTypes(),
toSVGPathNonInterpolableValue(*value.nonInterpolableValue)
.pathSegTypes()));
underlyingValueOwner.mutableValue().interpolableValue->scaleAndAdd(
neutralComponent, *value.interpolableValue);
underlyingValueOwner.mutableValue().nonInterpolableValue =
value.nonInterpolableValue.get();
}
std::unique_ptr<SVGPathByteStream> PathInterpolationFunctions::appliedValue(
const InterpolableValue& interpolableValue,
const NonInterpolableValue* nonInterpolableValue) {
std::unique_ptr<SVGPathByteStream> pathByteStream =
SVGPathByteStream::create();
InterpolatedSVGPathSource source(
toInterpolableList(
*toInterpolableList(interpolableValue).get(PathArgsIndex)),
toSVGPathNonInterpolableValue(nonInterpolableValue)->pathSegTypes());
SVGPathByteStreamBuilder builder(*pathByteStream);
SVGPathParser::parsePath(source, builder);
return pathByteStream;
}
} // namespace blink
|