blob: 847038c0ca2b0ed7e1391b01482b324fcd8fba3b [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
alp@webkit.orgd1e860e2008-09-20 03:43:42 +000028#include "Document.h"
rwlbuis65d35022006-10-15 10:00:13 +000029#include "FloatRect.h"
darin@apple.com9a925fa2009-05-04 18:00:34 +000030#include "MappedAttribute.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"
darin@apple.com9a925fa2009-05-04 18:00:34 +000035#include "TransformationMatrix.h"
darinb9481ed2006-03-20 02:57:59 +000036
eseideleb0627e2006-08-15 06:12:39 +000037namespace WebCore {
darinb9481ed2006-03-20 02:57:59 +000038
zimmermann@webkit.org057741d2008-07-19 15:46:48 +000039char SVGFitToViewBoxIdentifier[] = "SVGFitToViewBox";
40
darinb9481ed2006-03-20 02:57:59 +000041SVGFitToViewBox::SVGFitToViewBox()
zimmermann@webkit.org057741d2008-07-19 15:46:48 +000042 : m_viewBox(this, SVGNames::viewBoxAttr)
43 , m_preserveAspectRatio(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create())
darinb9481ed2006-03-20 02:57:59 +000044{
45}
46
47SVGFitToViewBox::~SVGFitToViewBox()
48{
49}
50
oliver859ead72007-10-12 15:41:08 +000051bool SVGFitToViewBox::parseViewBox(const UChar*& c, const UChar* end, float& x, float& y, float& w, float& h, bool validate)
darinb9481ed2006-03-20 02:57:59 +000052{
rwlbuis36fea0a2007-01-07 16:47:24 +000053 Document* doc = contextElement()->document();
oliverb64e4082007-10-12 13:13:51 +000054 String str(c, end - c);
rwlbuis56c753b2006-12-29 20:47:03 +000055
56 skipOptionalSpaces(c, end);
57
oliverb64e4082007-10-12 13:13:51 +000058 bool valid = (parseNumber(c, end, x) && parseNumber(c, end, y) &&
59 parseNumber(c, end, w) && parseNumber(c, end, h, false));
60 if (!validate)
61 return true;
62 if (!valid) {
rwlbuis36fea0a2007-01-07 16:47:24 +000063 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
oliverb64e4082007-10-12 13:13:51 +000064 return false;
rwlbuis36fea0a2007-01-07 16:47:24 +000065 }
darinb9481ed2006-03-20 02:57:59 +000066
oliverb64e4082007-10-12 13:13:51 +000067 if (w < 0.0) { // check that width is positive
rwlbuis36fea0a2007-01-07 16:47:24 +000068 doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
oliverb64e4082007-10-12 13:13:51 +000069 return false;
70 } else if (h < 0.0) { // check that height is positive
rwlbuis36fea0a2007-01-07 16:47:24 +000071 doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
oliverb64e4082007-10-12 13:13:51 +000072 return false;
73 } else {
rwlbuis36fea0a2007-01-07 16:47:24 +000074 skipOptionalSpaces(c, end);
oliverb64e4082007-10-12 13:13:51 +000075 if (c < end) { // nothing should come after the last, fourth number
rwlbuis36fea0a2007-01-07 16:47:24 +000076 doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
oliverb64e4082007-10-12 13:13:51 +000077 return false;
78 }
rwlbuis36fea0a2007-01-07 16:47:24 +000079 }
oliverb64e4082007-10-12 13:13:51 +000080
81 return true;
darinb9481ed2006-03-20 02:57:59 +000082}
83
dino@apple.com0cf2dc92009-01-06 03:00:14 +000084TransformationMatrix SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const
darinb9481ed2006-03-20 02:57:59 +000085{
eseidel18555aa2006-09-11 21:42:22 +000086 FloatRect viewBoxRect = viewBox();
87 if (!viewBoxRect.width() || !viewBoxRect.height())
dino@apple.com0cf2dc92009-01-06 03:00:14 +000088 return TransformationMatrix();
darinb9481ed2006-03-20 02:57:59 +000089
eseidel18555aa2006-09-11 21:42:22 +000090 return preserveAspectRatio()->getCTM(viewBoxRect.x(),
91 viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(),
darinb9481ed2006-03-20 02:57:59 +000092 0, 0, viewWidth, viewHeight);
93}
94
eseideleb0627e2006-08-15 06:12:39 +000095bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr)
darinb9481ed2006-03-20 02:57:59 +000096{
eseideleb0627e2006-08-15 06:12:39 +000097 if (attr->name() == SVGNames::viewBoxAttr) {
oliver859ead72007-10-12 15:41:08 +000098 float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
oliverb64e4082007-10-12 13:13:51 +000099 const UChar* c = attr->value().characters();
100 const UChar* end = c + attr->value().length();
101 if (parseViewBox(c, end, x, y, w, h))
102 setViewBoxBaseValue(FloatRect(x, y, w, h));
darinb9481ed2006-03-20 02:57:59 +0000103 return true;
eseideleb0627e2006-08-15 06:12:39 +0000104 } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
oliverb64e4082007-10-12 13:13:51 +0000105 const UChar* c = attr->value().characters();
106 const UChar* end = c + attr->value().length();
107 preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end);
darinb9481ed2006-03-20 02:57:59 +0000108 return true;
109 }
110
111 return false;
112}
113
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +0000114bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName)
115{
116 return (attrName == SVGNames::viewBoxAttr ||
117 attrName == SVGNames::preserveAspectRatioAttr);
118}
119
eseideleb0627e2006-08-15 06:12:39 +0000120}
121
mjsd2948ef2007-02-26 19:29:04 +0000122#endif // ENABLE(SVG)