SVGLinearGradientElement.cpp 7.8 KB

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