SVGLineElement.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SVGLineElement.h"
  7. #include <LibWeb/SVG/AttributeNames.h>
  8. #include <LibWeb/SVG/AttributeParser.h>
  9. namespace Web::SVG {
  10. SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  11. : SVGGeometryElement(document, qualified_name)
  12. {
  13. }
  14. void SVGLineElement::parse_attribute(FlyString const& name, String const& value)
  15. {
  16. SVGGeometryElement::parse_attribute(name, value);
  17. if (name == SVG::AttributeNames::x1) {
  18. m_x1 = AttributeParser::parse_coordinate(value);
  19. m_path.clear();
  20. } else if (name == SVG::AttributeNames::y1) {
  21. m_y1 = AttributeParser::parse_coordinate(value);
  22. m_path.clear();
  23. } else if (name == SVG::AttributeNames::x2) {
  24. m_x2 = AttributeParser::parse_coordinate(value);
  25. m_path.clear();
  26. } else if (name == SVG::AttributeNames::y2) {
  27. m_y2 = AttributeParser::parse_coordinate(value);
  28. m_path.clear();
  29. }
  30. }
  31. Gfx::Path& SVGLineElement::get_path()
  32. {
  33. if (m_path.has_value())
  34. return m_path.value();
  35. Gfx::Path path;
  36. float x1 = m_x1.value_or(0);
  37. float y1 = m_y1.value_or(0);
  38. float x2 = m_x2.value_or(0);
  39. float y2 = m_y2.value_or(0);
  40. // 1. perform an absolute moveto operation to absolute location (x1,y1)
  41. path.move_to({ x1, y1 });
  42. // 2. perform an absolute lineto operation to absolute location (x2,y2)
  43. path.line_to({ x2, y2 });
  44. m_path = move(path);
  45. return m_path.value();
  46. }
  47. // https://www.w3.org/TR/SVG11/shapes.html#LineElementX1Attribute
  48. NonnullRefPtr<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(0, m_x1.value_or(0));
  53. auto anim_length = SVGLength::create(0, m_x1.value_or(0));
  54. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  55. }
  56. // https://www.w3.org/TR/SVG11/shapes.html#LineElementY1Attribute
  57. NonnullRefPtr<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(0, m_y1.value_or(0));
  62. auto anim_length = SVGLength::create(0, m_y1.value_or(0));
  63. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  64. }
  65. // https://www.w3.org/TR/SVG11/shapes.html#LineElementX2Attribute
  66. NonnullRefPtr<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(0, m_x2.value_or(0));
  71. auto anim_length = SVGLength::create(0, m_x2.value_or(0));
  72. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  73. }
  74. // https://www.w3.org/TR/SVG11/shapes.html#LineElementY2Attribute
  75. NonnullRefPtr<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(0, m_y2.value_or(0));
  80. auto anim_length = SVGLength::create(0, m_y2.value_or(0));
  81. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  82. }
  83. }