SVGLinearGradientElement.cpp 7.7 KB

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