SVGTextContentElement.cpp [plain text]
#include "config.h"
#include "SVGTextContentElement.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "DOMPoint.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "RenderObject.h"
#include "RenderSVGResource.h"
#include "RenderSVGText.h"
#include "SVGDocumentExtensions.h"
#include "SVGNames.h"
#include "SVGPoint.h"
#include "SVGRect.h"
#include "SVGTextQuery.h"
#include "XMLNames.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGTextContentElement);
SVGTextContentElement::SVGTextContentElement(const QualifiedName& tagName, Document& document)
: SVGGraphicsElement(tagName, document)
, SVGExternalResourcesRequired(this)
{
registerAttributes();
}
unsigned SVGTextContentElement::getNumberOfChars()
{
document().updateLayoutIgnorePendingStylesheets();
return SVGTextQuery(renderer()).numberOfCharacters();
}
float SVGTextContentElement::getComputedTextLength()
{
document().updateLayoutIgnorePendingStylesheets();
return SVGTextQuery(renderer()).textLength();
}
ExceptionOr<float> SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars)
{
unsigned numberOfChars = getNumberOfChars();
if (charnum >= numberOfChars)
return Exception { IndexSizeError };
nchars = std::min(nchars, numberOfChars - charnum);
return SVGTextQuery(renderer()).subStringLength(charnum, nchars);
}
ExceptionOr<Ref<SVGPoint>> SVGTextContentElement::getStartPositionOfChar(unsigned charnum)
{
if (charnum > getNumberOfChars())
return Exception { IndexSizeError };
return SVGPoint::create(SVGTextQuery(renderer()).startPositionOfCharacter(charnum));
}
ExceptionOr<Ref<SVGPoint>> SVGTextContentElement::getEndPositionOfChar(unsigned charnum)
{
if (charnum > getNumberOfChars())
return Exception { IndexSizeError };
return SVGPoint::create(SVGTextQuery(renderer()).endPositionOfCharacter(charnum));
}
ExceptionOr<Ref<SVGRect>> SVGTextContentElement::getExtentOfChar(unsigned charnum)
{
if (charnum > getNumberOfChars())
return Exception { IndexSizeError };
return SVGRect::create(SVGTextQuery(renderer()).extentOfCharacter(charnum));
}
ExceptionOr<float> SVGTextContentElement::getRotationOfChar(unsigned charnum)
{
if (charnum > getNumberOfChars())
return Exception { IndexSizeError };
return SVGTextQuery(renderer()).rotationOfCharacter(charnum);
}
int SVGTextContentElement::getCharNumAtPosition(DOMPointInit&& pointInit)
{
document().updateLayoutIgnorePendingStylesheets();
FloatPoint transformPoint {static_cast<float>(pointInit.x), static_cast<float>(pointInit.y)};
return SVGTextQuery(renderer()).characterNumberAtPosition(transformPoint);
}
ExceptionOr<void> SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars)
{
unsigned numberOfChars = getNumberOfChars();
if (charnum >= numberOfChars)
return Exception { IndexSizeError };
nchars = std::min(nchars, numberOfChars - charnum);
ASSERT(document().frame());
FrameSelection& selection = document().frame()->selection();
VisiblePosition start(firstPositionInNode(const_cast<SVGTextContentElement*>(this)));
for (unsigned i = 0; i < charnum; ++i)
start = start.next();
VisiblePosition end(start);
for (unsigned i = 0; i < nchars; ++i)
end = end.next();
selection.setSelection(VisibleSelection(start, end));
return { };
}
bool SVGTextContentElement::isPresentationAttribute(const QualifiedName& name) const
{
if (name.matches(XMLNames::spaceAttr))
return true;
return SVGGraphicsElement::isPresentationAttribute(name);
}
void SVGTextContentElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style)
{
if (name.matches(XMLNames::spaceAttr)) {
if (value == "preserve")
addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre);
else
addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueNowrap);
return;
}
SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
}
void SVGTextContentElement::registerAttributes()
{
auto& registry = attributeRegistry();
if (!registry.isEmpty())
return;
registry.registerAttribute(SVGAnimatedCustomLengthAttributeAccessor::singleton<SVGNames::textLengthAttr, &SVGTextContentElement::m_textLength>());
registry.registerAttribute<SVGNames::lengthAdjustAttr, SVGLengthAdjustType, &SVGTextContentElement::m_lengthAdjust>();
}
void SVGTextContentElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
SVGParsingError parseError = NoError;
if (name == SVGNames::lengthAdjustAttr) {
auto propertyValue = SVGPropertyTraits<SVGLengthAdjustType>::fromString(value);
if (propertyValue > 0)
m_lengthAdjust.setValue(propertyValue);
} else if (name == SVGNames::textLengthAttr)
m_textLength.setValue(SVGLengthValue::construct(LengthModeOther, value, parseError, ForbidNegativeLengths));
reportAttributeParsingError(parseError, name, value);
SVGGraphicsElement::parseAttribute(name, value);
SVGExternalResourcesRequired::parseAttribute(name, value);
}
void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (isKnownAttribute(attrName)) {
if (attrName == SVGNames::textLengthAttr)
m_specifiedTextLength = m_textLength.value();
if (auto renderer = this->renderer()) {
InstanceInvalidationGuard guard(*this);
RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
}
return;
}
SVGGraphicsElement::svgAttributeChanged(attrName);
SVGExternalResourcesRequired::svgAttributeChanged(attrName);
}
bool SVGTextContentElement::selfHasRelativeLengths() const
{
return true;
}
SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer)
{
if (!renderer)
return nullptr;
if (!renderer->isSVGText() && !renderer->isSVGInline())
return nullptr;
SVGElement* element = downcast<SVGElement>(renderer->node());
ASSERT(element);
if (!is<SVGTextContentElement>(element))
return nullptr;
return downcast<SVGTextContentElement>(element);
}
}