KHTML
SVGResourceClipperQt.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 "SVGResourceClipper.h"
00029
00030
00031
00032 #include <QPainter>
00033 #include <QPainterPath>
00034
00035 #include "FloatRect.h"
00036
00037 namespace WebCore {
00038
00039 void SVGResourceClipper::applyClip(QPainter* painter, const FloatRect& boundingBox) const
00040 {
00041 if (m_clipData.clipData().size() < 1)
00042 return;
00043
00044
00045
00046
00047 QPainterPath newPath;
00048
00049 bool heterogenousClipRules = false;
00050 WindRule clipRule = m_clipData.clipData()[0].windRule;
00051
00052 unsigned int clipDataCount = m_clipData.clipData().size();
00053 for (unsigned int x = 0; x < clipDataCount; x++) {
00054 ClipData clipData = m_clipData.clipData()[x];
00055 if (clipData.windRule != clipRule)
00056 heterogenousClipRules = true;
00057
00058 QPainterPath path = *(clipData.path.platformPath());
00059 if (path.isEmpty())
00060 continue;
00061
00062 if (!newPath.isEmpty())
00063 newPath.closeSubpath();
00064
00065
00066 QMatrix transform;
00067
00068 if (clipData.bboxUnits) {
00069 transform.translate(boundingBox.x(), boundingBox.y());
00070 transform.scale(boundingBox.width(), boundingBox.height());
00071 }
00072
00073
00074
00075
00076 for (int i = 0; i < path.elementCount(); ++i) {
00077 const QPainterPath::Element &cur = path.elementAt(i);
00078
00079 switch (cur.type) {
00080 case QPainterPath::MoveToElement:
00081 newPath.moveTo(QPointF(cur.x, cur.y) * transform);
00082 break;
00083 case QPainterPath::LineToElement:
00084 newPath.lineTo(QPointF(cur.x, cur.y) * transform);
00085 break;
00086 case QPainterPath::CurveToElement:
00087 {
00088 const QPainterPath::Element &c1 = path.elementAt(i + 1);
00089 const QPainterPath::Element &c2 = path.elementAt(i + 2);
00090
00091 Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
00092 Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
00093
00094 newPath.cubicTo(QPointF(cur.x, cur.y) * transform,
00095 QPointF(c1.x, c1.y) * transform,
00096 QPointF(c2.x, c2.y) * transform);
00097
00098 i += 2;
00099 break;
00100 }
00101 case QPainterPath::CurveToDataElement:
00102 Q_ASSERT(false);
00103 break;
00104 }
00105 }
00106 }
00107
00108 if (m_clipData.clipData().size()) {
00109
00110
00111
00112
00113
00114 if (clipRule == RULE_EVENODD)
00115 newPath.setFillRule(Qt::OddEvenFill);
00116 else
00117 newPath.setFillRule(Qt::WindingFill);
00118
00119 }
00120
00121
00122
00123
00124 painter->setClipPath(newPath);
00125 }
00126
00127 }
00128
00129 #endif
00130
00131