SVGFitToViewBox.cpp [plain text]
#include "config.h"
#include "SVGFitToViewBox.h"
#include "AffineTransform.h"
#include "Document.h"
#include "FloatRect.h"
#include "SVGDocumentExtensions.h"
#include "SVGElement.h"
#include "SVGNames.h"
#include "SVGParserUtilities.h"
#include "SVGPreserveAspectRatioValue.h"
#include <wtf/text/StringView.h>
namespace WebCore {
SVGFitToViewBox::SVGFitToViewBox(SVGElement* contextElement, AnimatedPropertyState animatedState)
: m_attributeOwnerProxy(*this, *contextElement, animatedState)
{
registerAttributes();
}
void SVGFitToViewBox::registerAttributes()
{
auto& registry = attributeRegistry();
if (!registry.isEmpty())
return;
registry.registerAttribute<SVGNames::viewBoxAttr, &SVGFitToViewBox::m_viewBox>();
registry.registerAttribute<SVGNames::preserveAspectRatioAttr, &SVGFitToViewBox::m_preserveAspectRatio>();
}
void SVGFitToViewBox::setViewBox(const FloatRect& viewBox)
{
m_viewBox.setValue(viewBox);
m_isViewBoxValid = true;
}
void SVGFitToViewBox::resetViewBox()
{
m_viewBox.resetValue();
m_isViewBoxValid = false;
}
void SVGFitToViewBox::reset()
{
resetViewBox();
resetPreserveAspectRatio();
}
bool SVGFitToViewBox::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
if (name == SVGNames::viewBoxAttr) {
FloatRect viewBox;
if (!value.isNull() && parseViewBox(value, viewBox))
setViewBox(viewBox);
else
resetViewBox();
return true;
}
if (name == SVGNames::preserveAspectRatioAttr) {
SVGPreserveAspectRatioValue preserveAspectRatio;
preserveAspectRatio.parse(value);
setPreserveAspectRatio(preserveAspectRatio);
return true;
}
return false;
}
bool SVGFitToViewBox::parseViewBox(const AtomicString& value, FloatRect& viewBox)
{
auto upconvertedCharacters = StringView(value).upconvertedCharacters();
const UChar* characters = upconvertedCharacters;
return parseViewBox(characters, characters + value.length(), viewBox);
}
bool SVGFitToViewBox::parseViewBox(const UChar*& c, const UChar* end, FloatRect& viewBox, bool validate)
{
String str(c, end - c);
skipOptionalSVGSpaces(c, end);
float x = 0.0f;
float y = 0.0f;
float width = 0.0f;
float height = 0.0f;
bool valid = parseNumber(c, end, x) && parseNumber(c, end, y) && parseNumber(c, end, width) && parseNumber(c, end, height, false);
if (validate) {
Document& document = m_attributeOwnerProxy.element().document();
if (!valid) {
document.accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\"");
return false;
}
if (width < 0.0) {
document.accessSVGExtensions().reportError("A negative value for ViewBox width is not allowed");
return false;
}
if (height < 0.0) {
document.accessSVGExtensions().reportError("A negative value for ViewBox height is not allowed");
return false;
}
skipOptionalSVGSpaces(c, end);
if (c < end) {
document.accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\"");
return false;
}
}
viewBox = { x, y, width, height };
return true;
}
AffineTransform SVGFitToViewBox::viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatioValue& preserveAspectRatio, float viewWidth, float viewHeight)
{
if (!viewBoxRect.width() || !viewBoxRect.height() || !viewWidth || !viewHeight)
return AffineTransform();
return preserveAspectRatio.getCTM(viewBoxRect.x(), viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), viewWidth, viewHeight);
}
}