KHtml

SVGFitToViewBox.cpp
1 /*
2  Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <[email protected]>
3  2004, 2005, 2006, 2007 Rob Buis <[email protected]>
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., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "wtf/Platform.h"
24 
25 #if ENABLE(SVG)
26 #include "SVGFitToViewBox.h"
27 
28 #include "AffineTransform.h"
29 #include "FloatRect.h"
30 #include "SVGDocumentExtensions.h"
31 #include "SVGNames.h"
32 #include "SVGParserUtilities.h"
33 #include "SVGPreserveAspectRatio.h"
34 //#include "StringImpl.h"
35 
36 namespace WebCore
37 {
38 
39 SVGFitToViewBox::SVGFitToViewBox()
40  : m_viewBox()
41  , m_preserveAspectRatio(SVGPreserveAspectRatio::create())
42 {
43 }
44 
45 SVGFitToViewBox::~SVGFitToViewBox()
46 {
47 }
48 
49 ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, FloatRect, Rect, rect, ViewBox, viewBox, SVGNames::viewBoxAttr, m_viewBox)
50 ANIMATED_PROPERTY_DEFINITIONS_WITH_CONTEXT(SVGFitToViewBox, SVGPreserveAspectRatio *, PreserveAspectRatio, preserveAspectRatio, PreserveAspectRatio, preserveAspectRatio, SVGNames::preserveAspectRatioAttr, m_preserveAspectRatio.get())
51 
52 bool SVGFitToViewBox::parseViewBox(const UChar *&c, const UChar *end, float &x, float &y, float &w, float &h, bool validate)
53 {
54  Document *doc = contextElement()->document();
55  String str(c, end - c);
56 
57  skipOptionalSpaces(c, end);
58 
59  bool valid = (parseNumber(c, end, x) && parseNumber(c, end, y) &&
60  parseNumber(c, end, w) && parseNumber(c, end, h, false));
61  if (!validate) {
62  return true;
63  }
64  if (!valid) {
65  //FIXME vtokarev doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
66  return false;
67  }
68 
69  if (w < 0.0) { // check that width is positive
70  doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
71  return false;
72  } else if (h < 0.0) { // check that height is positive
73  doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
74  return false;
75  } else {
76  skipOptionalSpaces(c, end);
77  if (c < end) { // nothing should come after the last, fourth number
78  //FIXME vtokarev doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
79  return false;
80  }
81  }
82 
83  return true;
84 }
85 
86 AffineTransform SVGFitToViewBox::viewBoxToViewTransform(float viewWidth, float viewHeight) const
87 {
88  FloatRect viewBoxRect = viewBox();
89  if (!viewBoxRect.width() || !viewBoxRect.height()) {
90  return AffineTransform();
91  }
92 
93  return preserveAspectRatio()->getCTM(viewBoxRect.x(),
94  viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(),
95  0, 0, viewWidth, viewHeight);
96 }
97 
98 bool SVGFitToViewBox::parseMappedAttribute(MappedAttribute *attr)
99 {
100  if (attr->name() == SVGNames::viewBoxAttr) {
101  float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
102  const UChar *c = attr->value().characters();
103  const UChar *end = c + attr->value().length();
104  if (parseViewBox(c, end, x, y, w, h)) {
105  setViewBoxBaseValue(FloatRect(x, y, w, h));
106  }
107  return true;
108  } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
109  const UChar *c = attr->value().characters();
110  const UChar *end = c + attr->value().length();
111  preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end);
112  return true;
113  }
114 
115  return false;
116 }
117 
118 bool SVGFitToViewBox::isKnownAttribute(const QualifiedName &attrName)
119 {
120  return (attrName == SVGNames::viewBoxAttr ||
121  attrName == SVGNames::preserveAspectRatioAttr);
122 }
123 
124 }
125 
126 #endif // ENABLE(SVG)
QAction * create(StandardAction id, const QObject *recvr, Func slot, QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.