SVGPathPaintable.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.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 <LibGfx/Quad.h>
  9. #include <LibWeb/Painting/SVGPathPaintable.h>
  10. #include <LibWeb/Painting/SVGSVGPaintable.h>
  11. namespace Web::Painting {
  12. GC_DEFINE_ALLOCATOR(SVGPathPaintable);
  13. GC::Ref<SVGPathPaintable> SVGPathPaintable::create(Layout::SVGGraphicsBox const& layout_box)
  14. {
  15. return layout_box.heap().allocate<SVGPathPaintable>(layout_box);
  16. }
  17. SVGPathPaintable::SVGPathPaintable(Layout::SVGGraphicsBox const& layout_box)
  18. : SVGGraphicsPaintable(layout_box)
  19. {
  20. }
  21. Layout::SVGGraphicsBox const& SVGPathPaintable::layout_box() const
  22. {
  23. return static_cast<Layout::SVGGraphicsBox const&>(layout_node());
  24. }
  25. TraversalDecision SVGPathPaintable::hit_test(CSSPixelPoint position, HitTestType type, Function<TraversalDecision(HitTestResult)> const& callback) const
  26. {
  27. if (!computed_path().has_value())
  28. return TraversalDecision::Continue;
  29. auto transformed_bounding_box = computed_transforms().svg_to_css_pixels_transform().map_to_quad(computed_path()->bounding_box());
  30. if (!transformed_bounding_box.contains(position.to_type<float>()))
  31. return TraversalDecision::Continue;
  32. return SVGGraphicsPaintable::hit_test(position, type, callback);
  33. }
  34. static Gfx::WindingRule to_gfx_winding_rule(SVG::FillRule fill_rule)
  35. {
  36. switch (fill_rule) {
  37. case SVG::FillRule::Nonzero:
  38. return Gfx::WindingRule::Nonzero;
  39. case SVG::FillRule::Evenodd:
  40. return Gfx::WindingRule::EvenOdd;
  41. default:
  42. VERIFY_NOT_REACHED();
  43. }
  44. }
  45. void SVGPathPaintable::paint(PaintContext& context, PaintPhase phase) const
  46. {
  47. if (!is_visible() || !computed_path().has_value())
  48. return;
  49. SVGGraphicsPaintable::paint(context, phase);
  50. if (phase != PaintPhase::Foreground)
  51. return;
  52. auto& graphics_element = layout_box().dom_node();
  53. auto const* svg_node = layout_box().first_ancestor_of_type<Layout::SVGSVGBox>();
  54. auto svg_element_rect = svg_node->paintable_box()->absolute_rect();
  55. DisplayListRecorderStateSaver save_painter { context.display_list_recorder() };
  56. auto offset = context.rounded_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. auto 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. auto svg_viewport = [&] {
  70. if (maybe_view_box.has_value())
  71. return Gfx::FloatRect { maybe_view_box->min_x, maybe_view_box->min_y, maybe_view_box->width, maybe_view_box->height };
  72. return Gfx::FloatRect { {}, svg_element_rect.size().to_type<float>() };
  73. }();
  74. if (context.draw_svg_geometry_for_clip_path()) {
  75. // https://drafts.fxtf.org/css-masking/#ClipPathElement:
  76. // The raw geometry of each child element exclusive of rendering properties such as fill, stroke, stroke-width
  77. // within a clipPath conceptually defines a 1-bit mask (with the possible exception of anti-aliasing along
  78. // the edge of the geometry) which represents the silhouette of the graphics associated with that element.
  79. context.display_list_recorder().fill_path({
  80. .path = closed_path(),
  81. .color = Color::Black,
  82. .winding_rule = to_gfx_winding_rule(graphics_element.clip_rule().value_or(SVG::ClipRule::Nonzero)),
  83. .translation = offset,
  84. });
  85. return;
  86. }
  87. SVG::SVGPaintContext paint_context {
  88. .viewport = svg_viewport,
  89. .path_bounding_box = computed_path()->bounding_box(),
  90. .paint_transform = paint_transform,
  91. };
  92. auto fill_opacity = graphics_element.fill_opacity().value_or(1);
  93. auto winding_rule = to_gfx_winding_rule(graphics_element.fill_rule().value_or(SVG::FillRule::Nonzero));
  94. if (auto paint_style = graphics_element.fill_paint_style(paint_context); paint_style.has_value()) {
  95. context.display_list_recorder().fill_path({
  96. .path = closed_path(),
  97. .paint_style = *paint_style,
  98. .winding_rule = winding_rule,
  99. .opacity = fill_opacity,
  100. .translation = offset,
  101. });
  102. } else if (auto fill_color = graphics_element.fill_color(); fill_color.has_value()) {
  103. context.display_list_recorder().fill_path({
  104. .path = closed_path(),
  105. .color = fill_color->with_opacity(fill_opacity),
  106. .winding_rule = winding_rule,
  107. .translation = offset,
  108. });
  109. }
  110. Gfx::Path::CapStyle cap_style;
  111. switch (graphics_element.stroke_linecap().value_or(CSS::InitialValues::stroke_linecap())) {
  112. case CSS::StrokeLinecap::Butt:
  113. cap_style = Gfx::Path::CapStyle::Butt;
  114. break;
  115. case CSS::StrokeLinecap::Round:
  116. cap_style = Gfx::Path::CapStyle::Round;
  117. break;
  118. case CSS::StrokeLinecap::Square:
  119. cap_style = Gfx::Path::CapStyle::Square;
  120. break;
  121. }
  122. Gfx::Path::JoinStyle join_style;
  123. switch (graphics_element.stroke_linejoin().value_or(CSS::InitialValues::stroke_linejoin())) {
  124. case CSS::StrokeLinejoin::Miter:
  125. join_style = Gfx::Path::JoinStyle::Miter;
  126. break;
  127. case CSS::StrokeLinejoin::Round:
  128. join_style = Gfx::Path::JoinStyle::Round;
  129. break;
  130. case CSS::StrokeLinejoin::Bevel:
  131. join_style = Gfx::Path::JoinStyle::Bevel;
  132. break;
  133. }
  134. auto miter_limit = graphics_element.stroke_miterlimit().value_or(CSS::InitialValues::stroke_miterlimit()).resolved(layout_node());
  135. auto stroke_opacity = graphics_element.stroke_opacity().value_or(1);
  136. // Note: This is assuming .x_scale() == .y_scale() (which it does currently).
  137. auto viewbox_scale = paint_transform.x_scale();
  138. float stroke_thickness = graphics_element.stroke_width().value_or(1) * viewbox_scale;
  139. auto stroke_dasharray = graphics_element.stroke_dasharray();
  140. for (auto& value : stroke_dasharray)
  141. value *= viewbox_scale;
  142. float stroke_dashoffset = graphics_element.stroke_dashoffset().value_or(0) * viewbox_scale;
  143. if (auto paint_style = graphics_element.stroke_paint_style(paint_context); paint_style.has_value()) {
  144. context.display_list_recorder().stroke_path({
  145. .cap_style = cap_style,
  146. .join_style = join_style,
  147. .miter_limit = static_cast<float>(miter_limit),
  148. .dash_array = stroke_dasharray,
  149. .dash_offset = stroke_dashoffset,
  150. .path = path,
  151. .paint_style = *paint_style,
  152. .thickness = stroke_thickness,
  153. .opacity = stroke_opacity,
  154. .translation = offset,
  155. });
  156. } else if (auto stroke_color = graphics_element.stroke_color(); stroke_color.has_value()) {
  157. context.display_list_recorder().stroke_path({
  158. .cap_style = cap_style,
  159. .join_style = join_style,
  160. .miter_limit = static_cast<float>(miter_limit),
  161. .dash_array = stroke_dasharray,
  162. .dash_offset = stroke_dashoffset,
  163. .path = path,
  164. .color = stroke_color->with_opacity(stroke_opacity),
  165. .thickness = stroke_thickness,
  166. .translation = offset,
  167. });
  168. }
  169. }
  170. }