SVGPathPaintable.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/SVG/SVGSVGElement.h>
  10. namespace Web::Painting {
  11. JS::NonnullGCPtr<SVGPathPaintable> SVGPathPaintable::create(Layout::SVGGraphicsBox const& layout_box)
  12. {
  13. return layout_box.heap().allocate_without_realm<SVGPathPaintable>(layout_box);
  14. }
  15. SVGPathPaintable::SVGPathPaintable(Layout::SVGGraphicsBox const& layout_box)
  16. : SVGGraphicsPaintable(layout_box)
  17. {
  18. }
  19. Layout::SVGGraphicsBox const& SVGPathPaintable::layout_box() const
  20. {
  21. return static_cast<Layout::SVGGraphicsBox const&>(layout_node());
  22. }
  23. TraversalDecision SVGPathPaintable::hit_test(CSSPixelPoint position, HitTestType type, Function<TraversalDecision(HitTestResult)> const& callback) const
  24. {
  25. if (!computed_path().has_value())
  26. return TraversalDecision::Continue;
  27. auto transformed_bounding_box = computed_transforms().svg_to_css_pixels_transform().map_to_quad(computed_path()->bounding_box());
  28. if (!transformed_bounding_box.contains(position.to_type<float>()))
  29. return TraversalDecision::Continue;
  30. return SVGGraphicsPaintable::hit_test(position, type, callback);
  31. }
  32. static Gfx::Painter::WindingRule to_gfx_winding_rule(SVG::FillRule fill_rule)
  33. {
  34. switch (fill_rule) {
  35. case SVG::FillRule::Nonzero:
  36. return Gfx::Painter::WindingRule::Nonzero;
  37. case SVG::FillRule::Evenodd:
  38. return Gfx::Painter::WindingRule::EvenOdd;
  39. default:
  40. VERIFY_NOT_REACHED();
  41. }
  42. }
  43. void SVGPathPaintable::paint(PaintContext& context, PaintPhase phase) const
  44. {
  45. if (!is_visible() || !computed_path().has_value())
  46. return;
  47. SVGGraphicsPaintable::paint(context, phase);
  48. if (phase != PaintPhase::Foreground)
  49. return;
  50. auto& geometry_element = layout_box().dom_node();
  51. auto const* svg_element = geometry_element.shadow_including_first_ancestor_of_type<SVG::SVGSVGElement>();
  52. auto svg_element_rect = svg_element->paintable_box()->absolute_rect();
  53. // FIXME: This should not be trucated to an int.
  54. RecordingPainterStateSaver save_painter { context.recording_painter() };
  55. auto offset = context.floored_device_point(svg_element_rect.location()).to_type<int>().to_type<float>();
  56. auto maybe_view_box = svg_element->view_box();
  57. auto paint_transform = computed_transforms().svg_to_device_pixels_transform(context);
  58. Gfx::Path 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. // Note: This is assuming .x_scale() == .y_scale() (which it does currently).
  69. auto viewbox_scale = paint_transform.x_scale();
  70. auto svg_viewport = [&] {
  71. if (maybe_view_box.has_value())
  72. return Gfx::FloatRect { maybe_view_box->min_x, maybe_view_box->min_y, maybe_view_box->width, maybe_view_box->height };
  73. return Gfx::FloatRect { { 0, 0 }, svg_element_rect.size().to_type<float>() };
  74. }();
  75. SVG::SVGPaintContext paint_context {
  76. .viewport = svg_viewport,
  77. .path_bounding_box = computed_path()->bounding_box(),
  78. .transform = paint_transform
  79. };
  80. auto fill_opacity = geometry_element.fill_opacity().value_or(1);
  81. auto winding_rule = to_gfx_winding_rule(geometry_element.fill_rule().value_or(SVG::FillRule::Nonzero));
  82. if (auto paint_style = geometry_element.fill_paint_style(paint_context); paint_style.has_value()) {
  83. context.recording_painter().fill_path({
  84. .path = closed_path(),
  85. .paint_style = *paint_style,
  86. .winding_rule = winding_rule,
  87. .opacity = fill_opacity,
  88. .translation = offset,
  89. });
  90. } else if (auto fill_color = geometry_element.fill_color(); fill_color.has_value()) {
  91. context.recording_painter().fill_path({
  92. .path = closed_path(),
  93. .color = fill_color->with_opacity(fill_opacity),
  94. .winding_rule = winding_rule,
  95. .translation = offset,
  96. });
  97. }
  98. auto stroke_opacity = geometry_element.stroke_opacity().value_or(1);
  99. // Note: This is assuming .x_scale() == .y_scale() (which it does currently).
  100. float stroke_thickness = geometry_element.stroke_width().value_or(1) * viewbox_scale;
  101. if (auto paint_style = geometry_element.stroke_paint_style(paint_context); paint_style.has_value()) {
  102. context.recording_painter().stroke_path({
  103. .path = path,
  104. .paint_style = *paint_style,
  105. .thickness = stroke_thickness,
  106. .opacity = stroke_opacity,
  107. .translation = offset,
  108. });
  109. } else if (auto stroke_color = geometry_element.stroke_color(); stroke_color.has_value()) {
  110. context.recording_painter().stroke_path({
  111. .path = path,
  112. .color = stroke_color->with_opacity(stroke_opacity),
  113. .thickness = stroke_thickness,
  114. .translation = offset,
  115. });
  116. }
  117. }
  118. }