File: SVGResourceClipperQt.cpp

package info (click to toggle)
webkit 1.0.1-4%2Blenny2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 68,876 kB
  • ctags: 66,869
  • sloc: cpp: 369,003; ansic: 20,095; perl: 13,548; objc: 13,474; yacc: 2,562; python: 1,092; sh: 611; ruby: 405; makefile: 140; xml: 10
file content (127 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <wildfox@kde.org>
                  2004, 2005, 2006 Rob Buis <buis@kde.org>
                  2005 Apple Computer, Inc.

    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"

#if ENABLE(SVG)
#include "SVGResourceClipper.h"

#include "GraphicsContext.h"

#include <QPainter>
#include <QPainterPath>

namespace WebCore {

void SVGResourceClipper::applyClip(GraphicsContext* context, const FloatRect& boundingBox) const
{
    if (m_clipData.clipData().size() < 1)
        return;

    context->beginPath();

    QPainterPath newPath;

    bool heterogenousClipRules = false;
    WindRule clipRule = m_clipData.clipData()[0].windRule;

    unsigned int clipDataCount = m_clipData.clipData().size();
    for (unsigned int x = 0; x < clipDataCount; x++) {
        ClipData clipData = m_clipData.clipData()[x];
        if (clipData.windRule != clipRule)
            heterogenousClipRules = true;

        QPainterPath path = *(clipData.path.platformPath());
        if (path.isEmpty())
            continue;

        if (!newPath.isEmpty())
            newPath.closeSubpath();

        // Respect clipping units...
        QMatrix transform;

        if (clipData.bboxUnits) {
            transform.translate(boundingBox.x(), boundingBox.y());
            transform.scale(boundingBox.width(), boundingBox.height());
        }

        // TODO: support heterogenous clip rules!
        //clipRule = (clipData.windRule() == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);

        for (int i = 0; i < path.elementCount(); ++i) {
            const QPainterPath::Element &cur = path.elementAt(i);

            switch (cur.type) {
                case QPainterPath::MoveToElement:
                    newPath.moveTo(QPointF(cur.x, cur.y) * transform);
                    break;
                case QPainterPath::LineToElement:
                    newPath.lineTo(QPointF(cur.x, cur.y) * transform);
                    break;
                case QPainterPath::CurveToElement:
                {
                    const QPainterPath::Element &c1 = path.elementAt(i + 1);
                    const QPainterPath::Element &c2 = path.elementAt(i + 2);

                    Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
                    Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);

                    newPath.cubicTo(QPointF(cur.x, cur.y) * transform,
                                    QPointF(c1.x, c1.y) * transform,
                                    QPointF(c2.x, c2.y) * transform);

                    i += 2;
                    break;
                }
                case QPainterPath::CurveToDataElement:
                    Q_ASSERT(false);
                    break;
            }
        }
    }

    if (m_clipData.clipData().size()) {
        // FIXME!
        // We don't currently allow for heterogenous clip rules.
        // we would have to detect such, draw to a mask, and then clip
        // to that mask
        // if (!CGContextIsPathEmpty(cgContext)) {
            if (clipRule == RULE_EVENODD)
                newPath.setFillRule(Qt::OddEvenFill);
            else
                newPath.setFillRule(Qt::WindingFill);
        // }
    }

    QPainter* painter(context ? context->platformContext() : 0);
    Q_ASSERT(painter);

    painter->setClipPath(newPath);
}

} // namespace WebCore

#endif

// vim:ts=4:noet