1 /* 2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis (at) 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 "SVGException.h" 30 #include "SVGNames.h" 31 #include "SVGStyledLocatableElement.h" 32 33 namespace WebCore { 34 35 static bool isViewportElement(Node* node) 36 { 37 return (node->hasTagName(SVGNames::svgTag) 38 || node->hasTagName(SVGNames::symbolTag) 39 #if ENABLE(SVG_FOREIGN_OBJECT) 40 || node->hasTagName(SVGNames::foreignObjectTag) 41 #endif 42 || node->hasTagName(SVGNames::imageTag)); 43 } 44 45 SVGElement* SVGLocatable::nearestViewportElement(const SVGElement* element) 46 { 47 ASSERT(element); 48 for (ContainerNode* n = element->parentNode(); n; n = n->parentNode()) { 49 if (isViewportElement(n)) 50 return static_cast<SVGElement*>(n); 51 } 52 53 return 0; 54 } 55 56 SVGElement* SVGLocatable::farthestViewportElement(const SVGElement* element) 57 { 58 ASSERT(element); 59 SVGElement* farthest = 0; 60 for (ContainerNode* n = element->parentNode(); n; n = n->parentNode()) { 61 if (isViewportElement(n)) 62 farthest = static_cast<SVGElement*>(n); 63 } 64 return farthest; 65 } 66 67 FloatRect SVGLocatable::getBBox(const SVGElement* element, StyleUpdateStrategy styleUpdateStrategy) 68 { 69 ASSERT(element); 70 if (styleUpdateStrategy == AllowStyleUpdate) 71 element->document()->updateLayoutIgnorePendingStylesheets(); 72 73 // FIXME: Eventually we should support getBBox for detached elements. 74 if (!element->renderer()) 75 return FloatRect(); 76 77 return element->renderer()->objectBoundingBox(); 78 } 79 80 AffineTransform SVGLocatable::computeCTM(const SVGElement* element, CTMScope mode, StyleUpdateStrategy styleUpdateStrategy) 81 { 82 ASSERT(element); 83 if (styleUpdateStrategy == AllowStyleUpdate) 84 element->document()->updateLayoutIgnorePendingStylesheets(); 85 86 AffineTransform ctm; 87 88 SVGElement* stopAtElement = mode == NearestViewportScope ? nearestViewportElement(element) : 0; 89 90 Node* current = const_cast<SVGElement*>(element); 91 while (current && current->isSVGElement()) { 92 SVGElement* currentElement = static_cast<SVGElement*>(current); 93 if (currentElement->isStyled()) 94 // note that this modifies the AffineTransform returned by localCoordinateSpaceTransform(mode) too. 95 ctm = static_cast<SVGStyledElement*>(currentElement)->localCoordinateSpaceTransform(mode).multiply(ctm); 96 97 // For getCTM() computation, stop at the nearest viewport element 98 if (currentElement == stopAtElement) 99 break; 100 101 current = current->parentOrHostNode(); 102 } 103 104 return ctm; 105 } 106 107 AffineTransform SVGLocatable::getTransformToElement(SVGElement* target, ExceptionCode& ec, StyleUpdateStrategy styleUpdateStrategy) const 108 { 109 AffineTransform ctm = getCTM(styleUpdateStrategy); 110 111 if (target && target->isStyledLocatable()) { 112 AffineTransform targetCTM = static_cast<SVGStyledLocatableElement*>(target)->getCTM(styleUpdateStrategy); 113 if (!targetCTM.isInvertible()) { 114 ec = SVGException::SVG_MATRIX_NOT_INVERTABLE; 115 return ctm; 116 } 117 ctm = targetCTM.inverse() * ctm; 118 } 119 120 return ctm; 121 } 122 123 } 124 125 #endif // ENABLE(SVG) 126