SVGGraphicsPaintable.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Layout/SVGGraphicsBox.h>
  8. #include <LibWeb/Painting/SVGPaintable.h>
  9. namespace Web::Painting {
  10. class SVGGraphicsPaintable : public SVGPaintable {
  11. JS_CELL(SVGGraphicsPaintable, SVGPaintable);
  12. public:
  13. class ComputedTransforms {
  14. public:
  15. ComputedTransforms(Gfx::AffineTransform svg_to_viewbox_transform, Gfx::AffineTransform svg_transform)
  16. : m_svg_to_viewbox_transform(svg_to_viewbox_transform)
  17. , m_svg_transform(svg_transform)
  18. {
  19. }
  20. ComputedTransforms() = default;
  21. Gfx::AffineTransform const& svg_to_viewbox_transform() const { return m_svg_to_viewbox_transform; }
  22. Gfx::AffineTransform const& svg_transform() const { return m_svg_transform; }
  23. Gfx::AffineTransform svg_to_css_pixels_transform(
  24. Optional<Gfx::AffineTransform const&> additional_svg_transform = {}) const
  25. {
  26. return Gfx::AffineTransform {}.multiply(svg_to_viewbox_transform()).multiply(additional_svg_transform.value_or(Gfx::AffineTransform {})).multiply(svg_transform());
  27. }
  28. Gfx::AffineTransform svg_to_device_pixels_transform(PaintContext const& context) const
  29. {
  30. auto css_scale = context.device_pixels_per_css_pixel();
  31. return Gfx::AffineTransform {}.scale({ css_scale, css_scale }).multiply(svg_to_css_pixels_transform(context.svg_transform()));
  32. }
  33. private:
  34. Gfx::AffineTransform m_svg_to_viewbox_transform {};
  35. Gfx::AffineTransform m_svg_transform {};
  36. };
  37. static JS::NonnullGCPtr<SVGGraphicsPaintable> create(Layout::SVGGraphicsBox const&);
  38. Layout::SVGGraphicsBox const& layout_box() const;
  39. virtual bool forms_unconnected_subtree() const override;
  40. virtual Optional<CSSPixelRect> get_masking_area() const override;
  41. virtual Optional<Gfx::Bitmap::MaskKind> get_mask_type() const override;
  42. virtual RefPtr<Gfx::Bitmap> calculate_mask(PaintContext&, CSSPixelRect const& masking_area) const override;
  43. void set_computed_transforms(ComputedTransforms computed_transforms)
  44. {
  45. m_computed_transforms = computed_transforms;
  46. }
  47. ComputedTransforms const& computed_transforms() const
  48. {
  49. return m_computed_transforms;
  50. }
  51. protected:
  52. SVGGraphicsPaintable(Layout::SVGGraphicsBox const&);
  53. ComputedTransforms m_computed_transforms;
  54. };
  55. }