#include "config.h"
#include "RenderSVGText.h"
#include "FloatQuad.h"
#include "Font.h"
#include "GraphicsContext.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "LayoutRepainter.h"
#include "PointerEventsHitRules.h"
#include "RenderIterator.h"
#include "RenderSVGInline.h"
#include "RenderSVGInlineText.h"
#include "RenderSVGResource.h"
#include "RenderSVGRoot.h"
#include "SVGLengthList.h"
#include "SVGResourcesCache.h"
#include "SVGRootInlineBox.h"
#include "SVGTextElement.h"
#include "SVGURIReference.h"
#include "TransformState.h"
#include "VisiblePosition.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/StackStats.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGText);
RenderSVGText::RenderSVGText(SVGTextElement& element, RenderStyle&& style)
: RenderSVGBlock(element, WTFMove(style))
, m_needsReordering(false)
, m_needsPositioningValuesUpdate(false)
, m_needsTransformUpdate(true)
, m_needsTextMetricsUpdate(false)
{
}
RenderSVGText::~RenderSVGText()
{
ASSERT(m_layoutAttributes.isEmpty());
}
SVGTextElement& RenderSVGText::textElement() const
{
return downcast<SVGTextElement>(RenderSVGBlock::graphicsElement());
}
bool RenderSVGText::isChildAllowed(const RenderObject& child, const RenderStyle&) const
{
return child.isInline();
}
RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(RenderObject& start)
{
return lineageOfType<RenderSVGText>(start).first();
}
const RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(const RenderObject& start)
{
return lineageOfType<RenderSVGText>(start).first();
}
LayoutRect RenderSVGText::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
{
return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer);
}
LayoutRect RenderSVGText::computeRectForRepaint(const LayoutRect& rect, const RenderLayerModelObject* repaintContainer, RepaintContext context) const
{
return enclosingLayoutRect(computeFloatRectForRepaint(rect, repaintContainer, context.m_hasPositionFixedDescendant));
}
FloatRect RenderSVGText::computeFloatRectForRepaint(const FloatRect& repaintRect, const RenderLayerModelObject* repaintContainer, bool fixed) const
{
return SVGRenderSupport::computeFloatRectForRepaint(*this, repaintRect, repaintContainer, fixed);
}
void RenderSVGText::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
{
SVGRenderSupport::mapLocalToContainer(*this, repaintContainer, transformState, wasFixed);
}
const RenderObject* RenderSVGText::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
return SVGRenderSupport::pushMappingToContainer(*this, ancestorToStopAt, geometryMap);
}
static inline void collectLayoutAttributes(RenderObject* text, Vector<SVGTextLayoutAttributes*>& attributes)
{
for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
if (is<RenderSVGInlineText>(*descendant))
attributes.append(downcast<RenderSVGInlineText>(*descendant).layoutAttributes());
}
}
static inline bool findPreviousAndNextAttributes(RenderElement& start, RenderSVGInlineText* locateElement, bool& stopAfterNext, SVGTextLayoutAttributes*& previous, SVGTextLayoutAttributes*& next)
{
ASSERT(locateElement);
for (auto& child : childrenOfType<RenderObject>(start)) {
if (is<RenderSVGInlineText>(child)) {
auto& text = downcast<RenderSVGInlineText>(child);
if (locateElement != &text) {
if (stopAfterNext) {
next = text.layoutAttributes();
return true;
}
previous = text.layoutAttributes();
continue;
}
stopAfterNext = true;
continue;
}
if (!is<RenderSVGInline>(child))
continue;
if (findPreviousAndNextAttributes(downcast<RenderElement>(child), locateElement, stopAfterNext, previous, next))
return true;
}
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() || renderTreeBeingDestroyed())
return;
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) == notFound) {
bool stopAfterNext = false;
SVGTextLayoutAttributes* previous = 0;
SVGTextLayoutAttributes* next = 0;
ASSERT_UNUSED(child, &attributes->context() == child);
findPreviousAndNextAttributes(*this, &attributes->context(), stopAfterNext, 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]) != notFound || 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);
#else
UNUSED_PARAM(text);
UNUSED_PARAM(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;
auto& text = downcast<RenderSVGInlineText>(*child);
bool stopAfterNext = false;
SVGTextLayoutAttributes* previous = nullptr;
SVGTextLayoutAttributes* next = nullptr;
if (!renderTreeBeingDestroyed())
findPreviousAndNextAttributes(*this, &text, stopAfterNext, previous, next);
if (previous)
affectedAttributes.append(previous);
if (next)
affectedAttributes.append(next);
bool removed = m_layoutAttributes.removeFirst(text.layoutAttributes());
ASSERT_UNUSED(removed, removed);
}
void RenderSVGText::subtreeChildWasRemoved(const Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
{
if (!shouldHandleSubtreeMutations() || renderTreeBeingDestroyed()) {
ASSERT(affectedAttributes.isEmpty());
return;
}
unsigned size = affectedAttributes.size();
for (unsigned i = 0; i < size; ++i)
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(affectedAttributes[i]->context());
}
void RenderSVGText::subtreeStyleDidChange(RenderSVGInlineText* text)
{
ASSERT(text);
if (!shouldHandleSubtreeMutations() || renderTreeBeingDestroyed())
return;
checkLayoutAttributesConsistency(this, m_layoutAttributes);
for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
if (is<RenderSVGInlineText>(*descendant))
m_layoutAttributesBuilder.rebuildMetricsForTextRenderer(downcast<RenderSVGInlineText>(*descendant));
}
}
void RenderSVGText::subtreeTextDidChange(RenderSVGInlineText* text)
{
ASSERT(text);
ASSERT(!beingDestroyed());
if (!everHadLayout()) {
ASSERT(m_layoutAttributes.isEmpty());
ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
return;
}
if (!m_layoutAttributes.contains(text->layoutAttributes())) {
ASSERT(!text->everHadLayout());
return;
}
m_layoutAttributesBuilder.clearTextPositioningElements();
checkLayoutAttributesConsistency(this, m_layoutAttributes);
for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
if (is<RenderSVGInlineText>(*descendant))
m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(downcast<RenderSVGInlineText>(*descendant));
}
}
static inline void updateFontInAllDescendants(RenderObject* start, SVGTextLayoutAttributesBuilder* builder = nullptr)
{
for (RenderObject* descendant = start; descendant; descendant = descendant->nextInPreOrder(start)) {
if (!is<RenderSVGInlineText>(*descendant))
continue;
auto& text = downcast<RenderSVGInlineText>(*descendant);
text.updateScaledFont();
if (builder)
builder->rebuildMetricsForTextRenderer(text);
}
}
void RenderSVGText::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(*this));
bool updateCachedBoundariesInParents = false;
if (m_needsTransformUpdate) {
m_localTransform = textElement().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(!multiColumnFlow());
ASSERT(!positionedObjects());
ASSERT(!m_overflow);
ASSERT(!isAnonymousBlock());
if (!firstChild())
setChildrenInline(true);
FloatRect oldBoundaries = objectBoundingBox();
ASSERT(childrenInline());
LayoutUnit repaintLogicalTop = 0;
LayoutUnit repaintLogicalBottom = 0;
rebuildFloatingObjectSetFromIntrudingFloats();
layoutInlineChildren(true, repaintLogicalTop, repaintLogicalBottom);
if (m_needsReordering)
m_needsReordering = false;
if (!updateCachedBoundariesInParents)
updateCachedBoundariesInParents = oldBoundaries != objectBoundingBox();
if (everHadLayout() && selfNeedsLayout())
SVGResourcesCache::clientLayoutChanged(*this);
if (updateCachedBoundariesInParents)
RenderSVGBlock::setNeedsBoundariesUpdate();
repainter.repaintAfterLayout();
clearNeedsLayout();
}
std::unique_ptr<RootInlineBox> RenderSVGText::createRootInlineBox()
{
auto box = std::make_unique<SVGRootInlineBox>(*this);
box->setHasVirtualLogicalHeight();
return WTFMove(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() == 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().value_or(AffineTransform()).mapPoint(pointInParent);
if (!SVGRenderSupport::pointInClippingArea(*this, localPoint))
return false;
HitTestLocation hitTestLocation(LayoutPoint(flooredIntPoint(localPoint)));
return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), hitTestAction);
}
}
return false;
}
bool RenderSVGText::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
{
ASSERT_NOT_REACHED();
return false;
}
VisiblePosition RenderSVGText::positionForPoint(const LayoutPoint& pointInContents, const RenderFragmentContainer* fragment)
{
RootInlineBox* rootBox = firstRootBox();
if (!rootBox)
return createVisiblePosition(0, DOWNSTREAM);
ASSERT(!rootBox->nextRootBox());
ASSERT(childrenInline());
InlineBox* closestBox = downcast<SVGRootInlineBox>(*rootBox).closestLeafChildForPosition(pointInContents);
if (!closestBox)
return createVisiblePosition(0, DOWNSTREAM);
return closestBox->renderer().positionForPoint(LayoutPoint(pointInContents.x(), closestBox->y()), fragment);
}
void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
quads.append(localToAbsoluteQuad(strokeBoundingBox(), UseTransforms, wasFixed));
}
void RenderSVGText::paint(PaintInfo& paintInfo, const LayoutPoint&)
{
if (paintInfo.context().paintingDisabled())
return;
if (paintInfo.phase != PaintPhaseForeground
&& paintInfo.phase != PaintPhaseSelection)
return;
PaintInfo blockInfo(paintInfo);
GraphicsContextStateSaver stateSaver(blockInfo.context());
blockInfo.applyTransform(localToParentTransform());
RenderBlock::paint(blockInfo, LayoutPoint());
if (paintInfo.phase == PaintPhaseForeground) {
blockInfo.phase = PaintPhaseSelfOutline;
RenderBlock::paint(blockInfo, LayoutPoint());
}
}
FloatRect RenderSVGText::strokeBoundingBox() const
{
FloatRect strokeBoundaries = objectBoundingBox();
const SVGRenderStyle& svgStyle = style().svgStyle();
if (!svgStyle.hasStroke())
return strokeBoundaries;
SVGLengthContext lengthContext(&textElement());
strokeBoundaries.inflate(lengthContext.valueForLength(style().strokeWidth()));
return strokeBoundaries;
}
FloatRect RenderSVGText::repaintRectInLocalCoordinates() const
{
FloatRect repaintRect = strokeBoundingBox();
SVGRenderSupport::intersectRepaintRectWithResources(*this, repaintRect);
if (const ShadowData* textShadow = style().textShadow())
textShadow->adjustRectForShadow(repaintRect);
return repaintRect;
}
RenderBlock* RenderSVGText::firstLineBlock() const
{
return 0;
}
}