blob: feab23ddcd8dc1358d566ecca793c1c10ddc5d31 [file] [log] [blame]
darinb9481ed2006-03-20 02:57:59 +00001/*
2 Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
3 2004, 2005 Rob Buis <buis@kde.org>
4
5 This file is part of the KDE project
6
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.
11
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.
16
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21*/
22
23#include "config.h"
24#if SVG_SUPPORT
25#include <DeprecatedStringList.h>
26
27#include <kdom/core/Attr.h>
28#include "StringImpl.h"
29
30#include "SVGNames.h"
31#include "SVGRect.h"
32#include "SVGSVGElement.h"
33#include "SVGAnimatedRect.h"
34#include "SVGFitToViewBox.h"
35#include "SVGPreserveAspectRatio.h"
36#include "SVGAnimatedPreserveAspectRatio.h"
37
38using namespace WebCore;
39
40SVGFitToViewBox::SVGFitToViewBox()
41{
42}
43
44SVGFitToViewBox::~SVGFitToViewBox()
45{
46}
47
48SVGAnimatedRect *SVGFitToViewBox::viewBox() const
49{
50 if(!m_viewBox)
51 {
52 //const SVGStyledElement *context = dynamic_cast<const SVGStyledElement *>(this);
53 m_viewBox = new SVGAnimatedRect(0); // FIXME: 0 is a hack
54 }
55
56 return m_viewBox.get();
57}
58
59SVGAnimatedPreserveAspectRatio *SVGFitToViewBox::preserveAspectRatio() const
60{
61 if(!m_preserveAspectRatio)
62 {
63 //const SVGStyledElement *context = dynamic_cast<const SVGStyledElement *>(this);
64 m_preserveAspectRatio = new SVGAnimatedPreserveAspectRatio(0); // FIXME: 0 is a hack
65 }
66
67 return m_preserveAspectRatio.get();
68}
69
70void SVGFitToViewBox::parseViewBox(StringImpl *str)
71{
72 // allow for viewbox def with ',' or whitespace
73 DeprecatedString viewbox = String(str).deprecatedString();
74 DeprecatedStringList points = DeprecatedStringList::split(' ', viewbox.replace(',', ' ').simplifyWhiteSpace());
75
76 if (points.count() == 4) {
77 viewBox()->baseVal()->setX(points[0].toDouble());
78 viewBox()->baseVal()->setY(points[1].toDouble());
79 viewBox()->baseVal()->setWidth(points[2].toDouble());
80 viewBox()->baseVal()->setHeight(points[3].toDouble());
81 } else
82 fprintf(stderr, "WARNING: Malformed viewbox string: %s (l: %i)", viewbox.ascii(), viewbox.length());
83}
84
85SVGMatrix *SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const
86{
87 SVGRect *viewBoxRect = viewBox()->baseVal();
88 if(viewBoxRect->width() == 0 || viewBoxRect->height() == 0)
89 return SVGSVGElement::createSVGMatrix();
90
91 return preserveAspectRatio()->baseVal()->getCTM(viewBoxRect->x(),
92 viewBoxRect->y(), viewBoxRect->width(), viewBoxRect->height(),
93 0, 0, viewWidth, viewHeight);
94}
95
96bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute *attr)
97{
98 if (attr->name() == SVGNames::viewBoxAttr)
99 {
100 parseViewBox(attr->value().impl());
101 return true;
102 }
103 else if (attr->name() == SVGNames::preserveAspectRatioAttr)
104 {
105 preserveAspectRatio()->baseVal()->parsePreserveAspectRatio(attr->value().impl());
106 return true;
107 }
108
109 return false;
110}
111
112// vim:ts=4:noet
113#endif // SVG_SUPPORT
114