KHtml

SVGLength.cpp
1 /*
2  Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <[email protected]>
3  2004, 2005, 2006, 2007 Rob Buis <[email protected]>
4  2007 Apple Inc. All rights reserved.
5 
6  This file is part of the KDE project
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #include "wtf/Platform.h"
25 
26 #if ENABLE(SVG)
27 #include "SVGLength.h"
28 
29 #include "css/csshelper.h"
30 #include "FloatConversion.h"
31 /*#include "FrameView.h"*/
32 #include "RenderObject.h"
33 #include "RenderView.h"
34 #include "SVGParserUtilities.h"
35 #include "SVGSVGElement.h"
36 #include "SVGStyledElement.h"
37 
38 #include <math.h>
39 #include <wtf/Assertions.h>
40 
41 namespace WebCore
42 {
43 
44 // Helper functions
45 static inline unsigned int storeUnit(SVGLengthMode mode, SVGLengthType type)
46 {
47  return (mode << 4) | type;
48 }
49 
50 static inline SVGLengthMode extractMode(unsigned int unit)
51 {
52  unsigned int mode = unit >> 4;
53  return static_cast<SVGLengthMode>(mode);
54 }
55 
56 static inline SVGLengthType extractType(unsigned int unit)
57 {
58  unsigned int mode = unit >> 4;
59  unsigned int type = unit ^ (mode << 4);
60  return static_cast<SVGLengthType>(type);
61 }
62 
63 static inline String lengthTypeToString(SVGLengthType type)
64 {
65  switch (type) {
66  case LengthTypeUnknown:
67  case LengthTypeNumber:
68  return "";
69  case LengthTypePercentage:
70  return "%";
71  case LengthTypeEMS:
72  return "em";
73  case LengthTypeEXS:
74  return "ex";
75  case LengthTypePX:
76  return "px";
77  case LengthTypeCM:
78  return "cm";
79  case LengthTypeMM:
80  return "mm";
81  case LengthTypeIN:
82  return "in";
83  case LengthTypePT:
84  return "pt";
85  case LengthTypePC:
86  return "pc";
87  }
88 
89  return String();
90 }
91 
92 inline SVGLengthType stringToLengthType(const String &string)
93 {
94  if (string.endsWith("%")) {
95  return LengthTypePercentage;
96  } else if (string.endsWith("em")) {
97  return LengthTypeEMS;
98  } else if (string.endsWith("ex")) {
99  return LengthTypeEXS;
100  } else if (string.endsWith("px")) {
101  return LengthTypePX;
102  } else if (string.endsWith("cm")) {
103  return LengthTypeCM;
104  } else if (string.endsWith("mm")) {
105  return LengthTypeMM;
106  } else if (string.endsWith("in")) {
107  return LengthTypeIN;
108  } else if (string.endsWith("pt")) {
109  return LengthTypePT;
110  } else if (string.endsWith("pc")) {
111  return LengthTypePC;
112  } else if (!string.isEmpty()) {
113  return LengthTypeNumber;
114  }
115 
116  return LengthTypeUnknown;
117 }
118 
119 SVGLength::SVGLength(const SVGStyledElement *context, SVGLengthMode mode, const String &valueAsString)
120  : m_valueInSpecifiedUnits(0.0f)
121  , m_unit(storeUnit(mode, LengthTypeNumber))
122  , m_context(context)
123 {
124  setValueAsString(valueAsString);
125 }
126 
127 SVGLengthType SVGLength::unitType() const
128 {
129  return extractType(m_unit);
130 }
131 
132 float SVGLength::value() const
133 {
134  SVGLengthType type = extractType(m_unit);
135  if (type == LengthTypeUnknown) {
136  return 0.0f;
137  }
138 
139  switch (type) {
140  case LengthTypeNumber:
141  return m_valueInSpecifiedUnits;
142  case LengthTypePercentage:
143  return SVGLength::PercentageOfViewport(m_valueInSpecifiedUnits / 100.0f, m_context, extractMode(m_unit));
144  case LengthTypeEMS:
145  case LengthTypeEXS: {
146  /*RenderStyle* style = 0;
147  if (m_context && m_context->renderer())
148  style = m_context->renderer()->style();
149  if (style) {
150  float useSize = style->fontSize();
151  ASSERT(useSize > 0);
152  if (type == LengthTypeEMS)
153  return m_valueInSpecifiedUnits * useSize;
154  else {
155  float xHeight = style->font().xHeight();
156  // Use of ceil allows a pixel match to the W3Cs expected output of coords-units-03-b.svg
157  // if this causes problems in real world cases maybe it would be best to remove this
158  return m_valueInSpecifiedUnits * ceilf(xHeight);
159  }
160  }*/
161  return 0.0f;
162  }
163  case LengthTypePX:
164  return m_valueInSpecifiedUnits;
165  case LengthTypeCM:
166  return m_valueInSpecifiedUnits / 2.54f * cssPixelsPerInch;
167  case LengthTypeMM:
168  return m_valueInSpecifiedUnits / 25.4f * cssPixelsPerInch;
169  case LengthTypeIN:
170  return m_valueInSpecifiedUnits * cssPixelsPerInch;
171  case LengthTypePT:
172  return m_valueInSpecifiedUnits / 72.0f * cssPixelsPerInch;
173  case LengthTypePC:
174  return m_valueInSpecifiedUnits / 6.0f * cssPixelsPerInch;
175  default:
176  break;
177  }
178 
179  ASSERT_NOT_REACHED();
180  return 0.0f;
181 }
182 
183 void SVGLength::setValue(float value)
184 {
185  SVGLengthType type = extractType(m_unit);
186  ASSERT(type != LengthTypeUnknown);
187 
188  switch (type) {
189  case LengthTypeNumber:
190  m_valueInSpecifiedUnits = value;
191  break;
192  case LengthTypePercentage:
193  case LengthTypeEMS:
194  case LengthTypeEXS:
195  ASSERT_NOT_REACHED();
196  break;
197  case LengthTypePX:
198  m_valueInSpecifiedUnits = value;
199  break;
200  case LengthTypeCM:
201  m_valueInSpecifiedUnits = value * 2.54f / cssPixelsPerInch;
202  break;
203  case LengthTypeMM:
204  m_valueInSpecifiedUnits = value * 25.4f / cssPixelsPerInch;
205  break;
206  case LengthTypeIN:
207  m_valueInSpecifiedUnits = value / cssPixelsPerInch;
208  break;
209  case LengthTypePT:
210  m_valueInSpecifiedUnits = value * 72.0f / cssPixelsPerInch;
211  break;
212  case LengthTypePC:
213  m_valueInSpecifiedUnits = value / 6.0f * cssPixelsPerInch;
214  break;
215  default:
216  break;
217  }
218 }
219 
220 void SVGLength::setValueInSpecifiedUnits(float value)
221 {
222  m_valueInSpecifiedUnits = value;
223 }
224 
225 float SVGLength::valueInSpecifiedUnits() const
226 {
227  return m_valueInSpecifiedUnits;
228 }
229 
230 float SVGLength::valueAsPercentage() const
231 {
232  // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
233  if (extractType(m_unit) == LengthTypePercentage) {
234  return valueInSpecifiedUnits() / 100.0f;
235  }
236 
237  return valueInSpecifiedUnits();
238 }
239 
240 bool SVGLength::setValueAsString(const String &s)
241 {
242  if (s.isEmpty()) {
243  return false;
244  }
245 
246  float convertedNumber = 0.0f;
247  const UChar *ptr = s.characters();
248  const UChar *end = ptr + s.length();
249 
250  if (!parseNumber(ptr, end, convertedNumber, false)) {
251  return false;
252  }
253 
254  SVGLengthType type = stringToLengthType(s);
255  if (ptr != end && type == LengthTypeNumber) {
256  return false;
257  }
258 
259  // qCDebug(KHTML_LOG) << convertedNumber << type;
260 
261  m_unit = storeUnit(extractMode(m_unit), type);
262  m_valueInSpecifiedUnits = convertedNumber;
263  return true;
264 }
265 
266 String SVGLength::valueAsString() const
267 {
268  //return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(extractType(m_unit));
269  ASSERT(false);
270  return "";
271 }
272 
273 void SVGLength::newValueSpecifiedUnits(unsigned short type, float value)
274 {
275  ASSERT(type <= LengthTypePC);
276 
277  m_unit = storeUnit(extractMode(m_unit), (SVGLengthType) type);
278  m_valueInSpecifiedUnits = value;
279 }
280 
281 void SVGLength::convertToSpecifiedUnits(unsigned short type)
282 {
283  ASSERT(type <= LengthTypePC);
284 
285  float valueInUserUnits = value();
286  m_unit = storeUnit(extractMode(m_unit), (SVGLengthType) type);
287  setValue(valueInUserUnits);
288 }
289 
290 float SVGLength::PercentageOfViewport(float value, const SVGStyledElement *context, SVGLengthMode mode)
291 {
292  ASSERT(context);
293 
294  float width = 0.0f, height = 0.0f;
295  SVGElement *viewportElement = context->viewportElement();
296 
297  Document *doc = context->document();
298  if (doc->documentElement() == context) {
299  // We have to ask the canvas for the full "canvas size"...
300  RenderView *view = static_cast<RenderView *>(doc->renderer());
301  if (view && view->view()) {
302  width = view->view()->visibleWidth(); // TODO: recheck!
303  height = view->view()->visibleHeight(); // TODO: recheck!
304  }
305  } else if (viewportElement && viewportElement->isSVG()) {
306  const SVGSVGElement *svg = static_cast<const SVGSVGElement *>(viewportElement);
307  if (svg->hasAttribute(SVGNames::viewBoxAttr)) {
308  width = svg->viewBox().width();
309  height = svg->viewBox().height();
310  } else {
311  width = svg->width().value();
312  height = svg->height().value();
313  }
314  } else if (context->parent() && !context->parent()->isSVGElement()) {
315  if (RenderObject *renderer = context->renderer()) {
316  width = renderer->width();
317  height = renderer->height();
318  }
319  }
320 
321  if (mode == LengthModeWidth) {
322  return value * width;
323  } else if (mode == LengthModeHeight) {
324  return value * height;
325  } else if (mode == LengthModeOther) {
326  return value * sqrtf(powf(width, 2) + powf(height, 2)) / sqrtf(2.0f);
327  }
328 
329  return 0.0f;
330 }
331 
332 }
333 
334 #endif // ENABLE(SVG)
335 
const QList< QKeySequence > & end()
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.