blob: 632b4627e55a8db765b059885d2ea298f38504c7 [file] [log] [blame]
darinb9481ed2006-03-20 02:57:59 +00001/*
zimmermann@webkit.org711245f2010-08-03 19:39:01 +00002 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Rob Buis <buis@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
darinb9481ed2006-03-20 02:57:59 +000020
21#include "config.h"
darinb9481ed2006-03-20 02:57:59 +000022#include "SVGFitToViewBox.h"
rwlbuis65d35022006-10-15 10:00:13 +000023
krit@webkit.org2b95a9a2010-02-08 20:30:42 +000024#include "AffineTransform.h"
alp@webkit.orgd1e860e2008-09-20 03:43:42 +000025#include "Document.h"
rwlbuis65d35022006-10-15 10:00:13 +000026#include "FloatRect.h"
zimmermann@webkit.org5f86bfd2010-11-02 08:35:42 +000027#include "SVGDocumentExtensions.h"
eseideleb0627e2006-08-15 06:12:39 +000028#include "SVGNames.h"
rwlbuis56c753b2006-12-29 20:47:03 +000029#include "SVGParserUtilities.h"
darinb9481ed2006-03-20 02:57:59 +000030#include "SVGPreserveAspectRatio.h"
darin@apple.comaa5b6c52014-03-18 21:57:44 +000031#include <wtf/text/StringView.h>
darinb9481ed2006-03-20 02:57:59 +000032
eseideleb0627e2006-08-15 06:12:39 +000033namespace WebCore {
darinb9481ed2006-03-20 02:57:59 +000034
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000035bool SVGFitToViewBox::parseViewBox(Document* doc, const String& s, FloatRect& viewBox)
36{
darin@apple.comaa5b6c52014-03-18 21:57:44 +000037 auto upconvertedCharacters = StringView(s).upconvertedCharacters();
38 const UChar* characters = upconvertedCharacters;
39 return parseViewBox(doc, characters, characters + s.length(), viewBox, true);
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000040}
41
42bool SVGFitToViewBox::parseViewBox(Document* doc, const UChar*& c, const UChar* end, FloatRect& viewBox, bool validate)
darinb9481ed2006-03-20 02:57:59 +000043{
oliverb64e4082007-10-12 13:13:51 +000044 String str(c, end - c);
rwlbuis56c753b2006-12-29 20:47:03 +000045
darin@apple.com93e55e52011-08-18 12:25:04 +000046 skipOptionalSVGSpaces(c, end);
rwlbuis56c753b2006-12-29 20:47:03 +000047
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000048 float x = 0.0f;
49 float y = 0.0f;
50 float width = 0.0f;
51 float height = 0.0f;
krit@webkit.org6a0cb5b2010-12-22 20:15:51 +000052 bool valid = parseNumber(c, end, x) && parseNumber(c, end, y) && parseNumber(c, end, width) && parseNumber(c, end, height, false);
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000053 if (!validate) {
54 viewBox = FloatRect(x, y, width, height);
oliverb64e4082007-10-12 13:13:51 +000055 return true;
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000056 }
oliverb64e4082007-10-12 13:13:51 +000057 if (!valid) {
cdumez@apple.comdab95ab2014-09-23 18:44:41 +000058 doc->accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\"");
oliverb64e4082007-10-12 13:13:51 +000059 return false;
rwlbuis36fea0a2007-01-07 16:47:24 +000060 }
darinb9481ed2006-03-20 02:57:59 +000061
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000062 if (width < 0.0) { // check that width is positive
cdumez@apple.comdab95ab2014-09-23 18:44:41 +000063 doc->accessSVGExtensions().reportError("A negative value for ViewBox width is not allowed");
oliverb64e4082007-10-12 13:13:51 +000064 return false;
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000065 }
66 if (height < 0.0) { // check that height is positive
cdumez@apple.comdab95ab2014-09-23 18:44:41 +000067 doc->accessSVGExtensions().reportError("A negative value for ViewBox height is not allowed");
oliverb64e4082007-10-12 13:13:51 +000068 return false;
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000069 }
darin@apple.com93e55e52011-08-18 12:25:04 +000070 skipOptionalSVGSpaces(c, end);
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000071 if (c < end) { // nothing should come after the last, fourth number
cdumez@apple.comdab95ab2014-09-23 18:44:41 +000072 doc->accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\"");
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000073 return false;
rwlbuis36fea0a2007-01-07 16:47:24 +000074 }
oliverb64e4082007-10-12 13:13:51 +000075
rwlbuis@webkit.org18644972010-06-01 20:06:44 +000076 viewBox = FloatRect(x, y, width, height);
oliverb64e4082007-10-12 13:13:51 +000077 return true;
darinb9481ed2006-03-20 02:57:59 +000078}
79
krit@webkit.org2b95a9a2010-02-08 20:30:42 +000080AffineTransform SVGFitToViewBox::viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatio& preserveAspectRatio, float viewWidth, float viewHeight)
darinb9481ed2006-03-20 02:57:59 +000081{
commit-queue@webkit.org35a3c712015-04-20 20:43:13 +000082 if (!viewBoxRect.width() || !viewBoxRect.height() || !viewWidth || !viewHeight)
krit@webkit.org2b95a9a2010-02-08 20:30:42 +000083 return AffineTransform();
darinb9481ed2006-03-20 02:57:59 +000084
zimmermann@webkit.org5f86bfd2010-11-02 08:35:42 +000085 return preserveAspectRatio.getCTM(viewBoxRect.x(), viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), viewWidth, viewHeight);
darinb9481ed2006-03-20 02:57:59 +000086}
87
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +000088bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName)
89{
krit@webkit.org6a0cb5b2010-12-22 20:15:51 +000090 return attrName == SVGNames::viewBoxAttr || attrName == SVGNames::preserveAspectRatioAttr;
zimmermann@webkit.org52b39ec2008-02-03 23:18:53 +000091}
92
zimmermann@webkit.orgefff7252011-05-21 06:15:40 +000093void SVGFitToViewBox::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes)
94{
95 supportedAttributes.add(SVGNames::viewBoxAttr);
96 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
97}
98
eseideleb0627e2006-08-15 06:12:39 +000099}