SVGGradientElement.cpp 4.2 KB

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