SVGTextPathElement.cpp [plain text]
#include "config.h"
#include "SVGTextPathElement.h"
#include "RenderSVGResource.h"
#include "RenderSVGTextPath.h"
#include "SVGDocumentExtensions.h"
#include "SVGNames.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGTextPathElement);
inline SVGTextPathElement::SVGTextPathElement(const QualifiedName& tagName, Document& document)
: SVGTextContentElement(tagName, document)
, SVGURIReference(this)
{
ASSERT(hasTagName(SVGNames::textPathTag));
registerAttributes();
}
Ref<SVGTextPathElement> SVGTextPathElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new SVGTextPathElement(tagName, document));
}
SVGTextPathElement::~SVGTextPathElement()
{
clearResourceReferences();
}
void SVGTextPathElement::clearResourceReferences()
{
document().accessSVGExtensions().removeAllTargetReferencesForElement(this);
}
void SVGTextPathElement::registerAttributes()
{
auto& registry = attributeRegistry();
if (!registry.isEmpty())
return;
registry.registerAttribute<SVGNames::startOffsetAttr, &SVGTextPathElement::m_startOffset>();
registry.registerAttribute<SVGNames::methodAttr, SVGTextPathMethodType, &SVGTextPathElement::m_method>();
registry.registerAttribute<SVGNames::spacingAttr, SVGTextPathSpacingType, &SVGTextPathElement::m_spacing>();
}
void SVGTextPathElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
SVGParsingError parseError = NoError;
if (name == SVGNames::startOffsetAttr)
m_startOffset.setValue(SVGLengthValue::construct(LengthModeOther, value, parseError));
else if (name == SVGNames::methodAttr) {
SVGTextPathMethodType propertyValue = SVGPropertyTraits<SVGTextPathMethodType>::fromString(value);
if (propertyValue > 0)
m_method.setValue(propertyValue);
} else if (name == SVGNames::spacingAttr) {
SVGTextPathSpacingType propertyValue = SVGPropertyTraits<SVGTextPathSpacingType>::fromString(value);
if (propertyValue > 0)
m_spacing.setValue(propertyValue);
}
reportAttributeParsingError(parseError, name, value);
SVGTextContentElement::parseAttribute(name, value);
SVGURIReference::parseAttribute(name, value);
}
void SVGTextPathElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (isKnownAttribute(attrName)) {
InstanceInvalidationGuard guard(*this);
if (attrName == SVGNames::startOffsetAttr)
updateRelativeLengthsInformation();
if (auto renderer = this->renderer())
RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
return;
}
if (SVGURIReference::isKnownAttribute(attrName)) {
buildPendingResource();
if (auto renderer = this->renderer())
RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
return;
}
SVGTextContentElement::svgAttributeChanged(attrName);
}
RenderPtr<RenderElement> SVGTextPathElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
return createRenderer<RenderSVGTextPath>(*this, WTFMove(style));
}
bool SVGTextPathElement::childShouldCreateRenderer(const Node& child) const
{
if (child.isTextNode()
|| child.hasTagName(SVGNames::aTag)
|| child.hasTagName(SVGNames::trefTag)
|| child.hasTagName(SVGNames::tspanTag))
return true;
return false;
}
bool SVGTextPathElement::rendererIsNeeded(const RenderStyle& style)
{
if (parentNode()
&& (parentNode()->hasTagName(SVGNames::aTag)
|| parentNode()->hasTagName(SVGNames::textTag)))
return StyledElement::rendererIsNeeded(style);
return false;
}
void SVGTextPathElement::buildPendingResource()
{
clearResourceReferences();
if (!isConnected())
return;
auto target = SVGURIReference::targetElementFromIRIString(href(), treeScope());
if (!target.element) {
if (document().accessSVGExtensions().isPendingResource(this, target.identifier))
return;
if (!target.identifier.isEmpty()) {
document().accessSVGExtensions().addPendingResource(target.identifier, this);
ASSERT(hasPendingResources());
}
} else if (target.element->hasTagName(SVGNames::pathTag)) {
document().accessSVGExtensions().addElementReferencingTarget(this, downcast<SVGElement>(target.element.get()));
}
}
Node::InsertedIntoAncestorResult SVGTextPathElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
SVGTextContentElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
return InsertedIntoAncestorResult::NeedsPostInsertionCallback;
}
void SVGTextPathElement::didFinishInsertingNode()
{
buildPendingResource();
}
void SVGTextPathElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
SVGTextContentElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
if (removalType.disconnectedFromDocument)
clearResourceReferences();
}
bool SVGTextPathElement::selfHasRelativeLengths() const
{
return startOffset().isRelative()
|| SVGTextContentElement::selfHasRelativeLengths();
}
}