/src/3rdparty/webkit/Source/WebCore/svg/SVGViewSpec.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 168 lines · 127 code · 22 blank · 19 comment · 66 complexity · 52c4282ffaef77ac867c5ec466b01a26 MD5 · raw file

  1. /*
  2. * Copyright (C) 2007, 2010 Rob Buis <buis@kde.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include "config.h"
  20. #if ENABLE(SVG)
  21. #include "SVGViewSpec.h"
  22. #include "Document.h"
  23. #include "SVGNames.h"
  24. #include "SVGParserUtilities.h"
  25. #include "SVGSVGElement.h"
  26. #include "SVGTransformable.h"
  27. namespace WebCore {
  28. // Animated property definitions
  29. DEFINE_ANIMATED_RECT(SVGViewSpec, SVGNames::viewBoxAttr, ViewBox, viewBox)
  30. DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGViewSpec, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
  31. SVGViewSpec::SVGViewSpec(SVGElement* contextElement)
  32. : m_contextElement(contextElement)
  33. {
  34. }
  35. void SVGViewSpec::setTransform(const String& transform)
  36. {
  37. SVGTransformable::parseTransformAttribute(m_transform, transform);
  38. }
  39. void SVGViewSpec::setViewBoxString(const String& viewBoxStr)
  40. {
  41. FloatRect viewBox;
  42. const UChar* c = viewBoxStr.characters();
  43. const UChar* end = c + viewBoxStr.length();
  44. if (!parseViewBox(m_contextElement->document(), c, end, viewBox, false))
  45. return;
  46. setViewBoxBaseValue(viewBox);
  47. }
  48. void SVGViewSpec::setPreserveAspectRatioString(const String& preserve)
  49. {
  50. SVGPreserveAspectRatio::parsePreserveAspectRatio(this, preserve);
  51. }
  52. void SVGViewSpec::setViewTargetString(const String& viewTargetString)
  53. {
  54. m_viewTargetString = viewTargetString;
  55. }
  56. SVGElement* SVGViewSpec::viewTarget() const
  57. {
  58. return static_cast<SVGElement*>(m_contextElement->treeScope()->getElementById(m_viewTargetString));
  59. }
  60. static const UChar svgViewSpec[] = {'s', 'v', 'g', 'V', 'i', 'e', 'w'};
  61. static const UChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'};
  62. static const UChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'};
  63. static const UChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm'};
  64. static const UChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', 'a', 'n'};
  65. static const UChar viewTargetSpec[] = {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g', 'e', 't'};
  66. bool SVGViewSpec::parseViewSpec(const String& viewSpec)
  67. {
  68. const UChar* currViewSpec = viewSpec.characters();
  69. const UChar* end = currViewSpec + viewSpec.length();
  70. if (currViewSpec >= end)
  71. return false;
  72. if (!skipString(currViewSpec, end, svgViewSpec, WTF_ARRAY_LENGTH(svgViewSpec)))
  73. return false;
  74. if (currViewSpec >= end || *currViewSpec != '(')
  75. return false;
  76. currViewSpec++;
  77. while (currViewSpec < end && *currViewSpec != ')') {
  78. if (*currViewSpec == 'v') {
  79. if (skipString(currViewSpec, end, viewBoxSpec, WTF_ARRAY_LENGTH(viewBoxSpec))) {
  80. if (currViewSpec >= end || *currViewSpec != '(')
  81. return false;
  82. currViewSpec++;
  83. FloatRect viewBox;
  84. if (!parseViewBox(m_contextElement->document(), currViewSpec, end, viewBox, false))
  85. return false;
  86. setViewBoxBaseValue(viewBox);
  87. if (currViewSpec >= end || *currViewSpec != ')')
  88. return false;
  89. currViewSpec++;
  90. } else if (skipString(currViewSpec, end, viewTargetSpec, WTF_ARRAY_LENGTH(viewTargetSpec))) {
  91. if (currViewSpec >= end || *currViewSpec != '(')
  92. return false;
  93. const UChar* viewTargetStart = ++currViewSpec;
  94. while (currViewSpec < end && *currViewSpec != ')')
  95. currViewSpec++;
  96. if (currViewSpec >= end)
  97. return false;
  98. setViewTargetString(String(viewTargetStart, currViewSpec - viewTargetStart));
  99. currViewSpec++;
  100. } else
  101. return false;
  102. } else if (*currViewSpec == 'z') {
  103. if (!skipString(currViewSpec, end, zoomAndPanSpec, WTF_ARRAY_LENGTH(zoomAndPanSpec)))
  104. return false;
  105. if (currViewSpec >= end || *currViewSpec != '(')
  106. return false;
  107. currViewSpec++;
  108. if (!parseZoomAndPan(currViewSpec, end))
  109. return false;
  110. if (currViewSpec >= end || *currViewSpec != ')')
  111. return false;
  112. currViewSpec++;
  113. } else if (*currViewSpec == 'p') {
  114. if (!skipString(currViewSpec, end, preserveAspectRatioSpec, WTF_ARRAY_LENGTH(preserveAspectRatioSpec)))
  115. return false;
  116. if (currViewSpec >= end || *currViewSpec != '(')
  117. return false;
  118. currViewSpec++;
  119. bool result = false;
  120. setPreserveAspectRatioBaseValue(SVGPreserveAspectRatio::parsePreserveAspectRatio(currViewSpec, end, false, result));
  121. if (!result)
  122. return false;
  123. if (currViewSpec >= end || *currViewSpec != ')')
  124. return false;
  125. currViewSpec++;
  126. } else if (*currViewSpec == 't') {
  127. if (!skipString(currViewSpec, end, transformSpec, WTF_ARRAY_LENGTH(transformSpec)))
  128. return false;
  129. if (currViewSpec >= end || *currViewSpec != '(')
  130. return false;
  131. currViewSpec++;
  132. SVGTransformable::parseTransformAttribute(m_transform, currViewSpec, end, SVGTransformable::DoNotClearList);
  133. if (currViewSpec >= end || *currViewSpec != ')')
  134. return false;
  135. currViewSpec++;
  136. } else
  137. return false;
  138. if (currViewSpec < end && *currViewSpec == ';')
  139. currViewSpec++;
  140. }
  141. if (currViewSpec >= end || *currViewSpec != ')')
  142. return false;
  143. return true;
  144. }
  145. }
  146. #endif // ENABLE(SVG)