SVGLineElement.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/SVGLineElementPrototype.h>
  7. #include <LibWeb/HTML/Window.h>
  8. #include <LibWeb/SVG/AttributeNames.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGLineElement.h>
  11. namespace Web::SVG {
  12. SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : SVGGeometryElement(document, qualified_name)
  14. {
  15. set_prototype(&window().ensure_web_prototype<Bindings::SVGLineElementPrototype>("SVGLineElement"));
  16. }
  17. void SVGLineElement::parse_attribute(FlyString const& name, String const& value)
  18. {
  19. SVGGeometryElement::parse_attribute(name, value);
  20. if (name == SVG::AttributeNames::x1) {
  21. m_x1 = AttributeParser::parse_coordinate(value);
  22. m_path.clear();
  23. } else if (name == SVG::AttributeNames::y1) {
  24. m_y1 = AttributeParser::parse_coordinate(value);
  25. m_path.clear();
  26. } else if (name == SVG::AttributeNames::x2) {
  27. m_x2 = AttributeParser::parse_coordinate(value);
  28. m_path.clear();
  29. } else if (name == SVG::AttributeNames::y2) {
  30. m_y2 = AttributeParser::parse_coordinate(value);
  31. m_path.clear();
  32. }
  33. }
  34. Gfx::Path& SVGLineElement::get_path()
  35. {
  36. if (m_path.has_value())
  37. return m_path.value();
  38. Gfx::Path path;
  39. float x1 = m_x1.value_or(0);
  40. float y1 = m_y1.value_or(0);
  41. float x2 = m_x2.value_or(0);
  42. float y2 = m_y2.value_or(0);
  43. // 1. perform an absolute moveto operation to absolute location (x1,y1)
  44. path.move_to({ x1, y1 });
  45. // 2. perform an absolute lineto operation to absolute location (x2,y2)
  46. path.line_to({ x2, y2 });
  47. m_path = move(path);
  48. return m_path.value();
  49. }
  50. // https://www.w3.org/TR/SVG11/shapes.html#LineElementX1Attribute
  51. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x1() const
  52. {
  53. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  54. // FIXME: Create a proper animated value when animations are supported.
  55. auto base_length = SVGLength::create(window(), 0, m_x1.value_or(0));
  56. auto anim_length = SVGLength::create(window(), 0, m_x1.value_or(0));
  57. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  58. }
  59. // https://www.w3.org/TR/SVG11/shapes.html#LineElementY1Attribute
  60. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y1() const
  61. {
  62. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  63. // FIXME: Create a proper animated value when animations are supported.
  64. auto base_length = SVGLength::create(window(), 0, m_y1.value_or(0));
  65. auto anim_length = SVGLength::create(window(), 0, m_y1.value_or(0));
  66. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  67. }
  68. // https://www.w3.org/TR/SVG11/shapes.html#LineElementX2Attribute
  69. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x2() const
  70. {
  71. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  72. // FIXME: Create a proper animated value when animations are supported.
  73. auto base_length = SVGLength::create(window(), 0, m_x2.value_or(0));
  74. auto anim_length = SVGLength::create(window(), 0, m_x2.value_or(0));
  75. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  76. }
  77. // https://www.w3.org/TR/SVG11/shapes.html#LineElementY2Attribute
  78. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y2() const
  79. {
  80. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  81. // FIXME: Create a proper animated value when animations are supported.
  82. auto base_length = SVGLength::create(window(), 0, m_y2.value_or(0));
  83. auto anim_length = SVGLength::create(window(), 0, m_y2.value_or(0));
  84. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  85. }
  86. }