WebCore/svg/SVGLocatable.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2     Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
       
     3                   2004, 2005, 2006 Rob Buis <buis@kde.org>
       
     4     Copyright (C) 2009 Google, Inc.  All rights reserved.
       
     5     Copyright (C) Research In Motion Limited 2010. All rights reserved.
       
     6 
       
     7     This library is free software; you can redistribute it and/or
       
     8     modify it under the terms of the GNU Library General Public
       
     9     License as published by the Free Software Foundation; either
       
    10     version 2 of the License, or (at your option) any later version.
       
    11 
       
    12     This library is distributed in the hope that it will be useful,
       
    13     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15     Library General Public License for more details.
       
    16 
       
    17     You should have received a copy of the GNU Library General Public License
       
    18     along with this library; see the file COPYING.LIB.  If not, write to
       
    19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    20     Boston, MA 02110-1301, USA.
       
    21 */
       
    22 
       
    23 #include "config.h"
       
    24 
       
    25 #if ENABLE(SVG)
       
    26 #include "SVGLocatable.h"
       
    27 
       
    28 #include "RenderObject.h"
       
    29 #include "SVGStyledLocatableElement.h"
       
    30 #include "SVGException.h"
       
    31 
       
    32 namespace WebCore {
       
    33 
       
    34 SVGLocatable::SVGLocatable()
       
    35 {
       
    36 }
       
    37 
       
    38 SVGLocatable::~SVGLocatable()
       
    39 {
       
    40 }
       
    41 
       
    42 static bool isViewportElement(Node* node)
       
    43 {
       
    44     return (node->hasTagName(SVGNames::svgTag)
       
    45         || node->hasTagName(SVGNames::symbolTag)
       
    46 #if ENABLE(SVG_FOREIGN_OBJECT)
       
    47         || node->hasTagName(SVGNames::foreignObjectTag)
       
    48 #endif
       
    49         || node->hasTagName(SVGNames::imageTag));
       
    50 }
       
    51 
       
    52 SVGElement* SVGLocatable::nearestViewportElement(const SVGElement* element)
       
    53 {
       
    54     ASSERT(element);
       
    55     for (Node* n = element->parentNode(); n && !n->isDocumentNode(); n = n->parentNode()) {
       
    56         if (isViewportElement(n))
       
    57             return static_cast<SVGElement*>(n);
       
    58     }
       
    59 
       
    60     return 0;
       
    61 }
       
    62 
       
    63 SVGElement* SVGLocatable::farthestViewportElement(const SVGElement* element)
       
    64 {
       
    65     ASSERT(element);
       
    66     SVGElement* farthest = 0;
       
    67     for (Node* n = element->parentNode(); n && !n->isDocumentNode(); n = n->parentNode()) {
       
    68         if (isViewportElement(n))
       
    69             farthest = static_cast<SVGElement*>(n);
       
    70     }
       
    71     return farthest;
       
    72 }
       
    73 
       
    74 FloatRect SVGLocatable::getBBox(const SVGElement* element)
       
    75 {
       
    76     ASSERT(element);
       
    77     element->document()->updateLayoutIgnorePendingStylesheets();
       
    78 
       
    79     // FIXME: Eventually we should support getBBox for detached elements.
       
    80     if (!element->renderer())
       
    81         return FloatRect();
       
    82 
       
    83     return element->renderer()->objectBoundingBox();
       
    84 }
       
    85 
       
    86 AffineTransform SVGLocatable::computeCTM(const SVGElement* element, CTMScope mode)
       
    87 {
       
    88     ASSERT(element);
       
    89     element->document()->updateLayoutIgnorePendingStylesheets();
       
    90 
       
    91     AffineTransform ctm;
       
    92 
       
    93     SVGElement* stopAtElement = mode == NearestViewportScope ? nearestViewportElement(element) : 0;
       
    94 
       
    95     Node* current = const_cast<SVGElement*>(element);
       
    96     while (current && current->isSVGElement()) {
       
    97         SVGElement* currentElement = static_cast<SVGElement*>(current);
       
    98         if (currentElement->isStyled())
       
    99             ctm = static_cast<SVGStyledElement*>(currentElement)->localCoordinateSpaceTransform(mode).multLeft(ctm);
       
   100 
       
   101         // For getCTM() computation, stop at the nearest viewport element
       
   102         if (currentElement == stopAtElement)
       
   103             break;
       
   104 
       
   105         current = current->isShadowNode() ? current->shadowParentNode() : current->parentNode();
       
   106     }
       
   107 
       
   108     return ctm;
       
   109 }
       
   110 
       
   111 AffineTransform SVGLocatable::getTransformToElement(SVGElement* target, ExceptionCode& ec) const
       
   112 {
       
   113     AffineTransform ctm = getCTM();
       
   114 
       
   115     if (target && target->isStyledLocatable()) {
       
   116         AffineTransform targetCTM = static_cast<SVGStyledLocatableElement*>(target)->getCTM();
       
   117         if (!targetCTM.isInvertible()) {
       
   118             ec = SVGException::SVG_MATRIX_NOT_INVERTABLE;
       
   119             return ctm;
       
   120         }
       
   121         ctm *= targetCTM.inverse();
       
   122     }
       
   123 
       
   124     return ctm;
       
   125 }
       
   126 
       
   127 }
       
   128 
       
   129 #endif // ENABLE(SVG)