#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGText.h"
#include "FloatConversion.h"
#include "FloatQuad.h"
#include "FontCache.h"
#include "GraphicsContext.h"
#include "HitTestRequest.h"
#include "LayoutRepainter.h"
#include "PointerEventsHitRules.h"
#include "RenderSVGInlineText.h"
#include "RenderSVGResource.h"
#include "RenderSVGRoot.h"
#include "SVGLengthList.h"
#include "SVGRenderSupport.h"
#include "SVGResourcesCache.h"
#include "SVGRootInlineBox.h"
#include "SVGTextElement.h"
#include "SVGTextLayoutAttributesBuilder.h"
#include "SVGTextRunRenderingContext.h"
#include "SVGTransformList.h"
#include "SVGURIReference.h"
#include "SimpleFontData.h"
#include "TransformState.h"
#include "VisiblePosition.h"
namespace WebCore {
RenderSVGText::RenderSVGText(SVGTextElement* node)
: RenderSVGBlock(node)
, m_needsReordering(false)
, m_needsPositioningValuesUpdate(true)
, m_needsTransformUpdate(true)
, m_needsTextMetricsUpdate(true)
{
}
bool RenderSVGText::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isInline();
}
RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(RenderObject* start)
{
ASSERT(start);
while (start && !start->isSVGText())
start = start->parent();
if (!start || !start->isSVGText())
return 0;
return toRenderSVGText(start);
}
const RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(const RenderObject* start)
{
ASSERT(start);
while (start && !start->isSVGText())
start = start->parent();
if (!start || !start->isSVGText())
return 0;
return toRenderSVGText(start);
}
LayoutRect RenderSVGText::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
{
return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
}
void RenderSVGText::computeRectForRepaint(RenderBoxModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
{
FloatRect repaintRect = rect;
computeFloatRectForRepaint(repaintContainer, repaintRect, fixed);
rect = enclosingLayoutRect(repaintRect);
}
void RenderSVGText::computeFloatRectForRepaint(RenderBoxModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
{
SVGRenderSupport::computeFloatRectForRepaint(this, repaintContainer, repaintRect, fixed);
}
void RenderSVGText::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool , bool , TransformState& transformState, ApplyContainerFlipOrNot, bool* wasFixed) const
{
SVGRenderSupport::mapLocalToContainer(this, repaintContainer, transformState, wasFixed);
}
const RenderObject* RenderSVGText::pushMappingToContainer(const RenderBoxModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
return SVGRenderSupport::pushMappingToContainer(this, ancestorToStopAt, geometryMap);
}
static inline void recursiveUpdateLayoutAttributes(RenderObject* start, SVGTextLayoutAttributesBuilder& builder)
{
if (start->isSVGInlineText()) {
builder.buildLayoutAttributesForTextRenderer(toRenderSVGInlineText(start));
return;
}
for (RenderObject* child = start->firstChild(); child; child = child->nextSibling())
recursiveUpdateLayoutAttributes(child, builder);
}
void RenderSVGText::layoutAttributesChanged(RenderObject* child)
{
ASSERT(child);
if (m_needsPositioningValuesUpdate)
return;
FontCachePurgePreventer fontCachePurgePreventer;
recursiveUpdateLayoutAttributes(child, m_layoutAttributesBuilder);
rebuildLayoutAttributes();
}
static inline bool findPreviousAndNextAttributes(RenderObject* start, RenderSVGInlineText* locateElement, bool& stopAfterNext, SVGTextLayoutAttributes*& previous, SVGTextLayoutAttributes*& next)
{
ASSERT(start);
ASSERT(locateElement);
for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) {
if (child->isSVGInlineText()) {
RenderSVGInlineText* text = toRenderSVGInlineText(child);
if (locateElement != text) {
if (stopAfterNext) {
next = text->layoutAttributes();
return true;
}
previous = text->layoutAttributes();
continue;
}
stopAfterNext = true;
continue;
}
if (!child->isSVGInline())
continue;
if (findPreviousAndNextAttributes(child, locateElement, stopAfterNext, previous, next))
return true;
}
return false;
}
void RenderSVGText::layoutAttributesWillBeDestroyed(RenderSVGInlineText* text, Vector<SVGTextLayoutAttributes*>& affectedAttributes)
{
ASSERT(text);
if (m_needsPositioningValuesUpdate)
return;
bool stopAfterNext = false;
SVGTextLayoutAttributes* previous = 0;
SVGTextLayoutAttributes* next = 0;
findPreviousAndNextAttributes(this, text, stopAfterNext, previous, next);
if (previous)
affectedAttributes.append(previous);
if (next)
affectedAttributes.append(next);
}
void RenderSVGText::invalidateTextPositioningElements()
{
m_layoutAttributesBuilder.clearTextPositioningElements();
}
static inline void recursiveUpdateScaledFont(RenderObject* start)
{
for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) {
if (child->isSVGInlineText()) {
toRenderSVGInlineText(child)->updateScaledFont();
continue;
}
recursiveUpdateScaledFont(child);
}
}
void RenderSVGText::layout()
{
ASSERT(needsLayout());
LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
bool updateCachedBoundariesInParents = false;
if (m_needsTransformUpdate) {
SVGTextElement* text = static_cast<SVGTextElement*>(node());
m_localTransform = text->animatedLocalTransform();
m_needsTransformUpdate = false;
updateCachedBoundariesInParents = true;
}
if (m_needsTextMetricsUpdate || SVGRenderSupport::findTreeRootObject(this)->isLayoutSizeChanged()) {
recursiveUpdateScaledFont(this);
rebuildLayoutAttributes(true);
updateCachedBoundariesInParents = true;
m_needsTextMetricsUpdate = false;
}
if (m_needsPositioningValuesUpdate) {
m_layoutAttributesBuilder.buildLayoutAttributesForWholeTree(this);
m_needsReordering = true;
m_needsPositioningValuesUpdate = false;
updateCachedBoundariesInParents = true;
}
ASSERT(!isInline());
ASSERT(!simplifiedLayout());
ASSERT(!scrollsOverflow());
ASSERT(!hasControlClip());
ASSERT(!hasColumns());
ASSERT(!positionedObjects());
ASSERT(!m_overflow);
ASSERT(!isAnonymousBlock());
if (!firstChild())
setChildrenInline(true);
FloatRect oldBoundaries = objectBoundingBox();
ASSERT(childrenInline());
forceLayoutInlineChildren();
if (m_needsReordering)
m_needsReordering = false;
if (!updateCachedBoundariesInParents)
updateCachedBoundariesInParents = oldBoundaries != objectBoundingBox();
if (everHadLayout() && selfNeedsLayout())
SVGResourcesCache::clientLayoutChanged(this);
if (updateCachedBoundariesInParents)
RenderSVGBlock::setNeedsBoundariesUpdate();
repainter.repaintAfterLayout();
setNeedsLayout(false);
}
RootInlineBox* RenderSVGText::createRootInlineBox()
{
RootInlineBox* box = new (renderArena()) SVGRootInlineBox(this);
box->setHasVirtualLogicalHeight();
return box;
}
bool RenderSVGText::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
{
PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, request, 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))) {
FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
if (!SVGRenderSupport::pointInClippingArea(this, localPoint))
return false;
return RenderBlock::nodeAtPoint(request, result, flooredIntPoint(localPoint), IntPoint(), hitTestAction);
}
}
return false;
}
bool RenderSVGText::nodeAtPoint(const HitTestRequest&, HitTestResult&, const LayoutPoint&, const LayoutPoint&, HitTestAction)
{
ASSERT_NOT_REACHED();
return false;
}
VisiblePosition RenderSVGText::positionForPoint(const LayoutPoint& pointInContents)
{
RootInlineBox* rootBox = firstRootBox();
if (!rootBox)
return createVisiblePosition(0, DOWNSTREAM);
ASSERT(rootBox->isSVGRootInlineBox());
ASSERT(!rootBox->nextRootBox());
ASSERT(childrenInline());
InlineBox* closestBox = static_cast<SVGRootInlineBox*>(rootBox)->closestLeafChildForPosition(pointInContents);
if (!closestBox)
return createVisiblePosition(0, DOWNSTREAM);
return closestBox->renderer()->positionForPoint(LayoutPoint(pointInContents.x(), closestBox->y()));
}
void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
quads.append(localToAbsoluteQuad(strokeBoundingBox(), false, wasFixed));
}
void RenderSVGText::paint(PaintInfo& paintInfo, const LayoutPoint&)
{
if (paintInfo.context->paintingDisabled())
return;
if (paintInfo.phase != PaintPhaseForeground
&& paintInfo.phase != PaintPhaseSelfOutline
&& paintInfo.phase != PaintPhaseSelection)
return;
PaintInfo blockInfo(paintInfo);
GraphicsContextStateSaver stateSaver(*blockInfo.context);
blockInfo.applyTransform(localToParentTransform());
RenderBlock::paint(blockInfo, LayoutPoint());
}
FloatRect RenderSVGText::strokeBoundingBox() const
{
FloatRect strokeBoundaries = objectBoundingBox();
const SVGRenderStyle* svgStyle = style()->svgStyle();
if (!svgStyle->hasStroke())
return strokeBoundaries;
ASSERT(node());
ASSERT(node()->isSVGElement());
SVGLengthContext lengthContext(static_cast<SVGElement*>(node()));
strokeBoundaries.inflate(svgStyle->strokeWidth().value(lengthContext));
return strokeBoundaries;
}
FloatRect RenderSVGText::repaintRectInLocalCoordinates() const
{
FloatRect repaintRect = strokeBoundingBox();
SVGRenderSupport::intersectRepaintRectWithResources(this, repaintRect);
if (const ShadowData* textShadow = style()->textShadow())
textShadow->adjustRectForShadow(repaintRect);
return repaintRect;
}
void RenderSVGText::addChild(RenderObject* child, RenderObject* beforeChild)
{
RenderSVGBlock::addChild(child, beforeChild);
layoutAttributesChanged(child);
}
RenderBlock* RenderSVGText::firstLineBlock() const
{
return 0;
}
void RenderSVGText::updateFirstLetter()
{
}
static inline void recursiveCollectLayoutAttributes(RenderObject* start, Vector<SVGTextLayoutAttributes*>& attributes)
{
for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) {
if (child->isSVGInlineText()) {
attributes.append(toRenderSVGInlineText(child)->layoutAttributes());
continue;
}
recursiveCollectLayoutAttributes(child, attributes);
}
}
void RenderSVGText::rebuildLayoutAttributes(bool performFullRebuild)
{
if (performFullRebuild)
m_layoutAttributes.clear();
if (m_layoutAttributes.isEmpty()) {
recursiveCollectLayoutAttributes(this, m_layoutAttributes);
if (m_layoutAttributes.isEmpty() || !performFullRebuild)
return;
m_layoutAttributesBuilder.rebuildMetricsForWholeTree(this);
return;
}
Vector<SVGTextLayoutAttributes*> affectedAttributes;
rebuildLayoutAttributes(affectedAttributes);
}
void RenderSVGText::rebuildLayoutAttributes(Vector<SVGTextLayoutAttributes*>& affectedAttributes)
{
Vector<SVGTextLayoutAttributes*> newLayoutAttributes;
recursiveCollectLayoutAttributes(this, newLayoutAttributes);
if (newLayoutAttributes.isEmpty()) {
m_layoutAttributes.clear();
return;
}
size_t size = newLayoutAttributes.size();
for (size_t i = 0; i < size; ++i) {
SVGTextLayoutAttributes* attributes = newLayoutAttributes[i];
if (m_layoutAttributes.find(attributes) == notFound)
m_layoutAttributesBuilder.rebuildMetricsForTextRenderer(attributes->context());
}
size = affectedAttributes.size();
for (size_t i = 0; i < size; ++i)
m_layoutAttributesBuilder.rebuildMetricsForTextRenderer(affectedAttributes[i]->context());
m_layoutAttributes = newLayoutAttributes;
}
}
#endif // ENABLE(SVG)