KHtml

qt/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 using namespace WebCore;
42 
43 namespace khtml
44 {
45 
46 Path::Path()
47  : m_path(new QPainterPath())
48 {
49 }
50 
51 Path::~Path()
52 {
53  delete m_path;
54 }
55 
56 Path::Path(const Path &other)
57  : m_path(new QPainterPath(*other.platformPath()))
58 {
59 }
60 
61 Path &Path::operator=(const Path &other)
62 {
63  if (&other != this) {
64  delete m_path;
65  m_path = new QPainterPath(*other.platformPath());
66  }
67 
68  return *this;
69 }
70 
71 bool Path::contains(const FloatPoint &point, WindRule rule) const
72 {
73  Qt::FillRule savedRule = m_path->fillRule();
74  m_path->setFillRule(rule == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);
75 
76  bool contains = m_path->contains(point);
77 
78  m_path->setFillRule(savedRule);
79  return contains;
80 }
81 
82 void Path::translate(const FloatSize &size)
83 {
84  QMatrix matrix;
85  matrix.translate(size.width(), size.height());
86  *m_path = (*m_path) * matrix;
87 }
88 
89 FloatRect Path::boundingRect() const
90 {
91  return m_path->boundingRect();
92 }
93 
94 void Path::moveTo(const FloatPoint &point)
95 {
96  m_path->moveTo(point);
97 }
98 
99 void Path::addLineTo(const FloatPoint &p)
100 {
101  m_path->lineTo(p);
102 }
103 
104 void Path::addQuadCurveTo(const FloatPoint &cp, const FloatPoint &p)
105 {
106  m_path->quadTo(cp, p);
107 }
108 
109 void Path::addBezierCurveTo(const FloatPoint &cp1, const FloatPoint &cp2, const FloatPoint &p)
110 {
111  m_path->cubicTo(cp1, cp2, p);
112 }
113 
114 void Path::addArcTo(const FloatPoint &p1, const FloatPoint &p2, float radius)
115 {
116  //FIXME: busted
117  qWarning("arcTo is busted");
118  m_path->arcTo(p1.x(), p1.y(), p2.x(), p2.y(), radius, 90);
119 }
120 
121 void Path::closeSubpath()
122 {
123  m_path->closeSubpath();
124 }
125 
126 #define DEGREES(t) ((t) * 180.0 / M_PI)
127 void Path::addArc(const FloatPoint &p, float r, float sar, float ear, bool anticlockwise)
128 {
129  qreal xc = p.x();
130  qreal yc = p.y();
131  qreal radius = r;
132 
133  //### HACK
134  // In Qt we don't switch the coordinate system for degrees
135  // and still use the 0,0 as bottom left for degrees so we need
136  // to switch
137  sar = -sar;
138  ear = -ear;
139  anticlockwise = !anticlockwise;
140  //end hack
141 
142  float sa = DEGREES(sar);
143  float ea = DEGREES(ear);
144 
145  double span = 0;
146 
147  double xs = xc - radius;
148  double ys = yc - radius;
149  double width = radius * 2;
150  double height = radius * 2;
151 
152  if (!anticlockwise && (ea < sa)) {
153  span += 360;
154  } else if (anticlockwise && (sa < ea)) {
155  span -= 360;
156  }
157 
158  // this is also due to switched coordinate system
159  // we would end up with a 0 span instead of 360
160  if (!(qFuzzyCompare(span + (ea - sa), 0.0) &&
161  qFuzzyCompare(qAbs(span), 360.0))) {
162  span += ea - sa;
163  }
164 
165  m_path->moveTo(QPointF(xc + radius * cos(sar),
166  yc - radius * sin(sar)));
167 
168  m_path->arcTo(xs, ys, width, height, sa, span);
169 }
170 
171 void Path::addRect(const FloatRect &r)
172 {
173  m_path->addRect(r.x(), r.y(), r.width(), r.height());
174 }
175 
176 void Path::addEllipse(const FloatRect &r)
177 {
178  m_path->addEllipse(r.x(), r.y(), r.width(), r.height());
179 }
180 
181 void Path::clear()
182 {
183  *m_path = QPainterPath();
184 }
185 
186 bool Path::isEmpty() const
187 {
188  return m_path->isEmpty();
189 }
190 
191 DOM::DOMString Path::debugString() const
192 {
193  QString ret;
194  for (int i = 0; i < m_path->elementCount(); ++i) {
195  const QPainterPath::Element &cur = m_path->elementAt(i);
196 
197  switch (cur.type) {
199  ret += QString("M %1 %2").arg(cur.x).arg(cur.y);
200  break;
202  ret += QString("L %1 %2").arg(cur.x).arg(cur.y);
203  break;
205  const QPainterPath::Element &c1 = m_path->elementAt(i + 1);
206  const QPainterPath::Element &c2 = m_path->elementAt(i + 2);
207 
208  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
209  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
210 
211  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);
212 
213  i += 2;
214  break;
215  }
217  Q_ASSERT(false);
218  break;
219  }
220  }
221 
222  return ret;
223 }
224 
225 void Path::apply(void *info, PathApplierFunction function) const
226 {
227  PathElement pelement;
228  FloatPoint points[3];
229  pelement.points = points;
230  for (int i = 0; i < m_path->elementCount(); ++i) {
231  const QPainterPath::Element &cur = m_path->elementAt(i);
232 
233  switch (cur.type) {
235  pelement.type = PathElementMoveToPoint;
236  pelement.points[0] = QPointF(cur);
237  function(info, &pelement);
238  break;
240  pelement.type = PathElementAddLineToPoint;
241  pelement.points[0] = QPointF(cur);
242  function(info, &pelement);
243  break;
245  const QPainterPath::Element &c1 = m_path->elementAt(i + 1);
246  const QPainterPath::Element &c2 = m_path->elementAt(i + 2);
247 
248  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
249  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
250 
251  pelement.type = PathElementAddCurveToPoint;
252  pelement.points[0] = QPointF(cur);
253  pelement.points[1] = QPointF(c1);
254  pelement.points[2] = QPointF(c2);
255  function(info, &pelement);
256 
257  i += 2;
258  break;
259  }
261  Q_ASSERT(false);
262  }
263  }
264 }
265 
266 void Path::transform(const AffineTransform &transform)
267 {
268  if (m_path) {
269  QMatrix mat = transform;
270  QPainterPath temp = mat.map(*m_path);
271  delete m_path;
272  m_path = new QPainterPath(temp);
273  }
274 }
275 
276 }
277 
The CSSNamespaceRule interface represents an.
Definition: css_rule.h:455
This file is part of the HTML rendering engine for KDE.
FillRule
Qt::FillRule fillRule() const const
QMatrix & translate(qreal dx, qreal dy)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
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 Sat Oct 16 2021 22:47:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.