This source file includes following definitions.
- m_needsTextMetricsUpdate
- isChildAllowed
- locateRenderSVGTextAncestor
- locateRenderSVGTextAncestor
- computeRectForRepaint
- collectLayoutAttributes
- findPreviousAndNextAttributes
- shouldHandleSubtreeMutations
- subtreeChildWasAdded
- checkLayoutAttributesConsistency
- willBeDestroyed
- subtreeChildWillBeRemoved
- subtreeChildWasRemoved
- subtreeStyleDidChange
- subtreeTextDidChange
- updateFontInAllDescendants
- layout
- createRootInlineBox
- nodeAtFloatPoint
- positionForPoint
- absoluteQuads
- paint
- strokeBoundingBox
- repaintRectInLocalCoordinates
- addChild
- removeChild
- firstLineBlock
- updateFirstLetter
#include "config.h"
#include "core/rendering/svg/RenderSVGText.h"
#include "core/rendering/HitTestRequest.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/LayoutRectRecorder.h"
#include "core/rendering/LayoutRepainter.h"
#include "core/rendering/PointerEventsHitRules.h"
#include "core/rendering/style/ShadowList.h"
#include "core/rendering/svg/RenderSVGInlineText.h"
#include "core/rendering/svg/RenderSVGResource.h"
#include "core/rendering/svg/RenderSVGRoot.h"
#include "core/rendering/svg/SVGRenderSupport.h"
#include "core/rendering/svg/SVGResourcesCache.h"
#include "core/rendering/svg/SVGRootInlineBox.h"
#include "core/rendering/svg/SVGTextRunRenderingContext.h"
#include "core/svg/SVGLengthList.h"
#include "core/svg/SVGTextElement.h"
#include "core/svg/SVGTransformList.h"
#include "core/svg/SVGURIReference.h"
#include "platform/FloatConversion.h"
#include "platform/fonts/FontCache.h"
#include "platform/fonts/SimpleFontData.h"
#include "platform/geometry/FloatQuad.h"
#include "platform/geometry/TransformState.h"
#include "platform/graphics/GraphicsContextStateSaver.h"
namespace WebCore {
RenderSVGText::RenderSVGText(SVGTextElement* node)
: RenderSVGBlock(node)
, m_needsReordering(false)
, m_needsPositioningValuesUpdate(false)
, m_needsTransformUpdate(true)
, m_needsTextMetricsUpdate(false)
{
}
RenderSVGText::~RenderSVGText()
{
ASSERT(m_layoutAttributes.isEmpty());
}
bool RenderSVGText::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isSVGInline() || (child->isText() && SVGRenderSupport::isRenderableTextNode(child));
}
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);
}
void RenderSVGText::computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
{
FloatRect repaintRect = rect;
computeFloatRectForRepaint(repaintContainer, repaintRect, fixed);
rect = enclosingLayoutRect(repaintRect);
}
static inline void collectLayoutAttributes(RenderObject* text, Vector<SVGTextLayoutAttributes*>& attributes)
{
for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
if (descendant->isSVGInlineText())
attributes.append(toRenderSVGInlineText(descendant)->layoutAttributes());
}
}
static inline bool findPreviousAndNextAttributes(RenderObject* root, RenderSVGInlineText* locateElement, SVGTextLayoutAttributes*& previous, SVGTextLayoutAttributes*& next)
{
ASSERT(root);
ASSERT(locateElement);
bool stopAfterNext = false;
RenderObject* current = root->firstChild();
while (current) {
if (current->isSVGInlineText()) {
RenderSVGInlineText* text = toRenderSVGInlineText(current);
if (locateElement != text) {
if (stopAfterNext) {
next = text->layoutAttributes();
return true;
}
previous = text->layoutAttributes();
} else {
stopAfterNext = true;
}
} else if (current->isSVGInline()) {
if (RenderObject* child = current->firstChild()) {
current = child;
continue;
}
}
current = current->nextInPreOrderAfterChildren(root);
}
return false;
}
inline bool RenderSVGText::shouldHandleSubtreeMutations() const
{
if (beingDestroyed() || !everHadLayout()) {
ASSERT(m_layoutAttributes.isEmpty());
ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
return false;
}
return true;
}
void RenderSVGText::subtreeChildWasAdded(RenderObject* child)
{
ASSERT(child);
if (!shouldHandleSubtreeMutations() || documentBeingDestroyed())
return;
FontCachePurgePreventer fontCachePurgePreventer;
m_layoutAttributesBuilder.clearTextPositioningElements();
if (!child->isSVGInlineText() && !child->isSVGInline())
return;
Vector<SVGTextLayoutAttributes*> newLayoutAttributes;
collectLayoutAttributes(this, newLayoutAttributes);
if (newLayoutAttributes.isEmpty()) {
ASSERT(m_layoutAttributes.isEmpty());
return;
}
size_t size = newLayoutAttributes.size();
SVGTextLayoutAttributes* attributes = 0;
for (size_t i = 0; i < size; ++i) {
attributes = newLayoutAttributes[i];
if (m_layoutAttributes.find(attributes) == kNotFound) {
SVGTextLayoutAttributes* previous = 0;
SVGTextLayoutAttributes* next = 0;
ASSERT_UNUSED(child, attributes->context() == child);
findPreviousAndNextAttributes(this, attributes->context(), previous, next);
if (previous)
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(previous->context());
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(attributes->context());
if (next)
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(next->context());
break;
}
}
#ifndef NDEBUG
for (size_t i = 0; i < size; ++i)
ASSERT(m_layoutAttributes.find(newLayoutAttributes[i]) != kNotFound || newLayoutAttributes[i] == attributes);
#endif
m_layoutAttributes = newLayoutAttributes;
}
static inline void checkLayoutAttributesConsistency(RenderSVGText* text, Vector<SVGTextLayoutAttributes*>& expectedLayoutAttributes)
{
#ifndef NDEBUG
Vector<SVGTextLayoutAttributes*> newLayoutAttributes;
collectLayoutAttributes(text, newLayoutAttributes);
ASSERT(newLayoutAttributes == expectedLayoutAttributes);
#endif
}
void RenderSVGText::willBeDestroyed()
{
m_layoutAttributes.clear();
m_layoutAttributesBuilder.clearTextPositioningElements();
RenderSVGBlock::willBeDestroyed();
}
void RenderSVGText::subtreeChildWillBeRemoved(RenderObject* child, Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
{
ASSERT(child);
if (!shouldHandleSubtreeMutations())
return;
checkLayoutAttributesConsistency(this, m_layoutAttributes);
m_layoutAttributesBuilder.clearTextPositioningElements();
if (m_layoutAttributes.isEmpty() || !child->isSVGInlineText())
return;
RenderSVGInlineText* text = toRenderSVGInlineText(child);
SVGTextLayoutAttributes* previous = 0;
SVGTextLayoutAttributes* next = 0;
if (!documentBeingDestroyed())
findPreviousAndNextAttributes(this, text, previous, next);
if (previous)
affectedAttributes.append(previous);
if (next)
affectedAttributes.append(next);
size_t position = m_layoutAttributes.find(text->layoutAttributes());
ASSERT(position != kNotFound);
m_layoutAttributes.remove(position);
}
void RenderSVGText::subtreeChildWasRemoved(const Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
{
if (!shouldHandleSubtreeMutations() || documentBeingDestroyed()) {
ASSERT(affectedAttributes.isEmpty());
return;
}
unsigned size = affectedAttributes.size();
for (unsigned i = 0; i < size; ++i)
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(affectedAttributes[i]->context());
}
void RenderSVGText::subtreeStyleDidChange()
{
if (!shouldHandleSubtreeMutations() || documentBeingDestroyed())
return;
checkLayoutAttributesConsistency(this, m_layoutAttributes);
FontCachePurgePreventer fontCachePurgePreventer;
for (RenderObject* descendant = firstChild(); descendant; descendant = descendant->nextInPreOrder(this)) {
if (descendant->isSVGInlineText())
m_layoutAttributesBuilder.rebuildMetricsForTextRenderer(toRenderSVGInlineText(descendant));
}
}
void RenderSVGText::subtreeTextDidChange(RenderSVGInlineText* text)
{
ASSERT(text);
ASSERT(!beingDestroyed());
if (!everHadLayout()) {
ASSERT(m_layoutAttributes.isEmpty());
ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
return;
}
FontCachePurgePreventer fontCachePurgePreventer;
m_layoutAttributesBuilder.clearTextPositioningElements();
checkLayoutAttributesConsistency(this, m_layoutAttributes);
for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
if (descendant->isSVGInlineText())
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(toRenderSVGInlineText(descendant));
}
}
static inline void updateFontInAllDescendants(RenderObject* start, SVGTextLayoutAttributesBuilder* builder = 0)
{
for (RenderObject* descendant = start; descendant; descendant = descendant->nextInPreOrder(start)) {
if (!descendant->isSVGInlineText())
continue;
RenderSVGInlineText* text = toRenderSVGInlineText(descendant);
text->updateScaledFont();
if (builder)
builder->rebuildMetricsForTextRenderer(text);
}
}
void RenderSVGText::layout()
{
ASSERT(needsLayout());
subtreeStyleDidChange();
LayoutRectRecorder recorder(*this);
LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
bool updateCachedBoundariesInParents = false;
if (m_needsTransformUpdate) {
m_localTransform = toSVGTextElement(node())->animatedLocalTransform();
m_needsTransformUpdate = false;
updateCachedBoundariesInParents = true;
}
if (!everHadLayout()) {
ASSERT(m_layoutAttributes.isEmpty());
collectLayoutAttributes(this, m_layoutAttributes);
updateFontInAllDescendants(this);
m_layoutAttributesBuilder.buildLayoutAttributesForForSubtree(this);
m_needsReordering = true;
m_needsTextMetricsUpdate = false;
m_needsPositioningValuesUpdate = false;
updateCachedBoundariesInParents = true;
} else if (m_needsPositioningValuesUpdate) {
if (m_needsTextMetricsUpdate) {
updateFontInAllDescendants(this);
m_needsTextMetricsUpdate = false;
}
m_layoutAttributesBuilder.buildLayoutAttributesForForSubtree(this);
m_needsReordering = true;
m_needsPositioningValuesUpdate = false;
updateCachedBoundariesInParents = true;
} else if (m_needsTextMetricsUpdate || SVGRenderSupport::findTreeRootObject(this)->isLayoutSizeChanged()) {
updateFontInAllDescendants(this, &m_layoutAttributesBuilder);
ASSERT(!m_needsReordering);
ASSERT(!m_needsPositioningValuesUpdate);
m_needsTextMetricsUpdate = false;
updateCachedBoundariesInParents = true;
}
checkLayoutAttributesConsistency(this, m_layoutAttributes);
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());
rebuildFloatsFromIntruding();
LayoutUnit beforeEdge = borderBefore() + paddingBefore();
LayoutUnit afterEdge = borderAfter() + paddingAfter() + scrollbarLogicalHeight();
setLogicalHeight(beforeEdge);
LayoutUnit repaintLogicalTop = 0;
LayoutUnit repaintLogicalBottom = 0;
layoutInlineChildren(true, repaintLogicalTop, repaintLogicalBottom, afterEdge);
if (m_needsReordering)
m_needsReordering = false;
if (!updateCachedBoundariesInParents)
updateCachedBoundariesInParents = oldBoundaries != objectBoundingBox();
if (everHadLayout() && selfNeedsLayout())
SVGResourcesCache::clientLayoutChanged(this);
if (updateCachedBoundariesInParents)
RenderSVGBlock::setNeedsBoundariesUpdate();
repainter.repaintAfterLayout();
clearNeedsLayout();
}
RootInlineBox* RenderSVGText::createRootInlineBox()
{
RootInlineBox* box = new 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.canHitBoundingBox && !objectBoundingBox().isEmpty())
|| (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;
if (hitRules.canHitBoundingBox && !objectBoundingBox().contains(localPoint))
return false;
HitTestLocation hitTestLocation(LayoutPoint(flooredIntPoint(localPoint)));
return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), hitTestAction);
}
}
return false;
}
PositionWithAffinity RenderSVGText::positionForPoint(const LayoutPoint& pointInContents)
{
RootInlineBox* rootBox = firstRootBox();
if (!rootBox)
return createPositionWithAffinity(0, DOWNSTREAM);
ASSERT(!rootBox->nextRootBox());
ASSERT(childrenInline());
InlineBox* closestBox = toSVGRootInlineBox(rootBox)->closestLeafChildForPosition(pointInContents);
if (!closestBox)
return createPositionWithAffinity(0, DOWNSTREAM);
return closestBox->renderer().positionForPoint(LayoutPoint(pointInContents.x(), closestBox->y()));
}
void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
quads.append(localToAbsoluteQuad(strokeBoundingBox(), 0 , 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, false);
const AffineTransform& localTransform = localToParentTransform();
if (!localTransform.isIdentity()) {
stateSaver.save();
blockInfo.applyTransform(localTransform, false);
}
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(toSVGElement(node()));
strokeBoundaries.inflate(svgStyle->strokeWidth()->value(lengthContext));
return strokeBoundaries;
}
FloatRect RenderSVGText::repaintRectInLocalCoordinates() const
{
FloatRect repaintRect = strokeBoundingBox();
SVGRenderSupport::intersectRepaintRectWithResources(this, repaintRect);
if (const ShadowList* textShadow = style()->textShadow())
textShadow->adjustRectForShadow(repaintRect);
return repaintRect;
}
void RenderSVGText::addChild(RenderObject* child, RenderObject* beforeChild)
{
RenderSVGBlock::addChild(child, beforeChild);
SVGResourcesCache::clientWasAddedToTree(child, child->style());
subtreeChildWasAdded(child);
}
void RenderSVGText::removeChild(RenderObject* child)
{
SVGResourcesCache::clientWillBeRemovedFromTree(child);
Vector<SVGTextLayoutAttributes*, 2> affectedAttributes;
FontCachePurgePreventer fontCachePurgePreventer;
subtreeChildWillBeRemoved(child, affectedAttributes);
RenderSVGBlock::removeChild(child);
subtreeChildWasRemoved(affectedAttributes);
}
RenderBlock* RenderSVGText::firstLineBlock() const
{
return 0;
}
void RenderSVGText::updateFirstLetter()
{
}
}