SVGLinearGradientElement.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. JS::ThrowCompletionOr<void> SVGLinearGradientElement::initialize(JS::Realm& realm)
  18. {
  19. MUST_OR_THROW_OOM(Base::initialize(realm));
  20. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGLinearGradientElementPrototype>(realm, "SVGLinearGradientElement"));
  21. return {};
  22. }
  23. void SVGLinearGradientElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString 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);
  29. m_paint_style = nullptr;
  30. } else if (name == SVG::AttributeNames::y1) {
  31. m_y1 = AttributeParser::parse_number_percentage(value);
  32. m_paint_style = nullptr;
  33. } else if (name == SVG::AttributeNames::x2) {
  34. m_x2 = AttributeParser::parse_number_percentage(value);
  35. m_paint_style = nullptr;
  36. } else if (name == SVG::AttributeNames::y2) {
  37. m_y2 = AttributeParser::parse_number_percentage(value);
  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. if (m_x1.has_value())
  45. return *m_x1;
  46. if (auto href = linear_gradient_xlink_href())
  47. return href->start_x();
  48. // If the attribute is not specified, the effect is as if a value of '0%' were specified.
  49. return NumberPercentage::create_percentage(0);
  50. }
  51. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementY1Attribute
  52. NumberPercentage SVGLinearGradientElement::start_y() const
  53. {
  54. if (m_y1.has_value())
  55. return *m_y1;
  56. if (auto href = linear_gradient_xlink_href())
  57. return href->start_x();
  58. // If the attribute is not specified, the effect is as if a value of '0%' were specified.
  59. return NumberPercentage::create_percentage(0);
  60. }
  61. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementX2Attribute
  62. NumberPercentage SVGLinearGradientElement::end_x() const
  63. {
  64. if (m_x2.has_value())
  65. return *m_x2;
  66. if (auto href = linear_gradient_xlink_href())
  67. return href->start_x();
  68. // If the attribute is not specified, the effect is as if a value of '100%' were specified.
  69. return NumberPercentage::create_percentage(100);
  70. }
  71. // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementY2Attribute
  72. NumberPercentage SVGLinearGradientElement::end_y() const
  73. {
  74. if (m_y2.has_value())
  75. return *m_y2;
  76. if (auto href = linear_gradient_xlink_href())
  77. return href->start_x();
  78. // If the attribute is not specified, the effect is as if a value of '0%' were specified.
  79. return NumberPercentage::create_percentage(0);
  80. }
  81. Optional<Gfx::PaintStyle const&> SVGLinearGradientElement::to_gfx_paint_style(SVGPaintContext const& paint_context) const
  82. {
  83. auto units = gradient_units();
  84. // FIXME: Resolve percentages properly
  85. Gfx::FloatPoint start_point {};
  86. Gfx::FloatPoint end_point {};
  87. // https://svgwg.org/svg2-draft/pservers.html#LinearGradientElementGradientUnitsAttribute
  88. if (units == GradientUnits::ObjectBoundingBox) {
  89. // If gradientUnits="objectBoundingBox", the user coordinate system for attributes ‘x1’, ‘y1’, ‘x2’ and ‘y2’
  90. // is established using the bounding box of the element to which the gradient is applied (see Object bounding
  91. // box units) and then applying the transform specified by attribute ‘gradientTransform’. Percentages represent
  92. // values relative to the bounding box for the object.
  93. // Note: For gradientUnits="objectBoundingBox" both "100%" and "1" are treated the same.
  94. start_point = { start_x().value(), start_y().value() };
  95. end_point = { end_x().value(), end_y().value() };
  96. } else {
  97. // GradientUnits::UserSpaceOnUse
  98. // If gradientUnits="userSpaceOnUse", ‘x1’, ‘y1’, ‘x2’, and ‘y2’ represent values in the coordinate system
  99. // that results from taking the current user coordinate system in place at the time when the gradient element
  100. // is referenced (i.e., the user coordinate system for the element referencing the gradient element via a
  101. // fill or stroke property) and then applying the transform specified by attribute ‘gradientTransform’.
  102. // Percentages represent values relative to the current SVG viewport.
  103. start_point = Gfx::FloatPoint {
  104. start_x().resolve_relative_to(paint_context.viewport.width()),
  105. start_y().resolve_relative_to(paint_context.viewport.height()),
  106. };
  107. end_point = Gfx::FloatPoint {
  108. end_x().resolve_relative_to(paint_context.viewport.width()),
  109. end_y().resolve_relative_to(paint_context.viewport.height()),
  110. };
  111. }
  112. if (!m_paint_style) {
  113. m_paint_style = Gfx::SVGLinearGradientPaintStyle::create(start_point, end_point)
  114. .release_value_but_fixme_should_propagate_errors();
  115. // FIXME: Update stops in DOM changes:
  116. add_color_stops(*m_paint_style);
  117. } else {
  118. m_paint_style->set_start_point(start_point);
  119. m_paint_style->set_end_point(end_point);
  120. }
  121. m_paint_style->set_gradient_transform(gradient_paint_transform(paint_context));
  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. }