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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* 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"
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(PathStream);
Ref<PathStream> PathStream::create()
{
return adoptRef(*new PathStream);
}
Ref<PathStream> PathStream::create(PathSegment&& segment)
{
return adoptRef(*new PathStream(WTFMove(segment)));
}
Ref<PathStream> PathStream::create(Vector<PathSegment>&& segments)
{
return adoptRef(*new PathStream(WTFMove(segments)));
}
Ref<PathStream> PathStream::create(const Vector<PathSegment>& segments)
{
return adoptRef(*new PathStream(segments));
}
Ref<PathStream> PathStream::create(const Vector<FloatPoint>& points)
{
auto stream = PathStream::create();
if (points.size() < 2)
return stream;
stream->add(PathMoveTo { points[0] });
for (size_t i = 1; i < points.size(); ++i)
stream->add(PathLineTo { points[i] });
stream->add(PathCloseSubpath { });
return stream;
}
PathStream::PathStream(Vector<PathSegment>&& segments)
: m_segments(WTFMove(segments))
{
}
PathStream::PathStream(const Vector<PathSegment>& segments)
: m_segments(segments)
{
}
PathStream::PathStream(PathSegment&& segment)
: m_segments({ WTFMove(segment) })
{
}
bool PathStream::definitelyEqual(const PathImpl& other) const
{
RefPtr otherAsPathStream = dynamicDowncast<PathStream>(other);
if (!otherAsPathStream)
return false;
if (otherAsPathStream.get() == this)
return true;
return segments() == otherAsPathStream->segments();
}
Ref<PathImpl> PathStream::copy() const
{
return create(m_segments);
}
const PathMoveTo* PathStream::lastIfMoveTo() const
{
if (isEmpty())
return nullptr;
return std::get_if<PathMoveTo>(&m_segments.last().data());
}
void PathStream::add(PathMoveTo moveTo)
{
segments().append(moveTo);
}
void PathStream::add(PathLineTo lineTo)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataLine { moveTo->point, lineTo.point } };
else
segments().append(lineTo);
}
void PathStream::add(PathQuadCurveTo quadTo)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataQuadCurve { moveTo->point, quadTo.controlPoint, quadTo.endPoint } };
else
segments().append(quadTo);
}
void PathStream::add(PathBezierCurveTo bezierTo)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataBezierCurve { moveTo->point, bezierTo.controlPoint1, bezierTo.controlPoint2, bezierTo.endPoint } };
else
segments().append(bezierTo);
}
void PathStream::add(PathArcTo arcTo)
{
if (const auto* moveTo = lastIfMoveTo())
segments().last() = { PathDataArc { moveTo->point, arcTo.controlPoint1, arcTo.controlPoint2, arcTo.radius } };
else
segments().append(arcTo);
}
void PathStream::add(PathArc arc)
{
segments().append(arc);
}
void PathStream::add(PathClosedArc closedArc)
{
segments().append(closedArc);
}
void PathStream::add(PathRect rect)
{
segments().append(rect);
}
void PathStream::add(PathEllipse ellipse)
{
segments().append(ellipse);
}
void PathStream::add(PathEllipseInRect ellipseInRect)
{
segments().append(ellipseInRect);
}
void PathStream::add(PathRoundedRect roundedRect)
{
segments().append(roundedRect);
}
void PathStream::add(PathContinuousRoundedRect continuousRoundedRect)
{
segments().append(continuousRoundedRect);
}
void PathStream::add(PathCloseSubpath)
{
segments().append(PathCloseSubpath { });
}
void PathStream::applySegments(const PathSegmentApplier& applier) const
{
for (auto& segment : m_segments)
applier(segment);
}
bool PathStream::applyElements(const PathElementApplier& applier) const
{
for (auto& segment : m_segments) {
if (!segment.canApplyElements())
return false;
}
for (auto& segment : m_segments)
segment.applyElements(applier);
return true;
}
bool PathStream::transform(const AffineTransform& transform)
{
for (auto& segment : m_segments) {
if (!segment.canTransform())
return false;
}
for (auto& segment : m_segments)
segment.transform(transform);
return true;
}
std::optional<PathSegment> PathStream::singleSegment() const
{
if (m_segments.size() != 1)
return std::nullopt;
return m_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<PathRect> PathStream::singleRect() const
{
return singleDataType<PathRect>();
}
std::optional<PathRoundedRect> PathStream::singleRoundedRect() const
{
return singleDataType<PathRoundedRect>();
}
std::optional<PathContinuousRoundedRect> PathStream::singleContinuousRoundedRect() const
{
return singleDataType<PathContinuousRoundedRect>();
}
std::optional<PathArc> PathStream::singleArc() const
{
return singleDataType<PathArc>();
}
std::optional<PathClosedArc> PathStream::singleClosedArc() const
{
return singleDataType<PathClosedArc>();
}
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_segments.last().closesSubpath();
}
FloatPoint PathStream::currentPoint() const
{
if (m_segments.isEmpty())
return { };
if (auto result = m_segments.last().tryGetEndPointWithoutContext())
return result.value();
FloatPoint lastMoveToPoint;
FloatPoint currentPoint;
for (auto& segment : m_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;
}
bool PathStream::computeHasSubpaths(std::span<const PathSegment> segments)
{
FloatPoint lastMoveToPoint;
FloatPoint currentPoint;
FloatRect boundingRect = FloatRect::smallestRect();
for (auto& segment : segments) {
segment.extendFastBoundingRect(currentPoint, lastMoveToPoint, boundingRect);
if (!boundingRect.isSmallest() && (boundingRect.height() || boundingRect.width()))
return true;
currentPoint = segment.calculateEndPoint(currentPoint, lastMoveToPoint);
}
if (boundingRect.isSmallest())
boundingRect.extend(currentPoint);
return boundingRect.height() || boundingRect.width();
}
bool PathStream::hasSubpaths() const
{
return computeHasSubpaths(m_segments.span());
}
FloatRect PathStream::fastBoundingRect() const
{
return computeFastBoundingRect(m_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_segments.span());
}
} // namespace WebCore
|