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
|
/*
* Copyright (C) 2003-2023 Apple Inc. All rights reserved.
* Copyright (C) 2006 Rob Buis <buis@kde.org>
* Copyright (C) 2007-2008 Torch Mobile, Inc.
*
* 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.
*/
#pragma once
#include "PathElement.h"
#include "PathImpl.h"
#include "PathSegment.h"
#include "PlatformPath.h"
#include "WindRule.h"
#include <wtf/DataRef.h>
#include <wtf/TZoneMalloc.h>
namespace WebCore {
class GraphicsContext;
class LayoutRoundedRect;
class PathTraversalState;
class Path {
WTF_MAKE_TZONE_ALLOCATED(Path);
public:
Path() = default;
Path(PathSegment&&);
WEBCORE_EXPORT Path(Vector<PathSegment>&&);
explicit Path(const Vector<FloatPoint>& points);
Path(Ref<PathImpl>&&);
Path(const Path&) = default;
Path(Path&&) = default;
Path& operator=(const Path&) = default;
Path& operator=(Path&&) = default;
WEBCORE_EXPORT bool definitelyEqual(const Path&) const;
void moveTo(const FloatPoint&);
void addLineTo(const FloatPoint&);
void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
void addArcTo(const FloatPoint& point1, const FloatPoint& point2, float radius);
void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, RotationDirection);
void addEllipse(const FloatPoint&, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, RotationDirection);
void addEllipseInRect(const FloatRect&);
void addRect(const FloatRect&);
WEBCORE_EXPORT void addRoundedRect(const FloatRoundedRect&, PathRoundedRect::Strategy = PathRoundedRect::Strategy::PreferNative);
void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii, PathRoundedRect::Strategy = PathRoundedRect::Strategy::PreferNative);
void addContinuousRoundedRect(const FloatRect&, float cornerRadius);
void addContinuousRoundedRect(const FloatRect&, float cornerWidth, float cornerHeight);
void addRoundedRect(const LayoutRoundedRect&);
void closeSubpath();
WEBCORE_EXPORT void addPath(const Path&, const AffineTransform&);
void applySegments(const PathSegmentApplier&) const;
WEBCORE_EXPORT void applyElements(const PathElementApplier&) const;
void clear();
void translate(const FloatSize& delta);
void transform(const AffineTransform&);
static constexpr float circleControlPoint() { return PathImpl::circleControlPoint(); }
WEBCORE_EXPORT std::optional<PathSegment> singleSegment() const;
std::optional<PathDataLine> singleDataLine() const;
bool isEmpty() const;
bool definitelySingleLine() const;
WEBCORE_EXPORT PlatformPathPtr platformPath() const;
const PathSegment* singleSegmentIfExists() const { return asSingle(); }
WEBCORE_EXPORT const Vector<PathSegment>* segmentsIfExists() const;
WEBCORE_EXPORT Vector<PathSegment> segments() const;
float length() const;
bool isClosed() const;
bool hasSubpaths() const;
FloatPoint currentPoint() const;
PathTraversalState traversalStateAtLength(float length) const;
FloatPoint pointAtLength(float length) const;
WEBCORE_EXPORT bool contains(const FloatPoint&, WindRule = WindRule::NonZero) const;
WEBCORE_EXPORT bool strokeContains(const FloatPoint&, NOESCAPE const Function<void(GraphicsContext&)>& strokeStyleApplier) const;
WEBCORE_EXPORT FloatRect fastBoundingRect() const;
WEBCORE_EXPORT FloatRect boundingRect() const;
FloatRect strokeBoundingRect(NOESCAPE const Function<void(GraphicsContext&)>& strokeStyleApplier = { }) const;
WEBCORE_EXPORT void ensureImplForTesting();
private:
PlatformPathImpl& ensurePlatformPathImpl();
PathImpl& setImpl(Ref<PathImpl>&&);
WEBCORE_EXPORT PathImpl& ensureImpl();
WEBCORE_EXPORT Ref<PathImpl> ensureProtectedImpl();
PathSegment* asSingle() { return std::get_if<PathSegment>(&m_data); }
const PathSegment* asSingle() const { return std::get_if<PathSegment>(&m_data); }
RefPtr<PathImpl> asImpl();
RefPtr<const PathImpl> asImpl() const;
std::optional<FloatPoint> initialMoveToPoint() const;
Variant<std::monostate, PathSegment, DataRef<PathImpl>> m_data;
};
WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const Path&);
inline Path::Path(PathSegment&& segment)
: m_data(WTFMove(segment))
{
}
inline bool Path::isEmpty() const
{
return std::holds_alternative<std::monostate>(m_data);
}
inline void Path::moveTo(const FloatPoint& point)
{
if (isEmpty())
m_data = PathSegment(PathMoveTo { point });
else
ensureProtectedImpl()->add(PathMoveTo { point });
}
inline void Path::addLineTo(const FloatPoint& point)
{
if (auto initial = initialMoveToPoint())
m_data = PathSegment(PathDataLine { *initial, point });
else
ensureProtectedImpl()->add(PathLineTo { point });
}
inline void Path::addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint)
{
if (auto initial = initialMoveToPoint())
m_data = PathSegment(PathDataQuadCurve { *initial, controlPoint, endPoint });
else
ensureProtectedImpl()->add(PathQuadCurveTo { controlPoint, endPoint });
}
inline void Path::addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint)
{
if (auto initial = initialMoveToPoint())
m_data = PathSegment(PathDataBezierCurve { *initial, controlPoint1, controlPoint2, endPoint });
else
ensureProtectedImpl()->add(PathBezierCurveTo { controlPoint1, controlPoint2, endPoint });
}
inline void Path::addArcTo(const FloatPoint& point1, const FloatPoint& point2, float radius)
{
if (auto initial = initialMoveToPoint())
m_data = PathSegment(PathDataArc { *initial, point1, point2, radius });
else
ensureProtectedImpl()->add(PathArcTo { point1, point2, radius });
}
inline void Path::addArc(const FloatPoint& point, float radius, float startAngle, float endAngle, RotationDirection direction)
{
// Workaround for <rdar://problem/5189233> CGPathAddArc hangs or crashes when passed inf as start or end angle,
// as well as http://bugs.webkit.org/show_bug.cgi?id=16449, since cairo_arc() functions hang or crash when
// passed inf as radius or start/end angle.
if (!std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
return;
if (isEmpty())
m_data = PathSegment(PathArc { point, radius, startAngle, endAngle, direction });
else
ensureProtectedImpl()->add(PathArc { point, radius, startAngle, endAngle, direction });
}
inline void Path::addEllipse(const FloatPoint& point, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, RotationDirection direction)
{
if (isEmpty())
m_data = PathSegment(PathEllipse { point, radiusX, radiusY, rotation, startAngle, endAngle, direction });
else
ensureProtectedImpl()->add(PathEllipse { point, radiusX, radiusY, rotation, startAngle, endAngle, direction });
}
inline void Path::addEllipseInRect(const FloatRect& rect)
{
if (isEmpty())
m_data = PathSegment(PathEllipseInRect { rect });
else
ensureProtectedImpl()->add(PathEllipseInRect { rect });
}
inline void Path::addRect(const FloatRect& rect)
{
if (isEmpty())
m_data = PathSegment(PathRect { rect });
else
ensureProtectedImpl()->add(PathRect { rect });
}
inline void Path::closeSubpath()
{
if (std::holds_alternative<std::monostate>(m_data))
return;
if (const auto* segment = std::get_if<PathSegment>(&m_data)) {
const auto* data = &segment->data();
if (auto* arc = std::get_if<PathArc>(data)) {
m_data = PathSegment(PathClosedArc { *arc });
return;
}
if (std::holds_alternative<PathCloseSubpath>(*data))
return;
}
auto impl = ensureProtectedImpl();
if (impl->isClosed())
return;
impl->add(PathCloseSubpath { });
}
inline FloatPoint Path::currentPoint() const
{
if (std::holds_alternative<std::monostate>(m_data))
return { 0, 0 };
if (const auto* segment = std::get_if<PathSegment>(&m_data)) {
FloatPoint lastMoveToPoint;
return segment->calculateEndPoint({ }, lastMoveToPoint);
}
return asImpl()->currentPoint();
}
inline std::optional<FloatPoint> Path::initialMoveToPoint() const
{
if (std::holds_alternative<std::monostate>(m_data))
return FloatPoint { 0, 0 };
if (const auto* segment = std::get_if<PathSegment>(&m_data)) {
if (const auto* moveTo = std::get_if<PathMoveTo>(&segment->data()))
return moveTo->point;
}
return std::nullopt;
}
} // namespace WebCore
|