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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "PathStream.h"
#include "GeometryUtilities.h"
namespace WebCore {
UniqueRef<PathStream> PathStream::create()
{
return makeUniqueRef<PathStream>();
}
UniqueRef<PathStream> PathStream::create(const PathStream& pathStream)
{
return makeUniqueRef<PathStream>(pathStream);
}
UniqueRef<PathStream> PathStream::create(PathSegment&& segment)
{
return makeUniqueRef<PathStream>(WTFMove(segment));
}
UniqueRef<PathStream> PathStream::create(Vector<PathSegment>&& segments)
{
return makeUniqueRef<PathStream>(WTFMove(segments));
}
UniqueRef<PathStream> PathStream::create(const Vector<PathSegment>& segments)
{
return makeUniqueRef<PathStream>(segments);
}
UniqueRef<PathStream> PathStream::create(const Vector<FloatPoint>& points)
{
auto stream = PathStream::create();
if (points.size() < 2)
return stream;
stream->moveTo(points[0]);
for (size_t i = 1; i < points.size(); ++i)
stream->addLineTo(points[i]);
stream->closeSubpath();
return stream;
}
PathStream::PathStream()
: m_segmentsData(SegmentsData::create())
{
}
PathStream::PathStream(Vector<PathSegment>&& segments)
: m_segmentsData(SegmentsData::create(WTFMove(segments)))
{
}
PathStream::PathStream(const Vector<PathSegment>& segments)
: m_segmentsData(SegmentsData::create(segments))
{
}
PathStream::PathStream(const PathStream& pathStream)
: m_segmentsData(pathStream.m_segmentsData)
{
}
PathStream::PathStream(PathSegment&& segment)
: m_segmentsData(SegmentsData::create(WTFMove(segment)))
{
}
UniqueRef<PathImpl> PathStream::clone() const
{
return create(*this);
}
bool PathStream::operator==(const PathImpl& other) const
{
if (!is<PathStream>(other))
return false;
return m_segmentsData == downcast<PathStream>(other).m_segmentsData;
}
const PathMoveTo* PathStream::lastIfMoveTo() const
{
if (isEmpty())
return nullptr;
return std::get_if<PathMoveTo>(&m_segmentsData->segments.last().data());
}
void PathStream::moveTo(const FloatPoint& point)
{
segments().append(PathMoveTo { point });
}
void PathStream::addLineTo(const FloatPoint& point)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataLine { moveTo->point, point } };
else
segments().append(PathLineTo { point });
}
void PathStream::addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataQuadCurve { moveTo->point, controlPoint, endPoint } };
else
segments().append(PathQuadCurveTo { controlPoint, endPoint });
}
void PathStream::addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataBezierCurve { moveTo->point, controlPoint1, controlPoint2, endPoint } };
else
segments().append(PathBezierCurveTo { controlPoint1, controlPoint2, endPoint });
}
void PathStream::addArcTo(const FloatPoint& point1, const FloatPoint& point2, float radius)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataArc { moveTo->point, point1, point2, radius } };
else
segments().append(PathArcTo { point1, point2, radius });
}
void PathStream::addArc(const FloatPoint& point, float radius, float startAngle, float endAngle, RotationDirection direction)
{
segments().append(PathArc { point, radius, startAngle, endAngle, direction });
}
void PathStream::addRect(const FloatRect& rect)
{
segments().append(PathRect { rect });
}
void PathStream::addEllipse(const FloatPoint& point, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, RotationDirection direction)
{
segments().append(PathEllipse { point, radiusX, radiusY, rotation, startAngle, endAngle, direction });
}
void PathStream::addEllipseInRect(const FloatRect& rect)
{
segments().append(PathEllipseInRect { rect });
}
void PathStream::addRoundedRect(const FloatRoundedRect& roundedRect, PathRoundedRect::Strategy strategy)
{
segments().append(PathRoundedRect { roundedRect, strategy });
}
void PathStream::closeSubpath()
{
segments().append(PathCloseSubpath { });
}
const Vector<PathSegment>& PathStream::segments() const
{
return m_segmentsData->segments;
}
void PathStream::applySegments(const PathSegmentApplier& applier) const
{
for (auto& segment : m_segmentsData->segments)
applier(segment);
}
bool PathStream::applyElements(const PathElementApplier& applier) const
{
for (auto& segment : m_segmentsData->segments) {
if (!segment.canApplyElements())
return false;
}
for (auto& segment : m_segmentsData->segments)
segment.applyElements(applier);
return true;
}
bool PathStream::transform(const AffineTransform& transform)
{
for (auto& segment : m_segmentsData->segments) {
if (!segment.canTransform())
return false;
}
for (auto& segment : m_segmentsData.access().segments)
segment.transform(transform);
return true;
}
std::optional<PathSegment> PathStream::singleSegment() const
{
if (m_segmentsData->segments.size() != 1)
return std::nullopt;
return m_segmentsData->segments.first();
}
template<class DataType>
std::optional<DataType> PathStream::singleDataType() const
{
const auto segment = singleSegment();
if (!segment)
return std::nullopt;
const auto data = std::get_if<DataType>(&segment->data());
if (!data)
return std::nullopt;
return *data;
}
std::optional<PathDataLine> PathStream::singleDataLine() const
{
return singleDataType<PathDataLine>();
}
std::optional<PathArc> PathStream::singleArc() const
{
return singleDataType<PathArc>();
}
std::optional<PathDataQuadCurve> PathStream::singleQuadCurve() const
{
return singleDataType<PathDataQuadCurve>();
}
std::optional<PathDataBezierCurve> PathStream::singleBezierCurve() const
{
return singleDataType<PathDataBezierCurve>();
}
bool PathStream::isClosed() const
{
if (isEmpty())
return false;
return m_segmentsData->segments.last().isCloseSubPath();
}
FloatPoint PathStream::currentPoint() const
{
FloatPoint lastMoveToPoint;
FloatPoint currentPoint;
for (auto& segment : m_segmentsData->segments)
currentPoint = segment.calculateEndPoint(currentPoint, lastMoveToPoint);
return currentPoint;
}
FloatRect PathStream::computeFastBoundingRect(std::span<const PathSegment> segments)
{
FloatPoint lastMoveToPoint;
FloatPoint currentPoint;
FloatRect boundingRect = FloatRect::smallestRect();
for (auto& segment : segments) {
segment.extendFastBoundingRect(currentPoint, lastMoveToPoint, boundingRect);
currentPoint = segment.calculateEndPoint(currentPoint, lastMoveToPoint);
}
if (boundingRect.isSmallest())
boundingRect.extend(currentPoint);
return boundingRect;
}
FloatRect PathStream::fastBoundingRect() const
{
return computeFastBoundingRect(m_segmentsData->segments.span());
}
FloatRect PathStream::computeBoundingRect(std::span<const PathSegment> segments)
{
FloatPoint lastMoveToPoint;
FloatPoint currentPoint;
FloatRect boundingRect = FloatRect::smallestRect();
for (auto& segment : segments) {
segment.extendBoundingRect(currentPoint, lastMoveToPoint, boundingRect);
currentPoint = segment.calculateEndPoint(currentPoint, lastMoveToPoint);
}
if (boundingRect.isSmallest())
boundingRect.extend(currentPoint);
return boundingRect;
}
FloatRect PathStream::boundingRect() const
{
return computeBoundingRect(m_segmentsData->segments.span());
}
} // namespace WebCore
|