blob: 412228df96505b04b279ab3bc2b04a57ed71675d [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"
ddkilzer54f5cea2006-08-10 05:11:25 +000024#ifdef SVG_SUPPORT
darinb9481ed2006-03-20 02:57:59 +000025
eseidel40eb1b92006-03-25 22:20:36 +000026#include "Attr.h"
eseidel18555aa2006-09-11 21:42:22 +000027#include "FloatRect.h"
eseidela7f76262006-09-06 03:36:10 +000028#include "SVGPreserveAspectRatio.h"
darinb9481ed2006-03-20 02:57:59 +000029#include "SVGFitToViewBox.h"
eseideleb0627e2006-08-15 06:12:39 +000030#include "SVGNames.h"
darinb9481ed2006-03-20 02:57:59 +000031#include "SVGPreserveAspectRatio.h"
eseideleb0627e2006-08-15 06:12:39 +000032#include "SVGSVGElement.h"
33#include "StringImpl.h"
34#include "svgpathparser.h"
darinb9481ed2006-03-20 02:57:59 +000035
eseideleb0627e2006-08-15 06:12:39 +000036namespace WebCore {
darinb9481ed2006-03-20 02:57:59 +000037
38SVGFitToViewBox::SVGFitToViewBox()
eseidel18555aa2006-09-11 21:42:22 +000039 : m_viewBox()
eseidela7f76262006-09-06 03:36:10 +000040 , m_preserveAspectRatio(new SVGPreserveAspectRatio(0))
darinb9481ed2006-03-20 02:57:59 +000041{
42}
43
44SVGFitToViewBox::~SVGFitToViewBox()
45{
46}
47
eseidel18555aa2006-09-11 21:42:22 +000048ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, FloatRect, Rect, rect, ViewBox, viewBox, SVGNames::viewBoxAttr.localName(), m_viewBox)
eseidela7f76262006-09-06 03:36:10 +000049ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, SVGPreserveAspectRatio*, PreserveAspectRatio, preserveAspectRatio, PreserveAspectRatio, preserveAspectRatio, SVGNames::preserveAspectRatioAttr.localName(), m_preserveAspectRatio.get())
darinb9481ed2006-03-20 02:57:59 +000050
eseidel49ff1a82006-09-09 20:29:41 +000051void SVGFitToViewBox::parseViewBox(const String& str)
darinb9481ed2006-03-20 02:57:59 +000052{
rwlbuis987dbc42006-07-27 07:46:00 +000053 double x = 0, y = 0, w = 0, h = 0;
eseidel49ff1a82006-09-09 20:29:41 +000054 DeprecatedString viewbox = str.deprecatedString();
rwlbuis987dbc42006-07-27 07:46:00 +000055 const char *p = viewbox.latin1();
56 const char *end = p + viewbox.length();
57 const char *c = p;
58 p = parseCoord(c, x);
59 if (p == c)
60 goto bail_out;
darinb9481ed2006-03-20 02:57:59 +000061
rwlbuis987dbc42006-07-27 07:46:00 +000062 c = p;
63 p = parseCoord(c, y);
64 if (p == c)
65 goto bail_out;
66
rwlbuis987dbc42006-07-27 07:46:00 +000067 c = p;
68 p = parseCoord(c, w);
69 if(w < 0.0 || p == c) // check that width is positive
70 goto bail_out;
rwlbuis987dbc42006-07-27 07:46:00 +000071
72 c = p;
73 p = parseCoord(c, h);
74 if (h < 0.0 || p == c) // check that height is positive
75 goto bail_out;
eseidel49576902006-09-06 03:26:42 +000076
rwlbuis987dbc42006-07-27 07:46:00 +000077 if (p < end) // nothing should come after the last, fourth number
78 goto bail_out;
79
eseidel18555aa2006-09-11 21:42:22 +000080 setViewBoxBaseValue(FloatRect(x, y, w, h));
rwlbuis987dbc42006-07-27 07:46:00 +000081 return;
82
eseideleb0627e2006-08-15 06:12:39 +000083bail_out:;
84 // FIXME: Per the spec we are supposed to set the document into an "error state" here.
darinb9481ed2006-03-20 02:57:59 +000085}
86
eseideleb0627e2006-08-15 06:12:39 +000087SVGMatrix* SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const
darinb9481ed2006-03-20 02:57:59 +000088{
eseidel18555aa2006-09-11 21:42:22 +000089 FloatRect viewBoxRect = viewBox();
90 if (!viewBoxRect.width() || !viewBoxRect.height())
darinb9481ed2006-03-20 02:57:59 +000091 return SVGSVGElement::createSVGMatrix();
92
eseidel18555aa2006-09-11 21:42:22 +000093 return preserveAspectRatio()->getCTM(viewBoxRect.x(),
94 viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(),
darinb9481ed2006-03-20 02:57:59 +000095 0, 0, viewWidth, viewHeight);
96}
97
eseideleb0627e2006-08-15 06:12:39 +000098bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr)
darinb9481ed2006-03-20 02:57:59 +000099{
eseideleb0627e2006-08-15 06:12:39 +0000100 if (attr->name() == SVGNames::viewBoxAttr) {
eseidel49ff1a82006-09-09 20:29:41 +0000101 parseViewBox(attr->value());
darinb9481ed2006-03-20 02:57:59 +0000102 return true;
eseideleb0627e2006-08-15 06:12:39 +0000103 } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
eseidela7f76262006-09-06 03:36:10 +0000104 preserveAspectRatioBaseValue()->parsePreserveAspectRatio(attr->value().impl());
darinb9481ed2006-03-20 02:57:59 +0000105 return true;
106 }
107
108 return false;
109}
110
eseideleb0627e2006-08-15 06:12:39 +0000111}
112
darinb9481ed2006-03-20 02:57:59 +0000113// vim:ts=4:noet
114#endif // SVG_SUPPORT
115