SVGTextPathElement.cpp [plain text]
#include "config.h"
#include "SVGTextPathElement.h"
#include "Attribute.h"
#include "RenderSVGResource.h"
#include "RenderSVGTextPath.h"
#include "SVGElementInstance.h"
#include "SVGNames.h"
#include "XLinkNames.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
DEFINE_ANIMATED_LENGTH(SVGTextPathElement, SVGNames::startOffsetAttr, StartOffset, startOffset)
DEFINE_ANIMATED_ENUMERATION(SVGTextPathElement, SVGNames::methodAttr, Method, method, SVGTextPathMethodType)
DEFINE_ANIMATED_ENUMERATION(SVGTextPathElement, SVGNames::spacingAttr, Spacing, spacing, SVGTextPathSpacingType)
DEFINE_ANIMATED_STRING(SVGTextPathElement, XLinkNames::hrefAttr, Href, href)
BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGTextPathElement)
REGISTER_LOCAL_ANIMATED_PROPERTY(startOffset)
REGISTER_LOCAL_ANIMATED_PROPERTY(method)
REGISTER_LOCAL_ANIMATED_PROPERTY(spacing)
REGISTER_LOCAL_ANIMATED_PROPERTY(href)
REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTextContentElement)
END_REGISTER_ANIMATED_PROPERTIES
inline SVGTextPathElement::SVGTextPathElement(const QualifiedName& tagName, Document& document)
: SVGTextContentElement(tagName, document)
, m_startOffset(LengthModeOther)
, m_method(SVGTextPathMethodAlign)
, m_spacing(SVGTextPathSpacingExact)
{
ASSERT(hasTagName(SVGNames::textPathTag));
registerAnimatedPropertiesForSVGTextPathElement();
}
PassRefPtr<SVGTextPathElement> SVGTextPathElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(new SVGTextPathElement(tagName, document));
}
SVGTextPathElement::~SVGTextPathElement()
{
clearResourceReferences();
}
void SVGTextPathElement::clearResourceReferences()
{
document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
}
bool SVGTextPathElement::isSupportedAttribute(const QualifiedName& attrName)
{
static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
if (supportedAttributes.get().isEmpty()) {
SVGURIReference::addSupportedAttributes(supportedAttributes);
supportedAttributes.get().add(SVGNames::startOffsetAttr);
supportedAttributes.get().add(SVGNames::methodAttr);
supportedAttributes.get().add(SVGNames::spacingAttr);
}
return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
}
void SVGTextPathElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
SVGParsingError parseError = NoError;
if (!isSupportedAttribute(name))
SVGTextContentElement::parseAttribute(name, value);
else if (name == SVGNames::startOffsetAttr)
setStartOffsetBaseValue(SVGLength::construct(LengthModeOther, value, parseError));
else if (name == SVGNames::methodAttr) {
SVGTextPathMethodType propertyValue = SVGPropertyTraits<SVGTextPathMethodType>::fromString(value);
if (propertyValue > 0)
setMethodBaseValue(propertyValue);
} else if (name == SVGNames::spacingAttr) {
SVGTextPathSpacingType propertyValue = SVGPropertyTraits<SVGTextPathSpacingType>::fromString(value);
if (propertyValue > 0)
setSpacingBaseValue(propertyValue);
} else if (SVGURIReference::parseAttribute(name, value)) {
} else
ASSERT_NOT_REACHED();
reportAttributeParsingError(parseError, name, value);
}
void SVGTextPathElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (!isSupportedAttribute(attrName)) {
SVGTextContentElement::svgAttributeChanged(attrName);
return;
}
SVGElementInstance::InvalidationGuard invalidationGuard(this);
if (SVGURIReference::isKnownAttribute(attrName)) {
buildPendingResource();
return;
}
if (attrName == SVGNames::startOffsetAttr)
updateRelativeLengthsInformation();
if (auto renderer = this->renderer())
RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
}
RenderPtr<RenderElement> SVGTextPathElement::createElementRenderer(PassRef<RenderStyle> style)
{
return createRenderer<RenderSVGTextPath>(*this, WTF::move(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 (!inDocument())
return;
String id;
Element* target = SVGURIReference::targetElementFromIRIString(href(), document(), &id);
if (!target) {
if (document().accessSVGExtensions()->isPendingResource(this, id))
return;
if (!id.isEmpty()) {
document().accessSVGExtensions()->addPendingResource(id, this);
ASSERT(hasPendingResources());
}
} else if (target->hasTagName(SVGNames::pathTag)) {
document().accessSVGExtensions()->addElementReferencingTarget(this, toSVGElement(target));
}
}
Node::InsertionNotificationRequest SVGTextPathElement::insertedInto(ContainerNode& rootParent)
{
SVGTextContentElement::insertedInto(rootParent);
return InsertionShouldCallDidNotifySubtreeInsertions;
}
void SVGTextPathElement::didNotifySubtreeInsertions(ContainerNode*)
{
buildPendingResource();
}
void SVGTextPathElement::removedFrom(ContainerNode& rootParent)
{
SVGTextContentElement::removedFrom(rootParent);
if (rootParent.inDocument())
clearResourceReferences();
}
bool SVGTextPathElement::selfHasRelativeLengths() const
{
return startOffset().isRelative()
|| SVGTextContentElement::selfHasRelativeLengths();
}
}