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
|
/*
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
* Copyright (C) 2018-2019 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.
*/
#pragma once
#include "Path.h"
#include "SVGPathByteStream.h"
#include "SVGPathSeg.h"
#include "SVGPropertyList.h"
namespace WebCore {
class SVGPathSegList final : public SVGPropertyList<SVGPathSeg> {
friend class SVGAnimatedPathSegListAnimator;
friend class SVGPathSegListBuilder;
friend class SVGPathSegListSource;
using Base = SVGPropertyList<SVGPathSeg>;
using Base::Base;
public:
static Ref<SVGPathSegList> create(SVGPropertyOwner* owner, SVGPropertyAccess access)
{
return adoptRef(*new SVGPathSegList(owner, access));
}
static Ref<SVGPathSegList> create(const SVGPathSegList& other, SVGPropertyAccess access)
{
return adoptRef(*new SVGPathSegList(other, access));
}
static Ref<SVGPathSegList> create(Ref<SVGPathSeg>&& newItem)
{
return adoptRef(*new SVGPathSegList(WTFMove(newItem)));
}
SVGPathSegList& operator=(const SVGPathSegList& other)
{
pathByteStreamWillChange();
m_pathByteStream = other.pathByteStream();
return *this;
}
// Override SVGList::length() because numberOfItems() isn't virtual.
unsigned length() const { return numberOfItems(); }
unsigned numberOfItems() const
{
const_cast<SVGPathSegList*>(this)->ensureItems();
return Base::numberOfItems();
}
ExceptionOr<void> clear()
{
itemsWillChange();
return Base::clear();
}
ExceptionOr<Ref<SVGPathSeg>> getItem(unsigned index)
{
ensureItems();
return Base::getItem(index);
}
ExceptionOr<Ref<SVGPathSeg>> initialize(Ref<SVGPathSeg>&& newItem)
{
itemsWillChange();
return Base::initialize(WTFMove(newItem));
}
ExceptionOr<Ref<SVGPathSeg>> insertItemBefore(Ref<SVGPathSeg>&& newItem, unsigned index)
{
ensureItems();
itemsWillChange();
return Base::insertItemBefore(WTFMove(newItem), index);
}
ExceptionOr<Ref<SVGPathSeg>> replaceItem(Ref<SVGPathSeg>&& newItem, unsigned index)
{
ensureItems();
itemsWillChange();
return Base::replaceItem(WTFMove(newItem), index);
}
ExceptionOr<Ref<SVGPathSeg>> removeItem(unsigned index)
{
ensureItems();
itemsWillChange();
return Base::removeItem(index);
}
ExceptionOr<Ref<SVGPathSeg>> appendItem(Ref<SVGPathSeg>&& newItem)
{
ensureItems();
appendPathSegToPathByteStream(newItem);
clearPath();
return Base::appendItem(WTFMove(newItem));
}
// Override SVGList::setItem() because replaceItem() isn't virtual.
ExceptionOr<void> setItem(unsigned index, Ref<SVGPathSeg>&& newItem)
{
auto result = replaceItem(WTFMove(newItem), index);
if (result.hasException())
return result.releaseException();
return { };
}
const SVGPathByteStream& pathByteStream() const { return const_cast<SVGPathSegList*>(this)->pathByteStream(); }
SVGPathByteStream& pathByteStream()
{
ensurePathByteStream();
return m_pathByteStream;
}
bool parse(StringView value)
{
pathByteStreamWillChange();
return buildSVGPathByteStreamFromString(value, m_pathByteStream, UnalteredParsing);
}
Path path() const
{
if (!m_path)
m_path = buildPathFromByteStream(pathByteStream());
return *m_path;
}
size_t approximateMemoryCost() const
{
// This is an approximation for path memory cost since the path is parsed on demand.
size_t pathMemoryCost = (m_pathByteStream.size() / 10) * sizeof(FloatPoint);
// We need to account for the memory which is allocated by the m_path.
return m_path ? pathMemoryCost + sizeof(*m_path) : pathMemoryCost;
}
String valueAsString() const override
{
String value;
buildStringFromByteStream(pathByteStream(), value, UnalteredParsing);
return value;
}
private:
SVGPathSegList(const SVGPathSegList& other, SVGPropertyAccess access)
: Base(other.owner(), access)
, m_pathByteStream(other.pathByteStream())
{
}
// Used by appendPathSegToPathByteStream() to create a temporary SVGPathSegList with one item.
SVGPathSegList(Ref<SVGPathSeg>&& newItem)
{
append(WTFMove(newItem));
}
// Called when changing an item in the list.
void commitPropertyChange(SVGProperty* property) override
{
itemsWillChange();
Base::commitPropertyChange(property);
}
void ensureItems()
{
if (!m_items.isEmpty() || m_pathByteStream.isEmpty())
return;
buildSVGPathSegListFromByteStream(m_pathByteStream, *this, UnalteredParsing);
}
void ensurePathByteStream()
{
if (!m_pathByteStream.isEmpty() || m_items.isEmpty())
return;
buildSVGPathByteStreamFromSVGPathSegList(*this, m_pathByteStream, UnalteredParsing);
}
// Optimize appending an SVGPathSeg to the list. Instead of creating the whole
// byte stream, a temporary byte stream will be creating just for the new item
// and this temporary byte stream will be appended to m_pathByteStream.
void appendPathSegToPathByteStream(const Ref<SVGPathSeg>& item)
{
if (m_pathByteStream.isEmpty())
return;
Ref<SVGPathSegList> pathSegList = SVGPathSegList::create(item.copyRef());
SVGPathByteStream pathSegStream;
if (!buildSVGPathByteStreamFromSVGPathSegList(pathSegList, pathSegStream, UnalteredParsing, false))
return;
m_pathByteStream.append(pathSegStream);
}
void clearPathByteStream() { m_pathByteStream.clear(); }
void clearPath() { m_path = std::nullopt; }
void pathByteStreamWillChange()
{
clearItems();
clearPath();
}
void itemsWillChange()
{
clearPathByteStream();
clearPath();
}
SVGPathByteStream m_pathByteStream;
mutable std::optional<Path> m_path;
};
}
|