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