Skip to content

Commit

Permalink
Use an enum class for SVGPathSegType
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=260930

Reviewed by Ryosuke Niwa.

Use an enum class for SVGPathSegType instead of an old enum.
Use uint8_t as underlying type and update SVGPathByteStreamBuilder
& SVGPathByteStreamSource to encode/decode it as a uint8_t instead
of an `unsigned short`.

* Source/WebCore/svg/SVGPathBlender.cpp:
(WebCore::coordinateModeOfCommand):
(WebCore::isSegmentEqual):
(WebCore::SVGPathBlender::canBlendPaths):
(WebCore::SVGPathBlender::blendAnimatedPath):
* Source/WebCore/svg/SVGPathByteStreamBuilder.cpp:
(WebCore::SVGPathByteStreamBuilder::moveTo):
(WebCore::SVGPathByteStreamBuilder::lineTo):
(WebCore::SVGPathByteStreamBuilder::lineToHorizontal):
(WebCore::SVGPathByteStreamBuilder::lineToVertical):
(WebCore::SVGPathByteStreamBuilder::curveToCubic):
(WebCore::SVGPathByteStreamBuilder::curveToCubicSmooth):
(WebCore::SVGPathByteStreamBuilder::curveToQuadratic):
(WebCore::SVGPathByteStreamBuilder::curveToQuadraticSmooth):
(WebCore::SVGPathByteStreamBuilder::arcTo):
(WebCore::SVGPathByteStreamBuilder::closePath):
* Source/WebCore/svg/SVGPathByteStreamBuilder.h:
* Source/WebCore/svg/SVGPathParser.cpp:
(WebCore::SVGPathParser::parseCurveToCubicSmoothSegment):
(WebCore::SVGPathParser::parseCurveToQuadraticSmoothSegment):
(WebCore::SVGPathParser::parsePathData):
* Source/WebCore/svg/SVGPathParser.h:
* Source/WebCore/svg/SVGPathSeg.h:
* Source/WebCore/svg/SVGPathSegListSource.cpp:
(WebCore::SVGPathSegListSource::parseMoveToSegment):
(WebCore::SVGPathSegListSource::parseLineToSegment):
(WebCore::SVGPathSegListSource::parseLineToHorizontalSegment):
(WebCore::SVGPathSegListSource::parseLineToVerticalSegment):
(WebCore::SVGPathSegListSource::parseCurveToCubicSegment):
(WebCore::SVGPathSegListSource::parseCurveToCubicSmoothSegment):
(WebCore::SVGPathSegListSource::parseCurveToQuadraticSegment):
(WebCore::SVGPathSegListSource::parseCurveToQuadraticSmoothSegment):
(WebCore::SVGPathSegListSource::parseArcToSegment):
* Source/WebCore/svg/SVGPathStringViewSource.cpp:
(WebCore::nextCommandHelper):
(WebCore::SVGPathStringViewSource::parseSVGSegmentType):

Canonical link: https://commits.webkit.org/267497@main
  • Loading branch information
cdumez committed Aug 31, 2023
1 parent 93f8745 commit f8ea32b
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 224 deletions.
40 changes: 20 additions & 20 deletions Source/WebCore/bindings/js/JSSVGPathSegCustom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,45 @@ using namespace JSC;
JSValue toJSNewlyCreated(JSGlobalObject*, JSDOMGlobalObject* globalObject, Ref<SVGPathSeg>&& object)
{
switch (object->pathSegType()) {
case SVGPathSeg::PATHSEG_CLOSEPATH:
case SVGPathSegType::ClosePath:
return createWrapper<SVGPathSegClosePath>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_MOVETO_ABS:
case SVGPathSegType::MoveToAbs:
return createWrapper<SVGPathSegMovetoAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_MOVETO_REL:
case SVGPathSegType::MoveToRel:
return createWrapper<SVGPathSegMovetoRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_LINETO_ABS:
case SVGPathSegType::LineToAbs:
return createWrapper<SVGPathSegLinetoAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_LINETO_REL:
case SVGPathSegType::LineToRel:
return createWrapper<SVGPathSegLinetoRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_CUBIC_ABS:
case SVGPathSegType::CurveToCubicAbs:
return createWrapper<SVGPathSegCurvetoCubicAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_CUBIC_REL:
case SVGPathSegType::CurveToCubicRel:
return createWrapper<SVGPathSegCurvetoCubicRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_QUADRATIC_ABS:
case SVGPathSegType::CurveToQuadraticAbs:
return createWrapper<SVGPathSegCurvetoQuadraticAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_QUADRATIC_REL:
case SVGPathSegType::CurveToQuadraticRel:
return createWrapper<SVGPathSegCurvetoQuadraticRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_ARC_ABS:
case SVGPathSegType::ArcAbs:
return createWrapper<SVGPathSegArcAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_ARC_REL:
case SVGPathSegType::ArcRel:
return createWrapper<SVGPathSegArcRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_LINETO_HORIZONTAL_ABS:
case SVGPathSegType::LineToHorizontalAbs:
return createWrapper<SVGPathSegLinetoHorizontalAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_LINETO_HORIZONTAL_REL:
case SVGPathSegType::LineToHorizontalRel:
return createWrapper<SVGPathSegLinetoHorizontalRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_LINETO_VERTICAL_ABS:
case SVGPathSegType::LineToVerticalAbs:
return createWrapper<SVGPathSegLinetoVerticalAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_LINETO_VERTICAL_REL:
case SVGPathSegType::LineToVerticalRel:
return createWrapper<SVGPathSegLinetoVerticalRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
case SVGPathSegType::CurveToCubicSmoothAbs:
return createWrapper<SVGPathSegCurvetoCubicSmoothAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
case SVGPathSegType::CurveToCubicSmoothRel:
return createWrapper<SVGPathSegCurvetoCubicSmoothRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
case SVGPathSegType::CurveToQuadraticSmoothAbs:
return createWrapper<SVGPathSegCurvetoQuadraticSmoothAbs>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
case SVGPathSegType::CurveToQuadraticSmoothRel:
return createWrapper<SVGPathSegCurvetoQuadraticSmoothRel>(globalObject, WTFMove(object));
case SVGPathSeg::PATHSEG_UNKNOWN:
case SVGPathSegType::Unknown:
default:
return createWrapper<SVGPathSeg>(globalObject, WTFMove(object));
}
Expand Down
90 changes: 45 additions & 45 deletions Source/WebCore/svg/SVGPathBlender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,23 @@ bool SVGPathBlender::blendArcToSegment(float progress)

static inline PathCoordinateMode coordinateModeOfCommand(const SVGPathSegType& type)
{
if (type < PathSegMoveToAbs)
if (type < SVGPathSegType::MoveToAbs)
return AbsoluteCoordinates;

// Odd number = relative command
if (type % 2)
if (enumToUnderlyingType(type) % 2)
return RelativeCoordinates;

return AbsoluteCoordinates;
}

static inline bool isSegmentEqual(const SVGPathSegType& fromType, const SVGPathSegType& toType, const PathCoordinateMode& fromMode, const PathCoordinateMode& toMode)
{
if (fromType == toType && (fromType == PathSegUnknown || fromType == PathSegClosePath))
if (fromType == toType && (fromType == SVGPathSegType::Unknown || fromType == SVGPathSegType::ClosePath))
return true;

unsigned short from = fromType;
unsigned short to = toType;
auto from = enumToUnderlyingType(fromType);
auto to = enumToUnderlyingType(toType);
if (fromMode == toMode)
return from == to;
if (fromMode == AbsoluteCoordinates)
Expand Down Expand Up @@ -375,54 +375,54 @@ bool SVGPathBlender::canBlendPaths()
return false;

switch (toCommand) {
case PathSegMoveToRel:
case PathSegMoveToAbs:
case SVGPathSegType::MoveToRel:
case SVGPathSegType::MoveToAbs:
if (!blendMoveToSegment(progress))
return false;
break;
case PathSegLineToRel:
case PathSegLineToAbs:
case SVGPathSegType::LineToRel:
case SVGPathSegType::LineToAbs:
if (!blendLineToSegment(progress))
return false;
break;
case PathSegLineToHorizontalRel:
case PathSegLineToHorizontalAbs:
case SVGPathSegType::LineToHorizontalRel:
case SVGPathSegType::LineToHorizontalAbs:
if (!blendLineToHorizontalSegment(progress))
return false;
break;
case PathSegLineToVerticalRel:
case PathSegLineToVerticalAbs:
case SVGPathSegType::LineToVerticalRel:
case SVGPathSegType::LineToVerticalAbs:
if (!blendLineToVerticalSegment(progress))
return false;
break;
case PathSegClosePath:
case SVGPathSegType::ClosePath:
break;
case PathSegCurveToCubicRel:
case PathSegCurveToCubicAbs:
case SVGPathSegType::CurveToCubicRel:
case SVGPathSegType::CurveToCubicAbs:
if (!blendCurveToCubicSegment(progress))
return false;
break;
case PathSegCurveToCubicSmoothRel:
case PathSegCurveToCubicSmoothAbs:
case SVGPathSegType::CurveToCubicSmoothRel:
case SVGPathSegType::CurveToCubicSmoothAbs:
if (!blendCurveToCubicSmoothSegment(progress))
return false;
break;
case PathSegCurveToQuadraticRel:
case PathSegCurveToQuadraticAbs:
case SVGPathSegType::CurveToQuadraticRel:
case SVGPathSegType::CurveToQuadraticAbs:
if (!blendCurveToQuadraticSegment(progress))
return false;
break;
case PathSegCurveToQuadraticSmoothRel:
case PathSegCurveToQuadraticSmoothAbs:
case SVGPathSegType::CurveToQuadraticSmoothRel:
case SVGPathSegType::CurveToQuadraticSmoothAbs:
if (!blendCurveToQuadraticSmoothSegment(progress))
return false;
break;
case PathSegArcRel:
case PathSegArcAbs:
case SVGPathSegType::ArcRel:
case SVGPathSegType::ArcAbs:
if (!blendArcToSegment(progress))
return false;
break;
case PathSegUnknown:
case SVGPathSegType::Unknown:
return false;
}

Expand Down Expand Up @@ -464,55 +464,55 @@ bool SVGPathBlender::blendAnimatedPath(float progress)
return false;

switch (toCommand) {
case PathSegMoveToRel:
case PathSegMoveToAbs:
case SVGPathSegType::MoveToRel:
case SVGPathSegType::MoveToAbs:
if (!blendMoveToSegment(progress))
return false;
break;
case PathSegLineToRel:
case PathSegLineToAbs:
case SVGPathSegType::LineToRel:
case SVGPathSegType::LineToAbs:
if (!blendLineToSegment(progress))
return false;
break;
case PathSegLineToHorizontalRel:
case PathSegLineToHorizontalAbs:
case SVGPathSegType::LineToHorizontalRel:
case SVGPathSegType::LineToHorizontalAbs:
if (!blendLineToHorizontalSegment(progress))
return false;
break;
case PathSegLineToVerticalRel:
case PathSegLineToVerticalAbs:
case SVGPathSegType::LineToVerticalRel:
case SVGPathSegType::LineToVerticalAbs:
if (!blendLineToVerticalSegment(progress))
return false;
break;
case PathSegClosePath:
case SVGPathSegType::ClosePath:
m_consumer->closePath();
break;
case PathSegCurveToCubicRel:
case PathSegCurveToCubicAbs:
case SVGPathSegType::CurveToCubicRel:
case SVGPathSegType::CurveToCubicAbs:
if (!blendCurveToCubicSegment(progress))
return false;
break;
case PathSegCurveToCubicSmoothRel:
case PathSegCurveToCubicSmoothAbs:
case SVGPathSegType::CurveToCubicSmoothRel:
case SVGPathSegType::CurveToCubicSmoothAbs:
if (!blendCurveToCubicSmoothSegment(progress))
return false;
break;
case PathSegCurveToQuadraticRel:
case PathSegCurveToQuadraticAbs:
case SVGPathSegType::CurveToQuadraticRel:
case SVGPathSegType::CurveToQuadraticAbs:
if (!blendCurveToQuadraticSegment(progress))
return false;
break;
case PathSegCurveToQuadraticSmoothRel:
case PathSegCurveToQuadraticSmoothAbs:
case SVGPathSegType::CurveToQuadraticSmoothRel:
case SVGPathSegType::CurveToQuadraticSmoothAbs:
if (!blendCurveToQuadraticSmoothSegment(progress))
return false;
break;
case PathSegArcRel:
case PathSegArcAbs:
case SVGPathSegType::ArcRel:
case SVGPathSegType::ArcAbs:
if (!blendArcToSegment(progress))
return false;
break;
case PathSegUnknown:
case SVGPathSegType::Unknown:
return false;
}

Expand Down
10 changes: 3 additions & 7 deletions Source/WebCore/svg/SVGPathByteStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,10 @@ typedef union {
unsigned char bytes[sizeof(float)];
} FloatByte;

typedef union {
unsigned short value;
unsigned char bytes[sizeof(unsigned short)];
} UnsignedShortByte;

class SVGPathByteStream {
WTF_MAKE_FAST_ALLOCATED;
public:
typedef Vector<unsigned char> Data;
typedef Vector<uint8_t> Data;
typedef Data::const_iterator DataIterator;

SVGPathByteStream() { }
Expand Down Expand Up @@ -96,7 +91,8 @@ class SVGPathByteStream {
DataIterator begin() const { return m_data.begin(); }
DataIterator end() const { return m_data.end(); }

void append(std::span<const unsigned char> bytes) { m_data.append(bytes); }
void append(uint8_t byte) { m_data.append(byte); }
void append(std::span<const uint8_t> bytes) { m_data.append(bytes); }
void append(const SVGPathByteStream& other) { m_data.appendVector(other.m_data); }
void clear() { m_data.clear(); }
bool isEmpty() const { return m_data.isEmpty(); }
Expand Down
20 changes: 10 additions & 10 deletions Source/WebCore/svg/SVGPathByteStreamBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,59 +34,59 @@ SVGPathByteStreamBuilder::SVGPathByteStreamBuilder(SVGPathByteStream& byteStream

void SVGPathByteStreamBuilder::moveTo(const FloatPoint& targetPoint, bool, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegMoveToRel : PathSegMoveToAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::MoveToRel : SVGPathSegType::MoveToAbs);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::lineTo(const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegLineToRel : PathSegLineToAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::LineToRel : SVGPathSegType::LineToAbs);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::lineToHorizontal(float x, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegLineToHorizontalRel : PathSegLineToHorizontalAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::LineToHorizontalRel : SVGPathSegType::LineToHorizontalAbs);
writeFloat(x);
}

void SVGPathByteStreamBuilder::lineToVertical(float y, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegLineToVerticalRel : PathSegLineToVerticalAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::LineToVerticalRel : SVGPathSegType::LineToVerticalAbs);
writeFloat(y);
}

void SVGPathByteStreamBuilder::curveToCubic(const FloatPoint& point1, const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToCubicRel : PathSegCurveToCubicAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToCubicRel : SVGPathSegType::CurveToCubicAbs);
writeFloatPoint(point1);
writeFloatPoint(point2);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::curveToCubicSmooth(const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToCubicSmoothRel : PathSegCurveToCubicSmoothAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToCubicSmoothRel : SVGPathSegType::CurveToCubicSmoothAbs);
writeFloatPoint(point2);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToQuadraticRel : PathSegCurveToQuadraticAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToQuadraticRel : SVGPathSegType::CurveToQuadraticAbs);
writeFloatPoint(point1);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::curveToQuadraticSmooth(const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToQuadraticSmoothRel : PathSegCurveToQuadraticSmoothAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToQuadraticSmoothRel : SVGPathSegType::CurveToQuadraticSmoothAbs);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::arcTo(float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? PathSegArcRel : PathSegArcAbs);
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::ArcRel : SVGPathSegType::ArcAbs);
writeFloat(r1);
writeFloat(r2);
writeFloat(angle);
Expand All @@ -97,7 +97,7 @@ void SVGPathByteStreamBuilder::arcTo(float r1, float r2, float angle, bool large

void SVGPathByteStreamBuilder::closePath()
{
writeSegmentType(PathSegClosePath);
writeSegmentType(SVGPathSegType::ClosePath);
}

} // namespace WebCore
9 changes: 5 additions & 4 deletions Source/WebCore/svg/SVGPathByteStreamBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace WebCore {

enum class SVGPathSegType : uint8_t;

class SVGPathByteStreamBuilder final : public SVGPathConsumer {
public:
SVGPathByteStreamBuilder(SVGPathByteStream&);
Expand Down Expand Up @@ -75,11 +77,10 @@ class SVGPathByteStreamBuilder final : public SVGPathConsumer {
writeFloat(point.y());
}

void writeSegmentType(unsigned short value)
void writeSegmentType(SVGPathSegType type)
{
UnsignedShortByte data;
data.value = value;
writeType(data);
static_assert(std::is_same_v<std::underlying_type_t<SVGPathSegType>, uint8_t>);
m_byteStream.append(static_cast<uint8_t>(type));
}

SVGPathByteStream& m_byteStream;
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/svg/SVGPathByteStreamSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ bool SVGPathByteStreamSource::hasMoreData() const

SVGPathSegType SVGPathByteStreamSource::nextCommand(SVGPathSegType)
{
return static_cast<SVGPathSegType>(readSVGSegmentType());
return readSVGSegmentType();
}

std::optional<SVGPathSegType> SVGPathByteStreamSource::parseSVGSegmentType()
{
return static_cast<SVGPathSegType>(readSVGSegmentType());
return readSVGSegmentType();
}

std::optional<SVGPathSource::MoveToSegment> SVGPathByteStreamSource::parseMoveToSegment()
Expand Down
Loading

0 comments on commit f8ea32b

Please sign in to comment.