SVGLineElement.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/Bindings/SVGLineElementPrototype.h>
  8. #include <LibWeb/SVG/AttributeNames.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGLineElement.h>
  11. namespace Web::SVG {
  12. JS_DEFINE_ALLOCATOR(SVGLineElement);
  13. SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : SVGGeometryElement(document, qualified_name)
  15. {
  16. }
  17. void SVGLineElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLineElement);
  21. }
  22. void SVGLineElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  23. {
  24. SVGGeometryElement::attribute_changed(name, old_value, value);
  25. if (name == SVG::AttributeNames::x1) {
  26. m_x1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
  27. } else if (name == SVG::AttributeNames::y1) {
  28. m_y1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
  29. } else if (name == SVG::AttributeNames::x2) {
  30. m_x2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
  31. } else if (name == SVG::AttributeNames::y2) {
  32. m_y2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
  33. }
  34. }
  35. Gfx::DeprecatedPath SVGLineElement::get_path(CSSPixelSize viewport_size)
  36. {
  37. auto const viewport_width = viewport_size.width().to_float();
  38. auto const viewport_height = viewport_size.height().to_float();
  39. Gfx::DeprecatedPath path;
  40. float const x1 = m_x1.value_or({ 0, false }).resolve_relative_to(viewport_width);
  41. float const y1 = m_y1.value_or({ 0, false }).resolve_relative_to(viewport_height);
  42. float const x2 = m_x2.value_or({ 0, false }).resolve_relative_to(viewport_width);
  43. float const y2 = m_y2.value_or({ 0, false }).resolve_relative_to(viewport_height);
  44. // 1. perform an absolute moveto operation to absolute location (x1,y1)
  45. path.move_to({ x1, y1 });
  46. // 2. perform an absolute lineto operation to absolute location (x2,y2)
  47. path.line_to({ x2, y2 });
  48. return path;
  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(realm(), 0, m_x1.value_or({ 0, false }).value());
  56. auto anim_length = SVGLength::create(realm(), 0, m_x1.value_or({ 0, false }).value());
  57. return SVGAnimatedLength::create(realm(), 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(realm(), 0, m_y1.value_or({ 0, false }).value());
  65. auto anim_length = SVGLength::create(realm(), 0, m_y1.value_or({ 0, false }).value());
  66. return SVGAnimatedLength::create(realm(), 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(realm(), 0, m_x2.value_or({ 0, false }).value());
  74. auto anim_length = SVGLength::create(realm(), 0, m_x2.value_or({ 0, false }).value());
  75. return SVGAnimatedLength::create(realm(), 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(realm(), 0, m_y2.value_or({ 0, false }).value());
  83. auto anim_length = SVGLength::create(realm(), 0, m_y2.value_or({ 0, false }).value());
  84. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  85. }
  86. }