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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
|
/*
* Copyright (C) 2024 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 THE COPYRIGHT HOLDER "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 THE COPYRIGHT HOLDER 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 "BasicShapesShape.h"
#include "AnimationUtilities.h"
#include "BasicShapeConversion.h"
#include "CalculationValue.h"
#include "FloatRect.h"
#include "FloatRoundedRect.h"
#include "LengthFunctions.h"
#include "Path.h"
#include "RenderBox.h"
#include "SVGPathBuilder.h"
#include "SVGPathByteStreamSource.h"
#include "SVGPathParser.h"
#include "SVGPathSegList.h"
#include "SVGPathSegValue.h"
#include "SVGPathSource.h"
#include <wtf/MathExtras.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
class ShapeSVGPathSource final : public SVGPathSource {
public:
explicit ShapeSVGPathSource(const LengthPoint& startPoint, const BasicShapeShape& shape, const FloatSize& boxSize)
: m_start(startPoint)
, m_shape(shape)
, m_boxSize(boxSize)
, m_endIndex(shape.segments().size())
{
}
private:
bool hasMoreData() const override
{
return m_nextIndex < m_endIndex;
}
bool moveToNextToken() override { return true; }
SVGPathSegType nextCommand(SVGPathSegType) override
{
auto type = segmentTypeAtIndex(m_nextIndex);
++m_nextIndex;
return type;
}
std::optional<SVGPathSegType> parseSVGSegmentType() override
{
// This represents the initial move to to set the "from" position.
ASSERT(!m_nextIndex);
return SVGPathSegType::MoveToAbs;
}
std::optional<MoveToSegment> parseMoveToSegment() override
{
if (!m_nextIndex)
return MoveToSegment { floatPointForLengthPoint(m_start, m_boxSize) };
auto& moveSegment = currentValue<ShapeMoveSegment>();
return MoveToSegment { floatPointForLengthPoint(moveSegment.offset(), m_boxSize) };
}
std::optional<LineToSegment> parseLineToSegment() override
{
auto& lineSegment = currentValue<ShapeLineSegment>();
return LineToSegment { floatPointForLengthPoint(lineSegment.offset(), m_boxSize) };
}
std::optional<LineToHorizontalSegment> parseLineToHorizontalSegment() override
{
auto& lineSegment = currentValue<ShapeHorizontalLineSegment>();
return LineToHorizontalSegment { floatValueForLength(lineSegment.length(), m_boxSize.width()) };
}
std::optional<LineToVerticalSegment> parseLineToVerticalSegment() override
{
auto& lineSegment = currentValue<ShapeVerticalLineSegment>();
return LineToVerticalSegment { floatValueForLength(lineSegment.length(), m_boxSize.height()) };
}
std::optional<CurveToCubicSegment> parseCurveToCubicSegment() override
{
auto& curveSegment = currentValue<ShapeCurveSegment>();
ASSERT(curveSegment.controlPoint2());
return CurveToCubicSegment {
floatPointForLengthPoint(curveSegment.controlPoint1(), m_boxSize),
floatPointForLengthPoint(curveSegment.controlPoint2().value(), m_boxSize),
floatPointForLengthPoint(curveSegment.offset(), m_boxSize)
};
}
std::optional<CurveToQuadraticSegment> parseCurveToQuadraticSegment() override
{
auto& curveSegment = currentValue<ShapeCurveSegment>();
ASSERT(!curveSegment.controlPoint2());
return CurveToQuadraticSegment {
floatPointForLengthPoint(curveSegment.controlPoint1(), m_boxSize),
floatPointForLengthPoint(curveSegment.offset(), m_boxSize)
};
}
std::optional<CurveToCubicSmoothSegment> parseCurveToCubicSmoothSegment() override
{
auto& smoothSegment = currentValue<ShapeSmoothSegment>();
ASSERT(smoothSegment.intermediatePoint());
return CurveToCubicSmoothSegment {
floatPointForLengthPoint(smoothSegment.intermediatePoint().value(), m_boxSize),
floatPointForLengthPoint(smoothSegment.offset(), m_boxSize)
};
}
std::optional<CurveToQuadraticSmoothSegment> parseCurveToQuadraticSmoothSegment() override
{
auto& smoothSegment = currentValue<ShapeSmoothSegment>();
return CurveToQuadraticSmoothSegment { floatPointForLengthPoint(smoothSegment.offset(), m_boxSize) };
}
std::optional<ArcToSegment> parseArcToSegment() override
{
auto& arcSegment = currentValue<ShapeArcSegment>();
auto radius = floatSizeForLengthSize(arcSegment.ellipseSize(), m_boxSize);
return ArcToSegment {
.rx = radius.width(),
.ry = radius.height(),
.angle = narrowPrecisionToFloat(arcSegment.angle()),
.largeArc = arcSegment.arcSize() == ShapeArcSegment::ArcSize::Large,
.sweep = arcSegment.sweep() == RotationDirection::Clockwise,
.targetPoint = floatPointForLengthPoint(arcSegment.offset(), m_boxSize)
};
}
SVGPathSegType segmentTypeAtIndex(size_t index) const
{
if (index >= m_shape.segments().size())
return SVGPathSegType::Unknown;
auto& segment = m_shape.segments()[index];
return WTF::switchOn(segment,
[&](const ShapeMoveSegment& segment) {
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::MoveToAbs : SVGPathSegType::MoveToRel;
},
[&](const ShapeLineSegment& segment) {
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::LineToAbs : SVGPathSegType::LineToRel;
},
[&](const ShapeHorizontalLineSegment& segment) {
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::LineToHorizontalAbs : SVGPathSegType::LineToHorizontalRel;
},
[&](const ShapeVerticalLineSegment& segment) {
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::LineToVerticalAbs : SVGPathSegType::LineToVerticalRel;
},
[&](const ShapeCurveSegment& segment) {
if (segment.controlPoint2())
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::CurveToCubicAbs : SVGPathSegType::CurveToCubicRel;
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::CurveToQuadraticAbs : SVGPathSegType::CurveToQuadraticRel;
},
[&](const ShapeSmoothSegment& segment) {
if (segment.intermediatePoint())
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::CurveToCubicSmoothAbs : SVGPathSegType::CurveToCubicSmoothRel;
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::CurveToQuadraticSmoothAbs : SVGPathSegType::CurveToQuadraticSmoothRel;
},
[&](const ShapeArcSegment& segment) {
return segment.affinity() == CoordinateAffinity::Absolute ? SVGPathSegType::ArcAbs : SVGPathSegType::ArcRel;
},
[&](const ShapeCloseSegment&) {
return SVGPathSegType::ClosePath;
}
);
}
template<typename T>
const T& currentValue() const
{
ASSERT(m_nextIndex);
ASSERT(m_nextIndex <= m_shape.segments().size());
return std::get<T>(m_shape.segments()[m_nextIndex - 1]);
}
const LengthPoint& m_start;
const BasicShapeShape& m_shape;
FloatSize m_boxSize;
size_t m_endIndex { 0 };
size_t m_nextIndex { 0 };
};
// MARK: -
class ShapeConversionPathConsumer final : public SVGPathConsumer {
public:
ShapeConversionPathConsumer(Vector<BasicShapeShape::ShapeSegment>& segmentList)
: m_segmentList(segmentList)
{ }
const std::optional<CoordinatePair>& initialMove() const { return m_initialMove; }
private:
static CoordinateAffinity fromCoordinateMode(PathCoordinateMode mode)
{
switch (mode) {
case AbsoluteCoordinates:
return CoordinateAffinity::Absolute;
case RelativeCoordinates:
return CoordinateAffinity::Relative;
}
return CoordinateAffinity::Absolute;
}
static CoordinatePair fromPoint(FloatPoint p)
{
return { Length(p.x(), LengthType::Fixed), Length(p.y(), LengthType::Fixed) };
}
void incrementPathSegmentCount() override
{
}
bool continueConsuming() override
{
return true;
}
void moveTo(const FloatPoint& offsetPoint, bool, PathCoordinateMode mode) override
{
if (m_segmentList.isEmpty() && mode == PathCoordinateMode::AbsoluteCoordinates && !m_initialMove) {
m_initialMove = fromPoint(offsetPoint);
return;
}
auto offset = fromPoint(offsetPoint);
m_segmentList.append(ShapeMoveSegment(fromCoordinateMode(mode), WTFMove(offset)));
}
void lineTo(const FloatPoint& offsetPoint, PathCoordinateMode mode) override
{
auto offset = fromPoint(offsetPoint);
m_segmentList.append(ShapeLineSegment(fromCoordinateMode(mode), WTFMove(offset)));
}
void lineToHorizontal(float len, PathCoordinateMode mode) override
{
auto length = Length(len, LengthType::Fixed);
m_segmentList.append(ShapeHorizontalLineSegment(fromCoordinateMode(mode), WTFMove(length)));
}
void lineToVertical(float len, PathCoordinateMode mode) override
{
auto length = Length(len, LengthType::Fixed);
m_segmentList.append(ShapeVerticalLineSegment(fromCoordinateMode(mode), WTFMove(length)));
}
void curveToCubic(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& offsetPoint, PathCoordinateMode mode) override
{
auto offset = fromPoint(offsetPoint);
auto c1 = fromPoint(controlPoint1);
auto c2 = fromPoint(controlPoint2);
m_segmentList.append(ShapeCurveSegment(fromCoordinateMode(mode), WTFMove(offset), WTFMove(c1), WTFMove(c2)));
}
void curveToQuadratic(const FloatPoint& controlPoint, const FloatPoint& offsetPoint, PathCoordinateMode mode) override
{
auto offset = fromPoint(offsetPoint);
auto cp = fromPoint(controlPoint);
m_segmentList.append(ShapeCurveSegment(fromCoordinateMode(mode), WTFMove(offset), WTFMove(cp)));
}
void curveToCubicSmooth(const FloatPoint& controlPoint2, const FloatPoint& offsetPoint, PathCoordinateMode mode) override
{
auto offset = fromPoint(offsetPoint);
auto c2 = fromPoint(controlPoint2);
m_segmentList.append(ShapeSmoothSegment(fromCoordinateMode(mode), WTFMove(offset), WTFMove(c2)));
}
void curveToQuadraticSmooth(const FloatPoint& offsetPoint, PathCoordinateMode mode) override
{
auto offset = fromPoint(offsetPoint);
m_segmentList.append(ShapeSmoothSegment(fromCoordinateMode(mode), WTFMove(offset)));
}
void arcTo(float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, const FloatPoint& offsetPoint, PathCoordinateMode mode) override
{
auto offset = fromPoint(offsetPoint);
auto ellipseSize = LengthSize { Length(r1, LengthType::Fixed), Length(r2, LengthType::Fixed) };
auto sweep = sweepFlag ? RotationDirection::Clockwise : RotationDirection::Counterclockwise;
auto arcSize = largeArcFlag ? ShapeArcSegment::ArcSize::Large : ShapeArcSegment::ArcSize::Small;
m_segmentList.append(ShapeArcSegment(fromCoordinateMode(mode), WTFMove(offset), WTFMove(ellipseSize), sweep, arcSize, angle));
}
void closePath() override
{
m_segmentList.append(ShapeCloseSegment());
}
Vector<BasicShapeShape::ShapeSegment>& m_segmentList;
std::optional<CoordinatePair> m_initialMove;
};
// MARK: -
Ref<BasicShapeShape> BasicShapeShape::create(WindRule windRule, const CoordinatePair& startPoint, Vector<ShapeSegment>&& commands)
{
return adoptRef(* new BasicShapeShape(windRule, startPoint, WTFMove(commands)));
}
RefPtr<BasicShapeShape> BasicShapeShape::createFromPath(const BasicShapePath& path)
{
auto* pathData = path.pathData();
if (!pathData)
return nullptr;
// FIXME: Isn't not totally clear how to convert a initial Move command to the Shape's "from" parameter.
// https://github.com/w3c/csswg-drafts/issues/10740
Vector<ShapeSegment> shapeCommands;
ShapeConversionPathConsumer converter(shapeCommands);
SVGPathByteStreamSource source(*pathData);
if (!SVGPathParser::parse(source, converter, UnalteredParsing))
return nullptr;
return adoptRef(* new BasicShapeShape(path.windRule(), converter.initialMove().value_or(CoordinatePair { }), WTFMove(shapeCommands)));
}
BasicShapeShape::BasicShapeShape(WindRule windRule, const CoordinatePair& startPoint, Vector<ShapeSegment>&& commands)
: m_startPoint(startPoint)
, m_windRule(windRule)
, m_segments(WTFMove(commands))
{
}
Ref<BasicShape> BasicShapeShape::clone() const
{
auto segmentsCopy = m_segments;
return BasicShapeShape::create(windRule(), startPoint(), WTFMove(segmentsCopy));
}
Path BasicShapeShape::path(const FloatRect& referenceRect) const
{
// FIXME: We should do some caching here.
auto pathSource = ShapeSVGPathSource(m_startPoint, *this, referenceRect.size());
Path path;
SVGPathBuilder builder(path);
SVGPathParser::parse(pathSource, builder);
path.translate(toFloatSize(referenceRect.location()));
return path;
}
// https://drafts.csswg.org/css-shapes-2/#interpolating-shape
bool BasicShapeShape::canBlend(const ShapeSegment& segment1, const ShapeSegment& segment2)
{
// For now, just check the types. There's some discussion about allowing blending between different segment types.
if (segment1.index() != segment2.index())
return false;
return WTF::switchOn(segment1,
[&](const ShapeMoveSegment& segment) {
const auto& otherSegment = get<ShapeMoveSegment>(segment2);
return segment.affinity() == otherSegment.affinity();
},
[&](const ShapeLineSegment& segment) {
const auto& otherSegment = get<ShapeLineSegment>(segment2);
return segment.affinity() == otherSegment.affinity();
},
[&](const ShapeHorizontalLineSegment& segment) {
const auto& otherSegment = get<ShapeHorizontalLineSegment>(segment2);
return segment.affinity() == otherSegment.affinity();
},
[&](const ShapeVerticalLineSegment& segment) {
const auto& otherSegment = get<ShapeVerticalLineSegment>(segment2);
return segment.affinity() == otherSegment.affinity();
},
[&](const ShapeCurveSegment& segment) {
const auto& otherSegment = get<ShapeCurveSegment>(segment2);
if (segment.affinity() != otherSegment.affinity())
return false;
return segment.controlPoint2().has_value() == otherSegment.controlPoint2().has_value();
},
[&](const ShapeSmoothSegment& segment) {
const auto& otherSegment = get<ShapeSmoothSegment>(segment2);
return segment.intermediatePoint().has_value() == otherSegment.intermediatePoint().has_value();
},
[&](const ShapeArcSegment& segment) {
const auto& otherSegment = get<ShapeArcSegment>(segment2);
return segment.affinity() == otherSegment.affinity();
},
[&](const ShapeCloseSegment&) {
return true;
}
);
ASSERT_NOT_REACHED();
return false;
}
template <typename T>
T blendWithPreferredValue(const T& from, const T& to, const T& preferredValue, const BlendingContext& context)
{
if (context.progress <= 0)
return from;
if (context.progress >= 1)
return to;
if (from == to)
return from;
return preferredValue;
}
auto BasicShapeShape::blend(const ShapeSegment& fromSegment, const ShapeSegment& toSegment, const BlendingContext& context) -> ShapeSegment
{
ASSERT(fromSegment.index() == toSegment.index());
ASSERT(canBlend(fromSegment, toSegment));
return WTF::switchOn(fromSegment,
[&](const ShapeMoveSegment& fromSegment) {
const auto& toMoveSegment = get<ShapeMoveSegment>(toSegment);
auto result = fromSegment;
result.setOffset(WebCore::blend(fromSegment.offset(), toMoveSegment.offset(), context));
return ShapeSegment(result);
},
[&](const ShapeLineSegment& fromSegment) {
const auto& toLineSegment = get<ShapeLineSegment>(toSegment);
auto result = fromSegment;
result.setOffset(WebCore::blend(fromSegment.offset(), toLineSegment.offset(), context));
return ShapeSegment(result);
},
[&](const ShapeHorizontalLineSegment& fromSegment) {
const auto& toHLineSegment = get<ShapeHorizontalLineSegment>(toSegment);
auto result = fromSegment;
result.setLength(WebCore::blend(fromSegment.length(), toHLineSegment.length(), context));
return ShapeSegment(result);
},
[&](const ShapeVerticalLineSegment& fromSegment) {
const auto& toVLineSegment = get<ShapeVerticalLineSegment>(toSegment);
auto result = fromSegment;
result.setLength(WebCore::blend(fromSegment.length(), toVLineSegment.length(), context));
return ShapeSegment(result);
},
[&](const ShapeCurveSegment& fromSegment) {
const auto& toCurveSegment = get<ShapeCurveSegment>(toSegment);
auto result = fromSegment;
result.setOffset(WebCore::blend(fromSegment.offset(), toCurveSegment.offset(), context));
result.setControlPoint1(WebCore::blend(fromSegment.controlPoint1(), toCurveSegment.controlPoint1(), context));
if (fromSegment.controlPoint2()) {
ASSERT(toCurveSegment.controlPoint2().has_value());
result.setControlPoint2(WebCore::blend(fromSegment.controlPoint2().value(), toCurveSegment.controlPoint2().value(), context));
}
return ShapeSegment(result);
},
[&](const ShapeSmoothSegment& fromSegment) {
const auto& toSmoothSegment = get<ShapeSmoothSegment>(toSegment);
auto result = fromSegment;
result.setOffset(WebCore::blend(fromSegment.offset(), toSmoothSegment.offset(), context));
if (fromSegment.intermediatePoint()) {
ASSERT(toSmoothSegment.intermediatePoint().has_value());
result.setIntermediatePoint(WebCore::blend(fromSegment.intermediatePoint().value(), toSmoothSegment.intermediatePoint().value(), context));
}
return ShapeSegment(result);
},
[&](const ShapeArcSegment& fromSegment) {
const auto& toArcSegment = get<ShapeArcSegment>(toSegment);
auto result = fromSegment;
result.setOffset(WebCore::blend(fromSegment.offset(), toArcSegment.offset(), context));
result.setEllipseSize(WebCore::blend(fromSegment.ellipseSize(), toArcSegment.ellipseSize(), context));
result.setAngle(WebCore::blend(fromSegment.angle(), toArcSegment.angle(), context));
// /If an arc command has different <arc-sweep> between its starting and ending list,
// then the interpolated result uses cw for any progress value between 0 and 1.
// If it has different <arc-size> keywords, then the interpolated result uses large
// for any progress value between 0 and 1.
result.setSweep(blendWithPreferredValue(fromSegment.sweep(), toArcSegment.sweep(), RotationDirection::Clockwise, context));
result.setArcSize(blendWithPreferredValue(fromSegment.arcSize(), toArcSegment.arcSize(), ShapeArcSegment::ArcSize::Large, context));
return ShapeSegment(result);
},
[&](const ShapeCloseSegment& fromSegment) {
return ShapeSegment(fromSegment);
}
);
ASSERT_NOT_REACHED();
return ShapeCloseSegment();
}
bool BasicShapeShape::canBlend(const BasicShape& other) const
{
if (other.type() == Type::Path)
return canBlendWithPath(downcast<BasicShapePath>(other));
if (other.type() != type())
return false;
const auto& otherShape = downcast<BasicShapeShape>(other);
if (otherShape.windRule() != windRule())
return false;
if (otherShape.segments().size() != segments().size())
return false;
for (size_t i = 0; i < segments().size(); ++i) {
const auto& thisSegment = segments()[i];
const auto& otherSegment = otherShape.segments()[i];
if (!canBlend(thisSegment, otherSegment))
return false;
}
return true;
}
// https://drafts.csswg.org/css-shapes-2/#interpolating-shape
bool BasicShapeShape::canBlendWithPath(const BasicShapePath& path) const
{
if (path.windRule() != windRule())
return false;
RefPtr pathAsShape = BasicShapeShape::createFromPath(path);
return pathAsShape && canBlend(*pathAsShape);
}
Ref<BasicShape> BasicShapeShape::blend(const BasicShape& from, const BlendingContext& context) const
{
if (from.type() == Type::Path)
return BasicShapeShape::blendWithPath(from, *this, context);
ASSERT(type() == from.type());
const auto& fromShape = downcast<BasicShapeShape>(from);
auto startPoint = WebCore::blend(fromShape.startPoint(), this->startPoint(), context);
auto segmentsCopy = m_segments;
Ref result = BasicShapeShape::create(windRule(), startPoint, WTFMove(segmentsCopy));
ASSERT(fromShape.segments().size() == segments().size());
for (size_t i = 0; i < segments().size(); ++i) {
const auto& toSegment = result->segments()[i];
const auto& fromSegment = fromShape.segments()[i];
result->segments()[i] = blend(fromSegment, toSegment, context);
}
return result;
}
Ref<BasicShape> BasicShapeShape::blendWithPath(const BasicShape& from, const BasicShape& to, const BlendingContext& context)
{
if (from.type() == Type::Path) {
RefPtr fromAsShape = BasicShapeShape::createFromPath(downcast<BasicShapePath>(from));
if (!fromAsShape)
return to.clone();
return to.blend(*fromAsShape, context);
}
ASSERT(to.type() == Type::Path);
RefPtr toAsShape = BasicShapeShape::createFromPath(downcast<BasicShapePath>(to));
if (!toAsShape)
return from.clone();
return toAsShape->blend(from, context);
}
bool BasicShapeShape::operator==(const BasicShape& other) const
{
if (type() != other.type())
return false;
const auto& otherShape = downcast<BasicShapeShape>(other);
if (windRule() != otherShape.windRule())
return false;
if (startPoint() != otherShape.startPoint())
return false;
return segments() == otherShape.segments();
}
void BasicShapeShape::dump(TextStream& stream) const
{
stream.dumpProperty("wind rule", windRule());
stream.dumpProperty("start point", startPoint());
stream << segments();
}
TextStream& operator<<(TextStream& stream, const BasicShapeShape::ShapeSegment& segment)
{
WTF::switchOn(segment,
[&](const ShapeMoveSegment& segment) {
stream << "move" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.offset();
},
[&](const ShapeLineSegment& segment) {
stream << "line" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.offset();
},
[&](const ShapeHorizontalLineSegment& segment) {
stream << "hline" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.length();
},
[&](const ShapeVerticalLineSegment& segment) {
stream << "vline" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.length();
},
[&](const ShapeCurveSegment& segment) {
stream << "curve" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.offset() << " using " << segment.controlPoint1();
if (segment.controlPoint2())
stream << " " << segment.controlPoint2().value();
},
[&](const ShapeSmoothSegment& segment) {
stream << "smooth" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.offset();
if (segment.intermediatePoint())
stream << " using " << segment.intermediatePoint().value();
},
[&](const ShapeArcSegment& segment) {
stream << "arc" << (segment.affinity() == CoordinateAffinity::Relative ? " by "_s : " to "_s) << segment.offset() << " of " << segment.ellipseSize();
stream << " " << segment.sweep() << (segment.arcSize() == ShapeArcSegment::ArcSize::Small ? " small " : " large ") << " " << segment.angle() << "deg";
},
[&](const ShapeCloseSegment&) {
stream << "close";
}
);
return stream;
}
} // namespace WebCore
|