SVGPathBox.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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::prepare_for_replaced_layout()
  16. {
  17. auto& bounding_box = dom_node().get_path().bounding_box();
  18. set_has_intrinsic_width(true);
  19. set_has_intrinsic_height(true);
  20. set_intrinsic_width(bounding_box.width());
  21. set_intrinsic_height(bounding_box.height());
  22. // FIXME: This does not belong here! Someone at a higher level should place this box.
  23. set_offset(bounding_box.top_left());
  24. }
  25. void SVGPathBox::paint(PaintContext& context, PaintPhase phase)
  26. {
  27. if (!is_visible())
  28. return;
  29. SVGGraphicsBox::paint(context, phase);
  30. if (phase != PaintPhase::Foreground)
  31. return;
  32. auto& path_element = dom_node();
  33. auto& path = path_element.get_path();
  34. // We need to fill the path before applying the stroke, however the filled
  35. // path must be closed, whereas the stroke path may not necessary be closed.
  36. // Copy the path and close it for filling, but use the previous path for stroke
  37. auto closed_path = path;
  38. closed_path.close();
  39. // Fills are computed as though all paths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
  40. Gfx::AntiAliasingPainter painter { context.painter() };
  41. auto& svg_context = context.svg_context();
  42. auto offset = absolute_position() - effective_offset();
  43. painter.translate(offset);
  44. painter.fill_path(
  45. closed_path,
  46. path_element.fill_color().value_or(svg_context.fill_color()),
  47. Gfx::Painter::WindingRule::EvenOdd);
  48. painter.stroke_path(
  49. path,
  50. path_element.stroke_color().value_or(svg_context.stroke_color()),
  51. path_element.stroke_width().value_or(svg_context.stroke_width()));
  52. painter.translate(-offset);
  53. }
  54. }