SVGPathBox.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/AntiAliasingPainter.h>
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/Layout/SVGPathBox.h>
  9. #include <LibWeb/SVG/SVGPathElement.h>
  10. namespace Web::Layout {
  11. SVGPathBox::SVGPathBox(DOM::Document& document, SVG::SVGPathElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  12. : SVGGraphicsBox(document, element, properties)
  13. {
  14. }
  15. void SVGPathBox::paint(PaintContext& context, PaintPhase phase)
  16. {
  17. if (!is_visible())
  18. return;
  19. SVGGraphicsBox::paint(context, phase);
  20. if (phase != PaintPhase::Foreground)
  21. return;
  22. auto& path_element = dom_node();
  23. auto& path = path_element.get_path();
  24. // We need to fill the path before applying the stroke, however the filled
  25. // path must be closed, whereas the stroke path may not necessary be closed.
  26. // Copy the path and close it for filling, but use the previous path for stroke
  27. auto closed_path = path;
  28. closed_path.close();
  29. // Fills are computed as though all paths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
  30. Gfx::AntiAliasingPainter painter { context.painter() };
  31. auto& svg_context = context.svg_context();
  32. auto offset = absolute_position();
  33. painter.translate(offset);
  34. painter.fill_path(
  35. closed_path,
  36. path_element.fill_color().value_or(svg_context.fill_color()),
  37. Gfx::Painter::WindingRule::EvenOdd);
  38. painter.stroke_path(
  39. path,
  40. path_element.stroke_color().value_or(svg_context.stroke_color()),
  41. path_element.stroke_width().value_or(svg_context.stroke_width()));
  42. painter.translate(-offset);
  43. }
  44. }