KHtml

PathQt.cpp
1 /*
2  * Copyright (C) 2006 Zack Rusin <[email protected]>
3  * 2006 Rob Buis <[email protected]>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "Path.h"
30 
31 #include "FloatRect.h"
32 #include "PlatformString.h"
33 #include "AffineTransform.h"
34 #include <QPainterPath>
35 #include <QMatrix>
36 #include <QString>
37 
38 #define _USE_MATH_DEFINES
39 #include <math.h>
40 
41 namespace WebCore
42 {
43 
44 Path::Path()
45  : m_path(new QPainterPath())
46 {
47 }
48 
49 Path::~Path()
50 {
51  delete m_path;
52 }
53 
54 Path::Path(const Path &other)
55  : m_path(new QPainterPath(*other.platformPath()))
56 {
57 }
58 
59 Path &Path::operator=(const Path &other)
60 {
61  if (&other != this) {
62  delete m_path;
63  m_path = new QPainterPath(*other.platformPath());
64  }
65 
66  return *this;
67 }
68 
69 bool Path::contains(const FloatPoint &point, WindRule rule) const
70 {
71  Qt::FillRule savedRule = m_path->fillRule();
72  m_path->setFillRule(rule == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);
73 
74  bool contains = m_path->contains(point);
75 
76  m_path->setFillRule(savedRule);
77  return contains;
78 }
79 
80 void Path::translate(const FloatSize &size)
81 {
82  QMatrix matrix;
83  matrix.translate(size.width(), size.height());
84  *m_path = (*m_path) * matrix;
85 }
86 
87 FloatRect Path::boundingRect() const
88 {
89  return m_path->boundingRect();
90 }
91 
92 void Path::moveTo(const FloatPoint &point)
93 {
94  m_path->moveTo(point);
95 }
96 
97 void Path::addLineTo(const FloatPoint &p)
98 {
99  m_path->lineTo(p);
100 }
101 
102 void Path::addQuadCurveTo(const FloatPoint &cp, const FloatPoint &p)
103 {
104  m_path->quadTo(cp, p);
105 }
106 
107 void Path::addBezierCurveTo(const FloatPoint &cp1, const FloatPoint &cp2, const FloatPoint &p)
108 {
109  m_path->cubicTo(cp1, cp2, p);
110 }
111 
112 void Path::addArcTo(const FloatPoint &p1, const FloatPoint &p2, float radius)
113 {
114  //FIXME: busted
115  qWarning("arcTo is busted");
116  m_path->arcTo(p1.x(), p1.y(), p2.x(), p2.y(), radius, 90);
117 }
118 
119 void Path::closeSubpath()
120 {
121  m_path->closeSubpath();
122 }
123 
124 #define DEGREES(t) ((t) * 180.0 / M_PI)
125 void Path::addArc(const FloatPoint &p, float r, float sar, float ear, bool anticlockwise)
126 {
127  qreal xc = p.x();
128  qreal yc = p.y();
129  qreal radius = r;
130 
131  //### HACK
132  // In Qt we don't switch the coordinate system for degrees
133  // and still use the 0,0 as bottom left for degrees so we need
134  // to switch
135  sar = -sar;
136  ear = -ear;
137  anticlockwise = !anticlockwise;
138  //end hack
139 
140  float sa = DEGREES(sar);
141  float ea = DEGREES(ear);
142 
143  double span = 0;
144 
145  double xs = xc - radius;
146  double ys = yc - radius;
147  double width = radius * 2;
148  double height = radius * 2;
149 
150  if (!anticlockwise && (ea < sa)) {
151  span += 360;
152  } else if (anticlockwise && (sa < ea)) {
153  span -= 360;
154  }
155 
156  // this is also due to switched coordinate system
157  // we would end up with a 0 span instead of 360
158  if (!(qFuzzyCompare(span + (ea - sa), 0.0) &&
159  qFuzzyCompare(qAbs(span), 360.0))) {
160  span += ea - sa;
161  }
162 
163  m_path->moveTo(QPointF(xc + radius * cos(sar),
164  yc - radius * sin(sar)));
165 
166  m_path->arcTo(xs, ys, width, height, sa, span);
167 }
168 
169 void Path::addRect(const FloatRect &r)
170 {
171  m_path->addRect(r.x(), r.y(), r.width(), r.height());
172 }
173 
174 void Path::addEllipse(const FloatRect &r)
175 {
176  m_path->addEllipse(r.x(), r.y(), r.width(), r.height());
177 }
178 
179 void Path::clear()
180 {
181  *m_path = QPainterPath();
182 }
183 
184 bool Path::isEmpty() const
185 {
186  return m_path->isEmpty();
187 }
188 
189 String Path::debugString() const
190 {
191  QString ret;
192  for (int i = 0; i < m_path->elementCount(); ++i) {
193  const QPainterPath::Element &cur = m_path->elementAt(i);
194 
195  switch (cur.type) {
197  ret += QString("M %1 %2").arg(cur.x).arg(cur.y);
198  break;
200  ret += QString("L %1 %2").arg(cur.x).arg(cur.y);
201  break;
203  const QPainterPath::Element &c1 = m_path->elementAt(i + 1);
204  const QPainterPath::Element &c2 = m_path->elementAt(i + 2);
205 
206  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
207  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
208 
209  ret += QString("C %1 %2 %3 %4 %5 %6").arg(cur.x).arg(cur.y).arg(c1.x).arg(c1.y).arg(c2.x).arg(c2.y);
210 
211  i += 2;
212  break;
213  }
215  Q_ASSERT(false);
216  break;
217  }
218  }
219 
220  return ret;
221 }
222 
223 void Path::apply(void *info, PathApplierFunction function) const
224 {
225  PathElement pelement;
226  FloatPoint points[3];
227  pelement.points = points;
228  for (int i = 0; i < m_path->elementCount(); ++i) {
229  const QPainterPath::Element &cur = m_path->elementAt(i);
230 
231  switch (cur.type) {
233  pelement.type = PathElementMoveToPoint;
234  pelement.points[0] = QPointF(cur);
235  function(info, &pelement);
236  break;
238  pelement.type = PathElementAddLineToPoint;
239  pelement.points[0] = QPointF(cur);
240  function(info, &pelement);
241  break;
243  const QPainterPath::Element &c1 = m_path->elementAt(i + 1);
244  const QPainterPath::Element &c2 = m_path->elementAt(i + 2);
245 
246  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
247  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
248 
249  pelement.type = PathElementAddCurveToPoint;
250  pelement.points[0] = QPointF(cur);
251  pelement.points[1] = QPointF(c1);
252  pelement.points[2] = QPointF(c2);
253  function(info, &pelement);
254 
255  i += 2;
256  break;
257  }
259  Q_ASSERT(false);
260  }
261  }
262 }
263 
264 void Path::transform(const AffineTransform &transform)
265 {
266  if (m_path) {
267  QMatrix mat = transform;
268  QPainterPath temp = mat.map(*m_path);
269  delete m_path;
270  m_path = new QPainterPath(temp);
271  }
272 }
273 
274 }
275 
The CSSNamespaceRule interface represents an.
Definition: css_rule.h:455
FillRule
Qt::FillRule fillRule() const const
QMatrix & translate(qreal dx, qreal dy)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void map(int x, int y, int *tx, int *ty) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:06 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.