darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@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 |
| 19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 24 | #if ENABLE(SVG) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 25 | #include "SVGFitToViewBox.h" |
rwlbuis | 65d3502 | 2006-10-15 10:00:13 +0000 | [diff] [blame] | 26 | |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 27 | #include "AffineTransform.h" |
rwlbuis | 65d3502 | 2006-10-15 10:00:13 +0000 | [diff] [blame] | 28 | #include "FloatRect.h" |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 29 | #include "SVGDocumentExtensions.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 30 | #include "SVGNames.h" |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 31 | #include "SVGParserUtilities.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 32 | #include "SVGPreserveAspectRatio.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 33 | #include "StringImpl.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 34 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 35 | namespace WebCore { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 36 | |
| 37 | SVGFitToViewBox::SVGFitToViewBox() |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 38 | : m_viewBox() |
eseidel | a7f7626 | 2006-09-06 03:36:10 +0000 | [diff] [blame] | 39 | , m_preserveAspectRatio(new SVGPreserveAspectRatio(0)) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | SVGFitToViewBox::~SVGFitToViewBox() |
| 44 | { |
| 45 | } |
| 46 | |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 47 | ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, FloatRect, Rect, rect, ViewBox, viewBox, SVGNames::viewBoxAttr.localName(), m_viewBox) |
eseidel | a7f7626 | 2006-09-06 03:36:10 +0000 | [diff] [blame] | 48 | ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, SVGPreserveAspectRatio*, PreserveAspectRatio, preserveAspectRatio, PreserveAspectRatio, preserveAspectRatio, SVGNames::preserveAspectRatioAttr.localName(), m_preserveAspectRatio.get()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 49 | |
eseidel | 49ff1a8 | 2006-09-09 20:29:41 +0000 | [diff] [blame] | 50 | void SVGFitToViewBox::parseViewBox(const String& str) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 51 | { |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 52 | double x = 0, y = 0, w = 0, h = 0; |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 53 | const UChar* c = str.characters(); |
| 54 | const UChar* end = c + str.length(); |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 55 | Document* doc = contextElement()->document(); |
rwlbuis | 56c753b | 2006-12-29 20:47:03 +0000 | [diff] [blame] | 56 | |
| 57 | skipOptionalSpaces(c, end); |
| 58 | |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 59 | if (!(parseNumber(c, end, x) && parseNumber(c, end, y) && |
| 60 | parseNumber(c, end, w) && parseNumber(c, end, h, false))) { |
| 61 | doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\""); |
| 62 | return; |
| 63 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 64 | |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 65 | if (w < 0.0) // check that width is positive |
| 66 | doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed"); |
| 67 | else if (h < 0.0) // check that height is positive |
| 68 | doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed"); |
| 69 | else { |
| 70 | skipOptionalSpaces(c, end); |
| 71 | if (c < end) // nothing should come after the last, fourth number |
| 72 | doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\""); |
| 73 | else |
weinig | ffd4b77 | 2007-07-03 20:46:44 +0000 | [diff] [blame] | 74 | setViewBoxBaseValue(FloatRect::narrowPrecision(x, y, w, h)); |
rwlbuis | 36fea0a | 2007-01-07 16:47:24 +0000 | [diff] [blame] | 75 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 76 | } |
| 77 | |
zimmermann | c431e0c | 2006-12-12 11:19:34 +0000 | [diff] [blame] | 78 | AffineTransform SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 79 | { |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 80 | FloatRect viewBoxRect = viewBox(); |
| 81 | if (!viewBoxRect.width() || !viewBoxRect.height()) |
zimmermann | c431e0c | 2006-12-12 11:19:34 +0000 | [diff] [blame] | 82 | return AffineTransform(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 83 | |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 84 | return preserveAspectRatio()->getCTM(viewBoxRect.x(), |
| 85 | viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 86 | 0, 0, viewWidth, viewHeight); |
| 87 | } |
| 88 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 89 | bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 90 | { |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 91 | if (attr->name() == SVGNames::viewBoxAttr) { |
eseidel | 49ff1a8 | 2006-09-09 20:29:41 +0000 | [diff] [blame] | 92 | parseViewBox(attr->value()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 93 | return true; |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 94 | } else if (attr->name() == SVGNames::preserveAspectRatioAttr) { |
eseidel | ec398c4 | 2006-12-27 03:13:22 +0000 | [diff] [blame] | 95 | preserveAspectRatioBaseValue()->parsePreserveAspectRatio(attr->value()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 102 | } |
| 103 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 104 | // vim:ts=4:noet |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 105 | #endif // ENABLE(SVG) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 106 | |