darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1 | /* |
zimmermann@webkit.org | 52b39ec | 2008-02-03 23:18:53 +0000 | [diff] [blame] | 2 | Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 3 | 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 4 | |
| 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | Boston, MA 02110-1301, USA. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "config.h" |
zimmermann@webkit.org | 52b39ec | 2008-02-03 23:18:53 +0000 | [diff] [blame] | 24 | |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 25 | #if ENABLE(SVG) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 26 | #include "SVGFitToViewBox.h" |
rwlbuis | 65d3502 | 2006-10-15 10:00:13 +0000 | [diff] [blame] | 27 | |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 28 | #include "AffineTransform.h" |
alp@webkit.org | d1e860e | 2008-09-20 03:43:42 +0000 | [diff] [blame^] | 29 | #include "Document.h" |
rwlbuis | 65d3502 | 2006-10-15 10:00:13 +0000 | [diff] [blame] | 30 | #include "FloatRect.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 31 | #include "SVGNames.h" |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 32 | #include "SVGParserUtilities.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 33 | #include "SVGPreserveAspectRatio.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 34 | #include "StringImpl.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 35 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 36 | namespace WebCore { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 37 | |
zimmermann@webkit.org | 057741d | 2008-07-19 15:46:48 +0000 | [diff] [blame] | 38 | char SVGFitToViewBoxIdentifier[] = "SVGFitToViewBox"; |
| 39 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 40 | SVGFitToViewBox::SVGFitToViewBox() |
zimmermann@webkit.org | 057741d | 2008-07-19 15:46:48 +0000 | [diff] [blame] | 41 | : m_viewBox(this, SVGNames::viewBoxAttr) |
| 42 | , m_preserveAspectRatio(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | SVGFitToViewBox::~SVGFitToViewBox() |
| 47 | { |
| 48 | } |
| 49 | |
oliver | 859ead7 | 2007-10-12 15:41:08 +0000 | [diff] [blame] | 50 | bool SVGFitToViewBox::parseViewBox(const UChar*& c, const UChar* end, float& x, float& y, float& w, float& h, bool validate) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 51 | { |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 52 | Document* doc = contextElement()->document(); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 53 | String str(c, end - c); |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 54 | |
| 55 | skipOptionalSpaces(c, end); |
| 56 | |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 57 | 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) { |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 62 | doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\""); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 63 | return false; |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 64 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 65 | |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 66 | if (w < 0.0) { // check that width is positive |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 67 | doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed"); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } else if (h < 0.0) { // check that height is positive |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 70 | doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed"); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } else { |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 73 | skipOptionalSpaces(c, end); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 74 | if (c < end) { // nothing should come after the last, fourth number |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 75 | doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\""); |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 78 | } |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 79 | |
| 80 | return true; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 81 | } |
| 82 | |
zimmermann | c431e0c | 2006-12-12 11:19:34 +0000 | [diff] [blame] | 83 | AffineTransform SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 84 | { |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 85 | FloatRect viewBoxRect = viewBox(); |
| 86 | if (!viewBoxRect.width() || !viewBoxRect.height()) |
zimmermann | c431e0c | 2006-12-12 11:19:34 +0000 | [diff] [blame] | 87 | return AffineTransform(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 88 | |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 89 | return preserveAspectRatio()->getCTM(viewBoxRect.x(), |
| 90 | viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 91 | 0, 0, viewWidth, viewHeight); |
| 92 | } |
| 93 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 94 | bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 95 | { |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 96 | if (attr->name() == SVGNames::viewBoxAttr) { |
oliver | 859ead7 | 2007-10-12 15:41:08 +0000 | [diff] [blame] | 97 | float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f; |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 98 | 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)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 102 | return true; |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 103 | } else if (attr->name() == SVGNames::preserveAspectRatioAttr) { |
oliver | b64e408 | 2007-10-12 13:13:51 +0000 | [diff] [blame] | 104 | const UChar* c = attr->value().characters(); |
| 105 | const UChar* end = c + attr->value().length(); |
| 106 | preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
zimmermann@webkit.org | 52b39ec | 2008-02-03 23:18:53 +0000 | [diff] [blame] | 113 | bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName) |
| 114 | { |
| 115 | return (attrName == SVGNames::viewBoxAttr || |
| 116 | attrName == SVGNames::preserveAspectRatioAttr); |
| 117 | } |
| 118 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 121 | #endif // ENABLE(SVG) |