/
SVGFitToViewBox.cpp
155 lines (130 loc) · 5.22 KB
/
SVGFitToViewBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006, 2007, 2010 Rob Buis <buis@kde.org>
* Copyright (C) 2018 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#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/StringParsingBuffer.h>
#include <wtf/text/StringView.h>
namespace WebCore {
SVGFitToViewBox::SVGFitToViewBox(SVGElement* contextElement, SVGPropertyAccess access)
: m_viewBox(SVGAnimatedRect::create(contextElement, access))
, m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(contextElement, access))
{
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
PropertyRegistry::registerProperty<SVGNames::viewBoxAttr, &SVGFitToViewBox::m_viewBox>();
PropertyRegistry::registerProperty<SVGNames::preserveAspectRatioAttr, &SVGFitToViewBox::m_preserveAspectRatio>();
});
}
void SVGFitToViewBox::setViewBox(const FloatRect& viewBox)
{
m_viewBox->setBaseValInternal(viewBox);
m_isViewBoxValid = true;
}
void SVGFitToViewBox::resetViewBox()
{
m_viewBox->setBaseValInternal({ });
m_isViewBoxValid = false;
}
void SVGFitToViewBox::reset()
{
resetViewBox();
resetPreserveAspectRatio();
}
bool SVGFitToViewBox::parseAttribute(const QualifiedName& name, const AtomString& value)
{
if (name == SVGNames::viewBoxAttr) {
if (!value.isNull()) {
if (auto result = parseViewBox(value)) {
setViewBox(WTFMove(*result));
return true;
}
}
resetViewBox();
return true;
}
if (name == SVGNames::preserveAspectRatioAttr) {
setPreserveAspectRatio(SVGPreserveAspectRatioValue { value });
return true;
}
return false;
}
std::optional<FloatRect> SVGFitToViewBox::parseViewBox(StringView value)
{
return readCharactersForParsing(value, [&](auto buffer) {
return parseViewBoxGeneric(buffer);
});
}
std::optional<FloatRect> SVGFitToViewBox::parseViewBox(StringParsingBuffer<LChar>& buffer, bool validate)
{
return parseViewBoxGeneric(buffer, validate);
}
std::optional<FloatRect> SVGFitToViewBox::parseViewBox(StringParsingBuffer<UChar>& buffer, bool validate)
{
return parseViewBoxGeneric(buffer, validate);
}
template<typename CharacterType> std::optional<FloatRect> SVGFitToViewBox::parseViewBoxGeneric(StringParsingBuffer<CharacterType>& buffer, bool validate)
{
StringView stringToParse = buffer.stringViewOfCharactersRemaining();
skipOptionalSVGSpaces(buffer);
auto x = parseNumber(buffer);
auto y = parseNumber(buffer);
auto width = parseNumber(buffer);
auto height = parseNumber(buffer, SuffixSkippingPolicy::DontSkip);
if (validate) {
Document& document = m_viewBox->contextElement()->document();
if (!x || !y || !width || !height) {
document.accessSVGExtensions().reportWarning(makeString("Problem parsing viewBox=\"", stringToParse, "\""));
return std::nullopt;
}
// Check that width is positive.
if (*width < 0.0) {
document.accessSVGExtensions().reportError("A negative value for ViewBox width is not allowed"_s);
return std::nullopt;
}
// Check that height is positive.
if (*height < 0.0) {
document.accessSVGExtensions().reportError("A negative value for ViewBox height is not allowed"_s);
return std::nullopt;
}
// Nothing should come after the last, fourth number.
skipOptionalSVGSpaces(buffer);
if (buffer.hasCharactersRemaining()) {
document.accessSVGExtensions().reportWarning(makeString("Problem parsing viewBox=\"", stringToParse, "\""));
return std::nullopt;
}
}
return FloatRect { x.value_or(0), y.value_or(0), width.value_or(0), height.value_or(0) };
}
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);
}
}