SVGLineElement.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. JS_DEFINE_ALLOCATOR(SVGLineElement);
  12. SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : SVGGeometryElement(document, qualified_name)
  14. {
  15. }
  16. void SVGLineElement::initialize(JS::Realm& realm)
  17. {
  18. Base::initialize(realm);
  19. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLineElement);
  20. }
  21. void SVGLineElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  22. {
  23. SVGGeometryElement::attribute_changed(name, value);
  24. if (name == SVG::AttributeNames::x1) {
  25. m_x1 = AttributeParser::parse_coordinate(value.value_or(String {}));
  26. } else if (name == SVG::AttributeNames::y1) {
  27. m_y1 = AttributeParser::parse_coordinate(value.value_or(String {}));
  28. } else if (name == SVG::AttributeNames::x2) {
  29. m_x2 = AttributeParser::parse_coordinate(value.value_or(String {}));
  30. } else if (name == SVG::AttributeNames::y2) {
  31. m_y2 = AttributeParser::parse_coordinate(value.value_or(String {}));
  32. }
  33. }
  34. Gfx::Path SVGLineElement::get_path(CSSPixelSize)
  35. {
  36. Gfx::Path path;
  37. float x1 = m_x1.value_or(0);
  38. float y1 = m_y1.value_or(0);
  39. float x2 = m_x2.value_or(0);
  40. float y2 = m_y2.value_or(0);
  41. // 1. perform an absolute moveto operation to absolute location (x1,y1)
  42. path.move_to({ x1, y1 });
  43. // 2. perform an absolute lineto operation to absolute location (x2,y2)
  44. path.line_to({ x2, y2 });
  45. return path;
  46. }
  47. // https://www.w3.org/TR/SVG11/shapes.html#LineElementX1Attribute
  48. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x1() const
  49. {
  50. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  51. // FIXME: Create a proper animated value when animations are supported.
  52. auto base_length = SVGLength::create(realm(), 0, m_x1.value_or(0));
  53. auto anim_length = SVGLength::create(realm(), 0, m_x1.value_or(0));
  54. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  55. }
  56. // https://www.w3.org/TR/SVG11/shapes.html#LineElementY1Attribute
  57. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y1() const
  58. {
  59. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  60. // FIXME: Create a proper animated value when animations are supported.
  61. auto base_length = SVGLength::create(realm(), 0, m_y1.value_or(0));
  62. auto anim_length = SVGLength::create(realm(), 0, m_y1.value_or(0));
  63. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  64. }
  65. // https://www.w3.org/TR/SVG11/shapes.html#LineElementX2Attribute
  66. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x2() const
  67. {
  68. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  69. // FIXME: Create a proper animated value when animations are supported.
  70. auto base_length = SVGLength::create(realm(), 0, m_x2.value_or(0));
  71. auto anim_length = SVGLength::create(realm(), 0, m_x2.value_or(0));
  72. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  73. }
  74. // https://www.w3.org/TR/SVG11/shapes.html#LineElementY2Attribute
  75. JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y2() const
  76. {
  77. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  78. // FIXME: Create a proper animated value when animations are supported.
  79. auto base_length = SVGLength::create(realm(), 0, m_y2.value_or(0));
  80. auto anim_length = SVGLength::create(realm(), 0, m_y2.value_or(0));
  81. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  82. }
  83. }