• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.12
  • kdelibs
  • khtml
  • svg
SVGLength.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
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 "config.h"
25 #include "wtf/Platform.h"
26 
27 #if ENABLE(SVG)
28 #include "SVGLength.h"
29 
30 #include "css/csshelper.h"
31 #include "FloatConversion.h"
32 /*#include "FrameView.h"*/
33 #include "RenderObject.h"
34 #include "RenderView.h"
35 #include "SVGParserUtilities.h"
36 #include "SVGSVGElement.h"
37 #include "SVGStyledElement.h"
38 
39 #include <math.h>
40 #include <wtf/Assertions.h>
41 
42 namespace WebCore {
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  return LengthTypeUnknown;
116 }
117 
118 SVGLength::SVGLength(const SVGStyledElement* context, SVGLengthMode mode, const String& valueAsString)
119  : m_valueInSpecifiedUnits(0.0f)
120  , m_unit(storeUnit(mode, LengthTypeNumber))
121  , m_context(context)
122 {
123  setValueAsString(valueAsString);
124 }
125 
126 SVGLengthType SVGLength::unitType() const
127 {
128  return extractType(m_unit);
129 }
130 
131 float SVGLength::value() const
132 {
133  SVGLengthType type = extractType(m_unit);
134  if (type == LengthTypeUnknown)
135  return 0.0f;
136 
137  switch (type) {
138  case LengthTypeNumber:
139  return m_valueInSpecifiedUnits;
140  case LengthTypePercentage:
141  return SVGLength::PercentageOfViewport(m_valueInSpecifiedUnits / 100.0f, m_context, extractMode(m_unit));
142  case LengthTypeEMS:
143  case LengthTypeEXS:
144  {
145  /*RenderStyle* style = 0;
146  if (m_context && m_context->renderer())
147  style = m_context->renderer()->style();
148  if (style) {
149  float useSize = style->fontSize();
150  ASSERT(useSize > 0);
151  if (type == LengthTypeEMS)
152  return m_valueInSpecifiedUnits * useSize;
153  else {
154  float xHeight = style->font().xHeight();
155  // Use of ceil allows a pixel match to the W3Cs expected output of coords-units-03-b.svg
156  // if this causes problems in real world cases maybe it would be best to remove this
157  return m_valueInSpecifiedUnits * ceilf(xHeight);
158  }
159  }*/
160  return 0.0f;
161  }
162  case LengthTypePX:
163  return m_valueInSpecifiedUnits;
164  case LengthTypeCM:
165  return m_valueInSpecifiedUnits / 2.54f * cssPixelsPerInch;
166  case LengthTypeMM:
167  return m_valueInSpecifiedUnits / 25.4f * cssPixelsPerInch;
168  case LengthTypeIN:
169  return m_valueInSpecifiedUnits * cssPixelsPerInch;
170  case LengthTypePT:
171  return m_valueInSpecifiedUnits / 72.0f * cssPixelsPerInch;
172  case LengthTypePC:
173  return m_valueInSpecifiedUnits / 6.0f * cssPixelsPerInch;
174  default:
175  break;
176  }
177 
178  ASSERT_NOT_REACHED();
179  return 0.0f;
180 }
181 
182 void SVGLength::setValue(float value)
183 {
184  SVGLengthType type = extractType(m_unit);
185  ASSERT(type != LengthTypeUnknown);
186 
187  switch (type) {
188  case LengthTypeNumber:
189  m_valueInSpecifiedUnits = value;
190  break;
191  case LengthTypePercentage:
192  case LengthTypeEMS:
193  case LengthTypeEXS:
194  ASSERT_NOT_REACHED();
195  break;
196  case LengthTypePX:
197  m_valueInSpecifiedUnits = value;
198  break;
199  case LengthTypeCM:
200  m_valueInSpecifiedUnits = value * 2.54f / cssPixelsPerInch;
201  break;
202  case LengthTypeMM:
203  m_valueInSpecifiedUnits = value * 25.4f / cssPixelsPerInch;
204  break;
205  case LengthTypeIN:
206  m_valueInSpecifiedUnits = value / cssPixelsPerInch;
207  break;
208  case LengthTypePT:
209  m_valueInSpecifiedUnits = value * 72.0f / cssPixelsPerInch;
210  break;
211  case LengthTypePC:
212  m_valueInSpecifiedUnits = value / 6.0f * cssPixelsPerInch;
213  break;
214  default:
215  break;
216  }
217 }
218 
219 void SVGLength::setValueInSpecifiedUnits(float value)
220 {
221  m_valueInSpecifiedUnits = value;
222 }
223 
224 float SVGLength::valueInSpecifiedUnits() const
225 {
226  return m_valueInSpecifiedUnits;
227 }
228 
229 float SVGLength::valueAsPercentage() const
230 {
231  // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
232  if (extractType(m_unit) == LengthTypePercentage)
233  return valueInSpecifiedUnits() / 100.0f;
234 
235  return valueInSpecifiedUnits();
236 }
237 
238 bool SVGLength::setValueAsString(const String& s)
239 {
240  if (s.isEmpty())
241  return false;
242 
243  float convertedNumber = 0.0f;
244  const UChar* ptr = s.characters();
245  const UChar* end = ptr + s.length();
246 
247  if (!parseNumber(ptr, end, convertedNumber, false))
248  return false;
249 
250  SVGLengthType type = stringToLengthType(s);
251  if (ptr != end && type == LengthTypeNumber)
252  return false;
253 
254  kDebug() << convertedNumber << type;
255 
256  m_unit = storeUnit(extractMode(m_unit), type);
257  m_valueInSpecifiedUnits = convertedNumber;
258  return true;
259 }
260 
261 String SVGLength::valueAsString() const
262 {
263  //return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(extractType(m_unit));
264  ASSERT(false);
265  return "";
266 }
267 
268 void SVGLength::newValueSpecifiedUnits(unsigned short type, float value)
269 {
270  ASSERT(type <= LengthTypePC);
271 
272  m_unit = storeUnit(extractMode(m_unit), (SVGLengthType) type);
273  m_valueInSpecifiedUnits = value;
274 }
275 
276 void SVGLength::convertToSpecifiedUnits(unsigned short type)
277 {
278  ASSERT(type <= LengthTypePC);
279 
280  float valueInUserUnits = value();
281  m_unit = storeUnit(extractMode(m_unit), (SVGLengthType) type);
282  setValue(valueInUserUnits);
283 }
284 
285 float SVGLength::PercentageOfViewport(float value, const SVGStyledElement* context, SVGLengthMode mode)
286 {
287  ASSERT(context);
288 
289  float width = 0.0f, height = 0.0f;
290  SVGElement* viewportElement = context->viewportElement();
291 
292  Document* doc = context->document();
293  if (doc->documentElement() == context) {
294  // We have to ask the canvas for the full "canvas size"...
295  RenderView* view = static_cast<RenderView*>(doc->renderer());
296  if (view && view->view()) {
297  width = view->view()->visibleWidth(); // TODO: recheck!
298  height = view->view()->visibleHeight(); // TODO: recheck!
299  }
300  } else if (viewportElement && viewportElement->isSVG()) {
301  const SVGSVGElement* svg = static_cast<const SVGSVGElement*>(viewportElement);
302  if (svg->hasAttribute(SVGNames::viewBoxAttr)) {
303  width = svg->viewBox().width();
304  height = svg->viewBox().height();
305  } else {
306  width = svg->width().value();
307  height = svg->height().value();
308  }
309  } else if (context->parent() && !context->parent()->isSVGElement()) {
310  if (RenderObject* renderer = context->renderer()) {
311  width = renderer->width();
312  height = renderer->height();
313  }
314  }
315 
316  if (mode == LengthModeWidth)
317  return value * width;
318  else if (mode == LengthModeHeight)
319  return value * height;
320  else if (mode == LengthModeOther)
321  return value * sqrtf(powf(width, 2) + powf(height, 2)) / sqrtf(2.0f);
322 
323  return 0.0f;
324 }
325 
326 }
327 
328 #endif // ENABLE(SVG)
329 
330 // vim:ts=4:noet
SVGParserUtilities.h
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
SVGLength.h
SVGSVGElement.h
FloatConversion.h
WebCore::SVGNames::viewBoxAttr
DOM::QualifiedName viewBoxAttr
Definition: SVGNames.cpp:331
WebCore::RenderView
RenderCanvas RenderView
Definition: RenderView.h:9
SVGStyledElement.h
WebCore::String
DOM::DOMString String
Definition: PlatformString.h:8
RenderObject.h
end
const KShortcut & end()
RenderView.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:51:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal