KHtml

SVGPathSegList.cpp
1 /*
2  Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <[email protected]>
3  2004, 2005 Rob Buis <[email protected]>
4  Copyright (C) 2007 Eric Seidel <[email protected]>
5 
6  This file is part of the WebKit project
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #include "wtf/Platform.h"
25 
26 #if ENABLE(SVG)
27 #include "SVGPathSegList.h"
28 
29 #include "FloatPoint.h"
30 #include "Path.h"
31 #include "PathTraversalState.h"
32 #include "SVGPathSegMoveto.h"
33 #include "SVGPathSegLineto.h"
34 #include "SVGPathSegCurvetoCubic.h"
35 
36 namespace WebCore
37 {
38 
39 SVGPathSegList::SVGPathSegList(const QualifiedName &attributeName)
40  : SVGList<RefPtr<SVGPathSeg> >(attributeName)
41 {
42 }
43 
44 SVGPathSegList::~SVGPathSegList()
45 {
46 }
47 
48 unsigned SVGPathSegList::getPathSegAtLength(double)
49 {
50  // FIXME : to be useful this will need to support non-normalized SVGPathSegLists
51  ExceptionCode ec = 0;
52  int len = numberOfItems();
53  // FIXME: Eventually this will likely move to a "path applier"-like model, until then PathTraversalState is less useful as we could just use locals
54  PathTraversalState traversalState(PathTraversalState::TraversalSegmentAtLength);
55  for (int i = 0; i < len; ++i) {
56  SVGPathSeg *segment = getItem(i, ec).get();
57  float segmentLength = 0;
58  switch (segment->pathSegType()) {
59  case SVGPathSeg::PATHSEG_MOVETO_ABS: {
60  SVGPathSegMovetoAbs *moveTo = static_cast<SVGPathSegMovetoAbs *>(segment);
61  segmentLength = traversalState.moveTo(FloatPoint(moveTo->x(), moveTo->y()));
62  break;
63  }
64  case SVGPathSeg::PATHSEG_LINETO_ABS: {
65  SVGPathSegLinetoAbs *lineTo = static_cast<SVGPathSegLinetoAbs *>(segment);
66  segmentLength = traversalState.lineTo(FloatPoint(lineTo->x(), lineTo->y()));
67  break;
68  }
69  case SVGPathSeg::PATHSEG_CURVETO_CUBIC_ABS: {
70  SVGPathSegCurvetoCubicAbs *curveTo = static_cast<SVGPathSegCurvetoCubicAbs *>(segment);
71  segmentLength = traversalState.cubicBezierTo(FloatPoint(curveTo->x1(), curveTo->y1()),
72  FloatPoint(curveTo->x2(), curveTo->y2()),
73  FloatPoint(curveTo->x(), curveTo->y()));
74  break;
75  }
76  case SVGPathSeg::PATHSEG_CLOSEPATH:
77  segmentLength = traversalState.closeSubpath();
78  break;
79  default:
80  ASSERT(false); // FIXME: This only works with normalized/processed path data.
81  break;
82  }
83  traversalState.m_totalLength += segmentLength;
84  if ((traversalState.m_action == PathTraversalState::TraversalSegmentAtLength)
85  && (traversalState.m_totalLength > traversalState.m_desiredLength)) {
86  return traversalState.m_segmentIndex;
87  }
88  traversalState.m_segmentIndex++;
89  }
90 
91  return 0; // The SVG spec is unclear as to what to return when the distance is not on the path
92 }
93 
94 khtml::Path SVGPathSegList::toPathData()
95 {
96  // FIXME : This should also support non-normalized PathSegLists
97  Path pathData;
98  ExceptionCode ec = 0;
99  int len = numberOfItems();
100  for (int i = 0; i < len; ++i) {
101  SVGPathSeg *segment = getItem(i, ec).get();
102  switch (segment->pathSegType()) {
103  case SVGPathSeg::PATHSEG_MOVETO_ABS: {
104  SVGPathSegMovetoAbs *moveTo = static_cast<SVGPathSegMovetoAbs *>(segment);
105  pathData.moveTo(FloatPoint(moveTo->x(), moveTo->y()));
106  break;
107  }
108  case SVGPathSeg::PATHSEG_LINETO_ABS: {
109  SVGPathSegLinetoAbs *lineTo = static_cast<SVGPathSegLinetoAbs *>(segment);
110  pathData.addLineTo(FloatPoint(lineTo->x(), lineTo->y()));
111  break;
112  }
113  case SVGPathSeg::PATHSEG_CURVETO_CUBIC_ABS: {
114  SVGPathSegCurvetoCubicAbs *curveTo = static_cast<SVGPathSegCurvetoCubicAbs *>(segment);
115  pathData.addBezierCurveTo(FloatPoint(curveTo->x1(), curveTo->y1()),
116  FloatPoint(curveTo->x2(), curveTo->y2()),
117  FloatPoint(curveTo->x(), curveTo->y()));
118  break;
119  }
120  case SVGPathSeg::PATHSEG_CLOSEPATH:
121  pathData.closeSubpath();
122  break;
123  default:
124  ASSERT(false); // FIXME: This only works with normalized/processed path data.
125  break;
126  }
127  }
128 
129  return pathData;
130 }
131 
132 }
133 
134 #endif // ENABLE(SVG)
QString getItem(const QString &caption, const QString &label, const QStringList &list, int current=0, bool editable=false, bool *ok=nullptr, QWidget *parent=nullptr)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:48:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.