SVGLineElement.cpp 3.7 KB

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