blob: ca9c6ef6f617ecc522cd4bf260411f51ad5a4b14 [file] [log] [blame]
darinb9481ed2006-03-20 02:57:59 +00001/*
2 Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
rwlbuis36fea0a2007-01-07 16:47:24 +00003 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
darinb9481ed2006-03-20 02:57:59 +00004
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"
mjsd2948ef2007-02-26 19:29:04 +000024#if ENABLE(SVG)
darinb9481ed2006-03-20 02:57:59 +000025#include "SVGFitToViewBox.h"
rwlbuis65d35022006-10-15 10:00:13 +000026
rwlbuis36fea0a2007-01-07 16:47:24 +000027#include "AffineTransform.h"
rwlbuis65d35022006-10-15 10:00:13 +000028#include "FloatRect.h"
rwlbuis36fea0a2007-01-07 16:47:24 +000029#include "SVGDocumentExtensions.h"
eseideleb0627e2006-08-15 06:12:39 +000030#include "SVGNames.h"
rwlbuis56c753b2006-12-29 20:47:03 +000031#include "SVGParserUtilities.h"
darinb9481ed2006-03-20 02:57:59 +000032#include "SVGPreserveAspectRatio.h"
eseideleb0627e2006-08-15 06:12:39 +000033#include "StringImpl.h"
darinb9481ed2006-03-20 02:57:59 +000034
eseideleb0627e2006-08-15 06:12:39 +000035namespace WebCore {
darinb9481ed2006-03-20 02:57:59 +000036
37SVGFitToViewBox::SVGFitToViewBox()
eseidel18555aa2006-09-11 21:42:22 +000038 : m_viewBox()
eseidela7f76262006-09-06 03:36:10 +000039 , m_preserveAspectRatio(new SVGPreserveAspectRatio(0))
darinb9481ed2006-03-20 02:57:59 +000040{
41}
42
43SVGFitToViewBox::~SVGFitToViewBox()
44{
45}
46
eseidel18555aa2006-09-11 21:42:22 +000047ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, FloatRect, Rect, rect, ViewBox, viewBox, SVGNames::viewBoxAttr.localName(), m_viewBox)
eseidela7f76262006-09-06 03:36:10 +000048ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, SVGPreserveAspectRatio*, PreserveAspectRatio, preserveAspectRatio, PreserveAspectRatio, preserveAspectRatio, SVGNames::preserveAspectRatioAttr.localName(), m_preserveAspectRatio.get())
darinb9481ed2006-03-20 02:57:59 +000049
eseidel49ff1a82006-09-09 20:29:41 +000050void SVGFitToViewBox::parseViewBox(const String& str)
darinb9481ed2006-03-20 02:57:59 +000051{
rwlbuis987dbc42006-07-27 07:46:00 +000052 double x = 0, y = 0, w = 0, h = 0;
rwlbuis56c753b2006-12-29 20:47:03 +000053 const UChar* c = str.characters();
54 const UChar* end = c + str.length();
rwlbuis36fea0a2007-01-07 16:47:24 +000055 Document* doc = contextElement()->document();
rwlbuis56c753b2006-12-29 20:47:03 +000056
57 skipOptionalSpaces(c, end);
58
rwlbuis36fea0a2007-01-07 16:47:24 +000059 if (!(parseNumber(c, end, x) && parseNumber(c, end, y) &&
60 parseNumber(c, end, w) && parseNumber(c, end, h, false))) {
61 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
62 return;
63 }
darinb9481ed2006-03-20 02:57:59 +000064
rwlbuis36fea0a2007-01-07 16:47:24 +000065 if (w < 0.0) // check that width is positive
66 doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
67 else if (h < 0.0) // check that height is positive
68 doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
69 else {
70 skipOptionalSpaces(c, end);
71 if (c < end) // nothing should come after the last, fourth number
72 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
73 else
weinigffd4b772007-07-03 20:46:44 +000074 setViewBoxBaseValue(FloatRect::narrowPrecision(x, y, w, h));
rwlbuis36fea0a2007-01-07 16:47:24 +000075 }
darinb9481ed2006-03-20 02:57:59 +000076}
77
zimmermannc431e0c2006-12-12 11:19:34 +000078AffineTransform SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const
darinb9481ed2006-03-20 02:57:59 +000079{
eseidel18555aa2006-09-11 21:42:22 +000080 FloatRect viewBoxRect = viewBox();
81 if (!viewBoxRect.width() || !viewBoxRect.height())
zimmermannc431e0c2006-12-12 11:19:34 +000082 return AffineTransform();
darinb9481ed2006-03-20 02:57:59 +000083
eseidel18555aa2006-09-11 21:42:22 +000084 return preserveAspectRatio()->getCTM(viewBoxRect.x(),
85 viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(),
darinb9481ed2006-03-20 02:57:59 +000086 0, 0, viewWidth, viewHeight);
87}
88
eseideleb0627e2006-08-15 06:12:39 +000089bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr)
darinb9481ed2006-03-20 02:57:59 +000090{
eseideleb0627e2006-08-15 06:12:39 +000091 if (attr->name() == SVGNames::viewBoxAttr) {
eseidel49ff1a82006-09-09 20:29:41 +000092 parseViewBox(attr->value());
darinb9481ed2006-03-20 02:57:59 +000093 return true;
eseideleb0627e2006-08-15 06:12:39 +000094 } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
eseidelec398c42006-12-27 03:13:22 +000095 preserveAspectRatioBaseValue()->parsePreserveAspectRatio(attr->value());
darinb9481ed2006-03-20 02:57:59 +000096 return true;
97 }
98
99 return false;
100}
101
eseideleb0627e2006-08-15 06:12:39 +0000102}
103
darinb9481ed2006-03-20 02:57:59 +0000104// vim:ts=4:noet
mjsd2948ef2007-02-26 19:29:04 +0000105#endif // ENABLE(SVG)
darinb9481ed2006-03-20 02:57:59 +0000106