SVGPathBox.cpp 2.1 KB

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