SVGGeometryBox.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/SVGGeometryBox.h>
  9. #include <LibWeb/SVG/SVGPathElement.h>
  10. namespace Web::Layout {
  11. SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  12. : SVGGraphicsBox(document, element, properties)
  13. {
  14. }
  15. void SVGGeometryBox::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& geometry_element = dom_node();
  23. auto& path = geometry_element.get_path();
  24. Gfx::AntiAliasingPainter painter { context.painter() };
  25. auto& svg_context = context.svg_context();
  26. auto offset = absolute_position();
  27. painter.translate(offset);
  28. if (auto fill_color = geometry_element.fill_color().value_or(svg_context.fill_color()); fill_color.alpha() > 0) {
  29. // We need to fill the path before applying the stroke, however the filled
  30. // path must be closed, whereas the stroke path may not necessary be closed.
  31. // Copy the path and close it for filling, but use the previous path for stroke
  32. auto closed_path = path;
  33. closed_path.close();
  34. // Fills are computed as though all paths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
  35. painter.fill_path(
  36. closed_path,
  37. fill_color,
  38. Gfx::Painter::WindingRule::EvenOdd);
  39. }
  40. if (auto stroke_color = geometry_element.stroke_color().value_or(svg_context.stroke_color()); stroke_color.alpha() > 0) {
  41. painter.stroke_path(
  42. path,
  43. stroke_color,
  44. geometry_element.stroke_width().value_or(svg_context.stroke_width()));
  45. }
  46. painter.translate(-offset);
  47. }
  48. }