#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGText.h"
#include "FloatConversion.h"
#include "FloatQuad.h"
#include "GraphicsContext.h"
#include "PointerEventsHitRules.h"
#include "RenderSVGRoot.h"
#include "SVGLengthList.h"
#include "SVGResourceFilter.h"
#include "SVGRootInlineBox.h"
#include "SVGTextElement.h"
#include "SVGTransformList.h"
#include "SVGURIReference.h"
#include "SimpleFontData.h"
namespace WebCore {
RenderSVGText::RenderSVGText(SVGTextElement* node)
: RenderSVGBlock(node)
{
}
IntRect RenderSVGText::clippedOverflowRectForRepaint(RenderBox* )
{
FloatRect repaintRect = absoluteTransform().mapRect(relativeBBox(true));
#if ENABLE(SVG_FILTERS)
SVGResourceFilter* filter = getFilterById(document(), style()->svgStyle()->filter());
if (filter)
repaintRect.unite(filter->filterBBoxForItemBBox(repaintRect));
#endif
if (!repaintRect.isEmpty())
repaintRect.inflate(1);
return enclosingIntRect(repaintRect);
}
bool RenderSVGText::calculateLocalTransform()
{
TransformationMatrix oldTransform = m_localTransform;
m_localTransform = static_cast<SVGTextElement*>(element())->animatedLocalTransform();
return (oldTransform != m_localTransform);
}
void RenderSVGText::layout()
{
ASSERT(needsLayout());
setNeedsLayout(true);
LayoutRepainter repainter(*this, checkForRepaintDuringLayout(), &m_absoluteBounds);
SVGTextElement* text = static_cast<SVGTextElement*>(element());
int xOffset = (int)(text->x()->getFirst().value(text));
int yOffset = (int)(text->y()->getFirst().value(text));
setLocation(xOffset, yOffset);
calculateLocalTransform();
RenderBlock::layout();
m_absoluteBounds = absoluteClippedOverflowRect();
repainter.repaintAfterLayout();
setNeedsLayout(false);
}
InlineBox* RenderSVGText::createInlineBox(bool, bool, bool)
{
ASSERT(!isRenderInline());
InlineFlowBox* flowBox = new (renderArena()) SVGRootInlineBox(this);
if (!m_firstLineBox)
m_firstLineBox = m_lastLineBox = flowBox;
else {
m_lastLineBox->setNextLineBox(flowBox);
flowBox->setPreviousLineBox(m_lastLineBox);
m_lastLineBox = flowBox;
}
return flowBox;
}
bool RenderSVGText::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int _x, int _y, int _tx, int _ty, HitTestAction hitTestAction)
{
PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, style()->pointerEvents());
bool isVisible = (style()->visibility() == VISIBLE);
if (isVisible || !hitRules.requireVisible) {
if ((hitRules.canHitStroke && (style()->svgStyle()->hasStroke() || !hitRules.requireStroke))
|| (hitRules.canHitFill && (style()->svgStyle()->hasFill() || !hitRules.requireFill))) {
TransformationMatrix totalTransform = absoluteTransform();
double localX, localY;
totalTransform.inverse().map(_x, _y, localX, localY);
FloatPoint hitPoint(_x, _y);
return RenderBlock::nodeAtPoint(request, result, (int)localX, (int)localY, _tx, _ty, hitTestAction);
}
}
return false;
}
void RenderSVGText::absoluteRects(Vector<IntRect>& rects, int, int, bool)
{
RenderSVGRoot* root = findSVGRootObject(parent());
if (!root)
return;
FloatPoint absPos = localToAbsolute();
TransformationMatrix htmlParentCtm = root->RenderContainer::absoluteTransform();
for (InlineRunBox* runBox = firstLineBox(); runBox; runBox = runBox->nextLineBox()) {
ASSERT(runBox->isInlineFlowBox());
InlineFlowBox* flowBox = static_cast<InlineFlowBox*>(runBox);
for (InlineBox* box = flowBox->firstChild(); box; box = box->nextOnLine()) {
FloatRect boxRect(box->xPos(), box->yPos(), box->width(), box->height());
boxRect.move(narrowPrecisionToFloat(absPos.x() - htmlParentCtm.e()), narrowPrecisionToFloat(absPos.y() - htmlParentCtm.f()));
rects.append(enclosingIntRect(absoluteTransform().mapRect(boxRect)));
}
}
}
void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads, bool)
{
RenderSVGRoot* root = findSVGRootObject(parent());
if (!root)
return;
FloatPoint absPos = localToAbsolute();
TransformationMatrix htmlParentCtm = root->RenderContainer::absoluteTransform();
for (InlineRunBox* runBox = firstLineBox(); runBox; runBox = runBox->nextLineBox()) {
ASSERT(runBox->isInlineFlowBox());
InlineFlowBox* flowBox = static_cast<InlineFlowBox*>(runBox);
for (InlineBox* box = flowBox->firstChild(); box; box = box->nextOnLine()) {
FloatRect boxRect(box->xPos(), box->yPos(), box->width(), box->height());
boxRect.move(narrowPrecisionToFloat(absPos.x() - htmlParentCtm.e()), narrowPrecisionToFloat(absPos.y() - htmlParentCtm.f()));
quads.append(absoluteTransform().mapRect(boxRect));
}
}
}
void RenderSVGText::paint(PaintInfo& paintInfo, int, int)
{
RenderObject::PaintInfo pi(paintInfo);
pi.rect = absoluteTransform().inverse().mapRect(pi.rect);
RenderBlock::paint(pi, 0, 0);
}
FloatRect RenderSVGText::relativeBBox(bool includeStroke) const
{
FloatRect repaintRect;
for (InlineRunBox* runBox = firstLineBox(); runBox; runBox = runBox->nextLineBox()) {
ASSERT(runBox->isInlineFlowBox());
InlineFlowBox* flowBox = static_cast<InlineFlowBox*>(runBox);
for (InlineBox* box = flowBox->firstChild(); box; box = box->nextOnLine())
repaintRect.unite(FloatRect(box->xPos(), box->yPos(), box->width(), box->height()));
}
if (includeStroke && style()->svgStyle()->hasStroke()) {
float strokeWidth = SVGRenderStyle::cssPrimitiveToLength(this, style()->svgStyle()->strokeWidth(), 0.0f);
#if ENABLE(SVG_FONTS)
const Font& font = style()->font();
if (font.primaryFont()->isSVGFont()) {
float scale = font.unitsPerEm() > 0 ? font.size() / font.unitsPerEm() : 0.0f;
if (scale != 0.0f)
strokeWidth /= scale;
}
#endif
repaintRect.inflate(strokeWidth);
}
repaintRect.move(x(), y());
return repaintRect;
}
}
#endif // ENABLE(SVG)