SVGPathPaintable.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/AntiAliasingPainter.h>
  8. #include <LibWeb/Painting/SVGPathPaintable.h>
  9. #include <LibWeb/Painting/SVGSVGPaintable.h>
  10. namespace Web::Painting {
  11. JS_DEFINE_ALLOCATOR(SVGPathPaintable);
  12. JS::NonnullGCPtr<SVGPathPaintable> SVGPathPaintable::create(Layout::SVGGraphicsBox const& layout_box)
  13. {
  14. return layout_box.heap().allocate_without_realm<SVGPathPaintable>(layout_box);
  15. }
  16. SVGPathPaintable::SVGPathPaintable(Layout::SVGGraphicsBox const& layout_box)
  17. : SVGGraphicsPaintable(layout_box)
  18. {
  19. }
  20. Layout::SVGGraphicsBox const& SVGPathPaintable::layout_box() const
  21. {
  22. return static_cast<Layout::SVGGraphicsBox const&>(layout_node());
  23. }
  24. TraversalDecision SVGPathPaintable::hit_test(CSSPixelPoint position, HitTestType type, Function<TraversalDecision(HitTestResult)> const& callback) const
  25. {
  26. if (!computed_path().has_value())
  27. return TraversalDecision::Continue;
  28. auto transformed_bounding_box = computed_transforms().svg_to_css_pixels_transform().map_to_quad(computed_path()->bounding_box());
  29. if (!transformed_bounding_box.contains(position.to_type<float>()))
  30. return TraversalDecision::Continue;
  31. return SVGGraphicsPaintable::hit_test(position, type, callback);
  32. }
  33. static Gfx::WindingRule to_gfx_winding_rule(SVG::FillRule fill_rule)
  34. {
  35. switch (fill_rule) {
  36. case SVG::FillRule::Nonzero:
  37. return Gfx::WindingRule::Nonzero;
  38. case SVG::FillRule::Evenodd:
  39. return Gfx::WindingRule::EvenOdd;
  40. default:
  41. VERIFY_NOT_REACHED();
  42. }
  43. }
  44. void SVGPathPaintable::paint(PaintContext& context, PaintPhase phase) const
  45. {
  46. if (!is_visible() || !computed_path().has_value())
  47. return;
  48. SVGGraphicsPaintable::paint(context, phase);
  49. if (phase != PaintPhase::Foreground)
  50. return;
  51. auto& graphics_element = layout_box().dom_node();
  52. auto const* svg_node = layout_box().first_ancestor_of_type<Layout::SVGSVGBox>();
  53. auto svg_element_rect = svg_node->paintable_box()->absolute_rect();
  54. // FIXME: This should not be trucated to an int.
  55. DisplayListRecorderStateSaver save_painter { context.display_list_recorder() };
  56. auto offset = context.floored_device_point(svg_element_rect.location()).to_type<int>().to_type<float>();
  57. auto maybe_view_box = svg_node->dom_node().view_box();
  58. auto paint_transform = computed_transforms().svg_to_device_pixels_transform(context);
  59. Gfx::DeprecatedPath path = computed_path()->copy_transformed(paint_transform);
  60. // Fills are computed as though all subpaths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
  61. auto closed_path = [&] {
  62. // We need to fill the path before applying the stroke, however the filled
  63. // path must be closed, whereas the stroke path may not necessary be closed.
  64. // Copy the path and close it for filling, but use the previous path for stroke
  65. auto copy = path;
  66. copy.close_all_subpaths();
  67. return copy;
  68. };
  69. // Note: This is assuming .x_scale() == .y_scale() (which it does currently).
  70. auto viewbox_scale = paint_transform.x_scale();
  71. auto svg_viewport = [&] {
  72. if (maybe_view_box.has_value())
  73. return Gfx::FloatRect { maybe_view_box->min_x, maybe_view_box->min_y, maybe_view_box->width, maybe_view_box->height };
  74. return Gfx::FloatRect { { 0, 0 }, svg_element_rect.size().to_type<float>() };
  75. }();
  76. if (context.draw_svg_geometry_for_clip_path()) {
  77. // https://drafts.fxtf.org/css-masking/#ClipPathElement:
  78. // The raw geometry of each child element exclusive of rendering properties such as fill, stroke, stroke-width
  79. // within a clipPath conceptually defines a 1-bit mask (with the possible exception of anti-aliasing along
  80. // the edge of the geometry) which represents the silhouette of the graphics associated with that element.
  81. context.display_list_recorder().fill_path({
  82. .path = closed_path(),
  83. .color = Color::Black,
  84. .winding_rule = to_gfx_winding_rule(graphics_element.clip_rule().value_or(SVG::ClipRule::Nonzero)),
  85. .translation = offset,
  86. });
  87. return;
  88. }
  89. SVG::SVGPaintContext paint_context {
  90. .viewport = svg_viewport,
  91. .path_bounding_box = computed_path()->bounding_box(),
  92. .transform = paint_transform
  93. };
  94. auto fill_opacity = graphics_element.fill_opacity().value_or(1);
  95. auto winding_rule = to_gfx_winding_rule(graphics_element.fill_rule().value_or(SVG::FillRule::Nonzero));
  96. if (auto paint_style = graphics_element.fill_paint_style(paint_context); paint_style.has_value()) {
  97. context.display_list_recorder().fill_path({
  98. .path = closed_path(),
  99. .paint_style = *paint_style,
  100. .winding_rule = winding_rule,
  101. .opacity = fill_opacity,
  102. .translation = offset,
  103. });
  104. } else if (auto fill_color = graphics_element.fill_color(); fill_color.has_value()) {
  105. context.display_list_recorder().fill_path({
  106. .path = closed_path(),
  107. .color = fill_color->with_opacity(fill_opacity),
  108. .winding_rule = winding_rule,
  109. .translation = offset,
  110. });
  111. }
  112. auto stroke_opacity = graphics_element.stroke_opacity().value_or(1);
  113. // Note: This is assuming .x_scale() == .y_scale() (which it does currently).
  114. float stroke_thickness = graphics_element.stroke_width().value_or(1) * viewbox_scale;
  115. if (auto paint_style = graphics_element.stroke_paint_style(paint_context); paint_style.has_value()) {
  116. context.display_list_recorder().stroke_path({
  117. .path = path,
  118. .paint_style = *paint_style,
  119. .thickness = stroke_thickness,
  120. .opacity = stroke_opacity,
  121. .translation = offset,
  122. });
  123. } else if (auto stroke_color = graphics_element.stroke_color(); stroke_color.has_value()) {
  124. context.display_list_recorder().stroke_path({
  125. .path = path,
  126. .color = stroke_color->with_opacity(stroke_opacity),
  127. .thickness = stroke_thickness,
  128. .translation = offset,
  129. });
  130. }
  131. }
  132. }