SVGMaskElement.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/SVGMaskElementPrototype.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/Layout/SVGGraphicsBox.h>
  10. #include <LibWeb/SVG/AttributeNames.h>
  11. #include <LibWeb/SVG/SVGMaskElement.h>
  12. namespace Web::SVG {
  13. SVGMaskElement::SVGMaskElement(DOM::Document& document, DOM::QualifiedName tag_name)
  14. : SVGGraphicsElement(document, move(tag_name))
  15. {
  16. }
  17. SVGMaskElement::~SVGMaskElement() = default;
  18. void SVGMaskElement::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGMaskElementPrototype>(realm, "SVGMaskElement"));
  22. }
  23. JS::GCPtr<Layout::Node> SVGMaskElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  24. {
  25. return heap().allocate_without_realm<Layout::SVGGraphicsBox>(document(), *this, move(style));
  26. }
  27. void SVGMaskElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
  28. {
  29. SVGGraphicsElement::attribute_changed(name, value);
  30. if (name == AttributeNames::maskUnits) {
  31. m_mask_units = AttributeParser::parse_units(value);
  32. } else if (name == AttributeNames::maskContentUnits) {
  33. m_mask_content_units = AttributeParser::parse_units(value);
  34. }
  35. }
  36. MaskContentUnits SVGMaskElement::mask_content_units() const
  37. {
  38. return m_mask_content_units.value_or(MaskContentUnits::UserSpaceOnUse);
  39. }
  40. MaskUnits SVGMaskElement::mask_units() const
  41. {
  42. return m_mask_units.value_or(MaskUnits::ObjectBoundingBox);
  43. }
  44. CSSPixelRect SVGMaskElement::resolve_masking_area(CSSPixelRect const& mask_target) const
  45. {
  46. if (mask_units() == SVG::MaskUnits::UserSpaceOnUse) {
  47. dbgln("SVG: maskUnits=userSpaceOnUse is not supported");
  48. return {};
  49. }
  50. // TODO: Resolve this based on the x, y, width, and height of the mask.
  51. return mask_target.inflated(mask_target.size().scaled(CSSPixels(2) / 10));
  52. }
  53. }