SVGGradientElement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/SVGGradientElement.h>
  10. #include <LibWeb/SVG/SVGGraphicsElement.h>
  11. namespace Web::SVG {
  12. SVGGradientElement::SVGGradientElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : SVGElement(document, move(qualified_name))
  14. {
  15. }
  16. void SVGGradientElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  17. {
  18. SVGElement::attribute_changed(name, value);
  19. if (name == AttributeNames::gradientUnits) {
  20. m_gradient_units = AttributeParser::parse_units(value.value_or(String {}));
  21. } else if (name == AttributeNames::spreadMethod) {
  22. m_spread_method = AttributeParser::parse_spread_method(value.value_or(String {}));
  23. } else if (name == AttributeNames::gradientTransform) {
  24. if (auto transform_list = AttributeParser::parse_transform(value.value_or(String {})); transform_list.has_value()) {
  25. m_gradient_transform = transform_from_transform_list(*transform_list);
  26. } else {
  27. m_gradient_transform = {};
  28. }
  29. }
  30. }
  31. GradientUnits SVGGradientElement::gradient_units() const
  32. {
  33. if (m_gradient_units.has_value())
  34. return *m_gradient_units;
  35. if (auto gradient = linked_gradient())
  36. return gradient->gradient_units();
  37. return GradientUnits::ObjectBoundingBox;
  38. }
  39. SpreadMethod SVGGradientElement::spread_method() const
  40. {
  41. if (m_spread_method.has_value())
  42. return *m_spread_method;
  43. if (auto gradient = linked_gradient())
  44. return gradient->spread_method();
  45. return SpreadMethod::Pad;
  46. }
  47. Optional<Gfx::AffineTransform> SVGGradientElement::gradient_transform() const
  48. {
  49. if (m_gradient_transform.has_value())
  50. return m_gradient_transform;
  51. if (auto gradient = linked_gradient())
  52. return gradient->gradient_transform();
  53. return {};
  54. }
  55. // The gradient transform, appropriately scaled and combined with the paint transform.
  56. Gfx::AffineTransform SVGGradientElement::gradient_paint_transform(SVGPaintContext const& paint_context) const
  57. {
  58. auto transform = gradient_transform().value_or(Gfx::AffineTransform {});
  59. if (gradient_units() == GradientUnits::ObjectBoundingBox) {
  60. // Adjust transform to take place in the coordinate system defined by the bounding box:
  61. return Gfx::AffineTransform { paint_context.transform }
  62. .translate(paint_context.path_bounding_box.location())
  63. .scale(paint_context.path_bounding_box.width(), paint_context.path_bounding_box.height())
  64. .multiply(transform);
  65. }
  66. return Gfx::AffineTransform { paint_context.transform }.multiply(transform);
  67. }
  68. void SVGGradientElement::add_color_stops(Gfx::SVGGradientPaintStyle& paint_style) const
  69. {
  70. for_each_color_stop([&](auto& stop) {
  71. // https://svgwg.org/svg2-draft/pservers.html#StopNotes
  72. // Gradient offset values less than 0 (or less than 0%) are rounded up to 0%.
  73. // Gradient offset values greater than 1 (or greater than 100%) are rounded down to 100%.
  74. float stop_offset = AK::clamp(stop.stop_offset().value(), 0.0f, 1.0f);
  75. // FIXME: Each gradient offset value is required to be equal to or greater than the previous gradient
  76. // stop's offset value. If a given gradient stop's offset value is not equal to or greater than all
  77. // previous offset values, then the offset value is adjusted to be equal to the largest of all previous
  78. // offset values.
  79. paint_style.add_color_stop(stop_offset, stop.stop_color().with_opacity(stop.stop_opacity())).release_value_but_fixme_should_propagate_errors();
  80. });
  81. }
  82. JS::GCPtr<SVGGradientElement const> SVGGradientElement::linked_gradient() const
  83. {
  84. // FIXME: This entire function is an ad-hoc hack!
  85. // It can only resolve #<ids> in the same document.
  86. auto link = has_attribute(AttributeNames::href) ? get_attribute(AttributeNames::href) : get_attribute("xlink:href"_fly_string);
  87. if (auto href = link; href.has_value() && !link->is_empty()) {
  88. auto url = document().parse_url(*href);
  89. auto id = url.fragment();
  90. if (!id.has_value() || id->is_empty())
  91. return {};
  92. auto element = document().get_element_by_id(id.value());
  93. if (!element)
  94. return {};
  95. if (!is<SVGGradientElement>(*element))
  96. return {};
  97. return &verify_cast<SVGGradientElement>(*element);
  98. }
  99. return {};
  100. }
  101. void SVGGradientElement::initialize(JS::Realm& realm)
  102. {
  103. Base::initialize(realm);
  104. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGGradientElementPrototype>(realm, "SVGGradientElement"_fly_string));
  105. }
  106. }