SVGPolygonElement.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/SVGPolygonElement.h"
  6. #include "mozilla/dom/SVGPolygonElementBinding.h"
  7. #include "mozilla/gfx/2D.h"
  8. #include "SVGContentUtils.h"
  9. using namespace mozilla;
  10. using namespace mozilla::gfx;
  11. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(Polygon)
  12. namespace mozilla {
  13. namespace dom {
  14. JSObject*
  15. SVGPolygonElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  16. {
  17. return SVGPolygonElementBinding::Wrap(aCx, this, aGivenProto);
  18. }
  19. //----------------------------------------------------------------------
  20. // Implementation
  21. SVGPolygonElement::SVGPolygonElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  22. : SVGPolygonElementBase(aNodeInfo)
  23. {
  24. }
  25. //----------------------------------------------------------------------
  26. // nsIDOMNode methods
  27. NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGPolygonElement)
  28. //----------------------------------------------------------------------
  29. // nsSVGPathGeometryElement methods
  30. void
  31. SVGPolygonElement::GetMarkPoints(nsTArray<nsSVGMark> *aMarks)
  32. {
  33. nsSVGPolyElement::GetMarkPoints(aMarks);
  34. if (aMarks->IsEmpty() || aMarks->LastElement().type != nsSVGMark::eEnd) {
  35. return;
  36. }
  37. nsSVGMark *endMark = &aMarks->LastElement();
  38. nsSVGMark *startMark = &aMarks->ElementAt(0);
  39. float angle = atan2(startMark->y - endMark->y, startMark->x - endMark->x);
  40. endMark->type = nsSVGMark::eMid;
  41. endMark->angle = SVGContentUtils::AngleBisect(angle, endMark->angle);
  42. startMark->angle = SVGContentUtils::AngleBisect(angle, startMark->angle);
  43. // for a polygon (as opposed to a polyline) there's an implicit extra point
  44. // co-located with the start point that nsSVGPolyElement::GetMarkPoints
  45. // doesn't return
  46. aMarks->AppendElement(nsSVGMark(startMark->x, startMark->y, startMark->angle,
  47. nsSVGMark::eEnd));
  48. }
  49. already_AddRefed<Path>
  50. SVGPolygonElement::BuildPath(PathBuilder* aBuilder)
  51. {
  52. const SVGPointList &points = mPoints.GetAnimValue();
  53. if (points.IsEmpty()) {
  54. return nullptr;
  55. }
  56. aBuilder->MoveTo(points[0]);
  57. for (uint32_t i = 1; i < points.Length(); ++i) {
  58. aBuilder->LineTo(points[i]);
  59. }
  60. aBuilder->Close();
  61. return aBuilder->Finish();
  62. }
  63. } // namespace dom
  64. } // namespace mozilla