2 Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
5 This file is part of the KDE project
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
24 #include "wtf/Platform.h"
27 #include "SVGFitToViewBox.h"
29 #include "AffineTransform.h"
30 #include "FloatRect.h"
31 #include "SVGDocumentExtensions.h"
33 #include "SVGParserUtilities.h"
34 #include "SVGPreserveAspectRatio.h"
35 //#include "StringImpl.h"
39 SVGFitToViewBox::SVGFitToViewBox()
41 , m_preserveAspectRatio(SVGPreserveAspectRatio::create())
45 SVGFitToViewBox::~SVGFitToViewBox()
49 ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox
, FloatRect
, Rect
, rect
, ViewBox
, viewBox
, SVGNames::viewBoxAttr
, m_viewBox
)
50 ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox
, SVGPreserveAspectRatio
*, PreserveAspectRatio
, preserveAspectRatio
, PreserveAspectRatio
, preserveAspectRatio
, SVGNames::preserveAspectRatioAttr
, m_preserveAspectRatio
.get())
52 bool SVGFitToViewBox::parseViewBox(const UChar
*& c
, const UChar
* end
, float& x
, float& y
, float& w
, float& h
, bool validate
)
54 Document
* doc
= contextElement()->document();
55 String
str(c
, end
- c
);
57 skipOptionalSpaces(c
, end
);
59 bool valid
= (parseNumber(c
, end
, x
) && parseNumber(c
, end
, y
) &&
60 parseNumber(c
, end
, w
) && parseNumber(c
, end
, h
, false));
64 //FIXME vtokarev doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
68 if (w
< 0.0) { // check that width is positive
69 doc
->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
71 } else if (h
< 0.0) { // check that height is positive
72 doc
->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
75 skipOptionalSpaces(c
, end
);
76 if (c
< end
) { // nothing should come after the last, fourth number
77 //FIXME vtokarev doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
85 AffineTransform
SVGFitToViewBox::viewBoxToViewTransform(float viewWidth
, float viewHeight
) const
87 FloatRect viewBoxRect
= viewBox();
88 if (!viewBoxRect
.width() || !viewBoxRect
.height())
89 return AffineTransform();
91 return preserveAspectRatio()->getCTM(viewBoxRect
.x(),
92 viewBoxRect
.y(), viewBoxRect
.width(), viewBoxRect
.height(),
93 0, 0, viewWidth
, viewHeight
);
96 bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute
* attr
)
98 if (attr
->name() == SVGNames::viewBoxAttr
) {
99 float x
= 0.0f
, y
= 0.0f
, w
= 0.0f
, h
= 0.0f
;
100 const UChar
* c
= attr
->value().characters();
101 const UChar
* end
= c
+ attr
->value().length();
102 if (parseViewBox(c
, end
, x
, y
, w
, h
))
103 setViewBoxBaseValue(FloatRect(x
, y
, w
, h
));
105 } else if (attr
->name() == SVGNames::preserveAspectRatioAttr
) {
106 const UChar
* c
= attr
->value().characters();
107 const UChar
* end
= c
+ attr
->value().length();
108 preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c
, end
);
115 bool SVGFitToViewBox::isKnownAttribute(const QualifiedName
& attrName
)
117 return (attrName
== SVGNames::viewBoxAttr
||
118 attrName
== SVGNames::preserveAspectRatioAttr
);
123 #endif // ENABLE(SVG)