SVGSVGPaintable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/ImageBox.h>
  7. #include <LibWeb/Painting/SVGSVGPaintable.h>
  8. namespace Web::Painting {
  9. GC_DEFINE_ALLOCATOR(SVGSVGPaintable);
  10. GC::Ref<SVGSVGPaintable> SVGSVGPaintable::create(Layout::SVGSVGBox const& layout_box)
  11. {
  12. return layout_box.heap().allocate<SVGSVGPaintable>(layout_box);
  13. }
  14. SVGSVGPaintable::SVGSVGPaintable(Layout::SVGSVGBox const& layout_box)
  15. : PaintableBox(layout_box)
  16. {
  17. }
  18. Layout::SVGSVGBox const& SVGSVGPaintable::layout_box() const
  19. {
  20. return static_cast<Layout::SVGSVGBox const&>(layout_node());
  21. }
  22. void SVGSVGPaintable::before_children_paint(PaintContext& context, PaintPhase phase) const
  23. {
  24. PaintableBox::before_children_paint(context, phase);
  25. if (phase != PaintPhase::Foreground)
  26. return;
  27. context.display_list_recorder().push_scroll_frame_id(scroll_frame_id());
  28. }
  29. void SVGSVGPaintable::after_children_paint(PaintContext& context, PaintPhase phase) const
  30. {
  31. PaintableBox::after_children_paint(context, phase);
  32. if (phase != PaintPhase::Foreground)
  33. return;
  34. context.display_list_recorder().pop_scroll_frame_id();
  35. }
  36. static Gfx::FloatMatrix4x4 matrix_with_scaled_translation(Gfx::FloatMatrix4x4 matrix, float scale)
  37. {
  38. auto* m = matrix.elements();
  39. m[0][3] *= scale;
  40. m[1][3] *= scale;
  41. m[2][3] *= scale;
  42. return matrix;
  43. }
  44. void SVGSVGPaintable::paint_svg_box(PaintContext& context, PaintableBox const& svg_box, PaintPhase phase)
  45. {
  46. auto const& computed_values = svg_box.computed_values();
  47. auto const& filter = svg_box.computed_values().filter();
  48. auto masking_area = svg_box.get_masking_area();
  49. auto needs_to_save_state = computed_values.opacity() < 1 || svg_box.has_css_transform() || svg_box.get_masking_area().has_value();
  50. if (needs_to_save_state) {
  51. context.display_list_recorder().save();
  52. }
  53. if (computed_values.opacity() < 1) {
  54. context.display_list_recorder().apply_opacity(computed_values.opacity());
  55. }
  56. if (!filter.is_none()) {
  57. context.display_list_recorder().apply_filters(filter);
  58. }
  59. if (svg_box.has_css_transform()) {
  60. auto transform_matrix = svg_box.transform();
  61. Gfx::FloatPoint transform_origin = svg_box.transform_origin().template to_type<float>();
  62. auto to_device_pixels_scale = float(context.device_pixels_per_css_pixel());
  63. context.display_list_recorder().apply_transform(transform_origin.scaled(to_device_pixels_scale), matrix_with_scaled_translation(transform_matrix, to_device_pixels_scale));
  64. }
  65. if (masking_area.has_value()) {
  66. if (masking_area->is_empty())
  67. return;
  68. auto mask_bitmap = svg_box.calculate_mask(context, *masking_area);
  69. if (mask_bitmap) {
  70. auto source_paintable_rect = context.enclosing_device_rect(*masking_area).template to_type<int>();
  71. auto origin = source_paintable_rect.location();
  72. context.display_list_recorder().apply_mask_bitmap(origin, mask_bitmap.release_nonnull(), *svg_box.get_mask_type());
  73. }
  74. }
  75. svg_box.before_paint(context, PaintPhase::Foreground);
  76. svg_box.paint(context, PaintPhase::Foreground);
  77. svg_box.after_paint(context, PaintPhase::Foreground);
  78. paint_descendants(context, svg_box, phase);
  79. if (!filter.is_none()) {
  80. context.display_list_recorder().restore();
  81. }
  82. if (needs_to_save_state) {
  83. context.display_list_recorder().restore();
  84. }
  85. }
  86. void SVGSVGPaintable::paint_descendants(PaintContext& context, PaintableBox const& paintable, PaintPhase phase)
  87. {
  88. if (phase != PaintPhase::Foreground)
  89. return;
  90. paintable.before_children_paint(context, PaintPhase::Foreground);
  91. paintable.for_each_child_of_type<PaintableBox>([&](PaintableBox& child) {
  92. paint_svg_box(context, child, phase);
  93. return IterationDecision::Continue;
  94. });
  95. paintable.after_children_paint(context, PaintPhase::Foreground);
  96. }
  97. }