darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1 | /* |
zimmermann@webkit.org | 711245f | 2010-08-03 19:39:01 +0000 | [diff] [blame] | 2 | * 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 | */ |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 20 | |
| 21 | #include "config.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 22 | #include "SVGFitToViewBox.h" |
rwlbuis | 65d3502 | 2006-10-15 10:00:13 +0000 | [diff] [blame] | 23 | |
krit@webkit.org | 2b95a9a | 2010-02-08 20:30:42 +0000 | [diff] [blame] | 24 | #include "AffineTransform.h" |
alp@webkit.org | d1e860e | 2008-09-20 03:43:42 +0000 | [diff] [blame] | 25 | #include "Document.h" |
rwlbuis | 65d3502 | 2006-10-15 10:00:13 +0000 | [diff] [blame] | 26 | #include "FloatRect.h" |
zimmermann@webkit.org | 5f86bfd | 2010-11-02 08:35:42 +0000 | [diff] [blame] | 27 | #include "SVGDocumentExtensions.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 28 | #include "SVGNames.h" |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 29 | #include "SVGParserUtilities.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 30 | #include "SVGPreserveAspectRatio.h" |
darin@apple.com | aa5b6c5 | 2014-03-18 21:57:44 +0000 | [diff] [blame] | 31 | #include <wtf/text/StringView.h> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 32 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 33 | namespace WebCore { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 34 | |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 35 | bool SVGFitToViewBox::parseViewBox(Document* doc, const String& s, FloatRect& viewBox) |
| 36 | { |
darin@apple.com | aa5b6c5 | 2014-03-18 21:57:44 +0000 | [diff] [blame] | 37 | auto upconvertedCharacters = StringView(s).upconvertedCharacters(); |
| 38 | const UChar* characters = upconvertedCharacters; |
| 39 | return parseViewBox(doc, characters, characters + s.length(), viewBox, true); |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool SVGFitToViewBox::parseViewBox(Document* doc, const UChar*& c, const UChar* end, FloatRect& viewBox, bool validate) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 43 | { |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 44 | String str(c, end - c); |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 45 | |
darin@apple.com | 93e55e5 | 2011-08-18 12:25:04 +0000 | [diff] [blame] | 46 | skipOptionalSVGSpaces(c, end); |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 47 | |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 48 | float x = 0.0f; |
| 49 | float y = 0.0f; |
| 50 | float width = 0.0f; |
| 51 | float height = 0.0f; |
krit@webkit.org | 6a0cb5b | 2010-12-22 20:15:51 +0000 | [diff] [blame] | 52 | bool valid = parseNumber(c, end, x) && parseNumber(c, end, y) && parseNumber(c, end, width) && parseNumber(c, end, height, false); |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 53 | if (!validate) { |
| 54 | viewBox = FloatRect(x, y, width, height); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 55 | return true; |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 56 | } |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 57 | if (!valid) { |
cdumez@apple.com | dab95ab | 2014-09-23 18:44:41 +0000 | [diff] [blame] | 58 | doc->accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\""); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 59 | return false; |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 60 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 61 | |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 62 | if (width < 0.0) { // check that width is positive |
cdumez@apple.com | dab95ab | 2014-09-23 18:44:41 +0000 | [diff] [blame] | 63 | doc->accessSVGExtensions().reportError("A negative value for ViewBox width is not allowed"); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 64 | return false; |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 65 | } |
| 66 | if (height < 0.0) { // check that height is positive |
cdumez@apple.com | dab95ab | 2014-09-23 18:44:41 +0000 | [diff] [blame] | 67 | doc->accessSVGExtensions().reportError("A negative value for ViewBox height is not allowed"); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 68 | return false; |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 69 | } |
darin@apple.com | 93e55e5 | 2011-08-18 12:25:04 +0000 | [diff] [blame] | 70 | skipOptionalSVGSpaces(c, end); |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 71 | if (c < end) { // nothing should come after the last, fourth number |
cdumez@apple.com | dab95ab | 2014-09-23 18:44:41 +0000 | [diff] [blame] | 72 | doc->accessSVGExtensions().reportWarning("Problem parsing viewBox=\"" + str + "\""); |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 73 | return false; |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 74 | } |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 75 | |
rwlbuis@webkit.org | 1864497 | 2010-06-01 20:06:44 +0000 | [diff] [blame] | 76 | viewBox = FloatRect(x, y, width, height); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 77 | return true; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
krit@webkit.org | 2b95a9a | 2010-02-08 20:30:42 +0000 | [diff] [blame] | 80 | AffineTransform SVGFitToViewBox::viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatio& preserveAspectRatio, float viewWidth, float viewHeight) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 81 | { |
commit-queue@webkit.org | 35a3c71 | 2015-04-20 20:43:13 +0000 | [diff] [blame] | 82 | if (!viewBoxRect.width() || !viewBoxRect.height() || !viewWidth || !viewHeight) |
krit@webkit.org | 2b95a9a | 2010-02-08 20:30:42 +0000 | [diff] [blame] | 83 | return AffineTransform(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 84 | |
zimmermann@webkit.org | 5f86bfd | 2010-11-02 08:35:42 +0000 | [diff] [blame] | 85 | return preserveAspectRatio.getCTM(viewBoxRect.x(), viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), viewWidth, viewHeight); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 86 | } |
| 87 | |
zimmermann@webkit.org | 52b39ec | 2008-02-03 23:18:53 +0000 | [diff] [blame] | 88 | bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName) |
| 89 | { |
krit@webkit.org | 6a0cb5b | 2010-12-22 20:15:51 +0000 | [diff] [blame] | 90 | return attrName == SVGNames::viewBoxAttr || attrName == SVGNames::preserveAspectRatioAttr; |
zimmermann@webkit.org | 52b39ec | 2008-02-03 23:18:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
zimmermann@webkit.org | efff725 | 2011-05-21 06:15:40 +0000 | [diff] [blame] | 93 | void SVGFitToViewBox::addSupportedAttributes(HashSet<QualifiedName>& supportedAttributes) |
| 94 | { |
| 95 | supportedAttributes.add(SVGNames::viewBoxAttr); |
| 96 | supportedAttributes.add(SVGNames::preserveAspectRatioAttr); |
| 97 | } |
| 98 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 99 | } |