2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Rob Buis <buis@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "SVGFitToViewBox.h"
24 #include "AffineTransform.h"
25 #include "Attribute.h"
27 #include "FloatRect.h"
28 #include "SVGDocumentExtensions.h"
30 #include "SVGParserUtilities.h"
31 #include "SVGPreserveAspectRatio.h"
32 #include <wtf/text/StringView.h>
36 bool SVGFitToViewBox::parseViewBox(Document* doc, const String& s, FloatRect& viewBox)
38 auto upconvertedCharacters = StringView(s).upconvertedCharacters();
39 const UChar* characters = upconvertedCharacters;
40 return parseViewBox(doc, characters, characters + s.length(), viewBox, true);
43 bool SVGFitToViewBox::parseViewBox(Document* doc, const UChar*& c, const UChar* end, FloatRect& viewBox, bool validate)
45 String str(c, end - c);
47 skipOptionalSVGSpaces(c, end);
53 bool valid = parseNumber(c, end, x) && parseNumber(c, end, y) && parseNumber(c, end, width) && parseNumber(c, end, height, false);
55 viewBox = FloatRect(x, y, width, height);
59 doc->accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\"");
63 if (width < 0.0) { // check that width is positive
64 doc->accessSVGExtensions().reportError("A negative value for ViewBox width is not allowed");
67 if (height < 0.0) { // check that height is positive
68 doc->accessSVGExtensions().reportError("A negative value for ViewBox height is not allowed");
71 skipOptionalSVGSpaces(c, end);
72 if (c < end) { // nothing should come after the last, fourth number
73 doc->accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\"");
77 viewBox = FloatRect(x, y, width, height);
81 AffineTransform SVGFitToViewBox::viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatio& preserveAspectRatio, float viewWidth, float viewHeight)
83 if (!viewBoxRect.width() || !viewBoxRect.height())
84 return AffineTransform();
86 return preserveAspectRatio.getCTM(viewBoxRect.x(), viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), viewWidth, viewHeight);
89 bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName)
91 return attrName == SVGNames::viewBoxAttr || attrName == SVGNames::preserveAspectRatioAttr;
94 void SVGFitToViewBox::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
96 supportedAttributes.add(SVGNames::viewBoxAttr);
97 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);