SVGPathPaintable.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/Quad.h>
  8. #include <LibWeb/Painting/SVGPathPaintable.h>
  9. #include <LibWeb/Painting/SVGSVGPaintable.h>
  10. namespace Web::Painting {
  11. GC_DEFINE_ALLOCATOR(SVGPathPaintable);
  12. GC::Ref<SVGPathPaintable> SVGPathPaintable::create(Layout::SVGGraphicsBox const& layout_box)
  13. {
  14. return layout_box.heap().allocate<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. DisplayListRecorderStateSaver save_painter { context.display_list_recorder() };
  55. auto offset = context.rounded_device_point(svg_element_rect.location()).to_type<int>().to_type<float>();
  56. auto maybe_view_box = svg_node->dom_node().view_box();
  57. auto paint_transform = computed_transforms().svg_to_device_pixels_transform(context);
  58. auto path = computed_path()->copy_transformed(paint_transform);
  59. // Fills are computed as though all subpaths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
  60. auto closed_path = [&] {
  61. // We need to fill the path before applying the stroke, however the filled
  62. // path must be closed, whereas the stroke path may not necessary be closed.
  63. // Copy the path and close it for filling, but use the previous path for stroke
  64. auto copy = path;
  65. copy.close_all_subpaths();
  66. return copy;
  67. };
  68. auto svg_viewport = [&] {
  69. if (maybe_view_box.has_value())
  70. return Gfx::FloatRect { maybe_view_box->min_x, maybe_view_box->min_y, maybe_view_box->width, maybe_view_box->height };
  71. return Gfx::FloatRect { {}, svg_element_rect.size().to_type<float>() };
  72. }();
  73. if (context.draw_svg_geometry_for_clip_path()) {
  74. // https://drafts.fxtf.org/css-masking/#ClipPathElement:
  75. // The raw geometry of each child element exclusive of rendering properties such as fill, stroke, stroke-width
  76. // within a clipPath conceptually defines a 1-bit mask (with the possible exception of anti-aliasing along
  77. // the edge of the geometry) which represents the silhouette of the graphics associated with that element.
  78. context.display_list_recorder().fill_path({
  79. .path = closed_path(),
  80. .color = Color::Black,
  81. .winding_rule = to_gfx_winding_rule(graphics_element.clip_rule().value_or(SVG::ClipRule::Nonzero)),
  82. .translation = offset,
  83. });
  84. return;
  85. }
  86. SVG::SVGPaintContext paint_context {
  87. .viewport = svg_viewport,
  88. .path_bounding_box = computed_path()->bounding_box(),
  89. .paint_transform = paint_transform,
  90. };
  91. auto fill_opacity = graphics_element.fill_opacity().value_or(1);
  92. auto winding_rule = to_gfx_winding_rule(graphics_element.fill_rule().value_or(SVG::FillRule::Nonzero));
  93. if (auto paint_style = graphics_element.fill_paint_style(paint_context); paint_style.has_value()) {
  94. context.display_list_recorder().fill_path({
  95. .path = closed_path(),
  96. .paint_style = *paint_style,
  97. .winding_rule = winding_rule,
  98. .opacity = fill_opacity,
  99. .translation = offset,
  100. });
  101. } else if (auto fill_color = graphics_element.fill_color(); fill_color.has_value()) {
  102. context.display_list_recorder().fill_path({
  103. .path = closed_path(),
  104. .color = fill_color->with_opacity(fill_opacity),
  105. .winding_rule = winding_rule,
  106. .translation = offset,
  107. });
  108. }
  109. Gfx::Path::CapStyle cap_style;
  110. switch (graphics_element.stroke_linecap().value_or(CSS::InitialValues::stroke_linecap())) {
  111. case CSS::StrokeLinecap::Butt:
  112. cap_style = Gfx::Path::CapStyle::Butt;
  113. break;
  114. case CSS::StrokeLinecap::Round:
  115. cap_style = Gfx::Path::CapStyle::Round;
  116. break;
  117. case CSS::StrokeLinecap::Square:
  118. cap_style = Gfx::Path::CapStyle::Square;
  119. break;
  120. }
  121. Gfx::Path::JoinStyle join_style;
  122. switch (graphics_element.stroke_linejoin().value_or(CSS::InitialValues::stroke_linejoin())) {
  123. case CSS::StrokeLinejoin::Miter:
  124. join_style = Gfx::Path::JoinStyle::Miter;
  125. break;
  126. case CSS::StrokeLinejoin::Round:
  127. join_style = Gfx::Path::JoinStyle::Round;
  128. break;
  129. case CSS::StrokeLinejoin::Bevel:
  130. join_style = Gfx::Path::JoinStyle::Bevel;
  131. break;
  132. }
  133. auto miter_limit = graphics_element.stroke_miterlimit().value_or(CSS::InitialValues::stroke_miterlimit()).resolved(layout_node());
  134. auto stroke_opacity = graphics_element.stroke_opacity().value_or(1);
  135. // Note: This is assuming .x_scale() == .y_scale() (which it does currently).
  136. auto viewbox_scale = paint_transform.x_scale();
  137. float stroke_thickness = graphics_element.stroke_width().value_or(1) * viewbox_scale;
  138. auto stroke_dasharray = graphics_element.stroke_dasharray();
  139. for (auto& value : stroke_dasharray)
  140. value *= viewbox_scale;
  141. float stroke_dashoffset = graphics_element.stroke_dashoffset().value_or(0) * viewbox_scale;
  142. if (auto paint_style = graphics_element.stroke_paint_style(paint_context); paint_style.has_value()) {
  143. context.display_list_recorder().stroke_path({
  144. .cap_style = cap_style,
  145. .join_style = join_style,
  146. .miter_limit = static_cast<float>(miter_limit),
  147. .dash_array = stroke_dasharray,
  148. .dash_offset = stroke_dashoffset,
  149. .path = path,
  150. .paint_style = *paint_style,
  151. .thickness = stroke_thickness,
  152. .opacity = stroke_opacity,
  153. .translation = offset,
  154. });
  155. } else if (auto stroke_color = graphics_element.stroke_color(); stroke_color.has_value()) {
  156. context.display_list_recorder().stroke_path({
  157. .cap_style = cap_style,
  158. .join_style = join_style,
  159. .miter_limit = static_cast<float>(miter_limit),
  160. .dash_array = stroke_dasharray,
  161. .dash_offset = stroke_dashoffset,
  162. .path = path,
  163. .color = stroke_color->with_opacity(stroke_opacity),
  164. .thickness = stroke_thickness,
  165. .translation = offset,
  166. });
  167. }
  168. }
  169. }