blob: 69e077e21f303ac947414527012693e71e586a40 [file] [log] [blame]
darinb9481ed2006-03-20 02:57:59 +00001/*
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +00002 Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@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
ddkilzerc8eccec2007-09-26 02:29:57 +000019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
darinb9481ed2006-03-20 02:57:59 +000021*/
22
23#include "config.h"
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +000024
mjsd2948ef2007-02-26 19:29:04 +000025#if ENABLE(SVG)
darinb9481ed2006-03-20 02:57:59 +000026#include "SVGFitToViewBox.h"
rwlbuis65d35022006-10-15 10:00:13 +000027
rwlbuis36fea0a2007-01-07 16:47:24 +000028#include "AffineTransform.h"
alp@webkit.orgd1e860e2008-09-20 03:43:42 +000029#include "Document.h"
rwlbuis65d35022006-10-15 10:00:13 +000030#include "FloatRect.h"
eseideleb0627e2006-08-15 06:12:39 +000031#include "SVGNames.h"
rwlbuis56c753b2006-12-29 20:47:03 +000032#include "SVGParserUtilities.h"
darinb9481ed2006-03-20 02:57:59 +000033#include "SVGPreserveAspectRatio.h"
eseideleb0627e2006-08-15 06:12:39 +000034#include "StringImpl.h"
darinb9481ed2006-03-20 02:57:59 +000035
eseideleb0627e2006-08-15 06:12:39 +000036namespace WebCore {
darinb9481ed2006-03-20 02:57:59 +000037
zimmermann@webkit.org057741d2008-07-19 15:46:48 +000038char SVGFitToViewBoxIdentifier[] = "SVGFitToViewBox";
39
darinb9481ed2006-03-20 02:57:59 +000040SVGFitToViewBox::SVGFitToViewBox()
zimmermann@webkit.org057741d2008-07-19 15:46:48 +000041 : m_viewBox(this, SVGNames::viewBoxAttr)
42 , m_preserveAspectRatio(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create())
darinb9481ed2006-03-20 02:57:59 +000043{
44}
45
46SVGFitToViewBox::~SVGFitToViewBox()
47{
48}
49
oliver859ead72007-10-12 15:41:08 +000050bool SVGFitToViewBox::parseViewBox(const UChar*& c, const UChar* end, float& x, float& y, float& w, float& h, bool validate)
darinb9481ed2006-03-20 02:57:59 +000051{
rwlbuis36fea0a2007-01-07 16:47:24 +000052 Document* doc = contextElement()->document();
oliverb64e4082007-10-12 13:13:51 +000053 String str(c, end - c);
rwlbuis56c753b2006-12-29 20:47:03 +000054
55 skipOptionalSpaces(c, end);
56
oliverb64e4082007-10-12 13:13:51 +000057 bool valid = (parseNumber(c, end, x) && parseNumber(c, end, y) &&
58 parseNumber(c, end, w) && parseNumber(c, end, h, false));
59 if (!validate)
60 return true;
61 if (!valid) {
rwlbuis36fea0a2007-01-07 16:47:24 +000062 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
oliverb64e4082007-10-12 13:13:51 +000063 return false;
rwlbuis36fea0a2007-01-07 16:47:24 +000064 }
darinb9481ed2006-03-20 02:57:59 +000065
oliverb64e4082007-10-12 13:13:51 +000066 if (w < 0.0) { // check that width is positive
rwlbuis36fea0a2007-01-07 16:47:24 +000067 doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
oliverb64e4082007-10-12 13:13:51 +000068 return false;
69 } else if (h < 0.0) { // check that height is positive
rwlbuis36fea0a2007-01-07 16:47:24 +000070 doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
oliverb64e4082007-10-12 13:13:51 +000071 return false;
72 } else {
rwlbuis36fea0a2007-01-07 16:47:24 +000073 skipOptionalSpaces(c, end);
oliverb64e4082007-10-12 13:13:51 +000074 if (c < end) { // nothing should come after the last, fourth number
rwlbuis36fea0a2007-01-07 16:47:24 +000075 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
oliverb64e4082007-10-12 13:13:51 +000076 return false;
77 }
rwlbuis36fea0a2007-01-07 16:47:24 +000078 }
oliverb64e4082007-10-12 13:13:51 +000079
80 return true;
darinb9481ed2006-03-20 02:57:59 +000081}
82
zimmermannc431e0c2006-12-12 11:19:34 +000083AffineTransform SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const
darinb9481ed2006-03-20 02:57:59 +000084{
eseidel18555aa2006-09-11 21:42:22 +000085 FloatRect viewBoxRect = viewBox();
86 if (!viewBoxRect.width() || !viewBoxRect.height())
zimmermannc431e0c2006-12-12 11:19:34 +000087 return AffineTransform();
darinb9481ed2006-03-20 02:57:59 +000088
eseidel18555aa2006-09-11 21:42:22 +000089 return preserveAspectRatio()->getCTM(viewBoxRect.x(),
90 viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(),
darinb9481ed2006-03-20 02:57:59 +000091 0, 0, viewWidth, viewHeight);
92}
93
eseideleb0627e2006-08-15 06:12:39 +000094bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr)
darinb9481ed2006-03-20 02:57:59 +000095{
eseideleb0627e2006-08-15 06:12:39 +000096 if (attr->name() == SVGNames::viewBoxAttr) {
oliver859ead72007-10-12 15:41:08 +000097 float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
oliverb64e4082007-10-12 13:13:51 +000098 const UChar* c = attr->value().characters();
99 const UChar* end = c + attr->value().length();
100 if (parseViewBox(c, end, x, y, w, h))
101 setViewBoxBaseValue(FloatRect(x, y, w, h));
darinb9481ed2006-03-20 02:57:59 +0000102 return true;
eseideleb0627e2006-08-15 06:12:39 +0000103 } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
oliverb64e4082007-10-12 13:13:51 +0000104 const UChar* c = attr->value().characters();
105 const UChar* end = c + attr->value().length();
106 preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end);
darinb9481ed2006-03-20 02:57:59 +0000107 return true;
108 }
109
110 return false;
111}
112
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +0000113bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName)
114{
115 return (attrName == SVGNames::viewBoxAttr ||
116 attrName == SVGNames::preserveAspectRatioAttr);
117}
118
eseideleb0627e2006-08-15 06:12:39 +0000119}
120
mjsd2948ef2007-02-26 19:29:04 +0000121#endif // ENABLE(SVG)