webengine/osswebengine/WebCore/platform/graphics/svg/qt/SVGResourceClipperQt.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2     Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <wildfox@kde.org>
       
     3                   2004, 2005, 2006 Rob Buis <buis@kde.org>
       
     4                   2005 Apple Computer, Inc.
       
     5 
       
     6     This file is part of the KDE 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     aint 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 "config.h"
       
    25 
       
    26 #if ENABLE(SVG)
       
    27 #include "SVGResourceClipper.h"
       
    28 
       
    29 #include "GraphicsContext.h"
       
    30 
       
    31 #include <QPainter>
       
    32 #include <QPainterPath>
       
    33 
       
    34 namespace WebCore {
       
    35 
       
    36 void SVGResourceClipper::applyClip(GraphicsContext* context, const FloatRect& boundingBox) const
       
    37 {
       
    38     if (m_clipData.clipData().size() < 1)
       
    39         return;
       
    40 
       
    41     context->beginPath();
       
    42 
       
    43     QPainterPath newPath;
       
    44 
       
    45     bool heterogenousClipRules = false;
       
    46     WindRule clipRule = m_clipData.clipData()[0].windRule;
       
    47 
       
    48     unsigned int clipDataCount = m_clipData.clipData().size();
       
    49     for (unsigned int x = 0; x < clipDataCount; x++) {
       
    50         ClipData clipData = m_clipData.clipData()[x];
       
    51         if (clipData.windRule != clipRule)
       
    52             heterogenousClipRules = true;
       
    53 
       
    54         QPainterPath path = *(clipData.path.platformPath());
       
    55         if (path.isEmpty())
       
    56             continue;
       
    57 
       
    58         if (!newPath.isEmpty())
       
    59             newPath.closeSubpath();
       
    60 
       
    61         // Respect clipping units...
       
    62         QMatrix transform;
       
    63 
       
    64         if (clipData.bboxUnits) {
       
    65             transform.translate(boundingBox.x(), boundingBox.y());
       
    66             transform.scale(boundingBox.width(), boundingBox.height());
       
    67         }
       
    68 
       
    69         // TODO: support heterogenous clip rules!
       
    70         //clipRule = (clipData.windRule() == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);
       
    71 
       
    72         for (int i = 0; i < path.elementCount(); ++i) {
       
    73             const QPainterPath::Element &cur = path.elementAt(i);
       
    74 
       
    75             switch (cur.type) {
       
    76                 case QPainterPath::MoveToElement:
       
    77                     newPath.moveTo(QPointF(cur.x, cur.y) * transform);
       
    78                     break;
       
    79                 case QPainterPath::LineToElement:
       
    80                     newPath.lineTo(QPointF(cur.x, cur.y) * transform);
       
    81                     break;
       
    82                 case QPainterPath::CurveToElement:
       
    83                 {
       
    84                     const QPainterPath::Element &c1 = path.elementAt(i + 1);
       
    85                     const QPainterPath::Element &c2 = path.elementAt(i + 2);
       
    86 
       
    87                     Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
       
    88                     Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
       
    89 
       
    90                     newPath.cubicTo(QPointF(cur.x, cur.y) * transform,
       
    91                                     QPointF(c1.x, c1.y) * transform,
       
    92                                     QPointF(c2.x, c2.y) * transform);
       
    93 
       
    94                     i += 2;
       
    95                     break;
       
    96                 }
       
    97                 case QPainterPath::CurveToDataElement:
       
    98                     Q_ASSERT(false);
       
    99                     break;
       
   100             }
       
   101         }
       
   102     }
       
   103 
       
   104     if (m_clipData.clipData().size()) {
       
   105         // FIXME!
       
   106         // We don't currently allow for heterogenous clip rules.
       
   107         // we would have to detect such, draw to a mask, and then clip
       
   108         // to that mask
       
   109         // if (!CGContextIsPathEmpty(cgContext)) {
       
   110             if (clipRule == RULE_EVENODD)
       
   111                 newPath.setFillRule(Qt::OddEvenFill);
       
   112             else
       
   113                 newPath.setFillRule(Qt::WindingFill);
       
   114         // }
       
   115     }
       
   116 
       
   117     QPainter* painter(context ? context->platformContext() : 0);
       
   118     Q_ASSERT(painter);
       
   119 
       
   120     painter->setClipPath(newPath);
       
   121 }
       
   122 
       
   123 } // namespace WebCore
       
   124 
       
   125 #endif
       
   126 
       
   127 // vim:ts=4:noet