KHTML
SVGPathSegList.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025 #include "wtf/Platform.h"
00026
00027 #if ENABLE(SVG)
00028 #include "SVGPathSegList.h"
00029
00030 #include "FloatPoint.h"
00031 #include "Path.h"
00032 #include "PathTraversalState.h"
00033 #include "SVGPathSegMoveto.h"
00034 #include "SVGPathSegLineto.h"
00035 #include "SVGPathSegCurvetoCubic.h"
00036
00037 namespace WebCore {
00038
00039 SVGPathSegList::SVGPathSegList(const QualifiedName& attributeName)
00040 : SVGList<RefPtr<SVGPathSeg> >(attributeName)
00041 {
00042 }
00043
00044 SVGPathSegList::~SVGPathSegList()
00045 {
00046 }
00047
00048 unsigned SVGPathSegList::getPathSegAtLength(double)
00049 {
00050
00051 ExceptionCode ec = 0;
00052 int len = numberOfItems();
00053
00054 PathTraversalState traversalState(PathTraversalState::TraversalSegmentAtLength);
00055 for (int i = 0; i < len; ++i) {
00056 SVGPathSeg* segment = getItem(i, ec).get();
00057 float segmentLength = 0;
00058 switch (segment->pathSegType()) {
00059 case SVGPathSeg::PATHSEG_MOVETO_ABS:
00060 {
00061 SVGPathSegMovetoAbs* moveTo = static_cast<SVGPathSegMovetoAbs*>(segment);
00062 segmentLength = traversalState.moveTo(FloatPoint(moveTo->x(), moveTo->y()));
00063 break;
00064 }
00065 case SVGPathSeg::PATHSEG_LINETO_ABS:
00066 {
00067 SVGPathSegLinetoAbs* lineTo = static_cast<SVGPathSegLinetoAbs*>(segment);
00068 segmentLength = traversalState.lineTo(FloatPoint(lineTo->x(), lineTo->y()));
00069 break;
00070 }
00071 case SVGPathSeg::PATHSEG_CURVETO_CUBIC_ABS:
00072 {
00073 SVGPathSegCurvetoCubicAbs* curveTo = static_cast<SVGPathSegCurvetoCubicAbs*>(segment);
00074 segmentLength = traversalState.cubicBezierTo(FloatPoint(curveTo->x1(), curveTo->y1()),
00075 FloatPoint(curveTo->x2(), curveTo->y2()),
00076 FloatPoint(curveTo->x(), curveTo->y()));
00077 break;
00078 }
00079 case SVGPathSeg::PATHSEG_CLOSEPATH:
00080 segmentLength = traversalState.closeSubpath();
00081 break;
00082 default:
00083 ASSERT(false);
00084 break;
00085 }
00086 traversalState.m_totalLength += segmentLength;
00087 if ((traversalState.m_action == PathTraversalState::TraversalSegmentAtLength)
00088 && (traversalState.m_totalLength > traversalState.m_desiredLength)) {
00089 return traversalState.m_segmentIndex;
00090 }
00091 traversalState.m_segmentIndex++;
00092 }
00093
00094 return 0;
00095 }
00096
00097 khtml::Path SVGPathSegList::toPathData()
00098 {
00099
00100 Path pathData;
00101 ExceptionCode ec = 0;
00102 int len = numberOfItems();
00103 for (int i = 0; i < len; ++i) {
00104 SVGPathSeg* segment = getItem(i, ec).get();;
00105 switch (segment->pathSegType())
00106 {
00107 case SVGPathSeg::PATHSEG_MOVETO_ABS:
00108 {
00109 SVGPathSegMovetoAbs* moveTo = static_cast<SVGPathSegMovetoAbs*>(segment);
00110 pathData.moveTo(FloatPoint(moveTo->x(), moveTo->y()));
00111 break;
00112 }
00113 case SVGPathSeg::PATHSEG_LINETO_ABS:
00114 {
00115 SVGPathSegLinetoAbs* lineTo = static_cast<SVGPathSegLinetoAbs*>(segment);
00116 pathData.addLineTo(FloatPoint(lineTo->x(), lineTo->y()));
00117 break;
00118 }
00119 case SVGPathSeg::PATHSEG_CURVETO_CUBIC_ABS:
00120 {
00121 SVGPathSegCurvetoCubicAbs* curveTo = static_cast<SVGPathSegCurvetoCubicAbs*>(segment);
00122 pathData.addBezierCurveTo(FloatPoint(curveTo->x1(), curveTo->y1()),
00123 FloatPoint(curveTo->x2(), curveTo->y2()),
00124 FloatPoint(curveTo->x(), curveTo->y()));
00125 break;
00126 }
00127 case SVGPathSeg::PATHSEG_CLOSEPATH:
00128 pathData.closeSubpath();
00129 break;
00130 default:
00131 ASSERT(false);
00132 break;
00133 }
00134 }
00135
00136 return pathData;
00137 }
00138
00139 }
00140
00141 #endif // ENABLE(SVG)