SVGMaskElement.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/SVG/AttributeNames.h>
  10. #include <LibWeb/SVG/SVGMaskElement.h>
  11. namespace Web::SVG {
  12. JS_DEFINE_ALLOCATOR(SVGMaskElement);
  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. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGMaskElement);
  22. }
  23. JS::GCPtr<Layout::Node> SVGMaskElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties>)
  24. {
  25. // Masks are handled as a special case in the TreeBuilder.
  26. return nullptr;
  27. }
  28. void SVGMaskElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  29. {
  30. SVGGraphicsElement::attribute_changed(name, value);
  31. if (name == AttributeNames::maskUnits) {
  32. m_mask_units = AttributeParser::parse_units(value.value_or(String {}));
  33. } else if (name == AttributeNames::maskContentUnits) {
  34. m_mask_content_units = AttributeParser::parse_units(value.value_or(String {}));
  35. }
  36. }
  37. MaskContentUnits SVGMaskElement::mask_content_units() const
  38. {
  39. return m_mask_content_units.value_or(MaskContentUnits::UserSpaceOnUse);
  40. }
  41. MaskUnits SVGMaskElement::mask_units() const
  42. {
  43. return m_mask_units.value_or(MaskUnits::ObjectBoundingBox);
  44. }
  45. CSSPixelRect SVGMaskElement::resolve_masking_area(CSSPixelRect const& mask_target) const
  46. {
  47. // TODO: Resolve this based on the x, y, width, and height of the mask.
  48. return mask_target.inflated(mask_target.size().scaled(CSSPixels(2) / 10));
  49. }
  50. }