#include "config.h"
#include "SVGLocatable.h"
#include "RenderElement.h"
#include "SVGException.h"
#include "SVGGraphicsElement.h"
#include "SVGImageElement.h"
#include "SVGNames.h"
namespace WebCore {
static bool isViewportElement(Node* node)
{
return (node->hasTagName(SVGNames::svgTag)
|| node->hasTagName(SVGNames::symbolTag)
|| node->hasTagName(SVGNames::foreignObjectTag)
|| isSVGImageElement(node));
}
SVGElement* SVGLocatable::nearestViewportElement(const SVGElement* element)
{
ASSERT(element);
for (Element* current = element->parentOrShadowHostElement(); current; current = current->parentOrShadowHostElement()) {
if (isViewportElement(current))
return toSVGElement(current);
}
return 0;
}
SVGElement* SVGLocatable::farthestViewportElement(const SVGElement* element)
{
ASSERT(element);
SVGElement* farthest = 0;
for (Element* current = element->parentOrShadowHostElement(); current; current = current->parentOrShadowHostElement()) {
if (isViewportElement(current))
farthest = toSVGElement(current);
}
return farthest;
}
FloatRect SVGLocatable::getBBox(SVGElement* element, StyleUpdateStrategy styleUpdateStrategy)
{
ASSERT(element);
if (styleUpdateStrategy == AllowStyleUpdate)
element->document().updateLayoutIgnorePendingStylesheets();
if (!element->renderer())
return FloatRect();
return element->renderer()->objectBoundingBox();
}
AffineTransform SVGLocatable::computeCTM(SVGElement* element, CTMScope mode, StyleUpdateStrategy styleUpdateStrategy)
{
ASSERT(element);
if (styleUpdateStrategy == AllowStyleUpdate)
element->document().updateLayoutIgnorePendingStylesheets();
AffineTransform ctm;
SVGElement* stopAtElement = mode == NearestViewportScope ? nearestViewportElement(element) : 0;
for (Element* currentElement = element; currentElement; currentElement = currentElement->parentOrShadowHostElement()) {
if (!currentElement->isSVGElement())
break;
ctm = toSVGElement(currentElement)->localCoordinateSpaceTransform(mode).multiply(ctm);
if (currentElement == stopAtElement)
break;
}
return ctm;
}
AffineTransform SVGLocatable::getTransformToElement(SVGElement* target, ExceptionCode& ec, StyleUpdateStrategy styleUpdateStrategy)
{
AffineTransform ctm = getCTM(styleUpdateStrategy);
if (target && target->isSVGGraphicsElement()) {
AffineTransform targetCTM = toSVGGraphicsElement(target)->getCTM(styleUpdateStrategy);
if (!targetCTM.isInvertible()) {
ec = SVGException::SVG_MATRIX_NOT_INVERTABLE;
return ctm;
}
ctm = targetCTM.inverse() * ctm;
}
return ctm;
}
}