darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org> |
| 3 | 2004, 2005 Rob Buis <buis@kde.org> |
| 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" |
ddkilzer | 54f5cea | 2006-08-10 05:11:25 +0000 | [diff] [blame] | 24 | #ifdef SVG_SUPPORT |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 25 | |
eseidel | 40eb1b9 | 2006-03-25 22:20:36 +0000 | [diff] [blame] | 26 | #include "Attr.h" |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 27 | #include "FloatRect.h" |
eseidel | a7f7626 | 2006-09-06 03:36:10 +0000 | [diff] [blame] | 28 | #include "SVGPreserveAspectRatio.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 29 | #include "SVGFitToViewBox.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 30 | #include "SVGNames.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 31 | #include "SVGPreserveAspectRatio.h" |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 32 | #include "SVGSVGElement.h" |
| 33 | #include "StringImpl.h" |
| 34 | #include "svgpathparser.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 | |
| 38 | SVGFitToViewBox::SVGFitToViewBox() |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 39 | : m_viewBox() |
eseidel | a7f7626 | 2006-09-06 03:36:10 +0000 | [diff] [blame] | 40 | , m_preserveAspectRatio(new SVGPreserveAspectRatio(0)) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | SVGFitToViewBox::~SVGFitToViewBox() |
| 45 | { |
| 46 | } |
| 47 | |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 48 | 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] | 49 | 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] | 50 | |
eseidel | 49ff1a8 | 2006-09-09 20:29:41 +0000 | [diff] [blame] | 51 | void SVGFitToViewBox::parseViewBox(const String& str) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 52 | { |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 53 | double x = 0, y = 0, w = 0, h = 0; |
eseidel | 49ff1a8 | 2006-09-09 20:29:41 +0000 | [diff] [blame] | 54 | DeprecatedString viewbox = str.deprecatedString(); |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 55 | const char *p = viewbox.latin1(); |
| 56 | const char *end = p + viewbox.length(); |
| 57 | const char *c = p; |
| 58 | p = parseCoord(c, x); |
| 59 | if (p == c) |
| 60 | goto bail_out; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 61 | |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 62 | c = p; |
| 63 | p = parseCoord(c, y); |
| 64 | if (p == c) |
| 65 | goto bail_out; |
| 66 | |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 67 | c = p; |
| 68 | p = parseCoord(c, w); |
| 69 | if(w < 0.0 || p == c) // check that width is positive |
| 70 | goto bail_out; |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 71 | |
| 72 | c = p; |
| 73 | p = parseCoord(c, h); |
| 74 | if (h < 0.0 || p == c) // check that height is positive |
| 75 | goto bail_out; |
eseidel | 4957690 | 2006-09-06 03:26:42 +0000 | [diff] [blame] | 76 | |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 77 | if (p < end) // nothing should come after the last, fourth number |
| 78 | goto bail_out; |
| 79 | |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 80 | setViewBoxBaseValue(FloatRect(x, y, w, h)); |
rwlbuis | 987dbc4 | 2006-07-27 07:46:00 +0000 | [diff] [blame] | 81 | return; |
| 82 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 83 | bail_out:; |
| 84 | // FIXME: Per the spec we are supposed to set the document into an "error state" here. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 85 | } |
| 86 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 87 | SVGMatrix* SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 88 | { |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 89 | FloatRect viewBoxRect = viewBox(); |
| 90 | if (!viewBoxRect.width() || !viewBoxRect.height()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 91 | return SVGSVGElement::createSVGMatrix(); |
| 92 | |
eseidel | 18555aa | 2006-09-11 21:42:22 +0000 | [diff] [blame] | 93 | return preserveAspectRatio()->getCTM(viewBoxRect.x(), |
| 94 | viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 95 | 0, 0, viewWidth, viewHeight); |
| 96 | } |
| 97 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 98 | bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute* attr) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 99 | { |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 100 | if (attr->name() == SVGNames::viewBoxAttr) { |
eseidel | 49ff1a8 | 2006-09-09 20:29:41 +0000 | [diff] [blame] | 101 | parseViewBox(attr->value()); |
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) { |
eseidel | a7f7626 | 2006-09-06 03:36:10 +0000 | [diff] [blame] | 104 | preserveAspectRatioBaseValue()->parsePreserveAspectRatio(attr->value().impl()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
eseidel | eb0627e | 2006-08-15 06:12:39 +0000 | [diff] [blame] | 111 | } |
| 112 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 113 | // vim:ts=4:noet |
| 114 | #endif // SVG_SUPPORT |
| 115 | |