SVGLinearGradientElement.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/SVG/AttributeNames.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGLinearGradientElement.h>
  11. #include <LibWeb/SVG/SVGStopElement.h>
  12. namespace Web::SVG {
  13. SVGLinearGradientElement::SVGLinearGradientElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : SVGGradientElement(document, qualified_name)
  15. {
  16. }
  17. void SVGLinearGradientElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGLinearGradientElementPrototype>(realm, "SVGLinearGradientElement"));
  21. }
  22. void SVGLinearGradientElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
  23. {
  24. SVGGradientElement::attribute_changed(name, value);
  25. // FIXME: Should allow for `<number-percentage> | <length>` for x1, x2, y1, y2
  26. if (name == SVG::AttributeNames::x1) {
  27. m_x1 = AttributeParser::parse_number_percentage(value);
  28. m_paint_style = nullptr;
  29. } else if (name == SVG::AttributeNames::y1) {
  30. m_y1 = AttributeParser::parse_number_percentage(value);
  31. m_paint_style = nullptr;
  32. } else if (name == SVG::AttributeNames::x2) {
  33. m_x2 = AttributeParser::parse_number_percentage(value);
  34. m_paint_style = nullptr;
  35. } else if (name == SVG::AttributeNames::y2) {
  36. m_y2 = AttributeParser::parse_number_percentage(value);
  37. m_paint_style = nullptr;
  38. }
  39. }
  40. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementX1Attribute
  41. NumberPercentage SVGLinearGradientElement::start_x() const
  42. {
  43. if (m_x1.has_value())
  44. return *m_x1;
  45. if (auto gradient = linked_linear_gradient())
  46. return gradient->start_x();
  47. // If the attribute is not specified, the effect is as if a value of '0%' were specified.
  48. return NumberPercentage::create_percentage(0);
  49. }
  50. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementY1Attribute
  51. NumberPercentage SVGLinearGradientElement::start_y() const
  52. {
  53. if (m_y1.has_value())
  54. return *m_y1;
  55. if (auto gradient = linked_linear_gradient())
  56. return gradient->start_x();
  57. // If the attribute is not specified, the effect is as if a value of '0%' were specified.
  58. return NumberPercentage::create_percentage(0);
  59. }
  60. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementX2Attribute
  61. NumberPercentage SVGLinearGradientElement::end_x() const
  62. {
  63. if (m_x2.has_value())
  64. return *m_x2;
  65. if (auto gradient = linked_linear_gradient())
  66. return gradient->start_x();
  67. // If the attribute is not specified, the effect is as if a value of '100%' were specified.
  68. return NumberPercentage::create_percentage(100);
  69. }
  70. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementY2Attribute
  71. NumberPercentage SVGLinearGradientElement::end_y() const
  72. {
  73. if (m_y2.has_value())
  74. return *m_y2;
  75. if (auto gradient = linked_linear_gradient())
  76. return gradient->start_x();
  77. // If the attribute is not specified, the effect is as if a value of '0%' were specified.
  78. return NumberPercentage::create_percentage(0);
  79. }
  80. Optional<Gfx::PaintStyle const&> SVGLinearGradientElement::to_gfx_paint_style(SVGPaintContext const& paint_context) const
  81. {
  82. auto units = gradient_units();
  83. // FIXME: Resolve percentages properly
  84. Gfx::FloatPoint start_point {};
  85. Gfx::FloatPoint end_point {};
  86. // https://svgwg.org/svg2-draft/pservers.html#LinearGradientElementGradientUnitsAttribute
  87. if (units == GradientUnits::ObjectBoundingBox) {
  88. // If gradientUnits="objectBoundingBox", the user coordinate system for attributes ‘x1’, ‘y1’, ‘x2’ and ‘y2’
  89. // is established using the bounding box of the element to which the gradient is applied (see Object bounding
  90. // box units) and then applying the transform specified by attribute ‘gradientTransform’. Percentages represent
  91. // values relative to the bounding box for the object.
  92. // Note: For gradientUnits="objectBoundingBox" both "100%" and "1" are treated the same.
  93. start_point = { start_x().value(), start_y().value() };
  94. end_point = { end_x().value(), end_y().value() };
  95. } else {
  96. // GradientUnits::UserSpaceOnUse
  97. // If gradientUnits="userSpaceOnUse", ‘x1’, ‘y1’, ‘x2’, and ‘y2’ represent values in the coordinate system
  98. // that results from taking the current user coordinate system in place at the time when the gradient element
  99. // is referenced (i.e., the user coordinate system for the element referencing the gradient element via a
  100. // fill or stroke property) and then applying the transform specified by attribute ‘gradientTransform’.
  101. // Percentages represent values relative to the current SVG viewport.
  102. start_point = Gfx::FloatPoint {
  103. start_x().resolve_relative_to(paint_context.viewport.width()),
  104. start_y().resolve_relative_to(paint_context.viewport.height()),
  105. };
  106. end_point = Gfx::FloatPoint {
  107. end_x().resolve_relative_to(paint_context.viewport.width()),
  108. end_y().resolve_relative_to(paint_context.viewport.height()),
  109. };
  110. }
  111. if (!m_paint_style) {
  112. m_paint_style = Gfx::SVGLinearGradientPaintStyle::create(start_point, end_point)
  113. .release_value_but_fixme_should_propagate_errors();
  114. // FIXME: Update stops in DOM changes:
  115. add_color_stops(*m_paint_style);
  116. } else {
  117. m_paint_style->set_start_point(start_point);
  118. m_paint_style->set_end_point(end_point);
  119. }
  120. m_paint_style->set_gradient_transform(gradient_paint_transform(paint_context));
  121. m_paint_style->set_spread_method(to_gfx_spread_method(spread_method()));
  122. return *m_paint_style;
  123. }
  124. JS::NonnullGCPtr<SVGAnimatedLength> SVGLinearGradientElement::x1() const
  125. {
  126. TODO();
  127. }
  128. JS::NonnullGCPtr<SVGAnimatedLength> SVGLinearGradientElement::y1() const
  129. {
  130. TODO();
  131. }
  132. JS::NonnullGCPtr<SVGAnimatedLength> SVGLinearGradientElement::x2() const
  133. {
  134. TODO();
  135. }
  136. JS::NonnullGCPtr<SVGAnimatedLength> SVGLinearGradientElement::y2() const
  137. {
  138. TODO();
  139. }
  140. }