SVGGraphicsElement.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGfx/PaintStyle.h>
  9. #include <LibGfx/Painter.h>
  10. #include <LibGfx/Path.h>
  11. #include <LibWeb/DOM/Node.h>
  12. #include <LibWeb/SVG/AttributeParser.h>
  13. #include <LibWeb/SVG/SVGElement.h>
  14. #include <LibWeb/SVG/SVGGradientElement.h>
  15. #include <LibWeb/SVG/TagNames.h>
  16. #include <LibWeb/SVG/ViewBox.h>
  17. namespace Web::SVG {
  18. class SVGGraphicsElement : public SVGElement {
  19. WEB_PLATFORM_OBJECT(SVGGraphicsElement, SVGElement);
  20. public:
  21. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  22. virtual void attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  23. Optional<Gfx::Color> fill_color() const;
  24. Optional<FillRule> fill_rule() const;
  25. Optional<Gfx::Color> stroke_color() const;
  26. Optional<float> stroke_width() const;
  27. Optional<float> fill_opacity() const;
  28. Optional<float> stroke_opacity() const;
  29. float visible_stroke_width() const
  30. {
  31. if (auto color = stroke_color(); color.has_value() && color->alpha() > 0)
  32. return stroke_width().value_or(0);
  33. return 0;
  34. }
  35. Gfx::AffineTransform get_transform() const;
  36. Optional<Gfx::PaintStyle const&> fill_paint_style(SVGPaintContext const&) const;
  37. Optional<Gfx::PaintStyle const&> stroke_paint_style(SVGPaintContext const&) const;
  38. JS::GCPtr<SVG::SVGMaskElement const> mask() const;
  39. Optional<ViewBox> view_box() const;
  40. protected:
  41. SVGGraphicsElement(DOM::Document&, DOM::QualifiedName);
  42. virtual void initialize(JS::Realm&) override;
  43. virtual Gfx::AffineTransform element_transform() const
  44. {
  45. return m_transform;
  46. }
  47. Optional<Gfx::PaintStyle const&> svg_paint_computed_value_to_gfx_paint_style(SVGPaintContext const& paint_context, Optional<CSS::SVGPaint> const& paint_value) const;
  48. Gfx::AffineTransform m_transform = {};
  49. template<typename T>
  50. JS::GCPtr<T> try_resolve_url_to(AK::URL const& url) const
  51. {
  52. if (!url.fragment().has_value())
  53. return {};
  54. auto node = document().get_element_by_id(*url.fragment());
  55. if (!node)
  56. return {};
  57. if (is<T>(*node))
  58. return static_cast<T&>(*node);
  59. return {};
  60. }
  61. private:
  62. virtual bool is_svg_graphics_element() const final { return true; }
  63. };
  64. Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list);
  65. }
  66. namespace Web::DOM {
  67. template<>
  68. inline bool Node::fast_is<SVG::SVGGraphicsElement>() const { return is_svg_graphics_element(); }
  69. }