1 /*
2 Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4
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.
9
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.
14
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.
19 */
20
21 #include "config.h"
22
23 #if ENABLE(SVG)
24 #include "SVGFitToViewBox.h"
25
26 #include "AffineTransform.h"
27 #include "Attr.h"
28 #include "Document.h"
29 #include "FloatRect.h"
30 #include "MappedAttribute.h"
31 #include "SVGNames.h"
32 #include "SVGParserUtilities.h"
33 #include "SVGPreserveAspectRatio.h"
34 #include "StringImpl.h"
35
36 namespace WebCore {
37
SVGFitToViewBox()38 SVGFitToViewBox::SVGFitToViewBox()
39 {
40 }
41
~SVGFitToViewBox()42 SVGFitToViewBox::~SVGFitToViewBox()
43 {
44 }
45
parseViewBox(Document * doc,const UChar * & c,const UChar * end,float & x,float & y,float & w,float & h,bool validate)46 bool SVGFitToViewBox::parseViewBox(Document* doc, const UChar*& c, const UChar* end, float& x, float& y, float& w, float& h, bool validate)
47 {
48 String str(c, end - c);
49
50 skipOptionalSpaces(c, end);
51
52 bool valid = (parseNumber(c, end, x) && parseNumber(c, end, y) &&
53 parseNumber(c, end, w) && parseNumber(c, end, h, false));
54 if (!validate)
55 return true;
56 if (!valid) {
57 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
58 return false;
59 }
60
61 if (w < 0.0) { // check that width is positive
62 doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
63 return false;
64 } else if (h < 0.0) { // check that height is positive
65 doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
66 return false;
67 } else {
68 skipOptionalSpaces(c, end);
69 if (c < end) { // nothing should come after the last, fourth number
70 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
71 return false;
72 }
73 }
74
75 return true;
76 }
77
viewBoxToViewTransform(const FloatRect & viewBoxRect,const SVGPreserveAspectRatio & preserveAspectRatio,float viewWidth,float viewHeight)78 AffineTransform SVGFitToViewBox::viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatio& preserveAspectRatio, float viewWidth, float viewHeight)
79 {
80 if (!viewBoxRect.width() || !viewBoxRect.height())
81 return AffineTransform();
82
83 return preserveAspectRatio.getCTM(viewBoxRect.x(), viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), 0, 0, viewWidth, viewHeight);
84 }
85
parseMappedAttribute(Document * document,MappedAttribute * attr)86 bool SVGFitToViewBox::parseMappedAttribute(Document* document, MappedAttribute* attr)
87 {
88 if (attr->name() == SVGNames::viewBoxAttr) {
89 float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
90 const UChar* c = attr->value().characters();
91 const UChar* end = c + attr->value().length();
92 if (parseViewBox(document, c, end, x, y, w, h))
93 setViewBoxBaseValue(FloatRect(x, y, w, h));
94 return true;
95 } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
96 SVGPreserveAspectRatio::parsePreserveAspectRatio(this, attr->value());
97 return true;
98 }
99
100 return false;
101 }
102
isKnownAttribute(const QualifiedName & attrName)103 bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName)
104 {
105 return (attrName == SVGNames::viewBoxAttr ||
106 attrName == SVGNames::preserveAspectRatioAttr);
107 }
108
109 }
110
111 #endif // ENABLE(SVG)
112